ClothConfigScreen.java 26 KB

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