Button.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.api.widgets;
  24. import com.mojang.blaze3d.vertex.PoseStack;
  25. import me.shedaniel.math.Point;
  26. import net.minecraft.network.chat.Component;
  27. import org.jetbrains.annotations.NotNull;
  28. import org.jetbrains.annotations.Nullable;
  29. import java.util.OptionalInt;
  30. import java.util.function.BiConsumer;
  31. import java.util.function.BiFunction;
  32. import java.util.function.Consumer;
  33. import java.util.function.Function;
  34. public abstract class Button extends BaseWidget<Button> {
  35. public abstract void setTextColor(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textColorFunction);
  36. public final Button textColor(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textColorFunction) {
  37. setTextColor(textColorFunction);
  38. return this;
  39. }
  40. public abstract int getTextColor(Point mouse);
  41. public abstract void setTextureId(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textureIdFunction);
  42. public final Button textureId(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textureIdFunction) {
  43. setTextureId(textureIdFunction);
  44. return this;
  45. }
  46. public abstract int getTextureId(Point mouse);
  47. public abstract void onClick();
  48. public abstract boolean isEnabled();
  49. public abstract void setEnabled(boolean enabled);
  50. public final Button enabled(boolean enabled) {
  51. setEnabled(enabled);
  52. return this;
  53. }
  54. public abstract OptionalInt getTint();
  55. public abstract void setTint(int tint);
  56. public abstract void removeTint();
  57. public final Button tint(@Nullable Integer tint) {
  58. if (tint == null)
  59. removeTint();
  60. else setTint(tint);
  61. return this;
  62. }
  63. @NotNull
  64. public abstract Component getText();
  65. public abstract void setText(@NotNull Component text);
  66. @NotNull
  67. public final Button text(@NotNull Component text) {
  68. setText(text);
  69. return this;
  70. }
  71. @Nullable
  72. public abstract Consumer<Button> getOnClick();
  73. public abstract void setOnClick(@Nullable Consumer<Button> onClick);
  74. @NotNull
  75. public final Button onClick(@Nullable Consumer<Button> onClick) {
  76. setOnClick(onClick);
  77. return this;
  78. }
  79. @Nullable
  80. public abstract BiConsumer<PoseStack, Button> getOnRender();
  81. public abstract void setOnRender(@Nullable BiConsumer<PoseStack, Button> onRender);
  82. @NotNull
  83. public final Button onRender(@Nullable BiConsumer<PoseStack, Button> onRender) {
  84. setOnRender(onRender);
  85. return this;
  86. }
  87. /**
  88. * @return whether the button is focusable by pressing tab, ignored if not clickable.
  89. */
  90. public abstract boolean isFocusable();
  91. /**
  92. * Sets whether the button is focusable by pressing tab, ignored if not clickable.
  93. *
  94. * @param focusable whether the button is focusable by pressing tab, ignored if not clickable.
  95. */
  96. public abstract void setFocusable(boolean focusable);
  97. /**
  98. * Sets whether the button is focusable by pressing tab, ignored if not clickable.
  99. *
  100. * @param focusable whether the label is focusable by pressing tab, ignored if not clickable.
  101. * @return the button itself.
  102. */
  103. @NotNull
  104. public final Button focusable(boolean focusable) {
  105. setFocusable(focusable);
  106. return this;
  107. }
  108. /**
  109. * @return the tooltip from the current tooltip function, null if no tooltip.
  110. */
  111. @Nullable
  112. public abstract String getTooltip();
  113. /**
  114. * Sets the tooltip function used to get the tooltip.
  115. *
  116. * @param tooltip the tooltip function used to get the tooltip.
  117. */
  118. public abstract void setTooltip(@Nullable Function<@NotNull Button, @Nullable String> tooltip);
  119. /**
  120. * Sets the tooltip.
  121. *
  122. * @param tooltip the lines of tooltip.
  123. * @return the button itself.
  124. */
  125. @NotNull
  126. public final Button tooltipLines(@NotNull String... tooltip) {
  127. return tooltipLine(String.join("\n", tooltip));
  128. }
  129. /**
  130. * Sets the tooltip.
  131. *
  132. * @param tooltip the line of tooltip.
  133. * @return the button itself.
  134. */
  135. @NotNull
  136. public final Button tooltipLine(@Nullable String tooltip) {
  137. return tooltipSupplier(label -> tooltip);
  138. }
  139. /**
  140. * Sets the tooltip function.
  141. *
  142. * @param tooltip the tooltip function used to get the tooltip.
  143. * @return the button itself.
  144. */
  145. @NotNull
  146. public final Button tooltipSupplier(@Nullable Function<@NotNull Button, @Nullable String> tooltip) {
  147. setTooltip(tooltip);
  148. return this;
  149. }
  150. public abstract boolean isFocused();
  151. }