RecipeChoosePageWidget.java 7.1 KB

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