TabWidget.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.api.Rectangle;
  25. import me.shedaniel.rei.api.ClientHelper;
  26. import me.shedaniel.rei.api.EntryStack;
  27. import me.shedaniel.rei.api.REIHelper;
  28. import me.shedaniel.rei.api.RecipeCategory;
  29. import me.shedaniel.rei.api.widgets.Tooltip;
  30. import net.minecraft.util.Formatting;
  31. import net.minecraft.util.Identifier;
  32. import org.jetbrains.annotations.ApiStatus;
  33. import org.jetbrains.annotations.Nullable;
  34. import java.util.Collections;
  35. import java.util.List;
  36. import java.util.function.Predicate;
  37. @ApiStatus.Internal
  38. public class TabWidget extends WidgetWithBounds {
  39. public static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
  40. public static final Identifier CHEST_GUI_TEXTURE_DARK = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer_dark.png");
  41. public boolean shown = false, selected = false;
  42. public EntryStack logo;
  43. public int id;
  44. public String categoryName;
  45. public Rectangle bounds;
  46. public RecipeCategory<?> category;
  47. public int u, v;
  48. @Nullable
  49. private Predicate<TabWidget> onClick;
  50. private TabWidget(int id, Rectangle bounds, int u, int v, @Nullable Predicate<TabWidget> onClick) {
  51. this.id = id;
  52. this.bounds = bounds;
  53. this.u = u;
  54. this.v = v;
  55. this.onClick = onClick;
  56. }
  57. @ApiStatus.Internal
  58. public static TabWidget create(int id, int tabSize, int leftX, int bottomY, int u, int v, @Nullable Predicate<TabWidget> onClick) {
  59. return new TabWidget(id, new Rectangle(leftX + id * tabSize, bottomY - tabSize, tabSize, tabSize), u, v, onClick);
  60. }
  61. @Override
  62. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  63. return button == 0 && containsMouse(mouseX, mouseY) && onClick.test(this);
  64. }
  65. public void setRenderer(RecipeCategory<?> category, EntryStack logo, String categoryName, boolean selected) {
  66. if (logo == null) {
  67. shown = false;
  68. this.logo = null;
  69. } else {
  70. shown = true;
  71. this.logo = logo;
  72. }
  73. this.category = category;
  74. this.selected = selected;
  75. this.categoryName = categoryName;
  76. }
  77. public boolean isSelected() {
  78. return selected;
  79. }
  80. public int getId() {
  81. return id;
  82. }
  83. public boolean isShown() {
  84. return shown;
  85. }
  86. @Override
  87. public List<Widget> children() {
  88. return Collections.emptyList();
  89. }
  90. @Override
  91. public void render(int mouseX, int mouseY, float delta) {
  92. if (shown) {
  93. minecraft.getTextureManager().bindTexture(REIHelper.getInstance().isDarkThemeEnabled() ? CHEST_GUI_TEXTURE_DARK : CHEST_GUI_TEXTURE);
  94. this.blit(bounds.x, bounds.y + 2, u + (selected ? bounds.width : 0), v, bounds.width, (selected ? bounds.height + 2 : bounds.height - 1));
  95. logo.setZ(100);
  96. logo.render(new Rectangle(bounds.getCenterX() - 8, bounds.getCenterY() - 5, 16, 16), mouseX, mouseY, delta);
  97. if (containsMouse(mouseX, mouseY)) {
  98. drawTooltip();
  99. }
  100. }
  101. }
  102. private void drawTooltip() {
  103. if (this.minecraft.options.advancedItemTooltips)
  104. Tooltip.create(categoryName, Formatting.DARK_GRAY.toString() + category.getIdentifier().toString(), ClientHelper.getInstance().getFormattedModFromIdentifier(category.getIdentifier())).queue();
  105. else
  106. Tooltip.create(categoryName, ClientHelper.getInstance().getFormattedModFromIdentifier(category.getIdentifier())).queue();
  107. }
  108. @Override
  109. public Rectangle getBounds() {
  110. return bounds;
  111. }
  112. }