SubsetsMenu.java 12 KB

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