ButtonWidget.java 5.2 KB

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