DisplayHelper.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. interface DisplayBoundsProvider<T> extends OverlayDecider {
  89. /**
  90. * @param screen the screen
  91. * @return the boundary of the base container panel.
  92. */
  93. Rectangle getScreenBounds(T screen);
  94. /**
  95. * Gets the base supported class for the bounds handler
  96. *
  97. * @return the base class
  98. */
  99. Class<?> getBaseSupportedClass();
  100. @Override
  101. default boolean isHandingScreen(Class<?> screen) {
  102. return getBaseSupportedClass().isAssignableFrom(screen);
  103. }
  104. }
  105. @Deprecated
  106. @ApiStatus.ScheduledForRemoval
  107. interface DisplayBoundsHandler<T> extends OverlayDecider {
  108. /**
  109. * Gets the base supported class for the bounds handler
  110. *
  111. * @return the base class
  112. */
  113. Class<?> getBaseSupportedClass();
  114. @Override
  115. default boolean isHandingScreen(Class<?> screen) {
  116. return getBaseSupportedClass().isAssignableFrom(screen);
  117. }
  118. /**
  119. * Gets the left bounds of the overlay
  120. *
  121. * @param screen the current screen
  122. * @return the left bounds
  123. */
  124. Rectangle getLeftBounds(T screen);
  125. /**
  126. * Gets the right bounds of the overlay
  127. *
  128. * @param screen the current screen
  129. * @return the right bounds
  130. */
  131. Rectangle getRightBounds(T screen);
  132. /**
  133. * Checks if item slot can fit the screen
  134. *
  135. * @param left the left x coordinates of the stack
  136. * @param top the top y coordinates for the stack
  137. * @param screen the current screen
  138. * @param fullBounds the current bounds
  139. * @return whether the item slot can fit
  140. * @see BaseBoundsHandler#registerExclusionZones(Class, Supplier) for easier api
  141. */
  142. default ActionResult canItemSlotWidgetFit(int left, int top, T screen, Rectangle fullBounds) {
  143. ActionResult fit = isInZone(left, top);
  144. if (fit == ActionResult.FAIL)
  145. return ActionResult.FAIL;
  146. ActionResult fit2 = isInZone(left + 18, top + 18);
  147. if (fit2 == ActionResult.FAIL)
  148. return ActionResult.FAIL;
  149. if (fit == ActionResult.SUCCESS && fit2 == ActionResult.SUCCESS)
  150. return ActionResult.SUCCESS;
  151. return PASS;
  152. }
  153. @Override
  154. default ActionResult isInZone(double mouseX, double mouseY) {
  155. return OverlayDecider.super.isInZone(mouseX, mouseY);
  156. }
  157. /**
  158. * Gets the item list bounds by the overlay bounds
  159. *
  160. * @param rectangle the overlay bounds
  161. * @return the item list bounds
  162. */
  163. default Rectangle getItemListArea(Rectangle rectangle) {
  164. 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));
  165. }
  166. default Rectangle getFavoritesListArea(Rectangle rectangle) {
  167. int offset = 31 + (ConfigObject.getInstance().doesShowUtilsButtons() ? 25 : 0);
  168. return new Rectangle(rectangle.x + 1, rectangle.y + 2 + offset, rectangle.width - 2, rectangle.height - 5 - offset);
  169. }
  170. @Deprecated
  171. @ApiStatus.ScheduledForRemoval
  172. default boolean shouldRecalculateArea(boolean isOnRightSide, Rectangle rectangle) {
  173. return false;
  174. }
  175. @Override
  176. default boolean shouldRecalculateArea(DisplayPanelLocation location, Rectangle rectangle) {
  177. return shouldRecalculateArea(location == DisplayPanelLocation.RIGHT, rectangle);
  178. }
  179. /**
  180. * Gets the priority of the handler, the higher it is, the earlier it is called.
  181. *
  182. * @return the priority in float
  183. */
  184. @Override
  185. float getPriority();
  186. }
  187. }