RecipeChoosePageWidget.java 7.6 KB

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