Skip to content

Commit

Permalink
Fix landscape coordinates (#68)
Browse files Browse the repository at this point in the history
* fix landscape coordinates calculation

The coordinates calculation was buggy, however it provided correct
results for portrait and anti-clockwise landscape orientation.

Signed-off-by: Pierre Crépieux <[email protected]>

* update version code and name for release

Signed-off-by: Pierre Crépieux <[email protected]>
  • Loading branch information
pcrepieux authored Mar 25, 2022
1 parent bec5898 commit 03e0f40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 10
versionName "2.4.8"
versionCode 11
versionName "2.4.9"
applicationId "jp.co.cyberagent.stf"
}

Expand Down
36 changes: 26 additions & 10 deletions app/src/main/java/jp/co/cyberagent/stf/MinitouchAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ private void injectEvent(InputEvent event) {
handler.post(() -> inputManager.injectInputEvent(event));
}

private float[] calculateCoordsForScreen(PointerEvent p){
float[] result = new float[2];
int x_translate=0, y_translate=0;
int rotation = windowManager.getRotation();
if (rotation == 1){
y_translate = width;
} else if (rotation == 3){
x_translate = height;
}
double rad = Math.toRadians(rotation*-90);
result[0] = (float)(p.lastX * Math.cos(rad) - p.lastY * Math.sin(rad))+x_translate;
result[1] = (float)(p.lastX * Math.sin(rad) + p.lastY * Math.cos(rad))+y_translate;
return result;
}

private MotionEvent getMotionEvent(PointerEvent p) {
return getMotionEvent(p,0);
}
Expand All @@ -132,10 +147,10 @@ private MotionEvent getMotionEvent(PointerEvent p, int idx) {
p.lastMouseDown = now;
}
MotionEvent.PointerCoords coords = pointerCoords[idx];
int rotation = windowManager.getRotation();
double rad = Math.toRadians(rotation * 90.0);
coords.x = (float)(p.lastX * Math.cos(-rad) - p.lastY * Math.sin(-rad));
coords.y = (rotation * width)+(float)(p.lastX * Math.sin(-rad) + p.lastY * Math.cos(-rad));

float[] screenCoords = calculateCoordsForScreen(p);
coords.x = screenCoords[0];
coords.y = screenCoords[1];
return MotionEvent.obtain(p.lastMouseDown, now, p.action, idx+1, pointerProperties,
pointerCoords, 0, 0, 1f, 1f, 0, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0);
Expand All @@ -150,14 +165,15 @@ private List<MotionEvent> getMotionEvent(PointerEvent p1, PointerEvent p2) {
} else {
MotionEvent.PointerCoords coords1 = pointerCoords[0];
MotionEvent.PointerCoords coords2 = pointerCoords[1];
int rotation = windowManager.getRotation();
double rad = Math.toRadians(rotation * 90.0);

coords1.x = (float) (p1.lastX * Math.cos(-rad) - p1.lastY * Math.sin(-rad));
coords1.y = (rotation * width) + (float) (p1.lastX * Math.sin(-rad) + p1.lastY * Math.cos(-rad));
float[] screenCoords1 = calculateCoordsForScreen(p1);
float[] screenCoords2 = calculateCoordsForScreen(p2);

coords1.x = screenCoords1[0];
coords1.y = screenCoords1[1];;

coords2.x = (float) (p2.lastX * Math.cos(-rad) - p2.lastY * Math.sin(-rad));
coords2.y = (rotation * width) + (float) (p2.lastX * Math.sin(-rad) + p2.lastY * Math.cos(-rad));
coords2.x = screenCoords2[0];
coords2.y = screenCoords2[1];

MotionEvent event = MotionEvent.obtain(p1.lastMouseDown, now, p1.action, 2, pointerProperties,
pointerCoords, 0, 0, 1f, 1f, 0, 0,
Expand Down

0 comments on commit 03e0f40

Please sign in to comment.