Button.java 5.7 KB

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