Draggable box issues

So I’m making a box where the content can be dragged. The dragging works perfectly until I make sure that the image can’t be dragged off the screen. When I try to clamp the offset, it gets all screwed up. I was hoping someone could show me what I’m missing.

How I clamp the value (this seems like a valid way to solve this problem, but something is getting screwed up somewhere):


public void cap(){
		// Clamp the offset so that the image can only be dragged enough so the far edge is rendered.
		// This makes the maxXoffset be actual translations for the window.
		// The minimum offset is 0, so that the close edge is always that, the edge
		float maxXoffset = max.width - firstCrop.width;
		offsetx = Math.max(-maxXoffset, Math.min(0, offsetx));
		float maxYoffset = max.height - firstCrop.height;
		offsety = Math.max(-maxYoffset, Math.min(0, offsety));
	}

Explanation: max is the region where the image is rendered, and firstCrop is the region that can be displayed. offsetx and offsety control the rendering offset.

How I render the panel:


@Override
	public void render(){
		if(!blocked(startx, starty)){ // Just checks if the starting point is a point that this image can be dragged from
			float dx = getDX(), dy = getDY(); // Gets the amount to drag
			offsetx += dx;
			offsety += dy;
			cap();
		}
		push();
		firstCrop.glScissor(inner); // Uses a Renderable interface to render and scissor the region.
		pop();
	}
	
	public void push(){
		GL11.glScalef(scale, scale, 1);
		GL11.glTranslatef(offsetx, offsety, 0);
	}
	public void pop(){
		GL11.glTranslatef(-offsetx, -offsety, 0);
		GL11.glScalef(1 / scale, 1 / scale, 1);
	}

I thought I had found my solution, until I ran the code, and found that the panel can be dragged off the screen to the top and left… However, the clamp of 0 seems to work fine.

Hope someone can help,
CopyableCougar4

So I divided the maximum offsets by two, and it “seems” to fix my problem?? I suppose that for now this is solved, but I still don’t understand how cutting the offsets in half appears to fix it. Any ideas?

CopyableCougar4

As you are using opengl the screen goes from -1 to 1 , hence the difference between them is 2 so the position must be divided in order to keep it inline with the screen , well thats what ive been going on.