RecipeChoosePageWidget.java 7.7 KB

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