LabelWidget.java 6.0 KB

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