Skip to content

Commit

Permalink
misc cleanup and refactor scrollbar widget to extend abstract widget
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Nov 19, 2024
1 parent 8a9b25a commit 56f687b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -121,4 +124,8 @@ public void setVisible(boolean visible) {
return null;
return super.nextFocusPath(event);
}

public boolean isVisible() {
return this.visible;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ControlElement> controls;
Expand All @@ -37,15 +39,15 @@ 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;
for (OptionGroup group : this.page.groups()) {
// 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);
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}

Expand All @@ -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() {
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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) {
}
}

0 comments on commit 56f687b

Please sign in to comment.