ClothConfigScreen.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package me.shedaniel.clothconfig2.gui;
  2. import com.google.common.collect.Lists;
  3. import com.google.common.collect.Maps;
  4. import com.google.common.util.concurrent.AtomicDouble;
  5. import com.mojang.blaze3d.systems.RenderSystem;
  6. import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
  7. import me.shedaniel.clothconfig2.api.*;
  8. import me.shedaniel.clothconfig2.gui.entries.KeyCodeEntry;
  9. import me.shedaniel.clothconfig2.gui.widget.DynamicElementListWidget;
  10. import me.shedaniel.math.Rectangle;
  11. import net.fabricmc.api.EnvType;
  12. import net.fabricmc.api.Environment;
  13. import net.minecraft.client.MinecraftClient;
  14. import net.minecraft.client.font.TextRenderer;
  15. import net.minecraft.client.gui.Element;
  16. import net.minecraft.client.gui.screen.ConfirmScreen;
  17. import net.minecraft.client.gui.screen.Screen;
  18. import net.minecraft.client.gui.widget.AbstractButtonWidget;
  19. import net.minecraft.client.gui.widget.AbstractPressableButtonWidget;
  20. import net.minecraft.client.gui.widget.ButtonWidget;
  21. import net.minecraft.client.render.BufferBuilder;
  22. import net.minecraft.client.render.Tessellator;
  23. import net.minecraft.client.render.VertexFormats;
  24. import net.minecraft.client.resource.language.I18n;
  25. import net.minecraft.client.util.InputUtil;
  26. import net.minecraft.text.LiteralText;
  27. import net.minecraft.text.TranslatableText;
  28. import net.minecraft.util.Identifier;
  29. import net.minecraft.util.Pair;
  30. import net.minecraft.util.Tickable;
  31. import net.minecraft.util.math.MathHelper;
  32. import org.jetbrains.annotations.Nullable;
  33. import java.util.LinkedHashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Optional;
  37. import java.util.stream.Collectors;
  38. @SuppressWarnings({"deprecation", "rawtypes", "unchecked", "DuplicatedCode"})
  39. @Environment(EnvType.CLIENT)
  40. public abstract class ClothConfigScreen extends Screen {
  41. private static final Identifier CONFIG_TEX = new Identifier("cloth-config2", "textures/gui/cloth_config.png");
  42. private final List<QueuedTooltip> queuedTooltips = Lists.newArrayList();
  43. public int nextTabIndex;
  44. public int selectedTabIndex;
  45. public double tabsScrollVelocity = 0d;
  46. public double tabsScrollProgress;
  47. public ListWidget<AbstractConfigEntry<AbstractConfigEntry>> listWidget;
  48. private KeyCodeEntry focusedBinding;
  49. private final Screen parent;
  50. private final LinkedHashMap<String, List<AbstractConfigEntry>> tabbedEntries;
  51. private final List<Pair<String, Integer>> tabs;
  52. private boolean edited;
  53. private boolean requiresRestart;
  54. private final boolean confirmSave;
  55. private AbstractButtonWidget quitButton, saveButton, applyButton, buttonLeftTab, buttonRightTab;
  56. private Rectangle tabsBounds, tabsLeftBounds, tabsRightBounds;
  57. private final String title;
  58. private double tabsMaximumScrolled = -1d;
  59. private final boolean displayErrors;
  60. private final List<ClothConfigTabButton> tabButtons;
  61. private boolean smoothScrollingTabs = true;
  62. private boolean smoothScrollingList;
  63. private final Identifier defaultBackgroundLocation;
  64. private final Map<String, Identifier> categoryBackgroundLocation;
  65. private boolean transparentBackground = false;
  66. private boolean editable = true;
  67. @Nullable private String defaultFallbackCategory = null;
  68. private boolean alwaysShowTabs = false;
  69. private ModifierKeyCode startedKeyCode = null;
  70. @Deprecated
  71. public ClothConfigScreen(Screen parent, String title, Map<String, List<Pair<String, Object>>> o, boolean confirmSave, boolean displayErrors, boolean smoothScrollingList, Identifier defaultBackgroundLocation, Map<String, Identifier> categoryBackgroundLocation) {
  72. super(new LiteralText(""));
  73. this.parent = parent;
  74. this.title = title;
  75. this.tabbedEntries = Maps.newLinkedHashMap();
  76. this.smoothScrollingList = smoothScrollingList;
  77. this.defaultBackgroundLocation = defaultBackgroundLocation;
  78. o.forEach((tab, pairs) -> {
  79. List<AbstractConfigEntry> list = Lists.newArrayList();
  80. for (Pair<String, Object> pair : pairs) {
  81. if (pair.getRight() instanceof AbstractConfigListEntry) {
  82. list.add((AbstractConfigListEntry) pair.getRight());
  83. } else {
  84. throw new IllegalArgumentException("Unsupported Type (" + pair.getLeft() + "): " + pair.getRight().getClass().getSimpleName());
  85. }
  86. }
  87. list.forEach(entry -> entry.setScreen(this));
  88. tabbedEntries.put(tab, list);
  89. });
  90. TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
  91. this.tabs = tabbedEntries.keySet().stream().map(s -> new Pair<>(s, textRenderer.getStringWidth(I18n.translate(s)) + 8)).collect(Collectors.toList());
  92. this.nextTabIndex = 0;
  93. this.selectedTabIndex = 0;
  94. for (int i = 0; i < tabs.size(); i++) {
  95. Pair<String, Integer> pair = tabs.get(i);
  96. if (pair.getLeft().equals(getFallbackCategory())) {
  97. this.nextTabIndex = i;
  98. this.selectedTabIndex = i;
  99. break;
  100. }
  101. }
  102. this.confirmSave = confirmSave;
  103. this.edited = false;
  104. this.requiresRestart = false;
  105. this.tabsScrollProgress = 0d;
  106. this.tabButtons = Lists.newArrayList();
  107. this.displayErrors = displayErrors;
  108. this.categoryBackgroundLocation = categoryBackgroundLocation;
  109. }
  110. public boolean isShowingTabs() {
  111. return isAlwaysShowTabs() || tabs.size() > 1;
  112. }
  113. public boolean isAlwaysShowTabs() {
  114. return alwaysShowTabs;
  115. }
  116. @Deprecated
  117. public void setAlwaysShowTabs(boolean alwaysShowTabs) {
  118. this.alwaysShowTabs = alwaysShowTabs;
  119. }
  120. public boolean isTransparentBackground() {
  121. return transparentBackground && MinecraftClient.getInstance().world != null;
  122. }
  123. @Deprecated
  124. public void setTransparentBackground(boolean transparentBackground) {
  125. this.transparentBackground = transparentBackground;
  126. }
  127. public String getFallbackCategory() {
  128. if (defaultFallbackCategory != null)
  129. return defaultFallbackCategory;
  130. return tabs.get(0).getLeft();
  131. }
  132. @Deprecated
  133. public void setFallbackCategory(@Nullable String defaultFallbackCategory) {
  134. this.defaultFallbackCategory = defaultFallbackCategory;
  135. }
  136. @Override
  137. public void tick() {
  138. super.tick();
  139. for (Element child : children())
  140. if (child instanceof Tickable)
  141. ((Tickable) child).tick();
  142. }
  143. public Identifier getBackgroundLocation() {
  144. if (categoryBackgroundLocation.containsKey(Lists.newArrayList(tabbedEntries.keySet()).get(selectedTabIndex)))
  145. return categoryBackgroundLocation.get(Lists.newArrayList(tabbedEntries.keySet()).get(selectedTabIndex));
  146. return defaultBackgroundLocation;
  147. }
  148. public boolean isSmoothScrollingList() {
  149. return smoothScrollingList;
  150. }
  151. @Deprecated
  152. public void setSmoothScrollingList(boolean smoothScrollingList) {
  153. this.smoothScrollingList = smoothScrollingList;
  154. }
  155. public boolean isSmoothScrollingTabs() {
  156. return smoothScrollingTabs;
  157. }
  158. @Deprecated
  159. public void setSmoothScrollingTabs(boolean smoothScrolling) {
  160. this.smoothScrollingTabs = smoothScrolling;
  161. }
  162. public boolean isEdited() {
  163. return edited;
  164. }
  165. @Deprecated
  166. public void setEdited(boolean edited) {
  167. this.edited = edited;
  168. quitButton.setMessage(edited ? I18n.translate("text.cloth-config.cancel_discard") : I18n.translate("gui.cancel"));
  169. saveButton.active = edited;
  170. }
  171. public void setEdited(boolean edited, boolean requiresRestart) {
  172. setEdited(edited);
  173. if (!this.requiresRestart && requiresRestart)
  174. this.requiresRestart = requiresRestart;
  175. }
  176. public void saveAll(boolean openOtherScreens) {
  177. for (List<AbstractConfigEntry> entries : Lists.newArrayList(tabbedEntries.values()))
  178. for (AbstractConfigEntry entry : entries)
  179. entry.save();
  180. save();
  181. setEdited(false);
  182. requiresRestart = false;
  183. if (openOtherScreens) {
  184. if (requiresRestart)
  185. ClothConfigScreen.this.client.openScreen(new ClothRequiresRestartScreen(parent));
  186. else
  187. ClothConfigScreen.this.client.openScreen(parent);
  188. }
  189. }
  190. @Override
  191. protected void init() {
  192. super.init();
  193. this.children.clear();
  194. this.tabButtons.clear();
  195. if (listWidget != null)
  196. tabbedEntries.put(tabs.get(selectedTabIndex).getLeft(), (List) listWidget.children());
  197. selectedTabIndex = nextTabIndex;
  198. children.add(listWidget = new ListWidget(client, width, height, isShowingTabs() ? 70 : 30, height - 32, getBackgroundLocation()));
  199. listWidget.setSmoothScrolling(this.smoothScrollingList);
  200. if (tabbedEntries.size() > selectedTabIndex)
  201. Lists.newArrayList(tabbedEntries.values()).get(selectedTabIndex).forEach(entry -> listWidget.children().add(entry));
  202. int buttonWidths = (width - 50 - 12) / 3;
  203. addButton(quitButton = new ButtonWidget(25, height - 26, buttonWidths, 20, edited ? I18n.translate("text.cloth-config.cancel_discard") : I18n.translate("gui.cancel"), widget -> {
  204. if (confirmSave && edited)
  205. client.openScreen(new ConfirmScreen(new QuitSaveConsumer(), new TranslatableText("text.cloth-config.quit_config"), new TranslatableText("text.cloth-config.quit_config_sure"), I18n.translate("text.cloth-config.quit_discard"), I18n.translate("gui.cancel")));
  206. else
  207. client.openScreen(parent);
  208. }));
  209. addButton(saveButton = new AbstractPressableButtonWidget(25 + 6 + buttonWidths, height - 26, buttonWidths, 20, "") {
  210. @Override
  211. public void onPress() {
  212. saveAll(true);
  213. }
  214. @Override
  215. public void render(int int_1, int int_2, float float_1) {
  216. boolean hasErrors = false;
  217. if (displayErrors)
  218. for (List<AbstractConfigEntry> entries : Lists.newArrayList(tabbedEntries.values())) {
  219. for (AbstractConfigEntry entry : entries)
  220. if (entry.getConfigError().isPresent()) {
  221. hasErrors = true;
  222. break;
  223. }
  224. if (hasErrors)
  225. break;
  226. }
  227. active = edited && !hasErrors;
  228. setMessage(displayErrors && hasErrors ? I18n.translate("text.cloth-config.error_cannot_save") : I18n.translate("text.cloth-config.save_and_done"));
  229. super.render(int_1, int_2, float_1);
  230. }
  231. });
  232. addButton(applyButton = new AbstractPressableButtonWidget(25 + (6 + buttonWidths) * 2, height - 26, buttonWidths, 20, I18n.translate("text.cloth-config.apply")) {
  233. @Override
  234. public void onPress() {
  235. saveAll(false);
  236. }
  237. @Override
  238. public void render(int int_1, int int_2, float float_1) {
  239. active = saveButton.active;
  240. super.render(int_1, int_2, float_1);
  241. }
  242. });
  243. saveButton.active = edited;
  244. if (isShowingTabs()) {
  245. tabsBounds = new Rectangle(0, 41, width, 24);
  246. tabsLeftBounds = new Rectangle(0, 41, 18, 24);
  247. tabsRightBounds = new Rectangle(width - 18, 41, 18, 24);
  248. children.add(buttonLeftTab = new AbstractPressableButtonWidget(4, 44, 12, 18, "") {
  249. @Override
  250. public void onPress() {
  251. tabsScrollProgress = Integer.MIN_VALUE;
  252. tabsScrollVelocity = 0d;
  253. clampTabsScrolled();
  254. }
  255. @Override
  256. public void renderButton(int int_1, int int_2, float float_1) {
  257. client.getTextureManager().bindTexture(CONFIG_TEX);
  258. RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
  259. int int_3 = this.getYImage(this.isHovered());
  260. RenderSystem.enableBlend();
  261. RenderSystem.blendFuncSeparate(770, 771, 0, 1);
  262. RenderSystem.blendFunc(770, 771);
  263. this.drawTexture(x, y, 12, 18 * int_3, width, height);
  264. }
  265. });
  266. int j = 0;
  267. for (Pair<String, Integer> tab : tabs) {
  268. tabButtons.add(new ClothConfigTabButton(this, j, -100, 43, tab.getRight(), 20, I18n.translate(tab.getLeft())));
  269. j++;
  270. }
  271. children.addAll(tabButtons);
  272. children.add(buttonRightTab = new AbstractPressableButtonWidget(width - 16, 44, 12, 18, "") {
  273. @Override
  274. public void onPress() {
  275. tabsScrollProgress = Integer.MAX_VALUE;
  276. tabsScrollVelocity = 0d;
  277. clampTabsScrolled();
  278. }
  279. @Override
  280. public void renderButton(int int_1, int int_2, float float_1) {
  281. client.getTextureManager().bindTexture(CONFIG_TEX);
  282. RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
  283. int int_3 = this.getYImage(this.isHovered());
  284. RenderSystem.enableBlend();
  285. RenderSystem.blendFuncSeparate(770, 771, 0, 1);
  286. RenderSystem.blendFunc(770, 771);
  287. this.drawTexture(x, y, 0, 18 * int_3, width, height);
  288. }
  289. });
  290. } else {
  291. tabsBounds = tabsLeftBounds = tabsRightBounds = new Rectangle();
  292. }
  293. }
  294. @Override
  295. public boolean mouseScrolled(double double_1, double double_2, double double_3) {
  296. if (tabsBounds.contains(double_1, double_2) && !tabsLeftBounds.contains(double_1, double_2) && !tabsRightBounds.contains(double_1, double_2) && double_3 != 0d) {
  297. if (double_3 < 0)
  298. tabsScrollVelocity += 16;
  299. if (double_3 > 0)
  300. tabsScrollVelocity -= 16;
  301. return true;
  302. }
  303. return super.mouseScrolled(double_1, double_2, double_3);
  304. }
  305. public double getTabsMaximumScrolled() {
  306. if (tabsMaximumScrolled == -1d) {
  307. AtomicDouble d = new AtomicDouble();
  308. tabs.forEach(pair -> d.addAndGet(pair.getRight() + 2));
  309. tabsMaximumScrolled = d.get();
  310. }
  311. return tabsMaximumScrolled + 8;
  312. }
  313. public void resetTabsMaximumScrolled() {
  314. tabsMaximumScrolled = -1d;
  315. tabsScrollVelocity = 0f;
  316. }
  317. public void clampTabsScrolled() {
  318. int xx = 0;
  319. for (ClothConfigTabButton tabButton : tabButtons)
  320. xx += tabButton.getWidth() + 2;
  321. if (xx > width - 40)
  322. tabsScrollProgress = MathHelper.clamp(tabsScrollProgress, 0, getTabsMaximumScrolled() - width + 40);
  323. else
  324. tabsScrollProgress = 0d;
  325. }
  326. @Override
  327. public void render(int int_1, int int_2, float float_1) {
  328. if (isShowingTabs()) {
  329. if (smoothScrollingTabs) {
  330. double change = tabsScrollVelocity * 0.2f;
  331. if (change != 0) {
  332. if (change > 0 && change < .2)
  333. change = .2;
  334. else if (change < 0 && change > -.2)
  335. change = -.2;
  336. tabsScrollProgress += change;
  337. tabsScrollVelocity -= change;
  338. if (change > 0 == tabsScrollVelocity < 0)
  339. tabsScrollVelocity = 0f;
  340. clampTabsScrolled();
  341. }
  342. } else {
  343. tabsScrollProgress += tabsScrollVelocity;
  344. tabsScrollVelocity = 0d;
  345. clampTabsScrolled();
  346. }
  347. int xx = 24 - (int) tabsScrollProgress;
  348. for (ClothConfigTabButton tabButton : tabButtons) {
  349. tabButton.x = xx;
  350. xx += tabButton.getWidth() + 2;
  351. }
  352. buttonLeftTab.active = tabsScrollProgress > 0d;
  353. buttonRightTab.active = tabsScrollProgress < getTabsMaximumScrolled() - width + 40;
  354. }
  355. if (isTransparentBackground()) {
  356. fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
  357. } else {
  358. renderDirtBackground(0);
  359. }
  360. listWidget.render(int_1, int_2, float_1);
  361. ScissorsHandler.INSTANCE.scissor(new Rectangle(listWidget.left, listWidget.top, listWidget.width, listWidget.bottom - listWidget.top));
  362. for (AbstractConfigEntry child : listWidget.children())
  363. child.lateRender(int_1, int_2, float_1);
  364. ScissorsHandler.INSTANCE.removeLastScissor();
  365. if (isShowingTabs()) {
  366. drawCenteredString(client.textRenderer, title, width / 2, 18, -1);
  367. Rectangle onlyInnerTabBounds = new Rectangle(tabsBounds.x + 20, tabsBounds.y, tabsBounds.width - 40, tabsBounds.height);
  368. ScissorsHandler.INSTANCE.scissor(onlyInnerTabBounds);
  369. if (isTransparentBackground())
  370. fillGradient(onlyInnerTabBounds.x, onlyInnerTabBounds.y, onlyInnerTabBounds.getMaxX(), onlyInnerTabBounds.getMaxY(), 0x68000000, 0x68000000);
  371. else
  372. overlayBackground(onlyInnerTabBounds, 32, 32, 32, 255, 255);
  373. tabButtons.forEach(widget -> widget.render(int_1, int_2, float_1));
  374. drawTabsShades(0, isTransparentBackground() ? 120 : 255);
  375. ScissorsHandler.INSTANCE.removeLastScissor();
  376. buttonLeftTab.render(int_1, int_2, float_1);
  377. buttonRightTab.render(int_1, int_2, float_1);
  378. } else
  379. drawCenteredString(client.textRenderer, title, width / 2, 12, -1);
  380. if (displayErrors && isEditable()) {
  381. List<String> errors = Lists.newArrayList();
  382. for (List<AbstractConfigEntry> entries : Lists.newArrayList(tabbedEntries.values()))
  383. for (AbstractConfigEntry entry : entries)
  384. if (entry.getConfigError().isPresent())
  385. errors.add(((Optional<String>) entry.getConfigError()).get());
  386. if (errors.size() > 0) {
  387. client.getTextureManager().bindTexture(CONFIG_TEX);
  388. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  389. String text = "§c" + (errors.size() == 1 ? errors.get(0) : I18n.translate("text.cloth-config.multi_error"));
  390. if (isTransparentBackground()) {
  391. int stringWidth = client.textRenderer.getStringWidth(text);
  392. fillGradient(8, 9, 20 + stringWidth, 14 + client.textRenderer.fontHeight, 0x68000000, 0x68000000);
  393. }
  394. drawTexture(10, 10, 0, 54, 3, 11);
  395. drawString(client.textRenderer, text, 18, 12, -1);
  396. }
  397. } else if (!isEditable()) {
  398. client.getTextureManager().bindTexture(CONFIG_TEX);
  399. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  400. String text = "§c" + I18n.translate("text.cloth-config.not_editable");
  401. if (isTransparentBackground()) {
  402. int stringWidth = client.textRenderer.getStringWidth(text);
  403. fillGradient(8, 9, 20 + stringWidth, 14 + client.textRenderer.fontHeight, 0x68000000, 0x68000000);
  404. }
  405. drawTexture(10, 10, 0, 54, 3, 11);
  406. drawString(client.textRenderer, text, 18, 12, -1);
  407. }
  408. super.render(int_1, int_2, float_1);
  409. queuedTooltips.forEach(queuedTooltip -> renderTooltip(queuedTooltip.getText(), queuedTooltip.getX(), queuedTooltip.getY()));
  410. queuedTooltips.clear();
  411. }
  412. public void queueTooltip(QueuedTooltip queuedTooltip) {
  413. queuedTooltips.add(queuedTooltip);
  414. }
  415. private void drawTabsShades(int lightColor, int darkColor) {
  416. RenderSystem.enableBlend();
  417. RenderSystem.blendFuncSeparate(770, 771, 0, 1);
  418. RenderSystem.disableAlphaTest();
  419. RenderSystem.shadeModel(7425);
  420. RenderSystem.disableTexture();
  421. Tessellator tessellator = Tessellator.getInstance();
  422. BufferBuilder buffer = tessellator.getBuffer();
  423. buffer.begin(7, VertexFormats.POSITION_TEXTURE_COLOR);
  424. buffer.vertex(tabsBounds.getMinX() + 20, tabsBounds.getMinY() + 4, 0.0D).texture(0, 1f).color(0, 0, 0, lightColor).next();
  425. buffer.vertex(tabsBounds.getMaxX() - 20, tabsBounds.getMinY() + 4, 0.0D).texture(1f, 1f).color(0, 0, 0, lightColor).next();
  426. buffer.vertex(tabsBounds.getMaxX() - 20, tabsBounds.getMinY(), 0.0D).texture(1f, 0).color(0, 0, 0, darkColor).next();
  427. buffer.vertex(tabsBounds.getMinX() + 20, tabsBounds.getMinY(), 0.0D).texture(0, 0).color(0, 0, 0, darkColor).next();
  428. tessellator.draw();
  429. buffer.begin(7, VertexFormats.POSITION_TEXTURE_COLOR);
  430. buffer.vertex(tabsBounds.getMinX() + 20, tabsBounds.getMaxY(), 0.0D).texture(0, 1f).color(0, 0, 0, darkColor).next();
  431. buffer.vertex(tabsBounds.getMaxX() - 20, tabsBounds.getMaxY(), 0.0D).texture(1f, 1f).color(0, 0, 0, darkColor).next();
  432. buffer.vertex(tabsBounds.getMaxX() - 20, tabsBounds.getMaxY() - 4, 0.0D).texture(1f, 0).color(0, 0, 0, lightColor).next();
  433. buffer.vertex(tabsBounds.getMinX() + 20, tabsBounds.getMaxY() - 4, 0.0D).texture(0, 0).color(0, 0, 0, lightColor).next();
  434. tessellator.draw();
  435. RenderSystem.enableTexture();
  436. RenderSystem.shadeModel(7424);
  437. RenderSystem.enableAlphaTest();
  438. RenderSystem.disableBlend();
  439. }
  440. @SuppressWarnings("deprecation")
  441. protected void overlayBackground(Rectangle rect, int red, int green, int blue, int startAlpha, int endAlpha) {
  442. if (isTransparentBackground())
  443. return;
  444. Tessellator tessellator = Tessellator.getInstance();
  445. BufferBuilder buffer = tessellator.getBuffer();
  446. client.getTextureManager().bindTexture(getBackgroundLocation());
  447. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  448. float f = 32.0F;
  449. buffer.begin(7, VertexFormats.POSITION_TEXTURE_COLOR);
  450. buffer.vertex(rect.getMinX(), rect.getMaxY(), 0.0D).texture(rect.getMinX() / 32.0F, rect.getMaxY() / 32.0F).color(red, green, blue, endAlpha).next();
  451. buffer.vertex(rect.getMaxX(), rect.getMaxY(), 0.0D).texture(rect.getMaxX() / 32.0F, rect.getMaxY() / 32.0F).color(red, green, blue, endAlpha).next();
  452. buffer.vertex(rect.getMaxX(), rect.getMinY(), 0.0D).texture(rect.getMaxX() / 32.0F, rect.getMinY() / 32.0F).color(red, green, blue, startAlpha).next();
  453. buffer.vertex(rect.getMinX(), rect.getMinY(), 0.0D).texture(rect.getMinX() / 32.0F, rect.getMinY() / 32.0F).color(red, green, blue, startAlpha).next();
  454. tessellator.draw();
  455. }
  456. public KeyCodeEntry getFocusedBinding() {
  457. return focusedBinding;
  458. }
  459. public void setFocusedBinding(KeyCodeEntry focusedBinding) {
  460. this.focusedBinding = focusedBinding;
  461. if (focusedBinding != null) {
  462. startedKeyCode = this.focusedBinding.getValue();
  463. startedKeyCode.setKeyCodeAndModifier(InputUtil.UNKNOWN_KEYCODE, Modifier.none());
  464. } else
  465. startedKeyCode = null;
  466. }
  467. @Override
  468. public boolean mouseReleased(double double_1, double double_2, int int_1) {
  469. if (this.focusedBinding != null && this.startedKeyCode != null && !this.startedKeyCode.isUnknown() && focusedBinding.isAllowMouse()) {
  470. focusedBinding.setValue(startedKeyCode);
  471. setFocusedBinding(null);
  472. return true;
  473. }
  474. return super.mouseReleased(double_1, double_2, int_1);
  475. }
  476. @Override
  477. public boolean keyReleased(int int_1, int int_2, int int_3) {
  478. if (this.focusedBinding != null && this.startedKeyCode != null && focusedBinding.isAllowKey()) {
  479. focusedBinding.setValue(startedKeyCode);
  480. setFocusedBinding(null);
  481. return true;
  482. }
  483. return super.keyReleased(int_1, int_2, int_3);
  484. }
  485. @Override
  486. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  487. if (this.focusedBinding != null && this.startedKeyCode != null && focusedBinding.isAllowMouse()) {
  488. if (startedKeyCode.isUnknown())
  489. startedKeyCode.setKeyCode(InputUtil.Type.MOUSE.createFromCode(int_1));
  490. else if (focusedBinding.isAllowModifiers()) {
  491. if (startedKeyCode.getType() == InputUtil.Type.KEYSYM) {
  492. int code = startedKeyCode.getKeyCode().getKeyCode();
  493. if (MinecraftClient.IS_SYSTEM_MAC ? (code == 343 || code == 347) : (code == 341 || code == 345)) {
  494. Modifier modifier = startedKeyCode.getModifier();
  495. startedKeyCode.setModifier(Modifier.of(modifier.hasAlt(), true, modifier.hasShift()));
  496. startedKeyCode.setKeyCode(InputUtil.Type.MOUSE.createFromCode(int_1));
  497. return true;
  498. } else if (code == 344 || code == 340) {
  499. Modifier modifier = startedKeyCode.getModifier();
  500. startedKeyCode.setModifier(Modifier.of(modifier.hasAlt(), modifier.hasControl(), true));
  501. startedKeyCode.setKeyCode(InputUtil.Type.MOUSE.createFromCode(int_1));
  502. return true;
  503. } else if (code == 342 || code == 346) {
  504. Modifier modifier = startedKeyCode.getModifier();
  505. startedKeyCode.setModifier(Modifier.of(true, modifier.hasControl(), modifier.hasShift()));
  506. startedKeyCode.setKeyCode(InputUtil.Type.MOUSE.createFromCode(int_1));
  507. return true;
  508. }
  509. }
  510. }
  511. return true;
  512. } else {
  513. if (this.focusedBinding != null)
  514. return true;
  515. return super.mouseClicked(double_1, double_2, int_1);
  516. }
  517. }
  518. @Override
  519. public boolean keyPressed(int int_1, int int_2, int int_3) {
  520. if (this.focusedBinding != null && focusedBinding.isAllowKey()) {
  521. if (startedKeyCode.isUnknown())
  522. startedKeyCode.setKeyCode(InputUtil.getKeyCode(int_1, int_2));
  523. else if (focusedBinding.isAllowModifiers()) {
  524. if (startedKeyCode.getType() == InputUtil.Type.KEYSYM) {
  525. int code = startedKeyCode.getKeyCode().getKeyCode();
  526. if (MinecraftClient.IS_SYSTEM_MAC ? (code == 343 || code == 347) : (code == 341 || code == 345)) {
  527. Modifier modifier = startedKeyCode.getModifier();
  528. startedKeyCode.setModifier(Modifier.of(modifier.hasAlt(), true, modifier.hasShift()));
  529. startedKeyCode.setKeyCode(InputUtil.getKeyCode(int_1, int_2));
  530. return true;
  531. } else if (code == 344 || code == 340) {
  532. Modifier modifier = startedKeyCode.getModifier();
  533. startedKeyCode.setModifier(Modifier.of(modifier.hasAlt(), modifier.hasControl(), true));
  534. startedKeyCode.setKeyCode(InputUtil.getKeyCode(int_1, int_2));
  535. return true;
  536. } else if (code == 342 || code == 346) {
  537. Modifier modifier = startedKeyCode.getModifier();
  538. startedKeyCode.setModifier(Modifier.of(true, modifier.hasControl(), modifier.hasShift()));
  539. startedKeyCode.setKeyCode(InputUtil.getKeyCode(int_1, int_2));
  540. return true;
  541. }
  542. }
  543. if (MinecraftClient.IS_SYSTEM_MAC ? (int_1 == 343 || int_1 == 347) : (int_1 == 341 || int_1 == 345)) {
  544. Modifier modifier = startedKeyCode.getModifier();
  545. startedKeyCode.setModifier(Modifier.of(modifier.hasAlt(), true, modifier.hasShift()));
  546. return true;
  547. } else if (int_1 == 344 || int_1 == 340) {
  548. Modifier modifier = startedKeyCode.getModifier();
  549. startedKeyCode.setModifier(Modifier.of(modifier.hasAlt(), modifier.hasControl(), true));
  550. return true;
  551. } else if (int_1 == 342 || int_1 == 346) {
  552. Modifier modifier = startedKeyCode.getModifier();
  553. startedKeyCode.setModifier(Modifier.of(true, modifier.hasControl(), modifier.hasShift()));
  554. return true;
  555. }
  556. }
  557. return true;
  558. }
  559. if (this.focusedBinding != null)
  560. return true;
  561. if (int_1 == 256 && this.shouldCloseOnEsc()) {
  562. if (confirmSave && edited)
  563. client.openScreen(new ConfirmScreen(new QuitSaveConsumer(), new TranslatableText("text.cloth-config.quit_config"), new TranslatableText("text.cloth-config.quit_config_sure"), I18n.translate("text.cloth-config.quit_discard"), I18n.translate("gui.cancel")));
  564. else
  565. client.openScreen(parent);
  566. return true;
  567. }
  568. return super.keyPressed(int_1, int_2, int_3);
  569. }
  570. public void save() {
  571. }
  572. public boolean isEditable() {
  573. return editable;
  574. }
  575. @Deprecated
  576. public void setEditable(boolean editable) {
  577. this.editable = editable;
  578. }
  579. private class QuitSaveConsumer implements BooleanConsumer {
  580. @Override
  581. public void accept(boolean t) {
  582. if (!t)
  583. client.openScreen(ClothConfigScreen.this);
  584. else
  585. client.openScreen(parent);
  586. }
  587. }
  588. public class ListWidget<R extends DynamicElementListWidget.ElementEntry<R>> extends DynamicElementListWidget<R> {
  589. public ListWidget(MinecraftClient client, int width, int height, int top, int bottom, Identifier backgroundLocation) {
  590. super(client, width, height, top, bottom, backgroundLocation);
  591. visible = false;
  592. }
  593. @Override
  594. public int getItemWidth() {
  595. return width - 80;
  596. }
  597. public ClothConfigScreen getScreen() {
  598. return ClothConfigScreen.this;
  599. }
  600. @Override
  601. protected int getScrollbarPosition() {
  602. return width - 36;
  603. }
  604. protected final void clearStuff() {
  605. this.clearItems();
  606. }
  607. @Override
  608. protected void renderItem(R item, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
  609. if (item instanceof AbstractConfigEntry)
  610. ((AbstractConfigEntry) item).updateSelected(isSelected);
  611. super.renderItem(item, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
  612. }
  613. @Override
  614. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  615. this.updateScrollingState(double_1, double_2, int_1);
  616. if (!this.isMouseOver(double_1, double_2)) {
  617. return false;
  618. } else {
  619. for (R entry : children()) {
  620. if (entry.mouseClicked(double_1, double_2, int_1)) {
  621. this.setFocused(entry);
  622. this.setDragging(true);
  623. return true;
  624. }
  625. }
  626. if (int_1 == 0) {
  627. this.clickedHeader((int) (double_1 - (double) (this.left + this.width / 2 - this.getItemWidth() / 2)), (int) (double_2 - (double) this.top) + (int) this.getScroll() - 4);
  628. return true;
  629. }
  630. return this.scrolling;
  631. }
  632. }
  633. @Override
  634. protected void renderBackBackground(BufferBuilder buffer, Tessellator tessellator) {
  635. if (!isTransparentBackground())
  636. super.renderBackBackground(buffer, tessellator);
  637. else {
  638. fillGradient(left, top, right, bottom, 0x68000000, 0x68000000);
  639. }
  640. }
  641. @Override
  642. protected void renderHoleBackground(int int_1, int int_2, int int_3, int int_4) {
  643. if (!isTransparentBackground())
  644. super.renderHoleBackground(int_1, int_2, int_3, int_4);
  645. }
  646. }
  647. }