DisplayHelper.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.api;
  24. import me.shedaniel.math.Rectangle;
  25. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  26. import me.shedaniel.rei.gui.config.DisplayPanelLocation;
  27. import me.shedaniel.rei.gui.config.SearchFieldLocation;
  28. import net.fabricmc.api.EnvType;
  29. import net.fabricmc.api.Environment;
  30. import net.minecraft.util.ActionResult;
  31. import org.jetbrains.annotations.ApiStatus;
  32. import org.jetbrains.annotations.NotNull;
  33. import java.util.List;
  34. import java.util.function.Supplier;
  35. import static net.minecraft.util.ActionResult.PASS;
  36. @Environment(EnvType.CLIENT)
  37. public interface DisplayHelper {
  38. /**
  39. * @return the api instance of {@link me.shedaniel.rei.impl.DisplayHelperImpl}
  40. */
  41. @NotNull
  42. static DisplayHelper getInstance() {
  43. return RoughlyEnoughItemsCore.getDisplayHelper();
  44. }
  45. /**
  46. * Gets the sorted version of all responsible bounds handlers
  47. *
  48. * @param screenClass the class for checking responsible bounds handlers
  49. * @return the sorted list of responsible bounds handlers
  50. * @see DisplayHelper#getResponsibleBoundsHandler(Class) for the unsorted version
  51. */
  52. @Deprecated
  53. @ApiStatus.ScheduledForRemoval
  54. List<DisplayBoundsHandler<?>> getSortedBoundsHandlers(Class<?> screenClass);
  55. List<OverlayDecider> getSortedOverlayDeciders(Class<?> screenClass);
  56. /**
  57. * Gets all registered overlay deciders
  58. *
  59. * @return the list of registered overlay deciders
  60. */
  61. List<OverlayDecider> getAllOverlayDeciders();
  62. /**
  63. * Gets the responsible bounds handlers
  64. *
  65. * @param screenClass the class for checking responsible bounds handlers
  66. * @return the the list of responsible bounds handlers
  67. * @see DisplayHelper#getSortedBoundsHandlers(Class) for the sorted version
  68. */
  69. @Deprecated
  70. @ApiStatus.ScheduledForRemoval
  71. DisplayBoundsHandler<?> getResponsibleBoundsHandler(Class<?> screenClass);
  72. /**
  73. * Registers a bounds decider
  74. *
  75. * @param decider the decider to register
  76. */
  77. void registerHandler(OverlayDecider decider);
  78. default <T> void registerProvider(DisplayBoundsProvider<T> provider) {
  79. registerHandler(provider);
  80. }
  81. /**
  82. * Gets the left bounds of the overlay
  83. *
  84. * @param screen the current screen
  85. * @return the left bounds
  86. */
  87. <T> Rectangle getOverlayBounds(DisplayPanelLocation location, T screen);
  88. @ApiStatus.Experimental
  89. void resetCache();
  90. interface DisplayBoundsProvider<T> extends OverlayDecider {
  91. /**
  92. * @param screen the screen
  93. * @return the boundary of the base container panel.
  94. */
  95. Rectangle getScreenBounds(T screen);
  96. /**
  97. * Gets the base supported class for the bounds handler
  98. *
  99. * @return the base class
  100. */
  101. Class<?> getBaseSupportedClass();
  102. @Override
  103. default boolean isHandingScreen(Class<?> screen) {
  104. return getBaseSupportedClass().isAssignableFrom(screen);
  105. }
  106. }
  107. @Deprecated
  108. @ApiStatus.ScheduledForRemoval
  109. interface DisplayBoundsHandler<T> extends OverlayDecider {
  110. /**
  111. * Gets the base supported class for the bounds handler
  112. *
  113. * @return the base class
  114. */
  115. Class<?> getBaseSupportedClass();
  116. @Override
  117. default boolean isHandingScreen(Class<?> screen) {
  118. return getBaseSupportedClass().isAssignableFrom(screen);
  119. }
  120. /**
  121. * Gets the left bounds of the overlay
  122. *
  123. * @param screen the current screen
  124. * @return the left bounds
  125. */
  126. Rectangle getLeftBounds(T screen);
  127. /**
  128. * Gets the right bounds of the overlay
  129. *
  130. * @param screen the current screen
  131. * @return the right bounds
  132. */
  133. Rectangle getRightBounds(T screen);
  134. /**
  135. * Checks if item slot can fit the screen
  136. *
  137. * @param left the left x coordinates of the stack
  138. * @param top the top y coordinates for the stack
  139. * @param screen the current screen
  140. * @param fullBounds the current bounds
  141. * @return whether the item slot can fit
  142. * @see BaseBoundsHandler#registerExclusionZones(Class, Supplier) for easier api
  143. */
  144. default ActionResult canItemSlotWidgetFit(int left, int top, T screen, Rectangle fullBounds) {
  145. ActionResult fit = isInZone(left, top);
  146. if (fit == ActionResult.FAIL)
  147. return ActionResult.FAIL;
  148. ActionResult fit2 = isInZone(left + 18, top + 18);
  149. if (fit2 == ActionResult.FAIL)
  150. return ActionResult.FAIL;
  151. if (fit == ActionResult.SUCCESS && fit2 == ActionResult.SUCCESS)
  152. return ActionResult.SUCCESS;
  153. return PASS;
  154. }
  155. @Override
  156. default ActionResult isInZone(double mouseX, double mouseY) {
  157. return OverlayDecider.super.isInZone(mouseX, mouseY);
  158. }
  159. /**
  160. * Gets the item list bounds by the overlay bounds
  161. *
  162. * @param rectangle the overlay bounds
  163. * @return the item list bounds
  164. */
  165. default Rectangle getItemListArea(Rectangle rectangle) {
  166. return new Rectangle(rectangle.x + 1, rectangle.y + 2 + (ConfigObject.getInstance().getSearchFieldLocation() == SearchFieldLocation.TOP_SIDE ? 24 : 0) + (ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22), rectangle.width - 2, rectangle.height - (ConfigObject.getInstance().getSearchFieldLocation() != SearchFieldLocation.CENTER ? 27 + 22 : 27) + (!ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22));
  167. }
  168. default Rectangle getFavoritesListArea(Rectangle rectangle) {
  169. int offset = 31 + (ConfigObject.getInstance().doesShowUtilsButtons() ? 25 : 0);
  170. return new Rectangle(rectangle.x + 1, rectangle.y + 2 + offset, rectangle.width - 2, rectangle.height - 5 - offset);
  171. }
  172. @Deprecated
  173. @ApiStatus.ScheduledForRemoval
  174. default boolean shouldRecalculateArea(boolean isOnRightSide, Rectangle rectangle) {
  175. return false;
  176. }
  177. @Override
  178. default boolean shouldRecalculateArea(DisplayPanelLocation location, Rectangle rectangle) {
  179. return shouldRecalculateArea(location == DisplayPanelLocation.RIGHT, rectangle);
  180. }
  181. /**
  182. * Gets the priority of the handler, the higher it is, the earlier it is called.
  183. *
  184. * @return the priority in float
  185. */
  186. @Override
  187. float getPriority();
  188. }
  189. }