Vaerys_Dawn

Button list

Feb 20th, 2021
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1.     public class ButtonList {
  2.         public int xPos;
  3.         public int yPos;
  4.         public int height;
  5.         public int width;
  6.         public int itemHeight;
  7.         public int itemWidth;
  8.         public int scrollPos = 0;
  9.         public TabButton button;
  10.         public boolean active = false;
  11.         Map<String, ? extends BeepediaPage> list;
  12.         Map<String, BeepediaPage> reducedList = new HashMap<>();
  13.  
  14.         public ButtonList(int xPos, int yPos, int height, int width, int itemHeight, int itemWidth, TabButton button, Map<String, ? extends BeepediaPage> list) {
  15.             this.xPos = xPos;
  16.             this.yPos = yPos;
  17.             this.height = height;
  18.             this.width = width;
  19.             this.itemHeight = itemHeight;
  20.             this.itemWidth = itemWidth;
  21.             this.list = list;
  22.             this.button = button;
  23.             updateReducedList(null);
  24.             list.forEach((s, b) -> b.listButton.setParent(this));
  25.         }
  26.  
  27.         public void updateReducedList(String search) {
  28.             reducedList.clear();
  29.             if (search != null && !search.isEmpty() && !search.equals("")) {
  30.                 list.forEach((s, b) -> {
  31.                     if (s.contains(search.toLowerCase()) || b.getTranslation().toLowerCase().contains(search.toLowerCase())) {
  32.                         reducedList.put(s, b);
  33.                     }
  34.                 });
  35.             } else {
  36.                 reducedList = new HashMap<>(list);
  37.             }
  38.         }
  39.  
  40.         public void updatePos(int newPos) {
  41.             // update position of list
  42.             if (height > reducedList.size() * 20) return;
  43.             scrollPos -= newPos;
  44.             if (scrollPos > 0) scrollPos = 0;
  45.             else if (scrollPos < -(reducedList.size() * 20 - height)) scrollPos = -(reducedList.size() * 20 - height);
  46.         }
  47.  
  48.         public void updateList() {
  49.             // update each button
  50.             AtomicInteger counter = new AtomicInteger();
  51.             reducedList.forEach((s, b) -> {
  52.                 b.updateListPosition(xPos, (yPos + scrollPos + counter.get() * itemHeight));
  53.                 counter.getAndIncrement();
  54.             });
  55.         }
  56.  
  57.         public void setActive(boolean active) {
  58.             this.active = active;
  59.             list.forEach((s, b) -> {
  60.                 if (b.listButton != null) b.listButton.visible = active;
  61.             });
  62.         }
  63.     }
  64.  
  65.     public class ListButton extends TabImageButton {
  66.         private final TextComponent text;
  67.         private final int textX;
  68.         private final int textY;
  69.         private FontRenderer fontRenderer;
  70.         private BeepediaScreen.ButtonList parent = null;
  71.  
  72.         public ListButton(int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn, int yDiffTextIn, ResourceLocation resourceLocationIn, @NotNull ItemStack displayItem, int itemX, int itemY, TextComponent text, int textX, int textY, IPressable onPressIn) {
  73.             super(xIn, yIn, widthIn, heightIn, xTexStartIn, yTexStartIn, yDiffTextIn, resourceLocationIn, displayItem, itemX, itemY, onPressIn);
  74.             this.text = text;
  75.             this.textX = textX;
  76.             this.textY = textY;
  77.             this.fontRenderer = Minecraft.getInstance().fontRenderer;
  78.         }
  79.  
  80.         public void setParent(BeepediaScreen.ButtonList parent) {
  81.             this.parent = parent;
  82.         }
  83.  
  84.         @Override
  85.         public void renderButton(@Nonnull MatrixStack matrix, int p_renderButton_1_, int p_renderButton_2_, float p_renderButton_3_) {
  86.             Minecraft minecraft = Minecraft.getInstance();
  87.             minecraft.getTextureManager().bindTexture(this.resourceLocation);
  88.             RenderSystem.disableDepthTest();
  89.             int i = this.yTexStart;
  90.             if (!this.active) {
  91.                 i += yDiffText * 2;
  92.             } else if (this.isHovered()) {
  93.                 i += this.yDiffText;
  94.             }
  95.             if (parent != null) {
  96.                 double scale = minecraft.getWindow().getGuiScaleFactor();
  97.                 GL11.glEnable(GL11.GL_SCISSOR_TEST);
  98.                 GL11.glScissor((int) (parent.xPos * scale), (int) (parent.yPos * scale), (int) (parent.width * scale), (int) (parent.height * scale));
  99.             }
  100.             drawTexture(matrix, this.x, this.y, (float) this.xTexStart, (float) i, this.width, this.height, 128, 128);
  101.             fontRenderer.draw(matrix, text, this.x + textX, this.y + textY, Color.parse("white").getRgb());
  102.  
  103.             if (this.displayItem != null)
  104.                 Minecraft.getInstance().getItemRenderer().renderItemAndEffectIntoGUI(this.displayItem, this.x + this.itemX, this.y + this.itemY);
  105.             RenderSystem.enableDepthTest();
  106.             if (parent != null) GL11.glDisable(GL11.GL_SCISSOR_TEST);
  107.         }
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment