ButtonWidget.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.gui.widget;
  6. import com.mojang.blaze3d.platform.GlStateManager;
  7. import me.shedaniel.rei.client.ScreenHelper;
  8. import net.minecraft.client.gui.Element;
  9. import net.minecraft.client.sound.PositionedSoundInstance;
  10. import net.minecraft.sound.SoundEvents;
  11. import net.minecraft.text.Text;
  12. import net.minecraft.util.Identifier;
  13. import net.minecraft.util.math.MathHelper;
  14. import java.awt.*;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Optional;
  18. public abstract class ButtonWidget extends WidgetWithBounds {
  19. public static final Identifier BUTTON_LOCATION = new Identifier("roughlyenoughitems", "textures/gui/button.png");
  20. public static final Identifier BUTTON_LOCATION_DARK = new Identifier("roughlyenoughitems", "textures/gui/button_dark.png");
  21. public String text;
  22. public boolean enabled;
  23. public boolean focused;
  24. private Rectangle bounds;
  25. public ButtonWidget(Rectangle rectangle, Text text) {
  26. this(rectangle, text.asFormattedString());
  27. }
  28. public ButtonWidget(Rectangle rectangle, String text) {
  29. this.bounds = rectangle;
  30. this.enabled = true;
  31. this.text = text;
  32. }
  33. public ButtonWidget(int x, int y, int width, int height, String text) {
  34. this(new Rectangle(x, y, width, height), text);
  35. }
  36. public ButtonWidget(int x, int y, int width, int height, Text text) {
  37. this(new Rectangle(x, y, width, height), text);
  38. }
  39. public Rectangle getBounds() {
  40. return bounds;
  41. }
  42. protected int getTextureId(boolean boolean_1) {
  43. int int_1 = 1;
  44. if (!this.enabled) {
  45. int_1 = 0;
  46. } else if (boolean_1) {
  47. int_1 = 2;
  48. }
  49. return int_1;
  50. }
  51. @Override
  52. public void render(int mouseX, int mouseY, float delta) {
  53. int x = bounds.x, y = bounds.y, width = bounds.width, height = bounds.height;
  54. minecraft.getTextureManager().bindTexture(ScreenHelper.isDarkModeEnabled() ? BUTTON_LOCATION_DARK : BUTTON_LOCATION);
  55. GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  56. int textureOffset = this.getTextureId(isHovered(mouseX, mouseY));
  57. GlStateManager.enableBlend();
  58. GlStateManager.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
  59. GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
  60. //Four Corners
  61. blit(x, y, 0, textureOffset * 80, 4, 4);
  62. blit(x + width - 4, y, 252, textureOffset * 80, 4, 4);
  63. blit(x, y + height - 4, 0, textureOffset * 80 + 76, 4, 4);
  64. blit(x + width - 4, y + height - 4, 252, textureOffset * 80 + 76, 4, 4);
  65. //Sides
  66. blit(x + 4, y, 4, textureOffset * 80, MathHelper.ceil((width - 8) / 2f), 4);
  67. blit(x + 4, y + height - 4, 4, textureOffset * 80 + 76, MathHelper.ceil((width - 8) / 2f), 4);
  68. blit(x + 4 + MathHelper.ceil((width - 8) / 2f), y + height - 4, 252 - MathHelper.floor((width - 8) / 2f), textureOffset * 80 + 76, MathHelper.floor((width - 8) / 2f), 4);
  69. blit(x + 4 + MathHelper.ceil((width - 8) / 2f), y, 252 - MathHelper.floor((width - 8) / 2f), textureOffset * 80, MathHelper.floor((width - 8) / 2f), 4);
  70. for(int i = y + 4; i < y + height - 4; i += 76) {
  71. blit(x, i, 0, 4 + textureOffset * 80, MathHelper.ceil(width / 2f), MathHelper.clamp(y + height - 4 - i, 0, 76));
  72. blit(x + MathHelper.ceil(width / 2f), i, 256 - MathHelper.floor(width / 2f), 4 + textureOffset * 80, MathHelper.floor(width / 2f), MathHelper.clamp(y + height - 4 - i, 0, 76));
  73. }
  74. int colour = 14737632;
  75. if (!this.enabled) {
  76. colour = 10526880;
  77. } else if (isHovered(mouseX, mouseY)) {
  78. colour = 16777120;
  79. }
  80. this.drawCenteredString(font, text, x + width / 2, y + (height - 8) / 2, colour);
  81. if (getTooltips().isPresent())
  82. if (!focused && containsMouse(mouseX, mouseY))
  83. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(getTooltips().get().split("\n")));
  84. else if (focused)
  85. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(new Point(x + width / 2, y + height / 2), getTooltips().get().split("\n")));
  86. }
  87. public boolean isHovered(int mouseX, int mouseY) {
  88. return isMouseOver(mouseX, mouseY) || focused;
  89. }
  90. @Override
  91. public boolean changeFocus(boolean boolean_1) {
  92. if (!enabled)
  93. return false;
  94. this.focused = !this.focused;
  95. return true;
  96. }
  97. @Override
  98. public List<? extends Element> children() {
  99. return Collections.emptyList();
  100. }
  101. @Override
  102. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  103. if (bounds.contains(mouseX, mouseY) && enabled && button == 0) {
  104. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  105. onPressed();
  106. return true;
  107. }
  108. return false;
  109. }
  110. @Override
  111. public boolean keyPressed(int int_1, int int_2, int int_3) {
  112. if (this.enabled && focused) {
  113. if (int_1 != 257 && int_1 != 32 && int_1 != 335) {
  114. return false;
  115. } else {
  116. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  117. this.onPressed();
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. public abstract void onPressed();
  124. public Optional<String> getTooltips() {
  125. return Optional.empty();
  126. }
  127. }