SubsetsMenu.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.gui.subsets;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import com.google.common.collect.Sets;
  27. import me.shedaniel.clothconfig2.ClothConfigInitializer;
  28. import me.shedaniel.clothconfig2.api.ScissorsHandler;
  29. import me.shedaniel.math.Point;
  30. import me.shedaniel.math.Rectangle;
  31. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  32. import me.shedaniel.rei.api.EntryRegistry;
  33. import me.shedaniel.rei.api.EntryStack;
  34. import me.shedaniel.rei.api.REIHelper;
  35. import me.shedaniel.rei.api.subsets.SubsetsRegistry;
  36. import me.shedaniel.rei.gui.subsets.entries.EntryStackMenuEntry;
  37. import me.shedaniel.rei.gui.subsets.entries.SubMenuEntry;
  38. import me.shedaniel.rei.gui.widget.LateRenderable;
  39. import me.shedaniel.rei.gui.widget.ScrollingContainer;
  40. import me.shedaniel.rei.gui.widget.WidgetWithBounds;
  41. import me.shedaniel.rei.impl.EntryRegistryImpl;
  42. import me.shedaniel.rei.utils.CollectionUtils;
  43. import net.minecraft.client.resource.language.I18n;
  44. import net.minecraft.client.util.math.MatrixStack;
  45. import net.minecraft.item.Item;
  46. import net.minecraft.item.ItemGroup;
  47. import net.minecraft.item.ItemStack;
  48. import net.minecraft.util.collection.DefaultedList;
  49. import net.minecraft.util.registry.Registry;
  50. import org.jetbrains.annotations.ApiStatus;
  51. import org.jetbrains.annotations.NotNull;
  52. import java.util.*;
  53. @ApiStatus.Experimental
  54. @ApiStatus.Internal
  55. public class SubsetsMenu extends WidgetWithBounds implements LateRenderable {
  56. public final Point menuStartPoint;
  57. private final List<SubsetsMenuEntry> entries = Lists.newArrayList();
  58. public final ScrollingContainer scrolling = new ScrollingContainer() {
  59. @Override
  60. public int getMaxScrollHeight() {
  61. int i = 0;
  62. for (SubsetsMenuEntry entry : children()) {
  63. i += entry.getEntryHeight();
  64. }
  65. return i;
  66. }
  67. @Override
  68. public Rectangle getBounds() {
  69. return SubsetsMenu.this.getInnerBounds();
  70. }
  71. @Override
  72. public boolean hasScrollBar() {
  73. return SubsetsMenu.this.hasScrollBar();
  74. }
  75. };
  76. public SubsetsMenu(Point menuStartPoint, Collection<SubsetsMenuEntry> entries) {
  77. this.menuStartPoint = menuStartPoint;
  78. buildEntries(entries);
  79. }
  80. public static SubsetsMenu createFromRegistry(Point menuStartPoint) {
  81. List<EntryStack> stacks = EntryRegistry.getInstance().getStacksList();
  82. Map<String, Object> entries = Maps.newHashMap();
  83. {
  84. // All Entries group
  85. Map<String, Object> allEntries = getOrCreateSubEntryInMap(entries, "roughlyenoughitems:all_entries");
  86. for (EntryStack stack : stacks) {
  87. putEntryInMap(allEntries, stack);
  88. }
  89. }
  90. {
  91. // Item Groups group
  92. Map<String, Object> itemGroups = getOrCreateSubEntryInMap(entries, "roughlyenoughitems:item_groups");
  93. for (Item item : Registry.ITEM) {
  94. ItemGroup group = item.getGroup();
  95. if (group == null)
  96. continue;
  97. DefaultedList<ItemStack> list;
  98. try {
  99. list = new EntryRegistryImpl.DefaultedLinkedList<>(Lists.newLinkedList(), null);
  100. item.appendStacks(group, list);
  101. if (list.isEmpty())
  102. list.add(item.getStackForRender());
  103. Map<String, Object> groupMenu = getOrCreateSubEntryInMap(itemGroups, "_item_group_" + group.getId());
  104. for (ItemStack stack : list) {
  105. putEntryInMap(groupMenu, EntryStack.create(stack));
  106. }
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. Set<String> paths = SubsetsRegistry.INSTANCE.getPaths();
  113. for (String path : paths) {
  114. Map<String, Object> lastMap = entries;
  115. String[] pathSegments = path.split("/");
  116. for (String pathSegment : pathSegments) {
  117. lastMap = getOrCreateSubEntryInMap(lastMap, pathSegment);
  118. }
  119. for (EntryStack entry : SubsetsRegistry.INSTANCE.getPathEntries(path)) {
  120. EntryStack firstStack = CollectionUtils.findFirstOrNullEqualsEntryIgnoreAmount(stacks, entry);
  121. if (firstStack != null)
  122. putEntryInMap(lastMap, firstStack);
  123. }
  124. }
  125. return new SubsetsMenu(menuStartPoint, buildEntries(entries));
  126. }
  127. private static Map<String, Object> getOrCreateSubEntryInMap(Map<String, Object> parent, String pathSegment) {
  128. putEntryInMap(parent, pathSegment);
  129. return (Map<String, Object>) parent.get(pathSegment);
  130. }
  131. private static void putEntryInMap(Map<String, Object> parent, String pathSegment) {
  132. if (!parent.containsKey(pathSegment)) {
  133. parent.put(pathSegment, Maps.newHashMap());
  134. }
  135. }
  136. private static void putEntryInMap(Map<String, Object> parent, EntryStack stack) {
  137. Set<EntryStack> items = (Set<EntryStack>) parent.get("items");
  138. if (items == null) {
  139. items = Sets.newLinkedHashSet();
  140. parent.put("items", items);
  141. }
  142. items.add(stack);
  143. }
  144. private static List<SubsetsMenuEntry> buildEntries(Map<String, Object> map) {
  145. List<SubsetsMenuEntry> entries = Lists.newArrayList();
  146. for (Map.Entry<String, Object> entry : map.entrySet()) {
  147. if (entry.getKey().equals("items")) {
  148. Set<EntryStack> items = (Set<EntryStack>) entry.getValue();
  149. for (EntryStack item : items) {
  150. entries.add(new EntryStackMenuEntry(item));
  151. }
  152. } else {
  153. Map<String, Object> entryMap = (Map<String, Object>) entry.getValue();
  154. if (entry.getKey().startsWith("_item_group_")) {
  155. entries.add(new SubMenuEntry(I18n.translate(entry.getKey().replace("_item_group_", "itemGroup.")), buildEntries(entryMap)));
  156. } else {
  157. String translationKey = "subsets.rei." + entry.getKey().replace(':', '.');
  158. if (!I18n.hasTranslation(translationKey))
  159. RoughlyEnoughItemsCore.LOGGER.warn("Subsets menu " + translationKey + " does not have a translation");
  160. entries.add(new SubMenuEntry(I18n.translate(translationKey), buildEntries(entryMap)));
  161. }
  162. }
  163. }
  164. return entries;
  165. }
  166. @SuppressWarnings("deprecation")
  167. private void buildEntries(Collection<SubsetsMenuEntry> entries) {
  168. this.entries.clear();
  169. this.entries.addAll(entries);
  170. this.entries.sort(Comparator.comparing(entry -> entry instanceof SubMenuEntry ? 0 : 1).thenComparing(entry -> entry instanceof SubMenuEntry ? ((SubMenuEntry) entry).text : ""));
  171. for (SubsetsMenuEntry entry : this.entries) {
  172. entry.parent = this;
  173. }
  174. }
  175. @Override
  176. public @NotNull Rectangle getBounds() {
  177. return new Rectangle(menuStartPoint.x, menuStartPoint.y, getMaxEntryWidth() + 2 + (hasScrollBar() ? 6 : 0), getInnerHeight() + 2);
  178. }
  179. public Rectangle getInnerBounds() {
  180. return new Rectangle(menuStartPoint.x + 1, menuStartPoint.y + 1, getMaxEntryWidth() + (hasScrollBar() ? 6 : 0), getInnerHeight());
  181. }
  182. public boolean hasScrollBar() {
  183. return scrolling.getMaxScrollHeight() > getInnerHeight();
  184. }
  185. public int getInnerHeight() {
  186. return Math.min(scrolling.getMaxScrollHeight(), minecraft.currentScreen.height - 20 - menuStartPoint.y);
  187. }
  188. public int getMaxEntryWidth() {
  189. int i = 0;
  190. for (SubsetsMenuEntry entry : children()) {
  191. if (entry.getEntryWidth() > i)
  192. i = entry.getEntryWidth();
  193. }
  194. return Math.max(10, i);
  195. }
  196. @Override
  197. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  198. Rectangle bounds = getBounds();
  199. Rectangle innerBounds = getInnerBounds();
  200. fill(matrices, bounds.x, bounds.y, bounds.getMaxX(), bounds.getMaxY(), containsMouse(mouseX, mouseY) ? (REIHelper.getInstance().isDarkThemeEnabled() ? -17587 : -1) :-6250336);
  201. fill(matrices, innerBounds.x, innerBounds.y, innerBounds.getMaxX(), innerBounds.getMaxY(), -16777216);
  202. boolean contains = innerBounds.contains(mouseX, mouseY);
  203. SubsetsMenuEntry focused = getFocused() instanceof SubsetsMenuEntry ? (SubsetsMenuEntry) getFocused() : null;
  204. int currentY = (int) (innerBounds.y - scrolling.scrollAmount);
  205. for (SubsetsMenuEntry child : children()) {
  206. boolean containsMouse = contains && mouseY >= currentY && mouseY < currentY + child.getEntryHeight();
  207. if (containsMouse) {
  208. focused = child;
  209. }
  210. currentY += child.getEntryHeight();
  211. }
  212. currentY = (int) (innerBounds.y - scrolling.scrollAmount);
  213. ScissorsHandler.INSTANCE.scissor(scrolling.getScissorBounds());
  214. for (SubsetsMenuEntry child : children()) {
  215. boolean rendering = currentY + child.getEntryHeight() >= innerBounds.y && currentY <= innerBounds.getMaxY();
  216. boolean containsMouse = contains && mouseY >= currentY && mouseY < currentY + child.getEntryHeight();
  217. child.updateInformation(innerBounds.x, currentY, focused == child || containsMouse, containsMouse, rendering, getMaxEntryWidth());
  218. if (rendering)
  219. child.render(matrices, mouseX, mouseY, delta);
  220. currentY += child.getEntryHeight();
  221. }
  222. ScissorsHandler.INSTANCE.removeLastScissor();
  223. setFocused(focused);
  224. scrolling.renderScrollBar();
  225. scrolling.updatePosition(delta);
  226. }
  227. @Override
  228. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  229. if (scrolling.updateDraggingState(mouseX, mouseY, button))
  230. return true;
  231. return super.mouseClicked(mouseX, mouseY, button);
  232. }
  233. @Override
  234. public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
  235. if (scrolling.mouseDragged(mouseX, mouseY, button, deltaX, deltaY))
  236. return true;
  237. return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
  238. }
  239. @Override
  240. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  241. if (getInnerBounds().contains(mouseX, mouseY)) {
  242. scrolling.offset(ClothConfigInitializer.getScrollStep() * -amount, true);
  243. return true;
  244. }
  245. for (SubsetsMenuEntry child : children()) {
  246. if (child instanceof SubMenuEntry) {
  247. if (child.mouseScrolled(mouseX, mouseY, amount))
  248. return true;
  249. }
  250. }
  251. return super.mouseScrolled(mouseX, mouseY, amount);
  252. }
  253. @Override
  254. public List<SubsetsMenuEntry> children() {
  255. return entries;
  256. }
  257. }