ScreenHelper.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.gui.ContainerScreenOverlay;
  31. import me.shedaniel.rei.gui.OverlaySearchField;
  32. import me.shedaniel.rei.listeners.ContainerScreenHooks;
  33. import net.fabricmc.api.ClientModInitializer;
  34. import net.fabricmc.fabric.api.event.client.ClientTickCallback;
  35. import net.minecraft.client.MinecraftClient;
  36. import net.minecraft.client.gui.screen.Screen;
  37. import net.minecraft.client.gui.screen.ingame.ContainerScreen;
  38. import net.minecraft.client.util.Window;
  39. import net.minecraft.item.ItemStack;
  40. import net.minecraft.util.ActionResult;
  41. import org.apache.logging.log4j.util.TriConsumer;
  42. import org.jetbrains.annotations.ApiStatus;
  43. import java.util.LinkedHashSet;
  44. import java.util.List;
  45. import java.util.Optional;
  46. public class ScreenHelper implements ClientModInitializer {
  47. /**
  48. * @deprecated Use getters instead
  49. */
  50. private static OverlaySearchField searchField;
  51. @ApiStatus.Internal public static List<ItemStack> inventoryStacks = Lists.newArrayList();
  52. private static ContainerScreenOverlay overlay;
  53. private static ContainerScreen<?> lastContainerScreen = null;
  54. private static LinkedHashSet<Screen> lastRecipeScreen = Sets.newLinkedHashSetWithExpectedSize(5);
  55. public static OverlaySearchField getSearchField() {
  56. return searchField;
  57. }
  58. @ApiStatus.Internal
  59. public static void setSearchField(OverlaySearchField searchField) {
  60. ScreenHelper.searchField = searchField;
  61. }
  62. public static void storeRecipeScreen(Screen screen) {
  63. while (lastRecipeScreen.size() >= 5)
  64. lastRecipeScreen.remove(Iterables.get(lastRecipeScreen, 0));
  65. lastRecipeScreen.add(screen);
  66. }
  67. public static boolean hasLastRecipeScreen() {
  68. return !lastRecipeScreen.isEmpty();
  69. }
  70. public static Screen getLastRecipeScreen() {
  71. Screen screen = Iterables.getLast(lastRecipeScreen);
  72. lastRecipeScreen.remove(screen);
  73. return screen;
  74. }
  75. public static void clearLastRecipeScreenData() {
  76. lastRecipeScreen.clear();
  77. }
  78. public static boolean isOverlayVisible() {
  79. return ConfigObject.getInstance().isOverlayVisible();
  80. }
  81. public static void toggleOverlayVisible() {
  82. ConfigObject.getInstance().setOverlayVisible(!ConfigObject.getInstance().isOverlayVisible());
  83. ConfigManager.getInstance().saveConfig();
  84. }
  85. public static Optional<ContainerScreenOverlay> getOptionalOverlay() {
  86. return Optional.ofNullable(overlay);
  87. }
  88. public static ContainerScreenOverlay getLastOverlay(boolean reset, boolean setPage) {
  89. if (overlay == null || reset) {
  90. overlay = new ContainerScreenOverlay();
  91. overlay.init();
  92. getSearchField().setFocused(false);
  93. }
  94. return overlay;
  95. }
  96. public static ContainerScreenOverlay getLastOverlay() {
  97. return getLastOverlay(false, false);
  98. }
  99. public static ContainerScreen<?> getLastContainerScreen() {
  100. return lastContainerScreen;
  101. }
  102. public static void setLastContainerScreen(ContainerScreen<?> lastContainerScreen) {
  103. ScreenHelper.lastContainerScreen = lastContainerScreen;
  104. }
  105. public static ContainerScreenHooks getLastContainerScreenHooks() {
  106. return (ContainerScreenHooks) lastContainerScreen;
  107. }
  108. public static void drawHoveringWidget(int x, int y, TriConsumer<Integer, Integer, Float> consumer, int width, int height, float delta) {
  109. Window window = MinecraftClient.getInstance().getWindow();
  110. drawHoveringWidget(window.getScaledWidth(), window.getScaledHeight(), x, y, consumer, width, height, delta);
  111. }
  112. public static void drawHoveringWidget(int screenWidth, int screenHeight, int x, int y, TriConsumer<Integer, Integer, Float> consumer, int width, int height, float delta) {
  113. int actualX = Math.max(x + 12, 6);
  114. int actualY = Math.min(y - height / 2, screenHeight - height - 6);
  115. if (actualX + width > screenWidth)
  116. actualX -= 24 + width;
  117. if (actualY < 6)
  118. actualY += 24;
  119. consumer.accept(actualX, actualY, delta);
  120. }
  121. public static boolean isDarkModeEnabled() {
  122. return ConfigObject.getInstance().isUsingDarkTheme();
  123. }
  124. @Override
  125. public void onInitializeClient() {
  126. ClothClientHooks.SCREEN_INIT_PRE.register((client, screen, screenHooks) -> {
  127. if (lastContainerScreen != screen && screen instanceof ContainerScreen)
  128. lastContainerScreen = (ContainerScreen<?>) screen;
  129. return ActionResult.PASS;
  130. });
  131. ClientTickCallback.EVENT.register(minecraftClient -> {
  132. if (isOverlayVisible() && getSearchField() != null)
  133. getSearchField().tick();
  134. });
  135. }
  136. }