RecipeChoosePageWidget.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.widget;
  24. import com.google.common.collect.Lists;
  25. import com.mojang.blaze3d.systems.RenderSystem;
  26. import me.shedaniel.math.api.Point;
  27. import me.shedaniel.math.api.Rectangle;
  28. import me.shedaniel.rei.gui.RecipeViewingScreen;
  29. import me.shedaniel.rei.impl.ScreenHelper;
  30. import net.minecraft.client.MinecraftClient;
  31. import net.minecraft.client.resource.language.I18n;
  32. import net.minecraft.client.util.Window;
  33. import net.minecraft.text.TranslatableText;
  34. import net.minecraft.util.math.MathHelper;
  35. import org.jetbrains.annotations.ApiStatus;
  36. import java.util.Collections;
  37. import java.util.List;
  38. import java.util.Optional;
  39. @ApiStatus.Internal
  40. public class RecipeChoosePageWidget extends DraggableWidget {
  41. private int currentPage;
  42. private int maxPage;
  43. private Rectangle bounds, grabBounds, dragBounds;
  44. private List<Widget> widgets;
  45. private RecipeViewingScreen recipeViewingScreen;
  46. private TextFieldWidget textFieldWidget;
  47. private CategoryBaseWidget base1, base2;
  48. private ButtonWidget btnDone;
  49. public RecipeChoosePageWidget(RecipeViewingScreen recipeViewingScreen, int currentPage, int maxPage) {
  50. super(getPointFromConfig());
  51. this.recipeViewingScreen = recipeViewingScreen;
  52. this.currentPage = currentPage;
  53. this.maxPage = maxPage;
  54. initWidgets(getMidPoint());
  55. }
  56. private static Point getPointFromConfig() {
  57. Window window = MinecraftClient.getInstance().getWindow();
  58. return new Point(window.getScaledWidth() * .5, window.getScaledHeight() * .5);
  59. }
  60. @Override
  61. public Rectangle getBounds() {
  62. return bounds;
  63. }
  64. @Override
  65. public Rectangle getGrabBounds() {
  66. return grabBounds;
  67. }
  68. @Override
  69. public Rectangle getDragBounds() {
  70. return dragBounds;
  71. }
  72. @Override
  73. public boolean containsMouse(double mouseX, double mouseY) {
  74. return getBounds().contains(mouseX, mouseY) || new Rectangle(bounds.x + bounds.width - 50, bounds.y + bounds.height - 3, 50, 36).contains(mouseX, mouseY);
  75. }
  76. @Override
  77. public void updateWidgets(Point midPoint) {
  78. this.bounds = new Rectangle(midPoint.x - 50, midPoint.y - 20, 100, 40);
  79. this.grabBounds = new Rectangle(midPoint.x - 50, midPoint.y - 20, 100, 16);
  80. this.dragBounds = new Rectangle(midPoint.x - 50, midPoint.y - 20, 100, 70);
  81. base1.getBounds().setLocation(bounds.x + bounds.width - 50, bounds.y + bounds.height - 6);
  82. base2.getBounds().setBounds(bounds);
  83. textFieldWidget.getBounds().setLocation(bounds.x + 7, bounds.y + 16);
  84. btnDone.getBounds().setLocation(bounds.x + bounds.width - 45, bounds.y + bounds.height + 3);
  85. }
  86. @Override
  87. protected void initWidgets(Point midPoint) {
  88. this.bounds = new Rectangle(midPoint.x - 50, midPoint.y - 20, 100, 40);
  89. this.grabBounds = new Rectangle(midPoint.x - 50, midPoint.y - 20, 100, 16);
  90. this.dragBounds = new Rectangle(midPoint.x - 50, midPoint.y - 20, 100, 70);
  91. this.widgets = Lists.newArrayList();
  92. this.widgets.add(base1 = new CategoryBaseWidget(new Rectangle(bounds.x + bounds.width - 50, bounds.y + bounds.height - 6, 50, 36)));
  93. this.widgets.add(base2 = new CategoryBaseWidget(bounds));
  94. this.widgets.add(new Widget() {
  95. @Override
  96. public List<Widget> children() {
  97. return Collections.emptyList();
  98. }
  99. @Override
  100. public void render(int i, int i1, float v) {
  101. font.draw(I18n.translate("text.rei.choose_page"), bounds.x + 5, bounds.y + 5, ScreenHelper.isDarkModeEnabled() ? 0xFFBBBBBB : 0xFF404040);
  102. String endString = String.format(" /%d", maxPage);
  103. int width = font.getStringWidth(endString);
  104. font.draw(endString, bounds.x + bounds.width - 5 - width, bounds.y + 22, ScreenHelper.isDarkModeEnabled() ? 0xFFBBBBBB : 0xFF404040);
  105. }
  106. });
  107. String endString = String.format(" /%d", maxPage);
  108. int width = font.getStringWidth(endString);
  109. this.widgets.add(textFieldWidget = new TextFieldWidget(bounds.x + 7, bounds.y + 16, bounds.width - width - 12, 18));
  110. textFieldWidget.setMaxLength(10000);
  111. textFieldWidget.stripInvalid = s -> {
  112. StringBuilder stringBuilder_1 = new StringBuilder();
  113. char[] var2 = s.toCharArray();
  114. int var3 = var2.length;
  115. for (char char_1 : var2) {
  116. if (Character.isDigit(char_1))
  117. stringBuilder_1.append(char_1);
  118. }
  119. return stringBuilder_1.toString();
  120. };
  121. textFieldWidget.setText(String.valueOf(currentPage + 1));
  122. widgets.add(btnDone = ButtonWidget.create(new Rectangle(bounds.x + bounds.width - 45, bounds.y + bounds.height + 3, 40, 20), new TranslatableText("gui.done"), buttonWidget -> {
  123. recipeViewingScreen.page = MathHelper.clamp(getIntFromString(textFieldWidget.getText()).orElse(0) - 1, 0, recipeViewingScreen.getTotalPages(recipeViewingScreen.getSelectedCategory()) - 1);
  124. recipeViewingScreen.choosePageActivated = false;
  125. recipeViewingScreen.init();
  126. }));
  127. textFieldWidget.setFocused(true);
  128. }
  129. @Override
  130. public Point processMidPoint(Point midPoint, Point mouse, Point startPoint, Window window, int relateX, int relateY) {
  131. return new Point(MathHelper.clamp(mouse.x - relateX, getDragBounds().width / 2, window.getScaledWidth() - getDragBounds().width / 2), MathHelper.clamp(mouse.y - relateY, 20, window.getScaledHeight() - 50));
  132. }
  133. @Override
  134. public List<Widget> children() {
  135. return widgets;
  136. }
  137. @Override
  138. public void render(int i, int i1, float v) {
  139. RenderSystem.translatef(0, 0, 800);
  140. for (Widget widget : widgets) {
  141. widget.render(i, i1, v);
  142. }
  143. RenderSystem.translatef(0, 0, -800);
  144. }
  145. @Override
  146. public boolean charTyped(char char_1, int int_1) {
  147. for (Widget widget : widgets)
  148. if (widget.charTyped(char_1, int_1))
  149. return true;
  150. return false;
  151. }
  152. @Override
  153. public boolean keyPressed(int int_1, int int_2, int int_3) {
  154. if (int_1 == 335 || int_1 == 257) {
  155. recipeViewingScreen.page = MathHelper.clamp(getIntFromString(textFieldWidget.getText()).orElse(0) - 1, 0, recipeViewingScreen.getTotalPages(recipeViewingScreen.getSelectedCategory()) - 1);
  156. recipeViewingScreen.choosePageActivated = false;
  157. recipeViewingScreen.init();
  158. return true;
  159. }
  160. for (Widget widget : widgets)
  161. if (widget.keyPressed(int_1, int_2, int_3))
  162. return true;
  163. return false;
  164. }
  165. public Optional<Integer> getIntFromString(String s) {
  166. try {
  167. return Optional.of(Integer.valueOf(s));
  168. } catch (Exception ignored) {
  169. }
  170. return Optional.empty();
  171. }
  172. @Override
  173. public void onMouseReleaseMidPoint(Point midPoint) {
  174. }
  175. }