VillagerRecipeViewingScreen.java 25 KB

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