ScreenHelper.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.*;
  35. import me.shedaniel.rei.api.widgets.Tooltip;
  36. import me.shedaniel.rei.gui.ContainerScreenOverlay;
  37. import me.shedaniel.rei.gui.OverlaySearchField;
  38. import me.shedaniel.rei.gui.RecipeScreen;
  39. import me.shedaniel.rei.gui.WarningAndErrorScreen;
  40. import me.shedaniel.rei.gui.config.SearchFieldLocation;
  41. import me.shedaniel.rei.gui.widget.TextFieldWidget;
  42. import net.fabricmc.api.ClientModInitializer;
  43. import net.fabricmc.api.EnvType;
  44. import net.fabricmc.api.Environment;
  45. import net.fabricmc.fabric.api.event.client.ClientTickCallback;
  46. import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
  47. import net.fabricmc.loader.api.FabricLoader;
  48. import net.minecraft.client.Minecraft;
  49. import net.minecraft.client.gui.screens.Screen;
  50. import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
  51. import net.minecraft.resources.ResourceLocation;
  52. import net.minecraft.world.InteractionResult;
  53. import net.minecraft.world.item.ItemStack;
  54. import org.apache.logging.log4j.util.TriConsumer;
  55. import org.jetbrains.annotations.ApiStatus;
  56. import org.jetbrains.annotations.NotNull;
  57. import org.jetbrains.annotations.Nullable;
  58. import java.util.LinkedHashSet;
  59. import java.util.List;
  60. import java.util.Optional;
  61. import static me.shedaniel.rei.impl.Internals.attachInstance;
  62. @ApiStatus.Internal
  63. @Environment(EnvType.CLIENT)
  64. public class ScreenHelper implements ClientModInitializer, REIHelper {
  65. private static final ResourceLocation DISPLAY_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/display.png");
  66. private static final ResourceLocation DISPLAY_TEXTURE_DARK = new ResourceLocation("roughlyenoughitems", "textures/gui/display_dark.png");
  67. private OverlaySearchField searchField;
  68. @ApiStatus.Internal
  69. public static List<ItemStack> inventoryStacks = Lists.newArrayList();
  70. private static ContainerScreenOverlay overlay;
  71. private static AbstractContainerScreen<?> previousContainerScreen = null;
  72. private static LinkedHashSet<RecipeScreen> lastRecipeScreen = Sets.newLinkedHashSetWithExpectedSize(5);
  73. private static ScreenHelper instance;
  74. /**
  75. * @return the instance of screen helper
  76. * @see REIHelper#getInstance()
  77. */
  78. @ApiStatus.Internal
  79. public static ScreenHelper getInstance() {
  80. return instance;
  81. }
  82. @Override
  83. public void queueTooltip(@Nullable Tooltip tooltip) {
  84. if (overlay != null && tooltip != null) {
  85. overlay.addTooltip(tooltip);
  86. }
  87. }
  88. @Override
  89. @Nullable
  90. public TextFieldWidget getSearchTextField() {
  91. return searchField;
  92. }
  93. @Override
  94. public @NotNull List<ItemStack> getInventoryStacks() {
  95. return inventoryStacks;
  96. }
  97. @Nullable
  98. public static OverlaySearchField getSearchField() {
  99. return (OverlaySearchField) getInstance().getSearchTextField();
  100. }
  101. @ApiStatus.Internal
  102. public static void setSearchField(OverlaySearchField searchField) {
  103. getInstance().searchField = searchField;
  104. }
  105. public static void storeRecipeScreen(RecipeScreen screen) {
  106. while (lastRecipeScreen.size() >= 5)
  107. lastRecipeScreen.remove(Iterables.get(lastRecipeScreen, 0));
  108. lastRecipeScreen.add(screen);
  109. }
  110. public static boolean hasLastRecipeScreen() {
  111. return !lastRecipeScreen.isEmpty();
  112. }
  113. public static Screen getLastRecipeScreen() {
  114. RecipeScreen screen = Iterables.getLast(lastRecipeScreen);
  115. lastRecipeScreen.remove(screen);
  116. screen.recalculateCategoryPage();
  117. return (Screen) screen;
  118. }
  119. @ApiStatus.Internal
  120. public static void clearLastRecipeScreenData() {
  121. lastRecipeScreen.clear();
  122. }
  123. public static boolean isOverlayVisible() {
  124. return ConfigObject.getInstance().isOverlayVisible();
  125. }
  126. public static void toggleOverlayVisible() {
  127. ConfigObject.getInstance().setOverlayVisible(!ConfigObject.getInstance().isOverlayVisible());
  128. ConfigManager.getInstance().saveConfig();
  129. }
  130. public static Optional<ContainerScreenOverlay> getOptionalOverlay() {
  131. return Optional.ofNullable(overlay);
  132. }
  133. @Override
  134. public Optional<REIOverlay> getOverlay() {
  135. return Optional.ofNullable(overlay);
  136. }
  137. public static ContainerScreenOverlay getLastOverlay(boolean reset, boolean setPage) {
  138. if (overlay == null || reset) {
  139. overlay = new ContainerScreenOverlay();
  140. overlay.init();
  141. getSearchField().setFocused(false);
  142. }
  143. return overlay;
  144. }
  145. public static ContainerScreenOverlay getLastOverlay() {
  146. return getLastOverlay(false, false);
  147. }
  148. /**
  149. * @see REIHelper#getPreviousContainerScreen()
  150. */
  151. @Deprecated
  152. @ApiStatus.ScheduledForRemoval
  153. public static AbstractContainerScreen<?> getLastHandledScreen() {
  154. return previousContainerScreen;
  155. }
  156. @Override
  157. public AbstractContainerScreen<?> getPreviousContainerScreen() {
  158. return previousContainerScreen;
  159. }
  160. public static void setPreviousContainerScreen(AbstractContainerScreen<?> previousContainerScreen) {
  161. ScreenHelper.previousContainerScreen = previousContainerScreen;
  162. }
  163. public static void drawHoveringWidget(PoseStack matrices, int x, int y, TriConsumer<PoseStack, Point, Float> consumer, int width, int height, float delta) {
  164. Window window = Minecraft.getInstance().getWindow();
  165. drawHoveringWidget(matrices, window.getGuiScaledWidth(), window.getGuiScaledHeight(), x, y, consumer, width, height, delta);
  166. }
  167. 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) {
  168. int actualX = Math.max(x + 12, 6);
  169. int actualY = Math.min(y - height / 2, screenHeight - height - 6);
  170. if (actualX + width > screenWidth)
  171. actualX -= 24 + width;
  172. if (actualY < 6)
  173. actualY += 24;
  174. consumer.accept(matrices, new Point(actualX, actualY), delta);
  175. }
  176. /**
  177. * @deprecated Please switch to {@link REIHelper#isDarkThemeEnabled()}
  178. */
  179. @Deprecated
  180. @ApiStatus.Internal
  181. @ApiStatus.ScheduledForRemoval
  182. public static boolean isDarkModeEnabled() {
  183. return ConfigObject.getInstance().isUsingDarkTheme();
  184. }
  185. @Override
  186. public boolean isDarkThemeEnabled() {
  187. return isDarkModeEnabled();
  188. }
  189. @Override
  190. public @NotNull ResourceLocation getDefaultDisplayTexture() {
  191. return isDarkThemeEnabled() ? DISPLAY_TEXTURE_DARK : DISPLAY_TEXTURE;
  192. }
  193. public ScreenHelper() {
  194. ScreenHelper.instance = this;
  195. attachInstance(instance, REIHelper.class);
  196. }
  197. public static SearchFieldLocation getContextualSearchFieldLocation() {
  198. Window window = Minecraft.getInstance().getWindow();
  199. for (OverlayDecider decider : DisplayHelper.getInstance().getSortedOverlayDeciders(Minecraft.getInstance().screen.getClass())) {
  200. if (decider instanceof DisplayHelper.DisplayBoundsProvider) {
  201. Rectangle containerBounds = ((DisplayHelper.DisplayBoundsProvider<Screen>) decider).getScreenBounds(Minecraft.getInstance().screen);
  202. if (window.getGuiScaledHeight() - 20 <= containerBounds.getMaxY())
  203. return SearchFieldLocation.BOTTOM_SIDE;
  204. else break;
  205. }
  206. }
  207. return ConfigObject.getInstance().getSearchFieldLocation();
  208. }
  209. public static Rectangle getItemListArea(Rectangle bounds) {
  210. SearchFieldLocation searchFieldLocation = ScreenHelper.getContextualSearchFieldLocation();
  211. int yOffset = 2;
  212. if (searchFieldLocation == SearchFieldLocation.TOP_SIDE) yOffset += 24;
  213. if (!ConfigObject.getInstance().isEntryListWidgetScrolled()) yOffset += 22;
  214. int heightOffset = 0;
  215. if (searchFieldLocation == SearchFieldLocation.BOTTOM_SIDE) heightOffset += 24;
  216. return new Rectangle(bounds.x, bounds.y + yOffset, bounds.width, bounds.height - 1 - yOffset - heightOffset);
  217. }
  218. public static Rectangle getFavoritesListArea(Rectangle bounds) {
  219. int yOffset = 8;
  220. if (ConfigObject.getInstance().doesShowUtilsButtons()) yOffset += 50;
  221. else if (!ConfigObject.getInstance().isLowerConfigButton()) yOffset += 25;
  222. return new Rectangle(bounds.x, bounds.y + yOffset, bounds.width, bounds.height - 3 - yOffset);
  223. }
  224. @Override
  225. public void onInitializeClient() {
  226. ClothClientHooks.SCREEN_INIT_PRE.register((client, screen, screenHooks) -> {
  227. if ((!RoughlyEnoughItemsState.getErrors().isEmpty() || !RoughlyEnoughItemsState.getWarnings().isEmpty()) && !(screen instanceof WarningAndErrorScreen)) {
  228. WarningAndErrorScreen warningAndErrorScreen = WarningAndErrorScreen.INSTANCE.get();
  229. warningAndErrorScreen.setParent(screen);
  230. try {
  231. if (client.screen != null) client.screen.removed();
  232. } catch (Throwable ignored) {
  233. }
  234. client.screen = null;
  235. client.setScreen(warningAndErrorScreen);
  236. } else if (previousContainerScreen != screen && screen instanceof AbstractContainerScreen)
  237. previousContainerScreen = (AbstractContainerScreen<?>) screen;
  238. return InteractionResult.PASS;
  239. });
  240. boolean loaded = FabricLoader.getInstance().isModLoaded("fabric-events-lifecycle-v0");
  241. if (!loaded) {
  242. RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all");
  243. return;
  244. }
  245. Executor.run(() -> () -> {
  246. ClientTickCallback.EVENT.register(minecraftClient -> {
  247. if (isOverlayVisible() && getSearchField() != null)
  248. getSearchField().tick();
  249. });
  250. });
  251. }
  252. }