ClothConfigTabButton.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * This file is part of Cloth Config.
  3. * Copyright (C) 2020 - 2021 shedaniel
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.clothconfig2.gui;
  20. import me.shedaniel.clothconfig2.api.Tooltip;
  21. import me.shedaniel.math.Point;
  22. import net.fabricmc.api.EnvType;
  23. import net.fabricmc.api.Environment;
  24. import net.minecraft.client.Minecraft;
  25. import net.minecraft.client.gui.components.AbstractButton;
  26. import net.minecraft.network.chat.Component;
  27. import net.minecraft.network.chat.FormattedText;
  28. import org.jetbrains.annotations.Nullable;
  29. import com.mojang.blaze3d.vertex.PoseStack;
  30. import java.util.Optional;
  31. import java.util.function.Supplier;
  32. @Environment(EnvType.CLIENT)
  33. public class ClothConfigTabButton extends AbstractButton {
  34. private final int index;
  35. private final ClothConfigScreen screen;
  36. @Nullable
  37. private final Supplier<Optional<FormattedText[]>> descriptionSupplier;
  38. public ClothConfigTabButton(ClothConfigScreen screen, int index, int int_1, int int_2, int int_3, int int_4, Component string_1, Supplier<Optional<FormattedText[]>> descriptionSupplier) {
  39. super(int_1, int_2, int_3, int_4, string_1);
  40. this.index = index;
  41. this.screen = screen;
  42. this.descriptionSupplier = descriptionSupplier;
  43. }
  44. public ClothConfigTabButton(ClothConfigScreen screen, int index, int int_1, int int_2, int int_3, int int_4, Component string_1) {
  45. this(screen, index, int_1, int_2, int_3, int_4, string_1, null);
  46. }
  47. @Override
  48. public void onPress() {
  49. if (index != -1)
  50. screen.selectedCategoryIndex = index;
  51. screen.init(Minecraft.getInstance(), screen.width, screen.height);
  52. }
  53. @Override
  54. public void render(PoseStack matrices, int int_1, int int_2, float float_1) {
  55. active = index != screen.selectedCategoryIndex;
  56. super.render(matrices, int_1, int_2, float_1);
  57. if (isMouseOver(int_1, int_2)) {
  58. Optional<FormattedText[]> tooltip = getTooltip();
  59. if (tooltip.isPresent() && tooltip.get().length > 0)
  60. screen.addTooltip(Tooltip.of(new Point(int_1, int_2), tooltip.get()));
  61. }
  62. }
  63. @Override
  64. protected boolean clicked(double double_1, double double_2) {
  65. return visible && active && isMouseOver(double_1, double_2);
  66. }
  67. @Override
  68. public boolean isMouseOver(double double_1, double double_2) {
  69. return this.visible && double_1 >= this.x && double_2 >= this.y && double_1 < this.x + this.width && double_2 < this.y + this.height && double_1 >= 20 && double_1 < screen.width - 20;
  70. }
  71. public Optional<FormattedText[]> getTooltip() {
  72. if (descriptionSupplier != null)
  73. return descriptionSupplier.get();
  74. return Optional.empty();
  75. }
  76. }