ScreenHelper.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.impl;
  24. import com.google.common.collect.Iterables;
  25. import com.google.common.collect.Lists;
  26. import com.google.common.collect.Sets;
  27. import com.mojang.blaze3d.platform.Window;
  28. import com.mojang.blaze3d.vertex.PoseStack;
  29. import me.shedaniel.cloth.api.client.events.v0.ClothClientHooks;
  30. import me.shedaniel.math.Point;
  31. import me.shedaniel.math.Rectangle;
  32. import me.shedaniel.math.api.Executor;
  33. import me.shedaniel.rei.RoughlyEnoughItemsState;
  34. import me.shedaniel.rei.api.ConfigManager;
  35. import me.shedaniel.rei.api.ConfigObject;
  36. import me.shedaniel.rei.api.REIHelper;
  37. import me.shedaniel.rei.api.REIOverlay;
  38. import me.shedaniel.rei.api.widgets.Tooltip;
  39. import me.shedaniel.rei.gui.ContainerScreenOverlay;
  40. import me.shedaniel.rei.gui.OverlaySearchField;
  41. import me.shedaniel.rei.gui.RecipeScreen;
  42. import me.shedaniel.rei.gui.WarningAndErrorScreen;
  43. import me.shedaniel.rei.gui.config.SearchFieldLocation;
  44. import me.shedaniel.rei.gui.widget.TextFieldWidget;
  45. import net.fabricmc.api.ClientModInitializer;
  46. import net.fabricmc.api.EnvType;
  47. import net.fabricmc.api.Environment;
  48. import net.fabricmc.fabric.api.event.client.ClientTickCallback;
  49. import net.fabricmc.loader.api.FabricLoader;
  50. import net.minecraft.client.Minecraft;
  51. import net.minecraft.client.gui.screens.Screen;
  52. import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
  53. import net.minecraft.resources.ResourceLocation;
  54. import net.minecraft.world.InteractionResult;
  55. import net.minecraft.world.item.ItemStack;
  56. import org.apache.logging.log4j.util.TriConsumer;
  57. import org.jetbrains.annotations.ApiStatus;
  58. import org.jetbrains.annotations.NotNull;
  59. import org.jetbrains.annotations.Nullable;
  60. import java.util.LinkedHashSet;
  61. import java.util.List;
  62. import java.util.Optional;
  63. import static me.shedaniel.rei.impl.Internals.attachInstance;
  64. @ApiStatus.Internal
  65. @Environment(EnvType.CLIENT)
  66. public class ScreenHelper implements ClientModInitializer, REIHelper {
  67. private static final ResourceLocation DISPLAY_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/display.png");
  68. private static final ResourceLocation DISPLAY_TEXTURE_DARK = new ResourceLocation("roughlyenoughitems", "textures/gui/display_dark.png");
  69. private OverlaySearchField searchField;
  70. @ApiStatus.Internal
  71. public static List<ItemStack> inventoryStacks = Lists.newArrayList();
  72. private static ContainerScreenOverlay overlay;
  73. private static AbstractContainerScreen<?> previousContainerScreen = null;
  74. private static LinkedHashSet<RecipeScreen> lastRecipeScreen = Sets.newLinkedHashSetWithExpectedSize(5);
  75. private static ScreenHelper instance;
  76. /**
  77. * @return the instance of screen helper
  78. * @see REIHelper#getInstance()
  79. */
  80. @ApiStatus.Internal
  81. public static ScreenHelper getInstance() {
  82. return instance;
  83. }
  84. @Override
  85. public void queueTooltip(@Nullable Tooltip tooltip) {
  86. if (overlay != null && tooltip != null) {
  87. overlay.addTooltip(tooltip);
  88. }
  89. }
  90. @Override
  91. @Nullable
  92. public TextFieldWidget getSearchTextField() {
  93. return searchField;
  94. }
  95. @Override
  96. public @NotNull List<ItemStack> getInventoryStacks() {
  97. return inventoryStacks;
  98. }
  99. @Nullable
  100. public static OverlaySearchField getSearchField() {
  101. return (OverlaySearchField) getInstance().getSearchTextField();
  102. }
  103. @ApiStatus.Internal
  104. public static void setSearchField(OverlaySearchField searchField) {
  105. getInstance().searchField = searchField;
  106. }
  107. public static void storeRecipeScreen(RecipeScreen screen) {
  108. while (lastRecipeScreen.size() >= 5)
  109. lastRecipeScreen.remove(Iterables.get(lastRecipeScreen, 0));
  110. lastRecipeScreen.add(screen);
  111. }
  112. public static boolean hasLastRecipeScreen() {
  113. return !lastRecipeScreen.isEmpty();
  114. }
  115. public static Screen getLastRecipeScreen() {
  116. RecipeScreen screen = Iterables.getLast(lastRecipeScreen);
  117. lastRecipeScreen.remove(screen);
  118. screen.recalculateCategoryPage();
  119. return (Screen) screen;
  120. }
  121. @ApiStatus.Internal
  122. public static void clearLastRecipeScreenData() {
  123. lastRecipeScreen.clear();
  124. }
  125. public static boolean isOverlayVisible() {
  126. return ConfigObject.getInstance().isOverlayVisible();
  127. }
  128. public static void toggleOverlayVisible() {
  129. ConfigObject.getInstance().setOverlayVisible(!ConfigObject.getInstance().isOverlayVisible());
  130. ConfigManager.getInstance().saveConfig();
  131. }
  132. public static Optional<ContainerScreenOverlay> getOptionalOverlay() {
  133. return Optional.ofNullable(overlay);
  134. }
  135. @Override
  136. public Optional<REIOverlay> getOverlay() {
  137. return Optional.ofNullable(overlay);
  138. }
  139. public static ContainerScreenOverlay getLastOverlay(boolean reset, boolean setPage) {
  140. if (overlay == null || reset) {
  141. overlay = new ContainerScreenOverlay();
  142. overlay.init();
  143. getSearchField().setFocused(false);
  144. }
  145. return overlay;
  146. }
  147. public static ContainerScreenOverlay getLastOverlay() {
  148. return getLastOverlay(false, false);
  149. }
  150. /**
  151. * @see REIHelper#getPreviousContainerScreen()
  152. */
  153. @Deprecated
  154. @ApiStatus.ScheduledForRemoval
  155. public static AbstractContainerScreen<?> getLastHandledScreen() {
  156. return previousContainerScreen;
  157. }
  158. @Override
  159. public AbstractContainerScreen<?> getPreviousContainerScreen() {
  160. return previousContainerScreen;
  161. }
  162. public static void setPreviousContainerScreen(AbstractContainerScreen<?> previousContainerScreen) {
  163. ScreenHelper.previousContainerScreen = previousContainerScreen;
  164. }
  165. public static void drawHoveringWidget(PoseStack matrices, int x, int y, TriConsumer<PoseStack, Point, Float> consumer, int width, int height, float delta) {
  166. Window window = Minecraft.getInstance().getWindow();
  167. drawHoveringWidget(matrices, window.getGuiScaledWidth(), window.getGuiScaledHeight(), x, y, consumer, width, height, delta);
  168. }
  169. public static void drawHoveringWidget(PoseStack matrices, int screenWidth, int screenHeight, int x, int y, TriConsumer<PoseStack, Point, Float> consumer, int width, int height, float delta) {
  170. int actualX = Math.max(x + 12, 6);
  171. int actualY = Math.min(y - height / 2, screenHeight - height - 6);
  172. if (actualX + width > screenWidth)
  173. actualX -= 24 + width;
  174. if (actualY < 6)
  175. actualY += 24;
  176. consumer.accept(matrices, new Point(actualX, actualY), delta);
  177. }
  178. /**
  179. * @deprecated Please switch to {@link REIHelper#isDarkThemeEnabled()}
  180. */
  181. @Deprecated
  182. @ApiStatus.Internal
  183. @ApiStatus.ScheduledForRemoval
  184. public static boolean isDarkModeEnabled() {
  185. return ConfigObject.getInstance().isUsingDarkTheme();
  186. }
  187. @Override
  188. public boolean isDarkThemeEnabled() {
  189. return isDarkModeEnabled();
  190. }
  191. @Override
  192. public @NotNull ResourceLocation getDefaultDisplayTexture() {
  193. return isDarkThemeEnabled() ? DISPLAY_TEXTURE_DARK : DISPLAY_TEXTURE;
  194. }
  195. public ScreenHelper() {
  196. ScreenHelper.instance = this;
  197. attachInstance(instance, REIHelper.class);
  198. }
  199. public static Rectangle getItemListArea(Rectangle bounds) {
  200. return new Rectangle(bounds.x, bounds.y + 2 + (ConfigObject.getInstance().getSearchFieldLocation() == SearchFieldLocation.TOP_SIDE ? 24 : 0) + (ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22), bounds.width, bounds.height - (ConfigObject.getInstance().getSearchFieldLocation() != SearchFieldLocation.CENTER ? 27 + 22 : 27) + (!ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22));
  201. }
  202. public static Rectangle getFavoritesListArea(Rectangle bounds) {
  203. int offset = 6 + (!ConfigObject.getInstance().isLowerConfigButton() ? 25 : 0) + (ConfigObject.getInstance().doesShowUtilsButtons() ? 25 : 0);
  204. return new Rectangle(bounds.x, bounds.y + 2 + offset, bounds.width, bounds.height - 5 - offset);
  205. }
  206. @Override
  207. public void onInitializeClient() {
  208. ClothClientHooks.SCREEN_INIT_PRE.register((client, screen, screenHooks) -> {
  209. if ((!RoughlyEnoughItemsState.getErrors().isEmpty() || !RoughlyEnoughItemsState.getWarnings().isEmpty()) && !(screen instanceof WarningAndErrorScreen)) {
  210. WarningAndErrorScreen warningAndErrorScreen = WarningAndErrorScreen.INSTANCE.get();
  211. warningAndErrorScreen.setParent(screen);
  212. try {
  213. if (client.screen != null) client.screen.removed();
  214. } catch (Throwable ignored) {
  215. }
  216. client.screen = null;
  217. client.setScreen(warningAndErrorScreen);
  218. } else if (previousContainerScreen != screen && screen instanceof AbstractContainerScreen)
  219. previousContainerScreen = (AbstractContainerScreen<?>) screen;
  220. return InteractionResult.PASS;
  221. });
  222. boolean loaded = FabricLoader.getInstance().isModLoaded("fabric-events-lifecycle-v0");
  223. if (!loaded) {
  224. RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all");
  225. return;
  226. }
  227. Executor.run(() -> () -> {
  228. ClientTickCallback.EVENT.register(minecraftClient -> {
  229. if (isOverlayVisible() && getSearchField() != null)
  230. getSearchField().tick();
  231. });
  232. });
  233. }
  234. }