Skip to content

Commit

Permalink
WebView: add basic direction handling with d-pad
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Feb 16, 2022
1 parent 3ea804b commit 17543fd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions android/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 63 additions & 5 deletions android/app/src/main/java/org/arianne/stendhal/client/DPad.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +40,13 @@ public class DPad {
private final List<ArrowView> arrows;
private final ConstraintLayout layout;

private enum Dir {
LEFT,
RIGHT,
UP,
DOWN;
}


public static DPad get() {
if (instance == null) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
});
}
}
}

0 comments on commit 17543fd

Please sign in to comment.