VillagerRecipeViewingScreen.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import com.mojang.blaze3d.systems.RenderSystem;
  27. import me.shedaniel.clothconfig2.ClothConfigInitializer;
  28. import me.shedaniel.clothconfig2.api.ScissorsHandler;
  29. import me.shedaniel.math.api.Point;
  30. import me.shedaniel.math.api.Rectangle;
  31. import me.shedaniel.math.impl.PointHelper;
  32. import me.shedaniel.rei.api.*;
  33. import me.shedaniel.rei.gui.entries.RecipeEntry;
  34. import me.shedaniel.rei.gui.widget.*;
  35. import me.shedaniel.rei.impl.ScreenHelper;
  36. import me.shedaniel.rei.utils.CollectionUtils;
  37. import net.minecraft.client.MinecraftClient;
  38. import net.minecraft.client.gui.Element;
  39. import net.minecraft.client.gui.screen.Screen;
  40. import net.minecraft.client.render.BufferBuilder;
  41. import net.minecraft.client.render.Tessellator;
  42. import net.minecraft.client.render.VertexFormats;
  43. import net.minecraft.client.resource.language.I18n;
  44. import net.minecraft.client.sound.PositionedSoundInstance;
  45. import net.minecraft.client.util.NarratorManager;
  46. import net.minecraft.sound.SoundEvents;
  47. import net.minecraft.text.LiteralText;
  48. import net.minecraft.text.TranslatableText;
  49. import net.minecraft.util.Formatting;
  50. import net.minecraft.util.math.MathHelper;
  51. import org.jetbrains.annotations.ApiStatus;
  52. import java.util.Collections;
  53. import java.util.List;
  54. import java.util.Map;
  55. import java.util.Optional;
  56. @ApiStatus.Internal
  57. public class VillagerRecipeViewingScreen extends Screen implements StackToNoticeScreen {
  58. private final Map<RecipeCategory<?>, List<RecipeDisplay>> categoryMap;
  59. private final List<RecipeCategory<?>> categories;
  60. private final List<Widget> widgets;
  61. private final List<ButtonWidget> buttonWidgets;
  62. private final List<RecipeEntry> recipeRenderers;
  63. private final List<TabWidget> tabs;
  64. public Rectangle bounds, scrollListBounds;
  65. private int tabsPerPage = 8;
  66. private int selectedCategoryIndex, selectedRecipeIndex;
  67. private double scroll;
  68. private double target;
  69. private long start;
  70. private long duration;
  71. private float scrollBarAlpha;
  72. private float scrollBarAlphaFuture;
  73. private long scrollBarAlphaFutureTime = -1;
  74. private boolean draggingScrollBar;
  75. private int tabsPage;
  76. private EntryStack ingredientStackToNotice = EntryStack.empty();
  77. private EntryStack resultStackToNotice = EntryStack.empty();
  78. public VillagerRecipeViewingScreen(Map<RecipeCategory<?>, List<RecipeDisplay>> map) {
  79. super(new LiteralText(""));
  80. this.widgets = Lists.newArrayList();
  81. this.categoryMap = Maps.newLinkedHashMap();
  82. this.selectedCategoryIndex = 0;
  83. this.selectedRecipeIndex = 0;
  84. this.scrollBarAlpha = 0;
  85. this.scrollBarAlphaFuture = 0;
  86. this.scroll = 0;
  87. this.draggingScrollBar = false;
  88. this.tabsPage = 0;
  89. this.categories = Lists.newArrayList();
  90. this.buttonWidgets = Lists.newArrayList();
  91. this.tabs = Lists.newArrayList();
  92. this.recipeRenderers = Lists.newArrayList();
  93. RecipeHelper.getInstance().getAllCategories().forEach(category -> {
  94. if (map.containsKey(category)) {
  95. categories.add(category);
  96. categoryMap.put(category, map.get(category));
  97. }
  98. });
  99. }
  100. @Override
  101. public void addIngredientStackToNotice(EntryStack stack) {
  102. ingredientStackToNotice = stack;
  103. }
  104. @Override
  105. public void addResultStackToNotice(EntryStack stack) {
  106. resultStackToNotice = stack;
  107. }
  108. @Override
  109. protected void init() {
  110. super.init();
  111. boolean isCompactTabs = ConfigObject.getInstance().isUsingCompactTabs();
  112. int tabSize = isCompactTabs ? 24 : 28;
  113. this.draggingScrollBar = false;
  114. this.children.clear();
  115. this.widgets.clear();
  116. this.buttonWidgets.clear();
  117. this.recipeRenderers.clear();
  118. this.tabs.clear();
  119. int largestWidth = width - 100;
  120. int largestHeight = height - 40;
  121. RecipeCategory<RecipeDisplay> category = (RecipeCategory<RecipeDisplay>) categories.get(selectedCategoryIndex);
  122. RecipeDisplay display = categoryMap.get(category).get(selectedRecipeIndex);
  123. int guiWidth = MathHelper.clamp(category.getDisplayWidth(display) + 30, 0, largestWidth) + 100;
  124. int guiHeight = MathHelper.clamp(category.getDisplayHeight() + 40, 166, largestHeight);
  125. this.tabsPerPage = Math.max(5, MathHelper.floor((guiWidth - 20d) / tabSize));
  126. this.bounds = new Rectangle(width / 2 - guiWidth / 2, height / 2 - guiHeight / 2, guiWidth, guiHeight);
  127. List<List<EntryStack>> workingStations = RecipeHelper.getInstance().getWorkingStations(category.getIdentifier());
  128. if (!workingStations.isEmpty()) {
  129. int ww = MathHelper.floor((bounds.width - 16) / 18f);
  130. int w = Math.min(ww, workingStations.size());
  131. int h = MathHelper.ceil(workingStations.size() / ((float) ww));
  132. int xx = bounds.x + 16;
  133. int yy = bounds.y + bounds.height + 2;
  134. widgets.add(new CategoryBaseWidget(new Rectangle(xx - 5, bounds.y + bounds.height - 5, 10 + w * 16, 12 + h * 16)));
  135. widgets.add(new SlotBaseWidget(new Rectangle(xx - 1, yy - 1, 2 + w * 16, 2 + h * 16)));
  136. int index = 0;
  137. List<String> list = Collections.singletonList(Formatting.YELLOW.toString() + I18n.translate("text.rei.working_station"));
  138. for (List<EntryStack> workingStation : workingStations) {
  139. widgets.add(new RecipeViewingScreen.WorkstationSlotWidget(xx, yy, CollectionUtils.map(workingStation, stack -> stack.copy().setting(EntryStack.Settings.TOOLTIP_APPEND_EXTRA, s -> list))));
  140. index++;
  141. xx += 16;
  142. if (index >= ww) {
  143. index = 0;
  144. xx = bounds.x + 16;
  145. yy += 16;
  146. }
  147. }
  148. }
  149. this.widgets.add(new CategoryBaseWidget(bounds));
  150. this.scrollListBounds = new Rectangle(bounds.x + 4, bounds.y + 17, 97 + 5, guiHeight - 17 - 7);
  151. this.widgets.add(new SlotBaseWidget(scrollListBounds));
  152. Rectangle recipeBounds = new Rectangle(bounds.x + 100 + (guiWidth - 100) / 2 - category.getDisplayWidth(display) / 2, bounds.y + bounds.height / 2 - category.getDisplayHeight() / 2, category.getDisplayWidth(display), category.getDisplayHeight());
  153. List<Widget> setupDisplay = category.setupDisplay(() -> display, recipeBounds);
  154. RecipeViewingScreen.transformIngredientNotice(setupDisplay, ingredientStackToNotice);
  155. RecipeViewingScreen.transformResultNotice(setupDisplay, resultStackToNotice);
  156. this.widgets.addAll(setupDisplay);
  157. Optional<ButtonAreaSupplier> supplier = RecipeHelper.getInstance().getAutoCraftButtonArea(category);
  158. if (supplier.isPresent() && supplier.get().get(recipeBounds) != null)
  159. this.widgets.add(new AutoCraftingButtonWidget(recipeBounds, supplier.get().get(recipeBounds), supplier.get().getButtonText(), () -> display, setupDisplay, category));
  160. int index = 0;
  161. for (RecipeDisplay recipeDisplay : categoryMap.get(category)) {
  162. int finalIndex = index;
  163. RecipeEntry recipeEntry;
  164. recipeRenderers.add(recipeEntry = category.getSimpleRenderer(recipeDisplay));
  165. buttonWidgets.add(new ButtonWidget(new Rectangle(bounds.x + 5, 0, recipeEntry.getWidth(), recipeEntry.getHeight()), NarratorManager.EMPTY) {
  166. @Override
  167. public void onPressed() {
  168. selectedRecipeIndex = finalIndex;
  169. VillagerRecipeViewingScreen.this.init();
  170. }
  171. @Override
  172. public boolean isHovered(int mouseX, int mouseY) {
  173. return (isMouseOver(mouseX, mouseY) && scrollListBounds.contains(mouseX, mouseY)) || focused;
  174. }
  175. @Override
  176. protected int getTextureId(boolean boolean_1) {
  177. enabled = selectedRecipeIndex != finalIndex;
  178. return super.getTextureId(boolean_1);
  179. }
  180. @Override
  181. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  182. if ((isMouseOver(mouseX, mouseY) && scrollListBounds.contains(mouseX, mouseY)) && enabled && button == 0) {
  183. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  184. onPressed();
  185. return true;
  186. }
  187. return false;
  188. }
  189. });
  190. index++;
  191. }
  192. int tabV = isCompactTabs ? 166 : 192;
  193. for (int i = 0; i < tabsPerPage; i++) {
  194. int j = i + tabsPage * tabsPerPage;
  195. if (categories.size() > j) {
  196. TabWidget tab;
  197. tabs.add(tab = new TabWidget(i, tabSize, bounds.x + bounds.width / 2 - Math.min(categories.size() - tabsPage * tabsPerPage, tabsPerPage) * tabSize / 2, bounds.y, 0, tabV) {
  198. @Override
  199. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  200. if (containsMouse(mouseX, mouseY)) {
  201. MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  202. if (getId() + tabsPage * tabsPerPage == selectedCategoryIndex)
  203. return false;
  204. selectedCategoryIndex = getId() + tabsPage * tabsPerPage;
  205. scroll = 0;
  206. selectedRecipeIndex = 0;
  207. VillagerRecipeViewingScreen.this.init();
  208. return true;
  209. }
  210. return false;
  211. }
  212. });
  213. tab.setRenderer(categories.get(j), categories.get(j).getLogo(), categories.get(j).getCategoryName(), tab.getId() + tabsPage * tabsPerPage == selectedCategoryIndex);
  214. }
  215. }
  216. ButtonWidget w, w2;
  217. this.widgets.add(w = ButtonWidget.create(new Rectangle(bounds.x + 2, bounds.y - 16, 10, 10), new TranslatableText("text.rei.left_arrow"), buttonWidget -> {
  218. tabsPage--;
  219. if (tabsPage < 0)
  220. tabsPage = MathHelper.ceil(categories.size() / (float) tabsPerPage) - 1;
  221. VillagerRecipeViewingScreen.this.init();
  222. }));
  223. this.widgets.add(w2 = ButtonWidget.create(new Rectangle(bounds.x + bounds.width - 12, bounds.y - 16, 10, 10), new TranslatableText("text.rei.right_arrow"), buttonWidget -> {
  224. tabsPage++;
  225. if (tabsPage > MathHelper.ceil(categories.size() / (float) tabsPerPage) - 1)
  226. tabsPage = 0;
  227. VillagerRecipeViewingScreen.this.init();
  228. }));
  229. w.enabled = w2.enabled = categories.size() > tabsPerPage;
  230. this.widgets.add(new ClickableLabelWidget(new Point(bounds.x + 4 + scrollListBounds.width / 2, bounds.y + 6), categories.get(selectedCategoryIndex).getCategoryName()) {
  231. @Override
  232. public void onLabelClicked() {
  233. MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  234. ClientHelper.getInstance().executeViewAllRecipesKeyBind();
  235. }
  236. @Override
  237. public Optional<String> getTooltips() {
  238. return Optional.ofNullable(I18n.translate("text.rei.view_all_categories"));
  239. }
  240. });
  241. this.children.addAll(buttonWidgets);
  242. this.widgets.addAll(tabs);
  243. this.children.addAll(widgets);
  244. this.children.add(ScreenHelper.getLastOverlay(true, false));
  245. ScreenHelper.getLastOverlay().init();
  246. }
  247. private double getMaxScroll() {
  248. return Math.max(0, this.getMaxScrollPosition() - (scrollListBounds.height - 2));
  249. }
  250. @Override
  251. public boolean mouseClicked(double mouseX, double mouseY, int int_1) {
  252. double height = getMaxScrollPosition();
  253. int actualHeight = scrollListBounds.height - 2;
  254. if (height > actualHeight && scrollBarAlpha > 0 && mouseY >= scrollListBounds.y + 1 && mouseY <= scrollListBounds.getMaxY() - 1) {
  255. double scrollbarPositionMinX = scrollListBounds.getMaxX() - 6;
  256. if (mouseX >= scrollbarPositionMinX & mouseX <= scrollbarPositionMinX + 8) {
  257. this.draggingScrollBar = true;
  258. scrollBarAlpha = 1;
  259. return false;
  260. }
  261. }
  262. this.draggingScrollBar = false;
  263. return super.mouseClicked(mouseX, mouseY, int_1);
  264. }
  265. @Override
  266. public boolean charTyped(char char_1, int int_1) {
  267. for (Element listener : children())
  268. if (listener.charTyped(char_1, int_1))
  269. return true;
  270. return super.charTyped(char_1, int_1);
  271. }
  272. public void offset(double value, boolean animated) {
  273. scrollTo(target + value, animated);
  274. }
  275. public void scrollTo(double value, boolean animated) {
  276. scrollTo(value, animated, ClothConfigInitializer.getScrollDuration());
  277. }
  278. public void scrollTo(double value, boolean animated, long duration) {
  279. target = ClothConfigInitializer.clamp(value, getMaxScroll());
  280. if (animated) {
  281. start = System.currentTimeMillis();
  282. this.duration = duration;
  283. } else
  284. scroll = target;
  285. }
  286. @Override
  287. public boolean mouseScrolled(double double_1, double double_2, double double_3) {
  288. double height = CollectionUtils.sumInt(buttonWidgets, b -> b.getBounds().getHeight());
  289. if (scrollListBounds.contains(double_1, double_2) && height > scrollListBounds.height - 2) {
  290. offset(ClothConfigInitializer.getScrollStep() * -double_3, true);
  291. if (scrollBarAlphaFuture == 0)
  292. scrollBarAlphaFuture = 1f;
  293. if (System.currentTimeMillis() - scrollBarAlphaFutureTime > 300f)
  294. scrollBarAlphaFutureTime = System.currentTimeMillis();
  295. return true;
  296. }
  297. for (Element listener : children())
  298. if (listener.mouseScrolled(double_1, double_2, double_3))
  299. return true;
  300. if (bounds.contains(PointHelper.fromMouse())) {
  301. if (double_3 < 0 && categoryMap.get(categories.get(selectedCategoryIndex)).size() > 1) {
  302. selectedRecipeIndex++;
  303. if (selectedRecipeIndex >= categoryMap.get(categories.get(selectedCategoryIndex)).size())
  304. selectedRecipeIndex = 0;
  305. init();
  306. } else if (categoryMap.get(categories.get(selectedCategoryIndex)).size() > 1) {
  307. selectedRecipeIndex--;
  308. if (selectedRecipeIndex < 0)
  309. selectedRecipeIndex = categoryMap.get(categories.get(selectedCategoryIndex)).size() - 1;
  310. init();
  311. return true;
  312. }
  313. }
  314. return super.mouseScrolled(double_1, double_2, double_3);
  315. }
  316. private double getMaxScrollPosition() {
  317. return CollectionUtils.sumInt(buttonWidgets, b -> b.getBounds().getHeight());
  318. }
  319. @Override
  320. public void render(int mouseX, int mouseY, float delta) {
  321. if (ConfigObject.getInstance().doesVillagerScreenHavePermanentScrollBar()) {
  322. scrollBarAlphaFutureTime = System.currentTimeMillis();
  323. scrollBarAlphaFuture = 0;
  324. scrollBarAlpha = 1;
  325. } else if (scrollBarAlphaFutureTime > 0) {
  326. long l = System.currentTimeMillis() - scrollBarAlphaFutureTime;
  327. if (l > 300f) {
  328. if (scrollBarAlphaFutureTime == 0) {
  329. scrollBarAlpha = scrollBarAlphaFuture;
  330. scrollBarAlphaFutureTime = -1;
  331. } else if (l > 2000f && scrollBarAlphaFuture == 1) {
  332. scrollBarAlphaFuture = 0;
  333. scrollBarAlphaFutureTime = System.currentTimeMillis();
  334. } else
  335. scrollBarAlpha = scrollBarAlphaFuture;
  336. } else {
  337. if (scrollBarAlphaFuture == 0)
  338. scrollBarAlpha = Math.min(scrollBarAlpha, 1 - Math.min(1f, l / 300f));
  339. else if (scrollBarAlphaFuture == 1)
  340. scrollBarAlpha = Math.max(Math.min(1f, l / 300f), scrollBarAlpha);
  341. }
  342. }
  343. updatePosition(delta);
  344. this.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
  345. int yOffset = 0;
  346. for (Widget widget : widgets) {
  347. widget.render(mouseX, mouseY, delta);
  348. }
  349. ScreenHelper.getLastOverlay().render(mouseX, mouseY, delta);
  350. RenderSystem.pushMatrix();
  351. ScissorsHandler.INSTANCE.scissor(new Rectangle(0, scrollListBounds.y + 1, width, scrollListBounds.height - 2));
  352. for (ButtonWidget buttonWidget : buttonWidgets) {
  353. buttonWidget.getBounds().y = scrollListBounds.y + 1 + yOffset - (int) scroll;
  354. if (buttonWidget.getBounds().getMaxY() > scrollListBounds.getMinY() && buttonWidget.getBounds().getMinY() < scrollListBounds.getMaxY()) {
  355. buttonWidget.render(mouseX, mouseY, delta);
  356. }
  357. yOffset += buttonWidget.getBounds().height;
  358. }
  359. for (int i = 0; i < buttonWidgets.size(); i++) {
  360. if (buttonWidgets.get(i).getBounds().getMaxY() > scrollListBounds.getMinY() && buttonWidgets.get(i).getBounds().getMinY() < scrollListBounds.getMaxY()) {
  361. recipeRenderers.get(i).setZ(1);
  362. recipeRenderers.get(i).render(buttonWidgets.get(i).getBounds(), mouseX, mouseY, delta);
  363. ScreenHelper.getLastOverlay().addTooltip(recipeRenderers.get(i).getTooltip(mouseX, mouseY));
  364. }
  365. }
  366. double maxScroll = getMaxScrollPosition();
  367. if (maxScroll > scrollListBounds.height - 2) {
  368. Tessellator tessellator = Tessellator.getInstance();
  369. BufferBuilder buffer = tessellator.getBuffer();
  370. int height = (int) (((scrollListBounds.height - 2) * (scrollListBounds.height - 2)) / this.getMaxScrollPosition());
  371. height = MathHelper.clamp(height, 32, scrollListBounds.height - 2 - 8);
  372. height -= Math.min((scroll < 0 ? (int) -scroll : scroll > getMaxScroll() ? (int) scroll - getMaxScroll() : 0), height * .95);
  373. height = Math.max(10, height);
  374. int minY = (int) Math.min(Math.max((int) scroll * (scrollListBounds.height - 2 - height) / getMaxScroll() + scrollListBounds.y + 1, scrollListBounds.y + 1), scrollListBounds.getMaxY() - 1 - height);
  375. int scrollbarPositionMinX = scrollListBounds.getMaxX() - 6, scrollbarPositionMaxX = scrollListBounds.getMaxX() - 1;
  376. boolean hovered = (new Rectangle(scrollbarPositionMinX, minY, scrollbarPositionMaxX - scrollbarPositionMinX, height)).contains(PointHelper.fromMouse());
  377. float bottomC = (hovered ? .67f : .5f) * (ScreenHelper.isDarkModeEnabled() ? 0.8f : 1f);
  378. float topC = (hovered ? .87f : .67f) * (ScreenHelper.isDarkModeEnabled() ? 0.8f : 1f);
  379. RenderSystem.disableTexture();
  380. RenderSystem.enableBlend();
  381. RenderSystem.disableAlphaTest();
  382. RenderSystem.blendFuncSeparate(770, 771, 1, 0);
  383. RenderSystem.shadeModel(7425);
  384. buffer.begin(7, VertexFormats.POSITION_COLOR);
  385. buffer.vertex(scrollbarPositionMinX, minY + height, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
  386. buffer.vertex(scrollbarPositionMaxX, minY + height, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
  387. buffer.vertex(scrollbarPositionMaxX, minY, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
  388. buffer.vertex(scrollbarPositionMinX, minY, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
  389. tessellator.draw();
  390. buffer.begin(7, VertexFormats.POSITION_COLOR);
  391. buffer.vertex(scrollbarPositionMinX, minY + height - 1, 800).color(topC, topC, topC, scrollBarAlpha).next();
  392. buffer.vertex(scrollbarPositionMaxX - 1, minY + height - 1, 800).color(topC, topC, topC, scrollBarAlpha).next();
  393. buffer.vertex(scrollbarPositionMaxX - 1, minY, 800).color(topC, topC, topC, scrollBarAlpha).next();
  394. buffer.vertex(scrollbarPositionMinX, minY, 800).color(topC, topC, topC, scrollBarAlpha).next();
  395. tessellator.draw();
  396. RenderSystem.shadeModel(7424);
  397. RenderSystem.disableBlend();
  398. RenderSystem.enableAlphaTest();
  399. RenderSystem.enableTexture();
  400. }
  401. ScissorsHandler.INSTANCE.removeLastScissor();
  402. RenderSystem.popMatrix();
  403. ScreenHelper.getLastOverlay().lateRender(mouseX, mouseY, delta);
  404. }
  405. private void updatePosition(float delta) {
  406. double[] target = new double[]{this.target};
  407. this.scroll = ClothConfigInitializer.handleScrollingPosition(target, this.scroll, this.getMaxScroll(), delta, this.start, this.duration);
  408. this.target = target[0];
  409. }
  410. @Override
  411. public boolean mouseDragged(double mouseX, double mouseY, int int_1, double double_3, double double_4) {
  412. if (int_1 == 0 && scrollBarAlpha > 0 && draggingScrollBar) {
  413. double height = CollectionUtils.sumInt(buttonWidgets, b -> b.getBounds().getHeight());
  414. int actualHeight = scrollListBounds.height - 2;
  415. if (height > actualHeight && mouseY >= scrollListBounds.y + 1 && mouseY <= scrollListBounds.getMaxY() - 1) {
  416. int int_3 = MathHelper.clamp((int) ((actualHeight * actualHeight) / height), 32, actualHeight - 8);
  417. double double_6 = Math.max(1.0D, Math.max(1d, height) / (double) (actualHeight - int_3));
  418. scrollBarAlphaFutureTime = System.currentTimeMillis();
  419. scrollBarAlphaFuture = 1f;
  420. scroll = target = MathHelper.clamp(scroll + double_4 * double_6, 0, height - scrollListBounds.height + 2);
  421. return true;
  422. }
  423. }
  424. return super.mouseDragged(mouseX, mouseY, int_1, double_3, double_4);
  425. }
  426. @Override
  427. public boolean keyPressed(int int_1, int int_2, int int_3) {
  428. if ((int_1 == 256 || this.minecraft.options.keyInventory.matchesKey(int_1, int_2)) && this.shouldCloseOnEsc()) {
  429. MinecraftClient.getInstance().openScreen(ScreenHelper.getLastContainerScreen());
  430. ScreenHelper.getLastOverlay().init();
  431. return true;
  432. }
  433. if (int_1 == 258) {
  434. boolean boolean_1 = !hasShiftDown();
  435. if (!this.changeFocus(boolean_1))
  436. this.changeFocus(boolean_1);
  437. return true;
  438. }
  439. if (ConfigObject.getInstance().getNextPageKeybind().matchesKey(int_1, int_2)) {
  440. if (categoryMap.get(categories.get(selectedCategoryIndex)).size() > 1) {
  441. selectedRecipeIndex++;
  442. if (selectedRecipeIndex >= categoryMap.get(categories.get(selectedCategoryIndex)).size())
  443. selectedRecipeIndex = 0;
  444. init();
  445. return true;
  446. }
  447. return false;
  448. } else if (ConfigObject.getInstance().getPreviousPageKeybind().matchesKey(int_1, int_2)) {
  449. if (categoryMap.get(categories.get(selectedCategoryIndex)).size() > 1) {
  450. selectedRecipeIndex--;
  451. if (selectedRecipeIndex < 0)
  452. selectedRecipeIndex = categoryMap.get(categories.get(selectedCategoryIndex)).size() - 1;
  453. init();
  454. return true;
  455. }
  456. return false;
  457. }
  458. for (Element element : children())
  459. if (element.keyPressed(int_1, int_2, int_3))
  460. return true;
  461. if (int_1 == 259) {
  462. if (ScreenHelper.hasLastRecipeScreen())
  463. minecraft.openScreen(ScreenHelper.getLastRecipeScreen());
  464. else
  465. minecraft.openScreen(ScreenHelper.getLastContainerScreen());
  466. return true;
  467. }
  468. return super.keyPressed(int_1, int_2, int_3);
  469. }
  470. }