Skip to content

Commit

Permalink
feat(dynamic ui): added slider
Browse files Browse the repository at this point in the history
  • Loading branch information
PepperLola committed Jul 4, 2023
1 parent 8d888a1 commit fba1d0a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public void handleMouseInput() throws IOException {
super.handleMouseInput();
}

/**
* On mouse clicked
* @param mouseX mouse X in screen coords; 0 is left side of screen
* @param mouseY mouse Y in screen coords; 0 is top side of screen
* @param btn mouse button pressed
* @throws IOException
*/
@Override
protected void mouseClicked(int mouseX, int mouseY, int btn) throws IOException {
super.mouseClicked(mouseX, mouseY, btn);
Expand Down Expand Up @@ -154,4 +161,10 @@ public GuiCheckbox createCheckbox(String label, int x, int y, int width, int hei
addComponent(checkbox);
return checkbox;
}

public GuiSlider createSlider(int x, int y, int width, int height, int min, int max, int step, String prefix, String suffix) {
GuiSlider slider = new GuiSlider(this, x + topLeft.x, y + topLeft.y, width, height, min, max, step, prefix, suffix);
addComponent(slider);
return slider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class GuiCheckbox extends DynamicGuiComponent {
private boolean enabled;
private boolean disabled; // whether or not the user is allowed to toggle
private boolean disabled; // whether the user is allowed to toggle
private GuiLabel label;

public GuiCheckbox(DynamicGuiScreen screen, Vector2<Integer> position, Vector2<Integer> size) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.palight.playerinfo.gui.dynamic.components;

import com.palight.playerinfo.gui.dynamic.DefaultUIConfig;
import com.palight.playerinfo.gui.dynamic.DynamicGuiScreen;
import com.palight.playerinfo.rendering.font.UnicodeFontRenderer;
import com.palight.playerinfo.util.math.Vector2;

public class GuiSlider extends DynamicGuiComponent {
private GuiLabel label;
private int min;
private int max;
private int step;
private String prefix;
private String suffix;
private double value = 0;

public GuiSlider(DynamicGuiScreen screen, Vector2<Integer> position, Vector2<Integer> size, int min, int max, int step, String prefix, String suffix) {
super(screen, position, size);
this.min = min;
this.max = max;
this.step = step;
this.prefix = prefix;
this.suffix = suffix;

this.label = new GuiLabel(screen, position.x + size.x / 2, position.y + size.y / 2, 0, size.y)
.setBaseline(UnicodeFontRenderer.Baseline.MIDDLE)
.setAlignment(UnicodeFontRenderer.Alignment.CENTER);
}

public GuiSlider(DynamicGuiScreen screen, int x, int y, int width, int height, int min, int max, int step, String prefix, String suffix) {
this(screen, new Vector2<>(x, y), new Vector2<>(width, height), min, max, step, prefix, suffix);
}

public GuiSlider(DynamicGuiScreen screen, int x, int y, int min, int max, int step, String prefix, String suffix) {
this(screen, new Vector2<>(x, y), new Vector2<>(0, 0), min, max, step, prefix, suffix);
}

public GuiSlider(DynamicGuiScreen screen, int min, int max, int step, String prefix, String suffix) {
this(screen, new Vector2<>(0, 0), new Vector2<>(0, 0), min, max, step, prefix, suffix);
}

public GuiSlider(DynamicGuiScreen screen, int x, int y, int width, int height, int min, int max, int step) {
this(screen, new Vector2<>(x, y), new Vector2<>(width, height), min, max, step, "", "");
}

public GuiSlider(DynamicGuiScreen screen, int x, int y, int min, int max, int step) {
this(screen, new Vector2<>(x, y), new Vector2<>(0, 0), min, max, step, "", "");
}

public GuiSlider(DynamicGuiScreen screen, int min, int max, int step) {
this(screen, new Vector2<>(0, 0), new Vector2<>(0, 0), min, max, step, "", "");
}

@Override
public void onClick(int mouseX, int mouseY, int mouseButton) {
double percent = (double) mouseX / this.size.x;
this.value = min + (max - min) * percent;
this.label.setText(prefix + Math.floor(value * 100) / 100 + suffix);
super.onClick(mouseX, mouseY, mouseButton);
}

@Override
public <T extends DynamicGuiComponent> T setPosition(Vector2<Integer> position) {
super.setPosition(position);
this.label.setPosition(this.position.x + this.size.x / 2, this.position.y + this.size.y / 2);
return (T) this;
}

@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.renderBackground();
this.label.render(mouseX, mouseY, partialTicks);

double percent = (value - min) / (max - min);
int barOffset = (int) (percent * this.size.x);
this.drawGradientRect(this.position.x + barOffset - 2, this.position.y, this.position.x + barOffset + 2, this.position.y + this.size.y, DefaultUIConfig.DEFAULT_LABEL_COLOR, DefaultUIConfig.DEFAULT_LABEL_COLOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public void setup() {
}));

stack.addComponent(this.createCheckbox("Test Checkbox", 0, 0, 20, 20, false));
stack.addComponent(this.createSlider(30, 0, 100, 20, 0, 100, 1, "PREFIX: ", " :SUFFIX"));
}
}

0 comments on commit fba1d0a

Please sign in to comment.