PanelWidget.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Rectangle;
  26. import me.shedaniel.rei.api.ConfigObject;
  27. import me.shedaniel.rei.api.REIHelper;
  28. import me.shedaniel.rei.api.widgets.Panel;
  29. import me.shedaniel.rei.gui.config.RecipeBorderType;
  30. import me.shedaniel.rei.gui.config.RecipeScreenType;
  31. import net.minecraft.client.gui.Element;
  32. import net.minecraft.util.Identifier;
  33. import org.jetbrains.annotations.NotNull;
  34. import java.util.Collections;
  35. import java.util.List;
  36. import java.util.Objects;
  37. import java.util.function.Predicate;
  38. public final class PanelWidget extends Panel {
  39. private static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
  40. private static final Identifier CHEST_GUI_TEXTURE_DARK = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer_dark.png");
  41. private static final PanelWidget TEMP = new PanelWidget(new Rectangle());
  42. private me.shedaniel.math.api.Rectangle bounds;
  43. private int color = -1;
  44. private int innerColor = REIHelper.getInstance().isDarkThemeEnabled() ? -13750738 : -3750202;
  45. private int xTextureOffset = 0;
  46. private int yTextureOffset = RecipeBorderType.DEFAULT.getYOffset();
  47. @NotNull
  48. private Predicate<Panel> rendering = PanelWidget::isRendering;
  49. public static boolean isRendering(Panel panel) {
  50. return ConfigObject.getInstance().getRecipeScreenType() != RecipeScreenType.VILLAGER;
  51. }
  52. public PanelWidget(@NotNull Rectangle bounds) {
  53. this.bounds = new me.shedaniel.math.api.Rectangle(bounds);
  54. }
  55. public static void render(@NotNull Rectangle bounds, int color) {
  56. TEMP.bounds.setBounds(bounds);
  57. TEMP.color = color;
  58. TEMP.render(0, 0, 0);
  59. }
  60. @Override
  61. public int getInnerColor() {
  62. return innerColor;
  63. }
  64. @Override
  65. public void setInnerColor(int innerColor) {
  66. this.innerColor = innerColor;
  67. }
  68. @Override
  69. public int getXTextureOffset() {
  70. return xTextureOffset;
  71. }
  72. @Override
  73. public void setXTextureOffset(int xTextureOffset) {
  74. this.xTextureOffset = xTextureOffset;
  75. }
  76. @Override
  77. public int getYTextureOffset() {
  78. return yTextureOffset;
  79. }
  80. @Override
  81. public void setYTextureOffset(int yTextureOffset) {
  82. this.yTextureOffset = yTextureOffset;
  83. }
  84. @Override
  85. public int getColor() {
  86. return color;
  87. }
  88. @Override
  89. public void setColor(int color) {
  90. this.color = color;
  91. }
  92. @Override
  93. public @NotNull Predicate<Panel> getRendering() {
  94. return rendering;
  95. }
  96. @Override
  97. public void setRendering(@NotNull Predicate<Panel> rendering) {
  98. this.rendering = Objects.requireNonNull(rendering);
  99. }
  100. @NotNull
  101. @Override
  102. public me.shedaniel.math.api.Rectangle getBounds() {
  103. return bounds;
  104. }
  105. @Override
  106. public void render(int mouseX, int mouseY, float delta) {
  107. if (!getRendering().test(this))
  108. return;
  109. float alpha = ((color >> 24) & 0xFF) / 255f;
  110. float red = ((color >> 16) & 0xFF) / 255f;
  111. float green = ((color >> 8) & 0xFF) / 255f;
  112. float blue = (color & 0xFF) / 255f;
  113. RenderSystem.color4f(red, green, blue, alpha);
  114. minecraft.getTextureManager().bindTexture(REIHelper.getInstance().isDarkThemeEnabled() ? CHEST_GUI_TEXTURE_DARK : CHEST_GUI_TEXTURE);
  115. int x = bounds.x, y = bounds.y, width = bounds.width, height = bounds.height;
  116. int xTextureOffset = getXTextureOffset();
  117. int yTextureOffset = getYTextureOffset();
  118. //Four Corners
  119. this.blit(x, y, 106 + xTextureOffset, 124 + yTextureOffset, 4, 4);
  120. this.blit(x + width - 4, y, 252 + xTextureOffset, 124 + yTextureOffset, 4, 4);
  121. this.blit(x, y + height - 4, 106 + xTextureOffset, 186 + yTextureOffset, 4, 4);
  122. this.blit(x + width - 4, y + height - 4, 252 + xTextureOffset, 186 + yTextureOffset, 4, 4);
  123. //Sides
  124. for (int xx = 4; xx < width - 4; xx += 128) {
  125. int thisWidth = Math.min(128, width - 4 - xx);
  126. this.blit(x + xx, y, 110 + xTextureOffset, 124 + yTextureOffset, thisWidth, 4);
  127. this.blit(x + xx, y + height - 4, 110 + xTextureOffset, 186 + yTextureOffset, thisWidth, 4);
  128. }
  129. for (int yy = 4; yy < height - 4; yy += 50) {
  130. int thisHeight = Math.min(50, height - 4 - yy);
  131. this.blit(x, y + yy, 106 + xTextureOffset, 128 + yTextureOffset, 4, thisHeight);
  132. this.blit(x + width - 4, y + yy, 252 + xTextureOffset, 128 + yTextureOffset, 4, thisHeight);
  133. }
  134. fillGradient(x + 4, y + 4, x + width - 4, y + height - 4, getInnerColor(), getInnerColor());
  135. }
  136. @Override
  137. public List<? extends Element> children() {
  138. return Collections.emptyList();
  139. }
  140. }