Skip to content

Commit

Permalink
Sliders now work with Mouse DOWN rather than click.
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonk9043 committed Oct 8, 2023
1 parent 2254b23 commit 25db625
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/main/java/net/aoba/gui/tabs/components/SliderComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

import net.aoba.Aoba;
import net.aoba.core.settings.types.FloatSetting;
import net.aoba.core.utils.types.Vector2;
import net.aoba.event.events.LeftMouseDownEvent;
import net.aoba.event.events.LeftMouseUpEvent;
import net.aoba.event.events.MouseMoveEvent;
import net.aoba.event.listeners.LeftMouseDownListener;
import net.aoba.event.listeners.LeftMouseUpListener;
import net.aoba.event.listeners.MouseMoveListener;
import net.aoba.gui.Color;
import net.aoba.gui.HudManager;
import net.aoba.gui.IHudElement;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;

public class SliderComponent extends Component implements LeftMouseDownListener {
public class SliderComponent extends Component implements LeftMouseDownListener, LeftMouseUpListener, MouseMoveListener {

private String text;
private float currentSliderPosition = 0.4f;
float r;
float g;
float b;

private boolean isSliding = false;


FloatSetting slider;

public SliderComponent(String text, IHudElement parent) {
Expand All @@ -28,8 +36,9 @@ public SliderComponent(String text, IHudElement parent) {
this.setHeight(24);
this.setLeft(4);
this.setRight(4);

Aoba.getInstance().eventManager.AddListener(LeftMouseDownListener.class, this);
Aoba.getInstance().eventManager.AddListener(LeftMouseUpListener.class, this);
}

public SliderComponent(IHudElement parent, FloatSetting slider) {
Expand All @@ -39,6 +48,7 @@ public SliderComponent(IHudElement parent, FloatSetting slider) {
this.currentSliderPosition = (float) ((slider.getValue() - slider.min_value)
/ (slider.max_value - slider.min_value));
Aoba.getInstance().eventManager.AddListener(LeftMouseDownListener.class, this);
Aoba.getInstance().eventManager.AddListener(LeftMouseUpListener.class, this);
}

public float getSliderPosition() {
Expand All @@ -65,16 +75,29 @@ public void setColor(float r, float g, float b) {

@Override
public void OnLeftMouseDown(LeftMouseDownEvent event) {
System.out.println("click");
double mouseX = event.GetMouseX();
if (hovered) {
isSliding = true;
}
}

@Override
public void OnLeftMouseUp(LeftMouseUpEvent event) {
isSliding = false;
}

@Override
public void OnMouseMove(MouseMoveEvent event) {
super.OnMouseMove(event);

double mouseX = event.GetHorizontal();
if (Aoba.getInstance().hudManager.isClickGuiOpen() && this.isSliding) {
this.currentSliderPosition = (float) Math.min((((mouseX - ((actualX + 4))) - 1) / ((actualWidth - 12))),1f);
this.currentSliderPosition = (float) Math.max(0f, this.currentSliderPosition);
System.out.println("Inside slider at position: " + currentSliderPosition);
this.slider.setValue((this.currentSliderPosition * (slider.max_value - slider.min_value)) + slider.min_value);
}
}


@Override
public void update() {

Expand All @@ -93,5 +116,4 @@ public void draw(DrawContext drawContext, float partialTicks, Color color) {
renderUtils.drawString(drawContext, this.text + ": " + String.format("%.02f", this.slider.getValue()),
actualX + 10, actualY + 6, 0xFFFFFF);
}

}

0 comments on commit 25db625

Please sign in to comment.