From 17543fd7c8bffaa19c75d7144f95e8a71891a47c Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Wed, 16 Feb 2022 09:02:51 -0800 Subject: [PATCH] WebView: add basic direction handling with d-pad --- android/TODO.txt | 2 + .../org/arianne/stendhal/client/DPad.java | 68 +++++++++++++++++-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/android/TODO.txt b/android/TODO.txt index 3a132021f40..2ce1a2a5ec6 100644 --- a/android/TODO.txt +++ b/android/TODO.txt @@ -8,6 +8,8 @@ TODO: - take screenshot - set theme - add option to have joystick style d-pad instead of arrows +- spread out d-pad arrows & make bigger +- add setting to customize d-pad position BUILD TODO: - script to download Android SDK diff --git a/android/app/src/main/java/org/arianne/stendhal/client/DPad.java b/android/app/src/main/java/org/arianne/stendhal/client/DPad.java index 06686e2eeca..f97896e0491 100644 --- a/android/app/src/main/java/org/arianne/stendhal/client/DPad.java +++ b/android/app/src/main/java/org/arianne/stendhal/client/DPad.java @@ -14,6 +14,8 @@ import android.content.Context; import android.graphics.Point; import android.view.Display; +import android.view.KeyEvent; +import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import androidx.constraintlayout.widget.ConstraintLayout; @@ -38,6 +40,13 @@ public class DPad { private final List arrows; private final ConstraintLayout layout; + private enum Dir { + LEFT, + RIGHT, + UP, + DOWN; + } + public static DPad get() { if (instance == null) { @@ -54,10 +63,10 @@ private DPad() { layout = new ConstraintLayout(ctx); layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); - final ArrowView arrow_l = new ArrowView(ctx, R.drawable.dpad_arrow_left); - final ArrowView arrow_r = new ArrowView(ctx, R.drawable.dpad_arrow_right); - final ArrowView arrow_u = new ArrowView(ctx, R.drawable.dpad_arrow_up); - final ArrowView arrow_d = new ArrowView(ctx, R.drawable.dpad_arrow_down); + final ArrowView arrow_l = new ArrowView(ctx, R.drawable.dpad_arrow_left, Dir.LEFT); + final ArrowView arrow_r = new ArrowView(ctx, R.drawable.dpad_arrow_right, Dir.RIGHT); + final ArrowView arrow_u = new ArrowView(ctx, R.drawable.dpad_arrow_up, Dir.UP); + final ArrowView arrow_d = new ArrowView(ctx, R.drawable.dpad_arrow_down, Dir.DOWN); arrows.add(arrow_l); arrows.add(arrow_r); @@ -116,11 +125,60 @@ public void onRefreshView() { private static class ArrowView extends ImageView { - public ArrowView(final Context ctx, final int id) { + + private final Dir dir; + + private boolean keyDown = false; + + + public ArrowView(final Context ctx, final int id, final Dir dir) { super(ctx); + this.dir = dir; + setBackgroundColor(android.graphics.Color.TRANSPARENT); setImageResource(id); + + setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(final View view, final MotionEvent event) { + final int action = event.getAction(); + + Integer newAction = null; + Integer keyCode = null; + + if (action == MotionEvent.ACTION_DOWN && !keyDown) { + newAction = KeyEvent.ACTION_DOWN; + keyDown = true; + } else if (action == MotionEvent.ACTION_UP && keyDown) { + newAction = KeyEvent.ACTION_UP; + keyDown = false; + } + + if (newAction != null) { + + switch (dir) { + case LEFT: + keyCode = KeyEvent.KEYCODE_DPAD_LEFT; + break; + case RIGHT: + keyCode = KeyEvent.KEYCODE_DPAD_RIGHT; + break; + case UP: + keyCode = KeyEvent.KEYCODE_DPAD_UP; + break; + case DOWN: + keyCode = KeyEvent.KEYCODE_DPAD_DOWN; + break; + } + + MainActivity.get().dispatchKeyEvent(new KeyEvent(newAction, keyCode)); + return true; // consume event + } + + return false; + } + }); } } }