Skip to content

Commit

Permalink
Fix button toggling after being dragged
Browse files Browse the repository at this point in the history
  • Loading branch information
Mars committed Dec 8, 2022
1 parent 6854456 commit 6851f33
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/me/mars/DragTable.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package me.mars;

import arc.Core;
import arc.func.Floatc2;
import arc.input.KeyCode;
import arc.math.Mathf;
import arc.scene.Element;
import arc.scene.event.ChangeListener;
import arc.scene.event.InputEvent;
import arc.scene.event.InputListener;
import arc.scene.ui.layout.Table;

public class DragTable extends Table {
float xDelta, yDelta;

public DragTable() {
super();

addListener(new InputListener() {
float lastX, lastY;

@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
if(Core.app.isMobile() && pointer != 0) return;
DragTable.this.xDelta += x - this.lastX;
DragTable.this.yDelta += y - this.lastY;
x = Mathf.clamp(DragTable.this.x - this.lastX + x, 0, Core.graphics.getWidth()-DragTable.this.width);
y = Mathf.clamp(DragTable.this.y - this.lastY + y, 0, Core.graphics.getHeight()-DragTable.this.height);
DragTable.this.setPosition(x, y);
Expand All @@ -27,10 +33,17 @@ public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCod
if(Core.app.isMobile() && pointer != 0) return false;
this.lastX = x;
this.lastY = y;
DragTable.this.xDelta = 0;
DragTable.this.yDelta = 0;
return true;
}
});


addCaptureListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Element actor) {
if ((Math.abs(xDelta) > DragTable.this.width || Math.abs(yDelta) > DragTable.this.height)) event.stop();
}
});
}
}

0 comments on commit 6851f33

Please sign in to comment.