TabWidget.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2018, 2019, 2020 shedaniel
  3. * Licensed under the MIT License (the "License").
  4. */
  5. package me.shedaniel.rei.gui.widget;
  6. import me.shedaniel.math.api.Rectangle;
  7. import me.shedaniel.rei.api.ClientHelper;
  8. import me.shedaniel.rei.api.EntryStack;
  9. import me.shedaniel.rei.api.RecipeCategory;
  10. import me.shedaniel.rei.impl.ScreenHelper;
  11. import net.minecraft.util.Formatting;
  12. import net.minecraft.util.Identifier;
  13. import org.jetbrains.annotations.ApiStatus;
  14. import java.util.Collections;
  15. import java.util.List;
  16. @ApiStatus.Internal
  17. public class TabWidget extends WidgetWithBounds {
  18. public static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
  19. public static final Identifier CHEST_GUI_TEXTURE_DARK = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer_dark.png");
  20. public boolean shown = false, selected = false;
  21. public EntryStack logo;
  22. public int id;
  23. public String categoryName;
  24. public Rectangle bounds;
  25. public RecipeCategory<?> category;
  26. public int u, v;
  27. public TabWidget(int id, Rectangle bounds) {
  28. this(id, bounds, 0, 192);
  29. }
  30. public TabWidget(int id, Rectangle bounds, int u, int v) {
  31. this.id = id;
  32. this.bounds = bounds;
  33. this.u = u;
  34. this.v = v;
  35. }
  36. public TabWidget(int id, int tabSize, int leftX, int bottomY) {
  37. this(id, new Rectangle(leftX + id * tabSize, bottomY - tabSize, tabSize, tabSize));
  38. }
  39. public TabWidget(int id, int tabSize, int leftX, int bottomY, int u, int v) {
  40. this(id, new Rectangle(leftX + id * tabSize, bottomY - tabSize, tabSize, tabSize), u, v);
  41. }
  42. public void setRenderer(RecipeCategory<?> category, EntryStack logo, String categoryName, boolean selected) {
  43. if (logo == null) {
  44. shown = false;
  45. this.logo = null;
  46. } else {
  47. shown = true;
  48. this.logo = logo;
  49. }
  50. this.category = category;
  51. this.selected = selected;
  52. this.categoryName = categoryName;
  53. }
  54. public boolean isSelected() {
  55. return selected;
  56. }
  57. public int getId() {
  58. return id;
  59. }
  60. public boolean isShown() {
  61. return shown;
  62. }
  63. @Override
  64. public List<Widget> children() {
  65. return Collections.emptyList();
  66. }
  67. @Override
  68. public void render(int mouseX, int mouseY, float delta) {
  69. if (shown) {
  70. minecraft.getTextureManager().bindTexture(ScreenHelper.isDarkModeEnabled() ? CHEST_GUI_TEXTURE_DARK : CHEST_GUI_TEXTURE);
  71. this.blit(bounds.x, bounds.y + 2, u + (selected ? bounds.width : 0), v, bounds.width, (selected ? bounds.height + 2 : bounds.height - 1));
  72. logo.setZ(100);
  73. logo.render(new Rectangle(bounds.getCenterX() - 8, bounds.getCenterY() - 5, 16, 16), mouseX, mouseY, delta);
  74. if (containsMouse(mouseX, mouseY)) {
  75. drawTooltip();
  76. }
  77. }
  78. }
  79. private void drawTooltip() {
  80. if (this.minecraft.options.advancedItemTooltips)
  81. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(categoryName, Formatting.DARK_GRAY.toString() + category.getIdentifier().toString(), ClientHelper.getInstance().getFormattedModFromIdentifier(category.getIdentifier())));
  82. else
  83. ScreenHelper.getLastOverlay().addTooltip(QueuedTooltip.create(categoryName, ClientHelper.getInstance().getFormattedModFromIdentifier(category.getIdentifier())));
  84. }
  85. @Override
  86. public Rectangle getBounds() {
  87. return bounds;
  88. }
  89. }