LabelWidget.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.Point;
  25. import me.shedaniel.math.api.Rectangle;
  26. import me.shedaniel.rei.api.REIHelper;
  27. import me.shedaniel.rei.api.widgets.Tooltip;
  28. import me.shedaniel.rei.api.widgets.Widgets;
  29. import net.minecraft.client.gui.Element;
  30. import org.jetbrains.annotations.ApiStatus;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import java.util.Optional;
  34. import java.util.function.Consumer;
  35. import java.util.function.Supplier;
  36. /**
  37. * @see Widgets#createLabel(Point, String)
  38. * @see Widgets#createClickableLabel(Point, String, Consumer)
  39. */
  40. @Deprecated
  41. @ApiStatus.ScheduledForRemoval
  42. public class LabelWidget extends WidgetWithBounds {
  43. private Point pos;
  44. private String text;
  45. private int defaultColor;
  46. private boolean hasShadows = true;
  47. private boolean centered = true;
  48. private Supplier<String> tooltipSupplier;
  49. @ApiStatus.Internal
  50. public LabelWidget(me.shedaniel.math.api.Point point, String text) {
  51. this((Point) point, text);
  52. }
  53. @ApiStatus.Internal
  54. public LabelWidget(Point point, String text) {
  55. this.pos = point;
  56. this.text = text;
  57. this.defaultColor = REIHelper.getInstance().isDarkThemeEnabled() ? 0xFFBBBBBB : -1;
  58. }
  59. public static LabelWidget create(Point point, String text) {
  60. return new LabelWidget(point, text);
  61. }
  62. public static LabelWidget create(me.shedaniel.math.api.Point point, String text) {
  63. return new LabelWidget(point, text);
  64. }
  65. public static ClickableLabelWidget createClickable(me.shedaniel.math.api.Point point, String text, Consumer<ClickableLabelWidget> onClicked) {
  66. return createClickable((Point) point, text, onClicked);
  67. }
  68. public static ClickableLabelWidget createClickable(Point point, String text, Consumer<ClickableLabelWidget> onClicked) {
  69. ClickableLabelWidget[] widget = {null};
  70. widget[0] = new ClickableLabelWidget(point, text) {
  71. @Override
  72. public void onLabelClicked() {
  73. onClicked.accept(widget[0]);
  74. }
  75. };
  76. return widget[0];
  77. }
  78. public LabelWidget tooltip(Supplier<String> tooltipSupplier) {
  79. this.tooltipSupplier = tooltipSupplier;
  80. return this;
  81. }
  82. public boolean isCentered() {
  83. return centered;
  84. }
  85. public void setCentered(boolean centered) {
  86. this.centered = centered;
  87. }
  88. public LabelWidget centered() {
  89. setCentered(true);
  90. return this;
  91. }
  92. public LabelWidget leftAligned() {
  93. setCentered(false);
  94. return this;
  95. }
  96. public boolean isHasShadows() {
  97. return hasShadows;
  98. }
  99. public void setHasShadows(boolean hasShadows) {
  100. this.hasShadows = hasShadows;
  101. }
  102. public LabelWidget noShadow() {
  103. setHasShadows(false);
  104. return this;
  105. }
  106. public int getDefaultColor() {
  107. return defaultColor;
  108. }
  109. public void setDefaultColor(int defaultColor) {
  110. this.defaultColor = defaultColor;
  111. }
  112. /**
  113. * @return the position of this label
  114. * @deprecated Use {@link #getLocation()}
  115. */
  116. @Deprecated
  117. @ApiStatus.ScheduledForRemoval
  118. public me.shedaniel.math.api.Point getPosition() {
  119. return new me.shedaniel.math.api.Point(getLocation());
  120. }
  121. public Point getLocation() {
  122. return pos;
  123. }
  124. public LabelWidget setPosition(me.shedaniel.math.api.Point position) {
  125. return setLocation(position);
  126. }
  127. public LabelWidget setLocation(Point position) {
  128. this.pos = position;
  129. return this;
  130. }
  131. public String getText() {
  132. return text;
  133. }
  134. public LabelWidget setText(String text) {
  135. this.text = text;
  136. return this;
  137. }
  138. public LabelWidget color(int defaultColor) {
  139. this.defaultColor = defaultColor;
  140. return this;
  141. }
  142. public Optional<String> getTooltips() {
  143. return Optional.ofNullable(tooltipSupplier).map(Supplier::get);
  144. }
  145. @Override
  146. public Rectangle getBounds() {
  147. int width = font.getStringWidth(text);
  148. Point pos = getPosition();
  149. if (isCentered())
  150. return new Rectangle(pos.x - width / 2 - 1, pos.y - 5, width + 2, 14);
  151. return new Rectangle(pos.x - 1, pos.y - 5, width + 2, 14);
  152. }
  153. @Override
  154. public List<? extends Element> children() {
  155. return Collections.emptyList();
  156. }
  157. @Override
  158. public void render(int mouseX, int mouseY, float delta) {
  159. int width = font.getStringWidth(text);
  160. Point pos = getPosition();
  161. if (isCentered()) {
  162. if (hasShadows)
  163. font.drawWithShadow(text, pos.x - width / 2f, pos.y, defaultColor);
  164. else
  165. font.draw(text, pos.x - width / 2f, pos.y, defaultColor);
  166. } else {
  167. if (hasShadows)
  168. font.drawWithShadow(text, pos.x, pos.y, defaultColor);
  169. else
  170. font.draw(text, pos.x, pos.y, defaultColor);
  171. }
  172. }
  173. protected void drawTooltips(int mouseX, int mouseY) {
  174. if (getTooltips().isPresent())
  175. if (containsMouse(mouseX, mouseY))
  176. Tooltip.create(getTooltips().get().split("\n")).queue();
  177. }
  178. }