ButtonWidget.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.impl.widgets;
  24. import com.mojang.blaze3d.systems.RenderSystem;
  25. import me.shedaniel.math.api.Point;
  26. import me.shedaniel.math.api.Rectangle;
  27. import me.shedaniel.rei.api.REIHelper;
  28. import me.shedaniel.rei.api.widgets.Button;
  29. import me.shedaniel.rei.api.widgets.Tooltip;
  30. import net.minecraft.client.gui.Element;
  31. import net.minecraft.client.sound.PositionedSoundInstance;
  32. import net.minecraft.sound.SoundEvents;
  33. import net.minecraft.text.Text;
  34. import net.minecraft.util.Identifier;
  35. import net.minecraft.util.math.MathHelper;
  36. import org.jetbrains.annotations.NotNull;
  37. import org.jetbrains.annotations.Nullable;
  38. import java.util.Collections;
  39. import java.util.List;
  40. import java.util.Objects;
  41. import java.util.OptionalInt;
  42. import java.util.function.BiFunction;
  43. import java.util.function.Consumer;
  44. import java.util.function.Function;
  45. public class ButtonWidget extends Button {
  46. private static final Identifier BUTTON_LOCATION = new Identifier("roughlyenoughitems", "textures/gui/button.png");
  47. private static final Identifier BUTTON_LOCATION_DARK = new Identifier("roughlyenoughitems", "textures/gui/button_dark.png");
  48. @NotNull
  49. private Rectangle bounds;
  50. private boolean enabled = true;
  51. @NotNull
  52. private String text;
  53. @Nullable
  54. private Integer tint;
  55. @Nullable
  56. private Consumer<Button> onClick;
  57. @Nullable
  58. private Consumer<Button> onRender;
  59. private boolean focusable = false;
  60. private boolean focused = false;
  61. @Nullable
  62. private Function<@NotNull Button, @Nullable String> tooltipFunction;
  63. @Nullable
  64. private BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textColorFunction;
  65. @Nullable
  66. private BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textureIdFunction;
  67. public ButtonWidget(me.shedaniel.math.Rectangle rectangle, Text text) {
  68. this(rectangle, Objects.requireNonNull(text).asFormattedString());
  69. }
  70. public ButtonWidget(me.shedaniel.math.Rectangle rectangle, String text) {
  71. this.bounds = new Rectangle(Objects.requireNonNull(rectangle));
  72. this.text = Objects.requireNonNull(text);
  73. }
  74. @Override
  75. public final boolean isFocused() {
  76. return focused;
  77. }
  78. @Override
  79. public final boolean isEnabled() {
  80. return enabled;
  81. }
  82. @Override
  83. public final void setEnabled(boolean enabled) {
  84. this.enabled = enabled;
  85. }
  86. @Override
  87. public final OptionalInt getTint() {
  88. return OptionalInt.empty();
  89. }
  90. @Override
  91. public final void setTint(int tint) {
  92. this.tint = tint;
  93. }
  94. @Override
  95. public final void removeTint() {
  96. this.tint = null;
  97. }
  98. @Override
  99. @NotNull
  100. public final String getText() {
  101. return text;
  102. }
  103. @Override
  104. public final void setText(@NotNull String text) {
  105. this.text = text;
  106. }
  107. @Override
  108. public final @Nullable Consumer<Button> getOnClick() {
  109. return onClick;
  110. }
  111. @Override
  112. public final void setOnClick(@Nullable Consumer<Button> onClick) {
  113. this.onClick = onClick;
  114. }
  115. @Override
  116. public final @Nullable Consumer<Button> getOnRender() {
  117. return onRender;
  118. }
  119. @Override
  120. public final void setOnRender(@Nullable Consumer<Button> onRender) {
  121. this.onRender = onRender;
  122. }
  123. @Override
  124. public final boolean isFocusable() {
  125. return focusable;
  126. }
  127. @Override
  128. public final void setFocusable(boolean focusable) {
  129. this.focusable = focusable;
  130. }
  131. @Override
  132. public final @Nullable String getTooltip() {
  133. if (tooltipFunction == null)
  134. return null;
  135. return tooltipFunction.apply(this);
  136. }
  137. @Override
  138. public final void setTooltip(@Nullable Function<@NotNull Button, @Nullable String> tooltip) {
  139. this.tooltipFunction = tooltip;
  140. }
  141. @Override
  142. public final void setTextColor(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textColorFunction) {
  143. this.textColorFunction = textColorFunction;
  144. }
  145. @Override
  146. public final void setTextureId(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textureIdFunction) {
  147. this.textureIdFunction = textureIdFunction;
  148. }
  149. @Override
  150. public final int getTextColor(Point mouse) {
  151. if (this.textColorFunction != null) {
  152. Integer apply = this.textColorFunction.apply(this, mouse);
  153. if (apply != null)
  154. return apply;
  155. }
  156. if (!this.enabled) {
  157. return 10526880;
  158. } else if (isFocused(mouse.x, mouse.y)) {
  159. return 16777120;
  160. }
  161. return 14737632;
  162. }
  163. @Override
  164. public final @NotNull Rectangle getBounds() {
  165. return bounds;
  166. }
  167. @Override
  168. public void render(int mouseX, int mouseY, float delta) {
  169. if (onRender != null) {
  170. onRender.accept(this);
  171. }
  172. int x = bounds.x, y = bounds.y, width = bounds.width, height = bounds.height;
  173. renderBackground(x, y, width, height, this.getTextureId(new Point(mouseX, mouseY)));
  174. int color = 14737632;
  175. if (!this.enabled) {
  176. color = 10526880;
  177. } else if (isFocused(mouseX, mouseY)) {
  178. color = 16777120;
  179. }
  180. if (tint != null)
  181. fillGradient(x + 1, y + 1, x + width - 1, y + height - 1, tint, tint);
  182. this.drawCenteredString(font, getText(), x + width / 2, y + (height - 8) / 2, color);
  183. String tooltip = getTooltip();
  184. if (tooltip != null)
  185. if (!focused && containsMouse(mouseX, mouseY))
  186. Tooltip.create(tooltip.split("\n")).queue();
  187. else if (focused)
  188. Tooltip.create(new Point(x + width / 2, y + height / 2), tooltip.split("\n")).queue();
  189. }
  190. protected boolean isFocused(int mouseX, int mouseY) {
  191. return containsMouse(mouseX, mouseY) || focused;
  192. }
  193. @Override
  194. public boolean changeFocus(boolean boolean_1) {
  195. if (!enabled || !focusable)
  196. return false;
  197. this.focused = !this.focused;
  198. return true;
  199. }
  200. @Override
  201. public void onClick() {
  202. Consumer<Button> onClick = getOnClick();
  203. if (onClick != null)
  204. onClick.accept(this);
  205. }
  206. @Override
  207. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  208. if (containsMouse(mouseX, mouseY) && isEnabled() && button == 0) {
  209. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  210. onClick();
  211. return true;
  212. }
  213. return false;
  214. }
  215. @Override
  216. public boolean keyPressed(int int_1, int int_2, int int_3) {
  217. if (this.isEnabled() && focused) {
  218. if (int_1 != 257 && int_1 != 32 && int_1 != 335) {
  219. return false;
  220. } else {
  221. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  222. onClick();
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. @Override
  229. public List<? extends Element> children() {
  230. return Collections.emptyList();
  231. }
  232. @Override
  233. public final int getTextureId(Point mouse) {
  234. if (this.textureIdFunction != null) {
  235. Integer apply = this.textureIdFunction.apply(this, mouse);
  236. if (apply != null)
  237. return apply;
  238. }
  239. if (!this.isEnabled()) {
  240. return 0;
  241. } else if (containsMouse(mouse) || focused) {
  242. return 4; // 2 is the old blue highlight, 3 is the 1.15 outline, 4 is the 1.15 online + light hover
  243. }
  244. return 1;
  245. }
  246. protected void renderBackground(int x, int y, int width, int height, int textureOffset) {
  247. minecraft.getTextureManager().bindTexture(REIHelper.getInstance().isDarkThemeEnabled() ? BUTTON_LOCATION_DARK : BUTTON_LOCATION);
  248. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  249. RenderSystem.enableBlend();
  250. RenderSystem.blendFuncSeparate(770, 771, 1, 0);
  251. RenderSystem.blendFunc(770, 771);
  252. //Four Corners
  253. blit(x, y, getZOffset(), 0, textureOffset * 80, 4, 4, 512, 256);
  254. blit(x + width - 4, y, getZOffset(), 252, textureOffset * 80, 4, 4, 512, 256);
  255. blit(x, y + height - 4, getZOffset(), 0, textureOffset * 80 + 76, 4, 4, 512, 256);
  256. blit(x + width - 4, y + height - 4, getZOffset(), 252, textureOffset * 80 + 76, 4, 4, 512, 256);
  257. //Sides
  258. blit(x + 4, y, getZOffset(), 4, textureOffset * 80, MathHelper.ceil((width - 8) / 2f), 4, 512, 256);
  259. blit(x + 4, y + height - 4, getZOffset(), 4, textureOffset * 80 + 76, MathHelper.ceil((width - 8) / 2f), 4, 512, 256);
  260. blit(x + 4 + MathHelper.ceil((width - 8) / 2f), y + height - 4, getZOffset(), 252 - MathHelper.floor((width - 8) / 2f), textureOffset * 80 + 76, MathHelper.floor((width - 8) / 2f), 4, 512, 256);
  261. blit(x + 4 + MathHelper.ceil((width - 8) / 2f), y, getZOffset(), 252 - MathHelper.floor((width - 8) / 2f), textureOffset * 80, MathHelper.floor((width - 8) / 2f), 4, 512, 256);
  262. for (int i = y + 4; i < y + height - 4; i += 76) {
  263. blit(x, i, getZOffset(), 0, 4 + textureOffset * 80, MathHelper.ceil(width / 2f), MathHelper.clamp(y + height - 4 - i, 0, 76), 512, 256);
  264. blit(x + MathHelper.ceil(width / 2f), i, getZOffset(), 256 - MathHelper.floor(width / 2f), 4 + textureOffset * 80, MathHelper.floor(width / 2f), MathHelper.clamp(y + height - 4 - i, 0, 76), 512, 256);
  265. }
  266. }
  267. }