ButtonWidget.java 8.2 KB

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