[Solved]Custom Value Slider doesn't work

Hi

Edit: I think I fixed my issue for the most part. The code below is updated as of this edit

I was recently making a few GUI elements in LWJGL to use in my projects (I don’t use external libraries) and I got stuck on the Value Slider. I have a texture of the slider and I have a class that is supposed to handle dragging the slider, but it’s really glitchy and I was hoping someone on JGO could help me find what I’m doing wrong.

How I calculate the drag and drop: I set the state of the drag by polling the mouse and using a 2-layer if statement to test all possibilities. Then I get the delta for the mouse and I update an integer array of values. Then I have a method where I can get the delta, add it to the slider value, and clamp the slider value. However, the slider is too touchy and will just zoom to the sides.

Slider class


package warlord.opengl.gui;

import java.awt.Rectangle;

import static org.lwjgl.opengl.GL11.*;

import warlord.opengl.Texture;

public class Slider extends DragAndDrop {
	
	private float maxvalue, currentvalue;
	private Texture sliderbutton;
	private Rectangle clickbox;
	
	public Slider(float maxvalue){
		this(maxvalue, 0.0f);
	}
	public Slider(float maxvalue, float initialvalue){
		sliderbutton = Texture.loadTexture("slider.resource");
		clickbox = new Rectangle(0, 0, sliderbutton.getWidth(), sliderbutton.getHeight());
		currentvalue = initialvalue;
		this.maxvalue = maxvalue;
	}
	
	public float getPercent(){
		return currentvalue / maxvalue;
	}
	
	public void render(float x, float y){
		// check for drag and drop
		check(clickbox);
		currentvalue += getDX();
		currentvalue = clamp(currentvalue, 0.0f, maxvalue);
		// draw the actual bar
		glDisable(GL_TEXTURE_2D);
		glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_QUADS);
		glVertex2f(x, y);
		glVertex2f(x + maxvalue, y);
		glVertex2f(x + maxvalue, y + 3);
		glVertex2f(x, y + 3);
		glEnd();
		glColor3f(1.0f, 1.0f, 1.0f);
		glEnable(GL_TEXTURE_2D);
		sliderbutton.bind();
		clickbox.x = Math.round(x + currentvalue);
		clickbox.y = Math.round(y - clickbox.height / 2);
		glBegin(GL_QUADS);
		glVertex2f(clickbox.x, clickbox.y);
		glVertex2f(clickbox.x + clickbox.width, clickbox.y);
		glVertex2f(clickbox.x + clickbox.width, clickbox.y + clickbox.height);
		glVertex2f(clickbox.x, clickbox.y + clickbox.height);
		glEnd();
		glColor3f(1.0f, 1.0f, 1.0f);
	}

}

Abstract DragAndDrop class


package warlord.opengl.gui;

import java.awt.Rectangle;

import org.lwjgl.input.Mouse;

import warlord.opengl.Game;

public abstract class DragAndDrop {
	
	private boolean wasMouseDown;
	private int[] last, now;
	
	public DragAndDrop(){
		wasMouseDown = false;
		last = new int[2];
		now = new int[]{0, 0};
	}
	
	public void check(Rectangle exclusive, int startX, int endX){
		Mouse.poll();
		int x = Mouse.getX();
		int y = Game.getHeight() - Mouse.getY();
		if(!new Rectangle(startX - 5, exclusive.y - 5, endX - startX + 10, exclusive.height + 10).intersects(Game.getMouse(true))){
			wasMouseDown = false;
		}
		boolean down = Mouse.isButtonDown(0);
		if(wasMouseDown){
			if(down){
				// continue the drag and drop
				setDX(x, y);
				return;
			} else {
				// Drag and drop is over
				wasMouseDown = false;
			}
		} else {
			if(down){
				// start drag and drop by simply setting the wasMouseDown
				wasMouseDown = true;
			} else {
				// the mouse isn't down and it wasn't down
			}
		}
		clearDX(x, y);
	}
	
	protected float clamp(float value, float min, float max){
		return Math.max(min, Math.min(max, value));
	}
	
	private void setDX(int x, int y){
		last[0] = now[0];
		last[1] = now[1];
		now[0] = x;
		now[1] = y;
	}
	private void clearDX(int x, int y){
		last[0] = x;
		last[1] = y;
		now[0] = x;
		now[1] = y;
	}
	
	// Get the delta values
	
	public int[] getDelta(){
		return last;
	}
	public int getDX(){
		return now[0] - last[0];
	}
	public int getDY(){
		return now[1] - last[1];
	}

}

Please don’t try to change how I poll the mouse unless that is causing the issue. I prefer to just get the current state of the mouse because I handle the mouse in dozens of classes.

Hope someone can help me :slight_smile:

CopyableCougar4

So I got the drag working better, it’s just a little slow sometimes and sometimes not responsive. Also, if you move up after dragging for a while the slider keeps moving.

CopyableCougar4