ButtonWidget.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018, 2019, 2020 shedaniel
  3. * Licensed under the MIT License (the "License").
  4. */
  5. package me.shedaniel.rei.gui.widget;
  6. import com.mojang.blaze3d.systems.RenderSystem;
  7. import me.shedaniel.math.api.Point;
  8. import me.shedaniel.math.api.Rectangle;
  9. import me.shedaniel.rei.impl.ScreenHelper;
  10. import net.minecraft.client.gui.Element;
  11. import net.minecraft.client.sound.PositionedSoundInstance;
  12. import net.minecraft.sound.SoundEvents;
  13. import net.minecraft.text.LiteralText;
  14. import net.minecraft.text.Text;
  15. import net.minecraft.util.Identifier;
  16. import net.minecraft.util.math.MathHelper;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import java.util.Objects;
  20. import java.util.Optional;
  21. import java.util.function.Consumer;
  22. import java.util.function.Supplier;
  23. public abstract class ButtonWidget extends WidgetWithBounds {
  24. protected static final Identifier BUTTON_LOCATION = new Identifier("roughlyenoughitems", "textures/gui/button.png");
  25. protected static final Identifier BUTTON_LOCATION_DARK = new Identifier("roughlyenoughitems", "textures/gui/button_dark.png");
  26. public boolean enabled = true;
  27. public boolean focused = false;
  28. private boolean canChangeFocuses = true;
  29. private String text;
  30. private Rectangle bounds;
  31. private Supplier<String> tooltipSupplier;
  32. protected ButtonWidget(Rectangle rectangle, Text text) {
  33. this.bounds = Objects.requireNonNull(rectangle);
  34. this.text = Objects.requireNonNull(text).asFormattedString();
  35. }
  36. public static ButtonWidget create(Rectangle point, String text, Consumer<ButtonWidget> onClick) {
  37. return create(point, new LiteralText(text), onClick);
  38. }
  39. public static ButtonWidget create(Rectangle point, Text text, Consumer<ButtonWidget> onClick) {
  40. ButtonWidget[] widget = {null};
  41. widget[0] = new ButtonWidget(point, text) {
  42. @Override
  43. public void onPressed() {
  44. onClick.accept(widget[0]);
  45. }
  46. };
  47. return widget[0];
  48. }
  49. public ButtonWidget tooltip(Supplier<String> tooltipSupplier) {
  50. this.tooltipSupplier = tooltipSupplier;
  51. return this;
  52. }
  53. public ButtonWidget enabled(boolean enabled) {
  54. this.enabled = enabled;
  55. return this;
  56. }
  57. public ButtonWidget canChangeFocuses(boolean canChangeFocuses) {
  58. this.canChangeFocuses = canChangeFocuses;
  59. return this;
  60. }
  61. public boolean canChangeFocuses() {
  62. return canChangeFocuses;
  63. }
  64. public Rectangle getBounds() {
  65. return bounds;
  66. }
  67. public String getText() {
  68. return text;
  69. }
  70. public void setText(String text) {
  71. this.text = text;
  72. }
  73. protected int getTextureId(boolean boolean_1) {
  74. int int_1 = 1;
  75. if (!this.enabled) {
  76. int_1 = 0;
  77. } else if (boolean_1) {
  78. int_1 = 4; // 2 is the old blue highlight, 3 is the 1.15 outline, 4 is the 1.15 online + light hover
  79. }
  80. return int_1;
  81. }
  82. protected void renderBackground(int x, int y, int width, int height, int textureOffset) {
  83. minecraft.getTextureManager().bindTexture(ScreenHelper.isDarkModeEnabled() ? BUTTON_LOCATION_DARK : BUTTON_LOCATION);
  84. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  85. RenderSystem.enableBlend();
  86. RenderSystem.blendFuncSeparate(770, 771, 1, 0);
  87. RenderSystem.blendFunc(770, 771);
  88. //Four Corners
  89. blit(x, y, getBlitOffset(), 0, textureOffset * 80, 4, 4, 512, 256);
  90. blit(x + width - 4, y, getBlitOffset(), 252, textureOffset * 80, 4, 4, 512, 256);
  91. blit(x, y + height - 4, getBlitOffset(), 0, textureOffset * 80 + 76, 4, 4, 512, 256);
  92. blit(x + width - 4, y + height - 4, getBlitOffset(), 252, textureOffset * 80 + 76, 4, 4, 512, 256);
  93. //Sides
  94. blit(x + 4, y, getBlitOffset(), 4, textureOffset * 80, MathHelper.ceil((width - 8) / 2f), 4, 512, 256);
  95. blit(x + 4, y + height - 4, getBlitOffset(), 4, textureOffset * 80 + 76, MathHelper.ceil((width - 8) / 2f), 4, 512, 256);
  96. blit(x + 4 + MathHelper.ceil((width - 8) / 2f), y + height - 4, getBlitOffset(), 252 - MathHelper.floor((width - 8) / 2f), textureOffset * 80 + 76, MathHelper.floor((width - 8) / 2f), 4, 512, 256);
  97. blit(x + 4 + MathHelper.ceil((width - 8) / 2f), y, getBlitOffset(), 252 - MathHelper.floor((width - 8) / 2f), textureOffset * 80, MathHelper.floor((width - 8) / 2f), 4, 512, 256);
  98. for (int i = y + 4; i < y + height - 4; i += 76) {
  99. blit(x, i, getBlitOffset(), 0, 4 + textureOffset * 80, MathHelper.ceil(width / 2f), MathHelper.clamp(y + height - 4 - i, 0, 76), 512, 256);
  100. blit(x + MathHelper.ceil(width / 2f), i, getBlitOffset(), 256 - MathHelper.floor(width / 2f), 4 + textureOffset * 80, MathHelper.floor(width / 2f), MathHelper.clamp(y + height - 4 - i, 0, 76), 512, 256);
  101. }
  102. }
  103. @Override
  104. public void render(int mouseX, int mouseY, float delta) {
  105. int x = bounds.x, y = bounds.y, width = bounds.width, height = bounds.height;
  106. renderBackground(x, y, width, height, this.getTextureId(isHovered(mouseX, mouseY)));
  107. int color = 14737632;
  108. if (!this.enabled) {
  109. color = 10526880;
  110. } else if (isHovered(mouseX, mouseY)) {
  111. color = 16777120;
  112. }
  113. this.drawCenteredString(font, getText(), x + width / 2, y + (height - 8) / 2, color);
  114. if (getTooltips().isPresent())
  115. if (!focused && containsMouse(mouseX, mouseY))
  116. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(getTooltips().get().split("\n")));
  117. else if (focused)
  118. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(new Point(x + width / 2, y + height / 2), getTooltips().get().split("\n")));
  119. }
  120. public boolean isHovered(int mouseX, int mouseY) {
  121. return isMouseOver(mouseX, mouseY) || focused;
  122. }
  123. @Override
  124. public boolean changeFocus(boolean boolean_1) {
  125. if (!enabled || !canChangeFocuses)
  126. return false;
  127. this.focused = !this.focused;
  128. return true;
  129. }
  130. @Override
  131. public List<? extends Element> children() {
  132. return Collections.emptyList();
  133. }
  134. @Override
  135. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  136. if (isMouseOver(mouseX, mouseY) && enabled && button == 0) {
  137. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  138. onPressed();
  139. return true;
  140. }
  141. return false;
  142. }
  143. @Override
  144. public boolean keyPressed(int int_1, int int_2, int int_3) {
  145. if (this.enabled && focused) {
  146. if (int_1 != 257 && int_1 != 32 && int_1 != 335) {
  147. return false;
  148. } else {
  149. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  150. this.onPressed();
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. public abstract void onPressed();
  157. public Optional<String> getTooltips() {
  158. return Optional.ofNullable(tooltipSupplier).map(Supplier::get);
  159. }
  160. }