From 56f687bdc647c25a938e46bcc1b6a32e30564fc3 Mon Sep 17 00:00:00 2001 From: douira Date: Tue, 19 Nov 2024 04:26:55 +0100 Subject: [PATCH] misc cleanup and refactor scrollbar widget to extend abstract widget --- .../client/gui/prompt/ScreenPrompt.java | 1 - .../gui/widgets/AbstractParentWidget.java | 5 --- .../client/gui/widgets/FlatButtonWidget.java | 21 ++++++---- .../client/gui/widgets/OptionListWidget.java | 11 +++-- .../client/gui/widgets/ScrollbarWidget.java | 42 +++++++++---------- 5 files changed, 39 insertions(+), 41 deletions(-) diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/prompt/ScreenPrompt.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/prompt/ScreenPrompt.java index fe0101ae2f..43cd1a70f5 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/prompt/ScreenPrompt.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/prompt/ScreenPrompt.java @@ -75,7 +75,6 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { int textY = boxY + padding; int textMaxWidth = this.width - (padding * 2); - int textMaxHeight = this.height - (padding * 2); var font = Minecraft.getInstance().font; diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/AbstractParentWidget.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/AbstractParentWidget.java index ebcaea07ff..7adfa41078 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/AbstractParentWidget.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/AbstractParentWidget.java @@ -45,11 +45,6 @@ protected void clearChildren() { this.renderableChildren.clear(); } - protected void verticalScrollScissorGradient(GuiGraphics graphics, int y) { - var gradientHeight = 10; - graphics.fillGradient(this.getX(), y - gradientHeight, this.getLimitX(), y, 0xFFFF0000, 0xFF00FF00); - } - @Override public void render(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float delta) { for (Renderable element : this.renderableChildren) { diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/FlatButtonWidget.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/FlatButtonWidget.java index 7d81d31d53..825aea4da5 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/FlatButtonWidget.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/FlatButtonWidget.java @@ -27,7 +27,6 @@ public class FlatButtonWidget extends AbstractWidget implements Renderable { private boolean enabled = true; private boolean visible = true; - public FlatButtonWidget(Dim2i dim, Component label, Runnable action, boolean drawBackground, boolean leftAlign, ButtonTheme theme) { super(dim); this.label = label; @@ -50,26 +49,30 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { this.hovered = this.isMouseOver(mouseX, mouseY); int backgroundColor = this.enabled ? (this.hovered ? this.theme.bgHighlight : this.theme.bgDefault) : this.theme.bgInactive; - int textColor = this.enabled ? this.theme.themeLighter : this.theme.themeDarker; - - int strWidth = this.font.width(this.label); + int textColor = this.getTextColor(); if (this.drawBackground) { this.drawRect(graphics, this.getX(), this.getY(), this.getLimitX(), this.getLimitY(), backgroundColor); } - this.drawString(graphics, this.label, this.leftAlign ? this.getX() + Layout.TEXT_LEFT_PADDING : (this.getCenterX() - (strWidth / 2)), this.getCenterY() - 4, textColor); + if (this.label != null) { + int strWidth = this.font.width(this.label); + this.drawString(graphics, this.label, this.leftAlign ? this.getX() + Layout.TEXT_LEFT_PADDING : (this.getCenterX() - (strWidth / 2)), this.getCenterY() - 4, textColor); + } if (this.enabled && this.selected) { this.drawRect(graphics, this.getX(), this.getLimitY() - 1, this.getLimitX(), this.getLimitY(), Colors.THEME); } if (!this.drawBackground) { - this.drawBorder(graphics, this.getX(), this.getY(), this.getLimitX(), this.getLimitY(), 0x8000FFEE); - + this.drawBorder(graphics, this.getX(), this.getY(), this.getLimitX(), this.getLimitY(), Colors.BUTTON_BORDER); } } + protected int getTextColor() { + return this.enabled ? this.theme.themeLighter : this.theme.themeDarker; + } + public void setSelected(boolean selected) { this.selected = selected; } @@ -121,4 +124,8 @@ public void setVisible(boolean visible) { return null; return super.nextFocusPath(event); } + + public boolean isVisible() { + return this.visible; + } } diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/OptionListWidget.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/OptionListWidget.java index 6e464df185..de903ef893 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/OptionListWidget.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/OptionListWidget.java @@ -15,6 +15,8 @@ import java.util.List; public class OptionListWidget extends AbstractParentWidget { + private static final int SCROLLBAR_OFFSET = 5; + private final OptionPage page; private final ColorTheme theme; private final List controls; @@ -37,7 +39,7 @@ private void rebuild(Screen screen) { int maxWidth = 0; this.clearChildren(); - this.scrollbar = this.addRenderableChild(new ScrollbarWidget(new Dim2i(x + width - Layout.SCROLLBAR_WIDTH, y, Layout.SCROLLBAR_WIDTH, height))); + this.scrollbar = this.addRenderableChild(new ScrollbarWidget(new Dim2i(x + width + SCROLLBAR_OFFSET, y, Layout.SCROLLBAR_WIDTH, height))); int entryHeight = 18; int listHeight = 0; @@ -45,7 +47,7 @@ private void rebuild(Screen screen) { // Add each option's control element for (Option option : group.options()) { var control = option.getControl(); - var element = control.createElement(screen,this, new Dim2i(x, y + listHeight, width - 10, entryHeight), this.theme); + var element = control.createElement(screen,this, new Dim2i(x, y + listHeight, width, entryHeight), this.theme); this.addRenderableChild(element); this.controls.add(element); @@ -65,11 +67,8 @@ private void rebuild(Screen screen) { @Override public void render(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float delta) { - graphics.enableScissor(this.getX(), this.getY(), this.getLimitX(), this.getLimitY()); + graphics.enableScissor(this.getX(), this.getY(), this.getLimitX() + SCROLLBAR_OFFSET + Layout.SCROLLBAR_WIDTH, this.getLimitY()); super.render(graphics, mouseX, mouseY, delta); - -// this.verticalScrollScissorGradient(graphics, this.getLimitY()); - graphics.disableScissor(); } diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/ScrollbarWidget.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/ScrollbarWidget.java index 84f53d8d3c..38524c42b9 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/ScrollbarWidget.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/ScrollbarWidget.java @@ -3,13 +3,11 @@ import net.caffeinemc.mods.sodium.api.util.ColorABGR; import net.caffeinemc.mods.sodium.client.util.Dim2i; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.gui.components.AbstractWidget; import net.minecraft.client.gui.narration.NarrationElementOutput; -import net.minecraft.network.chat.Component; import org.jetbrains.annotations.NotNull; -// TODO: make this extend sodium's AbstractWidget -public class ScrollbarWidget extends /*net.minecraft.client.gui.components.*/AbstractWidget { +// TODO: override some methods to make it not focusable? +public class ScrollbarWidget extends AbstractWidget { private static final int COLOR = ColorABGR.pack(50, 50, 50, 150); private static final int HIGHLIGHT_COLOR = ColorABGR.pack(100, 100, 100, 150); @@ -27,7 +25,7 @@ public ScrollbarWidget(Dim2i dim2i) { } public ScrollbarWidget(Dim2i dim2i, boolean horizontal) { - super(dim2i.x(), dim2i.y(), dim2i.width(), dim2i.height(), Component.empty()); + super(dim2i); this.horizontal = horizontal; } @@ -38,7 +36,7 @@ public void setScrollbarContext(int visible, int total) { } public void setScrollbarContext(int total) { - this.setScrollbarContext(this.horizontal ? this.width : this.height, total); + this.setScrollbarContext(this.horizontal ? this.getWidth() : this.getHeight(), total); } public boolean canScroll() { @@ -55,7 +53,7 @@ public int getScrollAmount() { } @Override - protected void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float delta) { + public void render(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float delta) { if (!this.canScroll()) { return; } @@ -69,15 +67,15 @@ protected void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouse graphics.fill(this.getX(), this.getY(), this.getX() + this.getWidth(), this.getY() + this.getHeight(), COLOR); int x1, y1, x2, y2; if (this.horizontal) { - x1 = this.getX() + this.getHighlightStart(this.width); + x1 = this.getX() + this.getHighlightStart(this.getWidth()); y1 = this.getY(); - x2 = x1 + this.getHighlightLength(this.width); - y2 = y1 + this.height; + x2 = x1 + this.getHighlightLength(this.getWidth()); + y2 = y1 + this.getHeight(); } else { x1 = this.getX(); - y1 = this.getY() + this.getHighlightStart(this.height); - x2 = x1 + this.width; - y2 = y1 + this.getHighlightLength(this.height); + y1 = this.getY() + this.getHighlightStart(this.getHeight()); + x2 = x1 + this.getWidth(); + y2 = y1 + this.getHighlightLength(this.getHeight()); } graphics.fill(x1, y1, x2, y2, HIGHLIGHT_COLOR); } @@ -86,15 +84,15 @@ protected void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouse private boolean isMouseOverHighlight(double mouseX, double mouseY) { int x1, y1, x2, y2; if (this.horizontal) { - x1 = this.getX() + this.getHighlightStart(this.width); + x1 = this.getX() + this.getHighlightStart(this.getWidth()); y1 = this.getY(); - x2 = x1 + this.getHighlightLength(this.width); - y2 = y1 + this.height; + x2 = x1 + this.getHighlightLength(this.getWidth()); + y2 = y1 + this.getHeight(); } else { x1 = this.getX(); - y1 = this.getY() + this.getHighlightStart(this.height); - x2 = x1 + this.width; - y2 = y1 + this.getHighlightLength(this.height); + y1 = this.getY() + this.getHighlightStart(this.getHeight()); + x2 = x1 + this.getWidth(); + y2 = y1 + this.getHighlightLength(this.getHeight()); } return mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2; } @@ -116,9 +114,9 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { this.dragging = true; } else { if (this.horizontal) { - this.scroll(mouseX > this.getHighlightStart(this.width) ? this.width : -this.width); + this.scroll(mouseX > this.getHighlightStart(this.getWidth()) ? this.getWidth() : -this.getWidth()); } else { - this.scroll(mouseY > this.getHighlightStart(this.height) ? this.height : -this.height); + this.scroll(mouseY > this.getHighlightStart(this.getHeight()) ? this.getHeight() : -this.getHeight()); } } return true; @@ -141,6 +139,6 @@ public boolean mouseDragged(double mouseX, double mouseY, int button, double del } @Override - protected void updateWidgetNarration(@NotNull NarrationElementOutput output) { + public void updateNarration(NarrationElementOutput builder) { } }