Skip to content

Commit

Permalink
Add 'instant' update to slider
Browse files Browse the repository at this point in the history
updates instantly instead of when user releases slider
  • Loading branch information
Wyvest committed Aug 5, 2023
1 parent e32a04f commit c2913a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@
String category() default "General";

String subcategory() default "";

/**
* Whether the slider should be updated instantly when the user drags it.
* @return Whether the slider should be updated instantly when the user drags it.
*/
boolean instant() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ConfigSlider extends BasicOption implements IFocusable {
private final NumberInputField inputField;
private final float min, max;
private final int step;
private final boolean instant;
private boolean isFloat = true;
private boolean dragging = false;
private boolean mouseWasDown = false;
Expand All @@ -68,11 +69,12 @@ public class ConfigSlider extends BasicOption implements IFocusable {
private boolean animReset;
private float lastX = -1;

public ConfigSlider(Field field, Object parent, String name, String description, String category, String subcategory, float min, float max, int step) {
public ConfigSlider(Field field, Object parent, String name, String description, String category, String subcategory, float min, float max, int step, boolean instant) {
super(field, parent, name, description, category, subcategory, 2);
this.min = min;
this.max = max;
this.step = step;
this.instant = instant;
this.inputField = new NumberInputField(84, 32, 0, min, max, step == 0 ? 1 : step);
this.stepsAnimation = new DummyAnimation(0);
this.targetAnimation = new DummyAnimation(0);
Expand All @@ -81,7 +83,7 @@ public ConfigSlider(Field field, Object parent, String name, String description,

public static ConfigSlider create(Field field, Object parent) {
Slider slider = field.getAnnotation(Slider.class);
return new ConfigSlider(field, parent, slider.name(), slider.description(), slider.category(), slider.subcategory(), slider.min(), slider.max(), slider.step());
return new ConfigSlider(field, parent, slider.name(), slider.description(), slider.category(), slider.subcategory(), slider.min(), slider.max(), slider.step(), slider.instant());
}

@Override
Expand Down Expand Up @@ -113,6 +115,8 @@ public void draw(long vg, int x, int y, InputHandler inputHandler) {
value = MathUtils.map(xCoordinate, x + 352, x + 864, min, max);
}
setValue(value);
} else if (dragging && instant) {
setValue(value);
}

float stepPercent = stepsAnimation.get();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Object setValue(Object value) {
for (Field f : fieldArrayList) fields.put(f.getName(), f);
options.add(new ConfigHeader(field, hud, hudAnnotation.name(), category, subcategory, 2));
options.add(new ConfigSwitch(fields.get("enabled"), hud, "Enabled", "If the HUD is enabled", category, subcategory, 2));
options.add(new ConfigSlider(fields.get("scale"), hud, "Scale", "The scale of the HUD", category, subcategory, 0.3f, 10f, 0));
options.add(new ConfigSlider(fields.get("scale"), hud, "Scale", "The scale of the HUD", category, subcategory, 0.3f, 10f, 0, false));
options.addAll(ConfigUtils.getClassOptions(hud));
if (hud instanceof BasicHud) {
options.add(new ConfigCheckbox(fields.get("background"), hud, "Background", "If the background of the HUD is enabled.", category, subcategory, 1));
Expand All @@ -81,9 +81,9 @@ public Object setValue(Object value) {
options.get(options.size() - 1).addDependency("Background", () -> ((BasicHud) hud).background);
options.add(new ConfigColorElement(fields.get("borderColor"), hud, "Border color:", "The color of the border.", category, subcategory, 1, true));
options.get(options.size() - 1).addDependency("Border", () -> ((BasicHud) hud).border);
options.add(new ConfigSlider(fields.get("cornerRadius"), hud, "Corner radius:", "The corner radius of the background.", category, subcategory, 0, 10, 0));
options.add(new ConfigSlider(fields.get("cornerRadius"), hud, "Corner radius:", "The corner radius of the background.", category, subcategory, 0, 10, 0, false));
options.get(options.size() - 1).addDependency("Rounded", () -> ((BasicHud) hud).rounded);
options.add(new ConfigSlider(fields.get("borderSize"), hud, "Border thickness:", "The thickness of the outline.", category, subcategory, 0, 10, 0));
options.add(new ConfigSlider(fields.get("borderSize"), hud, "Border thickness:", "The thickness of the outline.", category, subcategory, 0, 10, 0, false));
options.get(options.size() - 1).addDependency("Border", () -> ((BasicHud) hud).border);
Field paddingX = fields.get("paddingX");
Field paddingY = fields.get("paddingY");
Expand All @@ -101,8 +101,8 @@ public Object setValue(Object value) {
} catch (Exception e) {
e.printStackTrace();
}
options.add(new ConfigSlider(paddingX, hud, "X-Padding", "The horizontal padding of the HUD.", category, subcategory, 0, 10, 0));
options.add(new ConfigSlider(paddingY, hud, "Y-Padding", "The vertical padding of the HUD.", category, subcategory, 0, 10, 0));
options.add(new ConfigSlider(paddingX, hud, "X-Padding", "The horizontal padding of the HUD.", category, subcategory, 0, 10, 0, false));
options.add(new ConfigSlider(paddingY, hud, "Y-Padding", "The vertical padding of the HUD.", category, subcategory, 0, 10, 0, false));
options.get(options.size() - 2).addDependency("Background or Border", () -> ((BasicHud) hud).background || ((BasicHud) hud).border);
options.get(options.size() - 1).addDependency("Background or Border", () -> ((BasicHud) hud).background || ((BasicHud) hud).border);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ private void generateOptionsList(OptionPage page) {
options.add(new ConfigDropdown(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), 2, attributes.getOptions().toArray(new String[0])));
break;
case PERCENT_SLIDER:
options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), 0, 1, 0));
options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), 0, 1, 0, false));
break;
case DECIMAL_SLIDER:
options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), attributes.getMinF(), attributes.getMaxF(), 0));
options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), attributes.getMinF(), attributes.getMaxF(), 0, false));
break;
case NUMBER:
options.add(new ConfigNumber(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), attributes.getMin(), attributes.getMax(), attributes.getIncrement(), 2));
break;
case SLIDER:
options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), attributes.getMin(), attributes.getMax(), 0));
options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), attributes.getMin(), attributes.getMax(), 0, false));
break;
case COLOR:
options.add(new CompatConfigColorElement(getFieldOfProperty(option), option.getInstance(), getName(attributes), attributes.getDescription(), getCategory(attributes), getSubcategory(attributes), 2, attributes.getAllowAlpha()));
Expand Down

0 comments on commit c2913a6

Please sign in to comment.