RecipeChoosePageWidget.java 8.5 KB

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