ClickableLabelWidget.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package me.shedaniel.rei.gui.widget;
  2. import me.shedaniel.rei.client.ScreenHelper;
  3. import java.awt.*;
  4. import java.util.Optional;
  5. public abstract class ClickableLabelWidget extends LabelWidget {
  6. public static final int hoveredColor = (new Color(102, 255, 204)).getRGB();
  7. public boolean focused;
  8. public ClickableLabelWidget(int x, int y, String text) {
  9. super(x, y, text);
  10. }
  11. @Override
  12. public void render(int mouseX, int mouseY, float delta) {
  13. int colour = -1;
  14. if (isHovered(mouseX, mouseY))
  15. colour = hoveredColor;
  16. drawCenteredString(textRenderer, (isHovered(mouseX, mouseY) ? "§n" : "") + text, x, y, colour);
  17. if (getTooltips().isPresent())
  18. if (!focused && isHighlighted(mouseX, mouseY))
  19. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(getTooltips().get().split("\n")));
  20. else if (focused)
  21. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(new Point(x, y), getTooltips().get().split("\n")));
  22. }
  23. @Override
  24. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  25. if (button == 0 && isHighlighted(mouseX, mouseY)) {
  26. onLabelClicked();
  27. return true;
  28. }
  29. return false;
  30. }
  31. public Optional<String> getTooltips() {
  32. return Optional.empty();
  33. }
  34. @Override
  35. public boolean keyPressed(int int_1, int int_2, int int_3) {
  36. if (int_1 != 257 && int_1 != 32 && int_1 != 335) {
  37. return false;
  38. } else {
  39. this.onLabelClicked();
  40. return true;
  41. }
  42. }
  43. @Override
  44. public boolean isPartOfFocusCycle() {
  45. return true;
  46. }
  47. public boolean isHovered(int mouseX, int mouseY) {
  48. return isHighlighted(mouseX, mouseY) || focused;
  49. }
  50. @Override
  51. public void onFocusChanged(boolean boolean_1, boolean boolean_2) {
  52. focused = boolean_2;
  53. }
  54. public abstract void onLabelClicked();
  55. }