Skip to content

Commit

Permalink
fix: move sliders and tickboxes when sccrolling
Browse files Browse the repository at this point in the history
Co-authored-by: contaria <[email protected]>
  • Loading branch information
douira committed Oct 27, 2024
1 parent 404ae3a commit 56f1147
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void rebuildGUI() {
}
}

this.rebuildGUIOptions(this.currentMod.theme());
this.rebuildGUIOptions();

this.pageList = new PageListWidget(this, new Dim2i(0, 0, 125, this.height));
this.undoButton = new FlatButtonWidget(new Dim2i(270, this.height - 30, 65, 20), Component.translatable("sodium.options.buttons.undo"), this::undoChanges, true, false);
Expand Down Expand Up @@ -231,7 +231,7 @@ private void hideDonationButton() {

private void rebuildGUIOptions() {
this.removeWidget(this.optionList);
this.optionList = this.addRenderableWidget(new OptionListWidget(new Dim2i(130, 0, 210, this.height), this.currentPage));
this.optionList = this.addRenderableWidget(new OptionListWidget(new Dim2i(130, 0, 210, this.height), this.currentPage, this.currentMod.theme()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.caffeinemc.mods.sodium.client.util.Dim2i;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.network.chat.Style;
import net.minecraft.util.Mth;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -52,7 +51,6 @@ public int getMaxWidth() {
private static class Button extends ControlElement<Integer> {
private static final int THUMB_WIDTH = 2, TRACK_HEIGHT = 1;

private final Rect2i sliderBounds;
private int contentWidth;
private final ControlValueFormatter formatter;
private final ColorTheme theme;
Expand All @@ -77,16 +75,15 @@ public Button(OptionListWidget list, Option<Integer> option, Dim2i dim, int min,
this.formatter = formatter;
this.theme = theme;

this.sliderBounds = new Rect2i(dim.getLimitX() - 96, dim.getCenterY() - 5, 90, 10);
this.sliderHeld = false;
}

@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
int sliderX = this.sliderBounds.getX();
int sliderY = this.sliderBounds.getY();
int sliderWidth = this.sliderBounds.getWidth();
int sliderHeight = this.sliderBounds.getHeight();
int sliderX = this.getSliderX();
int sliderY = this.getSliderY();
int sliderWidth = this.getSliderWidth();
int sliderHeight = this.getSliderHeight();

var value = this.option.getValidatedValue();
var isEnabled = this.option.isEnabled();
Expand Down Expand Up @@ -128,6 +125,26 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
}
}

public int getSliderX() {
return this.getLimitX() - 96;
}

public int getSliderY() {
return this.getCenterY() - 5;
}

public int getSliderWidth() {
return 90;
}

public int getSliderHeight() {
return 10;
}

public boolean isMouseOverSlider(double mouseX, double mouseY) {
return mouseX >= this.getSliderX() && mouseX < this.getSliderX() + this.getSliderWidth() && mouseY >= this.getSliderY() && mouseY < this.getSliderY() + this.getSliderHeight();
}

@Override
public int getContentWidth() {
return this.contentWidth;
Expand All @@ -150,7 +167,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
this.sliderHeld = false;

if (this.option.isEnabled() && button == 0 && this.isMouseOver(mouseX, mouseY)) {
if (this.sliderBounds.contains((int) mouseX, (int) mouseY)) {
if (this.isMouseOverSlider(mouseX, mouseY)) {
this.setValueFromMouse(mouseX);
this.sliderHeld = true;
}
Expand All @@ -162,7 +179,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
}

private void setValueFromMouse(double d) {
this.setValue((d - (double) this.sliderBounds.getX()) / (double) this.sliderBounds.getWidth());
this.setValue((d - (double) this.getSliderX()) / (double) this.getSliderWidth());
}

public void setValue(double d) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.caffeinemc.mods.sodium.client.util.Dim2i;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.navigation.CommonInputs;
import net.minecraft.client.renderer.Rect2i;

public class TickBoxControl implements Control<Boolean> {
private final Option<Boolean> option;
Expand All @@ -32,24 +31,22 @@ public Option<Boolean> getOption() {
}

private static class TickBoxControlElement extends ControlElement<Boolean> {
private final Rect2i button;
private final ColorTheme theme;

public TickBoxControlElement(OptionListWidget list, Option<Boolean> option, Dim2i dim, ColorTheme theme) {
super(list, option, dim);

this.button = new Rect2i(dim.getLimitX() - 16, dim.getCenterY() - 5, 10, 10);
this.theme = theme;
}

@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
super.render(graphics, mouseX, mouseY, delta);

final int x = this.button.getX();
final int y = this.button.getY();
final int w = x + this.button.getWidth();
final int h = y + this.button.getHeight();
final int x = this.getLimitX() - 16;
final int y = this.getCenterY() - 5;
final int w = x + 10;
final int h = y + 10;

final boolean enabled = this.option.isEnabled();
final boolean ticked = enabled && this.option.getValidatedValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.client.gui.navigation.CommonInputs;
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CenteredFlatWidget extends AbstractWidget implements Renderable {
Expand Down Expand Up @@ -44,7 +43,7 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
int textColor = this.selected || !this.isSelectable ? this.theme.themeLighter : this.theme.themeDarker;

var text = this.label.getString();
text = this.truncateTextToFit(text, this.dim.width() - 16);
text = this.truncateTextToFit(text, this.getWidth() - 16);

int x1 = this.getX();
int y1 = this.getY();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.caffeinemc.mods.sodium.client.config.structure.Option;
import net.caffeinemc.mods.sodium.client.config.structure.OptionGroup;
import net.caffeinemc.mods.sodium.client.config.structure.OptionPage;
import net.caffeinemc.mods.sodium.client.gui.ColorTheme;
import net.caffeinemc.mods.sodium.client.gui.options.control.Control;
import net.caffeinemc.mods.sodium.client.gui.options.control.ControlElement;
import net.caffeinemc.mods.sodium.client.util.Dim2i;
Expand All @@ -14,12 +15,14 @@

public class OptionListWidget extends AbstractParentWidget {
private final OptionPage page;
private final ColorTheme theme;
private final List<ControlElement<?>> controls;
private ScrollbarWidget scrollbar;

public OptionListWidget(Dim2i dim, OptionPage page) {
public OptionListWidget(Dim2i dim, OptionPage page, ColorTheme theme) {
super(dim);
this.page = page;
this.theme = theme;
this.controls = new ArrayList<>();
this.init();
}
Expand All @@ -41,7 +44,7 @@ private void init() {
// Add each option's control element
for (Option<?> option : group.options()) {
Control<?> control = option.getControl();
ControlElement<?> element = control.createElement(this, new Dim2i(x, y + listHeight, width - 10, entryHeight));
ControlElement<?> element = control.createElement(this, new Dim2i(x, y + listHeight, width - 10, entryHeight), this.theme);

this.addRenderableChild(element);
this.controls.add(element);
Expand All @@ -61,7 +64,6 @@ private void init() {

@Override
public void render(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float delta) {
graphics.fillGradient(this.getX(), this.getY(), this.getWidth(), this.getHeight(), 0x40000000, 0x90000000);
graphics.enableScissor(this.getX(), this.getY(), this.getLimitX(), this.getLimitY());
super.render(graphics, mouseX, mouseY, delta);
graphics.disableScissor();
Expand Down

0 comments on commit 56f1147

Please sign in to comment.