ScreenHelper.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 me.shedaniel.cloth.hooks.ClothClientHooks;
  28. import me.shedaniel.rei.api.ConfigManager;
  29. import me.shedaniel.rei.api.ConfigObject;
  30. import me.shedaniel.rei.api.REIHelper;
  31. import me.shedaniel.rei.gui.ContainerScreenOverlay;
  32. import me.shedaniel.rei.gui.OverlaySearchField;
  33. import me.shedaniel.rei.gui.RecipeScreen;
  34. import me.shedaniel.rei.gui.widget.QueuedTooltip;
  35. import me.shedaniel.rei.gui.widget.TextFieldWidget;
  36. import me.shedaniel.rei.listeners.ContainerScreenHooks;
  37. import net.fabricmc.api.ClientModInitializer;
  38. import net.fabricmc.fabric.api.event.client.ClientTickCallback;
  39. import net.minecraft.client.MinecraftClient;
  40. import net.minecraft.client.gui.screen.Screen;
  41. import net.minecraft.client.gui.screen.ingame.ScreenWithHandler;
  42. import net.minecraft.client.util.Window;
  43. import net.minecraft.item.ItemStack;
  44. import net.minecraft.util.ActionResult;
  45. import org.apache.logging.log4j.util.TriConsumer;
  46. import org.jetbrains.annotations.ApiStatus;
  47. import org.jetbrains.annotations.Nullable;
  48. import java.util.LinkedHashSet;
  49. import java.util.List;
  50. import java.util.Optional;
  51. @ApiStatus.Internal
  52. public class ScreenHelper implements ClientModInitializer, REIHelper {
  53. private OverlaySearchField searchField;
  54. @ApiStatus.Internal
  55. public static List<ItemStack> inventoryStacks = Lists.newArrayList();
  56. private static ContainerScreenOverlay overlay;
  57. private static ScreenWithHandler<?> lastScreenWithHandler = null;
  58. private static LinkedHashSet<RecipeScreen> lastRecipeScreen = Sets.newLinkedHashSetWithExpectedSize(5);
  59. private static ScreenHelper instance;
  60. /**
  61. * @return the instance of screen helper
  62. * @see REIHelper#getInstance()
  63. */
  64. @ApiStatus.Internal
  65. public static ScreenHelper getInstance() {
  66. return instance;
  67. }
  68. @Override
  69. public void addTooltip(@Nullable QueuedTooltip tooltip) {
  70. if (overlay != null && tooltip != null) {
  71. overlay.addTooltip(tooltip);
  72. }
  73. }
  74. @Override
  75. public TextFieldWidget getSearchTextField() {
  76. return searchField;
  77. }
  78. @Override
  79. public List<ItemStack> getInventoryStacks() {
  80. return inventoryStacks;
  81. }
  82. public static OverlaySearchField getSearchField() {
  83. return (OverlaySearchField) getInstance().getSearchTextField();
  84. }
  85. @ApiStatus.Internal
  86. public static void setSearchField(OverlaySearchField searchField) {
  87. getInstance().searchField = searchField;
  88. }
  89. public static void storeRecipeScreen(RecipeScreen screen) {
  90. while (lastRecipeScreen.size() >= 5)
  91. lastRecipeScreen.remove(Iterables.get(lastRecipeScreen, 0));
  92. lastRecipeScreen.add(screen);
  93. }
  94. public static boolean hasLastRecipeScreen() {
  95. return !lastRecipeScreen.isEmpty();
  96. }
  97. public static Screen getLastRecipeScreen() {
  98. RecipeScreen screen = Iterables.getLast(lastRecipeScreen);
  99. lastRecipeScreen.remove(screen);
  100. screen.recalculateCategoryPage();
  101. return (Screen) screen;
  102. }
  103. @ApiStatus.Internal
  104. public static void clearLastRecipeScreenData() {
  105. lastRecipeScreen.clear();
  106. }
  107. public static boolean isOverlayVisible() {
  108. return ConfigObject.getInstance().isOverlayVisible();
  109. }
  110. public static void toggleOverlayVisible() {
  111. ConfigObject.getInstance().setOverlayVisible(!ConfigObject.getInstance().isOverlayVisible());
  112. ConfigManager.getInstance().saveConfig();
  113. }
  114. public static Optional<ContainerScreenOverlay> getOptionalOverlay() {
  115. return Optional.ofNullable(overlay);
  116. }
  117. public static ContainerScreenOverlay getLastOverlay(boolean reset, boolean setPage) {
  118. if (overlay == null || reset) {
  119. overlay = new ContainerScreenOverlay();
  120. overlay.init();
  121. getSearchField().setFocused(false);
  122. }
  123. return overlay;
  124. }
  125. public static ContainerScreenOverlay getLastOverlay() {
  126. return getLastOverlay(false, false);
  127. }
  128. @Deprecated
  129. @ApiStatus.ScheduledForRemoval
  130. public static ScreenWithHandler<?> getLastContainerScreen() {
  131. return getLastScreenWithHandler();
  132. }
  133. public static ScreenWithHandler<?> getLastScreenWithHandler() {
  134. return lastScreenWithHandler;
  135. }
  136. @Deprecated
  137. @ApiStatus.ScheduledForRemoval
  138. public static void setLastContainerScreen(ScreenWithHandler<?> lastScreenWithHandler) {
  139. setLastScreenWithHandler(lastScreenWithHandler);
  140. }
  141. public static void setLastScreenWithHandler(ScreenWithHandler<?> lastScreenWithHandler) {
  142. ScreenHelper.lastScreenWithHandler = lastScreenWithHandler;
  143. }
  144. @Deprecated
  145. @ApiStatus.ScheduledForRemoval
  146. public static ContainerScreenHooks getLastContainerScreenHooks() {
  147. return getLastScreenWithHandlerHooks();
  148. }
  149. public static ContainerScreenHooks getLastScreenWithHandlerHooks() {
  150. return (ContainerScreenHooks) lastScreenWithHandler;
  151. }
  152. public static void drawHoveringWidget(int x, int y, TriConsumer<Integer, Integer, Float> consumer, int width, int height, float delta) {
  153. Window window = MinecraftClient.getInstance().getWindow();
  154. drawHoveringWidget(window.getScaledWidth(), window.getScaledHeight(), x, y, consumer, width, height, delta);
  155. }
  156. public static void drawHoveringWidget(int screenWidth, int screenHeight, int x, int y, TriConsumer<Integer, Integer, Float> consumer, int width, int height, float delta) {
  157. int actualX = Math.max(x + 12, 6);
  158. int actualY = Math.min(y - height / 2, screenHeight - height - 6);
  159. if (actualX + width > screenWidth)
  160. actualX -= 24 + width;
  161. if (actualY < 6)
  162. actualY += 24;
  163. consumer.accept(actualX, actualY, delta);
  164. }
  165. @Deprecated
  166. @ApiStatus.Internal
  167. @ApiStatus.ScheduledForRemoval
  168. public static boolean isDarkModeEnabled() {
  169. return ConfigObject.getInstance().isUsingDarkTheme();
  170. }
  171. @Override
  172. public boolean isDarkThemeEnabled() {
  173. return isDarkModeEnabled();
  174. }
  175. public ScreenHelper() {
  176. ScreenHelper.instance = this;
  177. }
  178. @Override
  179. public void onInitializeClient() {
  180. ClothClientHooks.SCREEN_INIT_PRE.register((client, screen, screenHooks) -> {
  181. if (lastScreenWithHandler != screen && screen instanceof ScreenWithHandler)
  182. lastScreenWithHandler = (ScreenWithHandler<?>) screen;
  183. return ActionResult.PASS;
  184. });
  185. ClientTickCallback.EVENT.register(minecraftClient -> {
  186. if (isOverlayVisible() && getSearchField() != null)
  187. getSearchField().tick();
  188. });
  189. }
  190. }