ClickableLabelWidget.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 me.shedaniel.math.api.Point;
  25. import me.shedaniel.rei.impl.ScreenHelper;
  26. import org.jetbrains.annotations.ApiStatus;
  27. public abstract class ClickableLabelWidget extends LabelWidget {
  28. public boolean focused;
  29. private boolean clickable = true;
  30. private int hoveredColor;
  31. @ApiStatus.Internal
  32. protected ClickableLabelWidget(Point point, String text) {
  33. super(point, text);
  34. this.hoveredColor = ScreenHelper.isDarkModeEnabled() ? -1 : 0xFF66FFCC;
  35. }
  36. public LabelWidget hoveredColor(int hoveredColor) {
  37. this.hoveredColor = hoveredColor;
  38. return this;
  39. }
  40. public LabelWidget clickable(boolean clickable) {
  41. this.clickable = clickable;
  42. return this;
  43. }
  44. public boolean isClickable() {
  45. return clickable;
  46. }
  47. @Override
  48. public void render(int mouseX, int mouseY, float delta) {
  49. int color = getDefaultColor();
  50. if (isClickable() && isHovered(mouseX, mouseY))
  51. color = getHoveredColor();
  52. Point pos = getPosition();
  53. int width = font.getStringWidth(getText());
  54. if (isCentered()) {
  55. if (isHasShadows())
  56. font.drawWithShadow(getText(), pos.x - width / 2f, pos.y, color);
  57. else
  58. font.draw(getText(), pos.x - width / 2f, pos.y, color);
  59. } else {
  60. if (isHasShadows())
  61. font.drawWithShadow(getText(), pos.x, pos.y, color);
  62. else
  63. font.draw(getText(), pos.x, pos.y, color);
  64. }
  65. drawTooltips(mouseX, mouseY);
  66. }
  67. @Override
  68. protected void drawTooltips(int mouseX, int mouseY) {
  69. if (isClickable() && getTooltips().isPresent())
  70. if (!focused && containsMouse(mouseX, mouseY))
  71. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(getTooltips().get().split("\n")));
  72. else if (focused)
  73. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(getPosition(), getTooltips().get().split("\n")));
  74. }
  75. public int getHoveredColor() {
  76. return hoveredColor;
  77. }
  78. @Override
  79. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  80. if (button == 0 && isClickable() && containsMouse(mouseX, mouseY)) {
  81. onLabelClicked();
  82. return true;
  83. }
  84. return false;
  85. }
  86. @Override
  87. public boolean keyPressed(int int_1, int int_2, int int_3) {
  88. if (!isClickable() || !focused)
  89. return false;
  90. if (int_1 != 257 && int_1 != 32 && int_1 != 335)
  91. return false;
  92. this.onLabelClicked();
  93. return true;
  94. }
  95. @Override
  96. public boolean changeFocus(boolean boolean_1) {
  97. if (!isClickable())
  98. return false;
  99. this.focused = !this.focused;
  100. return true;
  101. }
  102. public boolean isHovered(int mouseX, int mouseY) {
  103. return isClickable() && (containsMouse(mouseX, mouseY) || focused);
  104. }
  105. public abstract void onLabelClicked();
  106. }