DropdownMenuBuilder.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. package me.shedaniel.clothconfig2.impl.builders;
  2. import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry;
  3. import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.DefaultSelectionCellCreator;
  4. import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.DefaultSelectionTopCellElement;
  5. import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.SelectionCellCreator;
  6. import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.SelectionTopCellElement;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.client.MinecraftClient;
  9. import net.minecraft.client.render.item.ItemRenderer;
  10. import net.minecraft.item.Item;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.item.Items;
  13. import net.minecraft.util.Identifier;
  14. import net.minecraft.util.registry.Registry;
  15. import javax.annotation.Nonnull;
  16. import java.util.Collections;
  17. import java.util.Objects;
  18. import java.util.Optional;
  19. import java.util.function.Consumer;
  20. import java.util.function.Function;
  21. import java.util.function.Supplier;
  22. public class DropdownMenuBuilder<T> extends FieldBuilder<T, DropdownBoxEntry<T>> {
  23. protected SelectionTopCellElement<T> topCellElement;
  24. protected SelectionCellCreator<T> cellCreator;
  25. protected Function<T, Optional<String[]>> tooltipSupplier = str -> Optional.empty();
  26. protected Consumer<T> saveConsumer = null;
  27. protected Iterable<T> selections = Collections.emptyList();
  28. public DropdownMenuBuilder(String resetButtonKey, String fieldNameKey, SelectionTopCellElement<T> topCellElement, SelectionCellCreator<T> cellCreator) {
  29. super(resetButtonKey, fieldNameKey);
  30. this.topCellElement = Objects.requireNonNull(topCellElement);
  31. this.cellCreator = Objects.requireNonNull(cellCreator);
  32. }
  33. public DropdownMenuBuilder setSelections(Iterable<T> selections) {
  34. this.selections = selections;
  35. return this;
  36. }
  37. public DropdownMenuBuilder setDefaultValue(Supplier<T> defaultValue) {
  38. this.defaultValue = defaultValue;
  39. return this;
  40. }
  41. public DropdownMenuBuilder setDefaultValue(T defaultValue) {
  42. this.defaultValue = () -> Objects.requireNonNull(defaultValue);
  43. return this;
  44. }
  45. public DropdownMenuBuilder setSaveConsumer(Consumer<T> saveConsumer) {
  46. this.saveConsumer = saveConsumer;
  47. return this;
  48. }
  49. public DropdownMenuBuilder setTooltipSupplier(Supplier<Optional<String[]>> tooltipSupplier) {
  50. this.tooltipSupplier = str -> tooltipSupplier.get();
  51. return this;
  52. }
  53. public DropdownMenuBuilder setTooltipSupplier(Function<T, Optional<String[]>> tooltipSupplier) {
  54. this.tooltipSupplier = tooltipSupplier;
  55. return this;
  56. }
  57. public DropdownMenuBuilder setTooltip(Optional<String[]> tooltip) {
  58. this.tooltipSupplier = str -> tooltip;
  59. return this;
  60. }
  61. public DropdownMenuBuilder setTooltip(String... tooltip) {
  62. this.tooltipSupplier = str -> Optional.ofNullable(tooltip);
  63. return this;
  64. }
  65. public DropdownMenuBuilder requireRestart() {
  66. requireRestart(true);
  67. return this;
  68. }
  69. public DropdownMenuBuilder setErrorSupplier(Function<T, Optional<String>> errorSupplier) {
  70. this.errorSupplier = errorSupplier;
  71. return this;
  72. }
  73. @Nonnull
  74. @Override
  75. public DropdownBoxEntry<T> build() {
  76. DropdownBoxEntry<T> entry = new DropdownBoxEntry<T>(getFieldNameKey(), getResetButtonKey(), null, isRequireRestart(), defaultValue, saveConsumer, selections, topCellElement, cellCreator);
  77. entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
  78. if (errorSupplier != null)
  79. entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
  80. return entry;
  81. }
  82. public static class TopCellElementBuilder {
  83. public static final Function<String, Identifier> IDENTIFIER_FUNCTION = str -> {
  84. try {
  85. return new Identifier(str);
  86. } catch (NumberFormatException e) {
  87. return null;
  88. }
  89. };
  90. public static final Function<String, Identifier> ITEM_IDENTIFIER_FUNCTION = str -> {
  91. try {
  92. Identifier identifier = new Identifier(str);
  93. if (Registry.ITEM.getOrEmpty(identifier).isPresent())
  94. return identifier;
  95. } catch (Exception ignored) {
  96. }
  97. return null;
  98. };
  99. public static final Function<String, Identifier> BLOCK_IDENTIFIER_FUNCTION = str -> {
  100. try {
  101. Identifier identifier = new Identifier(str);
  102. if (Registry.BLOCK.getOrEmpty(identifier).isPresent())
  103. return identifier;
  104. } catch (Exception ignored) {
  105. }
  106. return null;
  107. };
  108. public static final Function<String, Item> ITEM_FUNCTION = str -> {
  109. try {
  110. return Registry.ITEM.getOrEmpty(new Identifier(str)).get();
  111. } catch (Exception ignored) {
  112. }
  113. return null;
  114. };
  115. public static final Function<String, Block> BLOCK_FUNCTION = str -> {
  116. try {
  117. return Registry.BLOCK.getOrEmpty(new Identifier(str)).get();
  118. } catch (Exception ignored) {
  119. }
  120. return null;
  121. };
  122. private static final ItemStack BARRIER = new ItemStack(Items.BARRIER);
  123. public static <T> SelectionTopCellElement<T> of(T value, Function<String, T> toObjectFunction) {
  124. return of(value, toObjectFunction, Object::toString);
  125. }
  126. public static <T> SelectionTopCellElement<T> of(T value, Function<String, T> toObjectFunction, Function<T, String> toStringFunction) {
  127. return new DefaultSelectionTopCellElement<>(value, toObjectFunction, toStringFunction);
  128. }
  129. public static SelectionTopCellElement<Identifier> ofItemIdentifier(Item item) {
  130. return new DefaultSelectionTopCellElement<Identifier>(Registry.ITEM.getId(item), ITEM_IDENTIFIER_FUNCTION, Identifier::toString) {
  131. @Override
  132. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  133. textFieldWidget.x = x + 4;
  134. textFieldWidget.y = y + 6;
  135. textFieldWidget.setWidth(width - 4 - 20);
  136. textFieldWidget.setEditable(getParent().isEditable());
  137. textFieldWidget.setEditableColor(getPreferredTextColor());
  138. textFieldWidget.render(mouseX, mouseY, delta);
  139. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  140. ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(Registry.ITEM.get(getValue()));
  141. itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
  142. }
  143. };
  144. }
  145. public static SelectionTopCellElement<Identifier> ofBlockIdentifier(Block block) {
  146. return new DefaultSelectionTopCellElement<Identifier>(Registry.BLOCK.getId(block), BLOCK_IDENTIFIER_FUNCTION, Identifier::toString) {
  147. @Override
  148. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  149. textFieldWidget.x = x + 4;
  150. textFieldWidget.y = y + 6;
  151. textFieldWidget.setWidth(width - 4 - 20);
  152. textFieldWidget.setEditable(getParent().isEditable());
  153. textFieldWidget.setEditableColor(getPreferredTextColor());
  154. textFieldWidget.render(mouseX, mouseY, delta);
  155. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  156. ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(Registry.BLOCK.get(getValue()));
  157. itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
  158. }
  159. };
  160. }
  161. public static SelectionTopCellElement<Item> ofItemObject(Item item) {
  162. return new DefaultSelectionTopCellElement<Item>(item, ITEM_FUNCTION, i -> Registry.ITEM.getId(i).toString()) {
  163. @Override
  164. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  165. textFieldWidget.x = x + 4;
  166. textFieldWidget.y = y + 6;
  167. textFieldWidget.setWidth(width - 4 - 20);
  168. textFieldWidget.setEditable(getParent().isEditable());
  169. textFieldWidget.setEditableColor(getPreferredTextColor());
  170. textFieldWidget.render(mouseX, mouseY, delta);
  171. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  172. ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(getValue());
  173. itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
  174. }
  175. };
  176. }
  177. public static SelectionTopCellElement<Block> ofBlockObject(Block block) {
  178. return new DefaultSelectionTopCellElement<Block>(block, BLOCK_FUNCTION, i -> Registry.BLOCK.getId(i).toString()) {
  179. @Override
  180. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  181. textFieldWidget.x = x + 4;
  182. textFieldWidget.y = y + 6;
  183. textFieldWidget.setWidth(width - 4 - 20);
  184. textFieldWidget.setEditable(getParent().isEditable());
  185. textFieldWidget.setEditableColor(getPreferredTextColor());
  186. textFieldWidget.render(mouseX, mouseY, delta);
  187. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  188. ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(getValue());
  189. itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
  190. }
  191. };
  192. }
  193. }
  194. public static class CellCreatorBuilder {
  195. public static <T> SelectionCellCreator<T> of() {
  196. return new DefaultSelectionCellCreator<>();
  197. }
  198. public static <T> SelectionCellCreator<T> of(Function<T, String> toStringFunction) {
  199. return new DefaultSelectionCellCreator<>(toStringFunction);
  200. }
  201. public static <T> SelectionCellCreator<T> ofWidth(int cellWidth) {
  202. return new DefaultSelectionCellCreator<T>() {
  203. @Override
  204. public int getCellWidth() {
  205. return cellWidth;
  206. }
  207. };
  208. }
  209. public static <T> SelectionCellCreator<T> ofWidth(int cellWidth, Function<T, String> toStringFunction) {
  210. return new DefaultSelectionCellCreator<T>(toStringFunction) {
  211. @Override
  212. public int getCellWidth() {
  213. return cellWidth;
  214. }
  215. };
  216. }
  217. public static <T> SelectionCellCreator<T> ofCellCount(int maxItems) {
  218. return new DefaultSelectionCellCreator<T>() {
  219. @Override
  220. public int getDropBoxMaxHeight() {
  221. return getCellHeight() * maxItems;
  222. }
  223. };
  224. }
  225. public static <T> SelectionCellCreator<T> ofCellCount(int maxItems, Function<T, String> toStringFunction) {
  226. return new DefaultSelectionCellCreator<T>(toStringFunction) {
  227. @Override
  228. public int getDropBoxMaxHeight() {
  229. return getCellHeight() * maxItems;
  230. }
  231. };
  232. }
  233. public static <T> SelectionCellCreator<T> of(int cellWidth, int maxItems) {
  234. return new DefaultSelectionCellCreator<T>() {
  235. @Override
  236. public int getCellWidth() {
  237. return cellWidth;
  238. }
  239. @Override
  240. public int getDropBoxMaxHeight() {
  241. return getCellHeight() * maxItems;
  242. }
  243. };
  244. }
  245. public static <T> SelectionCellCreator<T> of(int cellWidth, int maxItems, Function<T, String> toStringFunction) {
  246. return new DefaultSelectionCellCreator<T>(toStringFunction) {
  247. @Override
  248. public int getCellWidth() {
  249. return cellWidth;
  250. }
  251. @Override
  252. public int getDropBoxMaxHeight() {
  253. return getCellHeight() * maxItems;
  254. }
  255. };
  256. }
  257. public static <T> SelectionCellCreator<T> of(int cellHeight, int cellWidth, int maxItems) {
  258. return new DefaultSelectionCellCreator<T>() {
  259. @Override
  260. public int getCellHeight() {
  261. return cellHeight;
  262. }
  263. @Override
  264. public int getCellWidth() {
  265. return cellWidth;
  266. }
  267. @Override
  268. public int getDropBoxMaxHeight() {
  269. return getCellHeight() * maxItems;
  270. }
  271. };
  272. }
  273. public static <T> SelectionCellCreator<T> of(int cellHeight, int cellWidth, int maxItems, Function<T, String> toStringFunction) {
  274. return new DefaultSelectionCellCreator<T>(toStringFunction) {
  275. @Override
  276. public int getCellHeight() {
  277. return cellHeight;
  278. }
  279. @Override
  280. public int getCellWidth() {
  281. return cellWidth;
  282. }
  283. @Override
  284. public int getDropBoxMaxHeight() {
  285. return getCellHeight() * maxItems;
  286. }
  287. };
  288. }
  289. public static SelectionCellCreator<Identifier> ofItemIdentifier() {
  290. return ofItemIdentifier(20, 146, 7);
  291. }
  292. public static SelectionCellCreator<Identifier> ofItemIdentifier(int maxItems) {
  293. return ofItemIdentifier(20, 146, maxItems);
  294. }
  295. public static SelectionCellCreator<Identifier> ofItemIdentifier(int cellHeight, int cellWidth, int maxItems) {
  296. return new DefaultSelectionCellCreator<Identifier>() {
  297. @Override
  298. public DropdownBoxEntry.SelectionCellElement<Identifier> create(Identifier selection) {
  299. ItemStack s = new ItemStack(Registry.ITEM.get(selection));
  300. return new DropdownBoxEntry.DefaultSelectionCellElement<Identifier>(selection, toStringFunction) {
  301. @Override
  302. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  303. rendering = true;
  304. this.x = x;
  305. this.y = y;
  306. this.width = width;
  307. this.height = height;
  308. boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
  309. if (b)
  310. fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
  311. MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
  312. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  313. itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
  314. }
  315. };
  316. }
  317. @Override
  318. public int getCellHeight() {
  319. return cellHeight;
  320. }
  321. @Override
  322. public int getCellWidth() {
  323. return cellWidth;
  324. }
  325. @Override
  326. public int getDropBoxMaxHeight() {
  327. return getCellHeight() * maxItems;
  328. }
  329. };
  330. }
  331. public static SelectionCellCreator<Identifier> ofBlockIdentifier() {
  332. return ofBlockIdentifier(20, 146, 7);
  333. }
  334. public static SelectionCellCreator<Identifier> ofBlockIdentifier(int maxItems) {
  335. return ofBlockIdentifier(20, 146, maxItems);
  336. }
  337. public static SelectionCellCreator<Identifier> ofBlockIdentifier(int cellHeight, int cellWidth, int maxItems) {
  338. return new DefaultSelectionCellCreator<Identifier>() {
  339. @Override
  340. public DropdownBoxEntry.SelectionCellElement<Identifier> create(Identifier selection) {
  341. ItemStack s = new ItemStack(Registry.BLOCK.get(selection));
  342. return new DropdownBoxEntry.DefaultSelectionCellElement<Identifier>(selection, toStringFunction) {
  343. @Override
  344. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  345. rendering = true;
  346. this.x = x;
  347. this.y = y;
  348. this.width = width;
  349. this.height = height;
  350. boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
  351. if (b)
  352. fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
  353. MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
  354. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  355. itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
  356. }
  357. };
  358. }
  359. @Override
  360. public int getCellHeight() {
  361. return cellHeight;
  362. }
  363. @Override
  364. public int getCellWidth() {
  365. return cellWidth;
  366. }
  367. @Override
  368. public int getDropBoxMaxHeight() {
  369. return getCellHeight() * maxItems;
  370. }
  371. };
  372. }
  373. public static SelectionCellCreator<Item> ofItemObject() {
  374. return ofItemObject(20, 146, 7);
  375. }
  376. public static SelectionCellCreator<Item> ofItemObject(int maxItems) {
  377. return ofItemObject(20, 146, maxItems);
  378. }
  379. public static SelectionCellCreator<Item> ofItemObject(int cellHeight, int cellWidth, int maxItems) {
  380. return new DefaultSelectionCellCreator<Item>(i -> Registry.ITEM.getId(i).toString()) {
  381. @Override
  382. public DropdownBoxEntry.SelectionCellElement<Item> create(Item selection) {
  383. ItemStack s = new ItemStack(selection);
  384. return new DropdownBoxEntry.DefaultSelectionCellElement<Item>(selection, toStringFunction) {
  385. @Override
  386. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  387. rendering = true;
  388. this.x = x;
  389. this.y = y;
  390. this.width = width;
  391. this.height = height;
  392. boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
  393. if (b)
  394. fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
  395. MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
  396. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  397. itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
  398. }
  399. };
  400. }
  401. @Override
  402. public int getCellHeight() {
  403. return cellHeight;
  404. }
  405. @Override
  406. public int getCellWidth() {
  407. return cellWidth;
  408. }
  409. @Override
  410. public int getDropBoxMaxHeight() {
  411. return getCellHeight() * maxItems;
  412. }
  413. };
  414. }
  415. public static SelectionCellCreator<Block> ofBlockObject() {
  416. return ofBlockObject(20, 146, 7);
  417. }
  418. public static SelectionCellCreator<Block> ofBlockObject(int maxItems) {
  419. return ofBlockObject(20, 146, maxItems);
  420. }
  421. public static SelectionCellCreator<Block> ofBlockObject(int cellHeight, int cellWidth, int maxItems) {
  422. return new DefaultSelectionCellCreator<Block>(i -> Registry.BLOCK.getId(i).toString()) {
  423. @Override
  424. public DropdownBoxEntry.SelectionCellElement<Block> create(Block selection) {
  425. ItemStack s = new ItemStack(selection);
  426. return new DropdownBoxEntry.DefaultSelectionCellElement<Block>(selection, toStringFunction) {
  427. @Override
  428. public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
  429. rendering = true;
  430. this.x = x;
  431. this.y = y;
  432. this.width = width;
  433. this.height = height;
  434. boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
  435. if (b)
  436. fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
  437. MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
  438. ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
  439. itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
  440. }
  441. };
  442. }
  443. @Override
  444. public int getCellHeight() {
  445. return cellHeight;
  446. }
  447. @Override
  448. public int getCellWidth() {
  449. return cellWidth;
  450. }
  451. @Override
  452. public int getDropBoxMaxHeight() {
  453. return getCellHeight() * maxItems;
  454. }
  455. };
  456. }
  457. }
  458. }