Handling dragging

Alas, with a new input system comes a need for drag handling. I have 3 abstract methods that implementations use to handle parsed dragging.

My problem is getting the drag from raw mouse input.

How I calculate drag delta. startx and starty are the last known coordinates of the mouse

public int getDX(int x, int y) {
		Point mouse = new Point(x, y);
		int dx = mouse.x - startx;
		if (!Mouse.isButtonDown(0)) {
			return 0;
		} else {
			if (wasDown) {
				// Continue
			} else {
				wasDown = true;
				return 0;
			}
		}
		startx = mouse.x;
		return dx;
	}
	public int getDY(int x, int y) {
		Point mouse = new Point(x, y);
		int dy = mouse.y - starty;
		if (!Mouse.isButtonDown(0)) {
			return 0;
		} else {
			if (wasDown) {
				// Continue
			} else {
				wasDown = true;
				return 0;
			}
		}
		starty = mouse.y;
		return dy;
	}

This is where mouse input is forwarded. And yes, Y values have already been adjusted to go with the coordinate orientation

public void mouse(int x, int y, boolean down) {
		int dx = getDX(x, y);
		int dy = getDY(x, y);
		if (dy > 0 || dx > 0) drag(dx, dy);
		if (wasDown) {
			if (!down) {
				endDrag();
				wasDown = false;
			}
		} else {
			if (down) {
				startDrag(x, y);
				wasDown = true;
			}
		}
	}

Any thoughts on why the drag isn’t being handled would be much appreciated :slight_smile:

CopyableCougar4

Well, I found my solution :slight_smile: I needed to update whether the mouse was down or not…

Here’s my new code if anybody is interested.

public int getDX(int x, int y, boolean down) {
		Point mouse = new Point(x, y);
		int dx = mouse.x - lastx;
		lastx = mouse.x;
		return dx;
	}
	public int getDY(int x, int y, boolean down) {
		Point mouse = new Point(x, y);
		int dy = mouse.y - lasty;
		lasty = mouse.y;
		return dy;
	}
private boolean dragging = false;
	public void mouse(int x, int y, boolean down) {
		Mouse.poll();
		boolean isDown = Mouse.isButtonDown(0); 
		if (down != isDown) { 
			System.out.println("The mouse was down at trigger, but not anymore.");
			down = isDown;
		}
		if (wasDown) {
			if (!down) {
				System.out.println("Ending drag at " + x + "," + y);
				endDrag();
				dragging = false;
			}
		} else {
			if (down) {
				System.out.println("Starting drag at " + x + "," + y);
				startDrag(x, y);
				dragging = true;
			}
		}
		int dx = getDX(x, y, down);
		int dy = getDY(x, y, down);
		if (dragging) {
			drag(dx, dy);
			System.out.println("dragging " + dx + "," + dy);
		}
		wasDown = down;
	}

CopyableCougar4