From 4b8514ddd1495a072a9cf6524afe0e6f5f3d0791 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Wed, 16 Feb 2022 07:56:14 -0800 Subject: [PATCH] WebView: add mechanism to show a D-pad --- .../org/arianne/stendhal/client/DPad.java | 120 ++++++++++++++++++ .../arianne/stendhal/client/MainActivity.java | 19 +++ .../stendhal/client/PreferencesActivity.java | 55 ++++++-- .../src/main/res/drawable/dpad_arrow_down.png | Bin 0 -> 267 bytes .../src/main/res/drawable/dpad_arrow_left.png | Bin 0 -> 274 bytes .../main/res/drawable/dpad_arrow_right.png | Bin 0 -> 275 bytes .../src/main/res/drawable/dpad_arrow_up.png | Bin 0 -> 217 bytes 7 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 android/app/src/main/java/org/arianne/stendhal/client/DPad.java create mode 100644 android/app/src/main/res/drawable/dpad_arrow_down.png create mode 100644 android/app/src/main/res/drawable/dpad_arrow_left.png create mode 100644 android/app/src/main/res/drawable/dpad_arrow_right.png create mode 100644 android/app/src/main/res/drawable/dpad_arrow_up.png 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 new file mode 100644 index 00000000000..b44a5457345 --- /dev/null +++ b/android/app/src/main/java/org/arianne/stendhal/client/DPad.java @@ -0,0 +1,120 @@ +/*************************************************************************** + * Copyright © 2022 - Arianne * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package org.arianne.stendhal.client; + +import android.content.Context; +import android.graphics.Point; +import android.view.Display; +import android.view.View; +import android.widget.ImageView; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams; + +import java.util.LinkedList; +import java.util.List; + + +public class DPad { + + private static DPad instance; + + private static Context ctx; + + private final List arrows; + private final ConstraintLayout layout; + + + public static DPad get() { + if (instance == null) { + instance = new DPad(); + } + + return instance; + } + + private DPad() { + ctx = (Context) MainActivity.get(); + + arrows = new LinkedList<>(); + 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); + + arrows.add(arrow_l); + arrows.add(arrow_r); + arrows.add(arrow_u); + arrows.add(arrow_d); + + for (final ArrowView av: arrows) { + layout.addView(av); + } + } + + public ConstraintLayout getLayout() { + return layout; + } + + public void setVisibility(final int vis) { + for (final ArrowView av: arrows) { + av.setVisibility(vis); + } + } + + public void setPosition(final int x, final int y) { + ArrowView av = arrows.get(0); + av.setX(x); + av.setY(y + 40); + + av = arrows.get(1); + av.setX(x + 100); + av.setY(y + 40); + + av = arrows.get(2); + av.setX(x + 40); + av.setY(y); + + av = arrows.get(3); + av.setX(x + 40); + av.setY(y + 100); + } + + public void onRefreshView() { + final Point size = new Point(); + final Display disp = MainActivity.get().getWindowManager().getDefaultDisplay(); + disp.getSize(size); + + int x = 100; + int y = size.y - 300; + + setPosition(x, y); + + if (PreferencesActivity.getSharedPreferences().getBoolean("show_dpad", false)) { + setVisibility(View.VISIBLE); + } else { + setVisibility(View.INVISIBLE); + } + } + + + private static class ArrowView extends ImageView { + public ArrowView(final Context ctx, final int id) { + super(ctx); + + setBackgroundColor(android.graphics.Color.TRANSPARENT); + setImageResource(id); + } + } +} diff --git a/android/app/src/main/java/org/arianne/stendhal/client/MainActivity.java b/android/app/src/main/java/org/arianne/stendhal/client/MainActivity.java index b599e5876ce..574aec4f396 100644 --- a/android/app/src/main/java/org/arianne/stendhal/client/MainActivity.java +++ b/android/app/src/main/java/org/arianne/stendhal/client/MainActivity.java @@ -14,14 +14,17 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.res.Configuration; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; +import androidx.constraintlayout.widget.ConstraintLayout; public class MainActivity extends AppCompatActivity { private static MainActivity instance; + private ConstraintLayout layout; private StendhalWebView client; private Menu menu; @@ -43,8 +46,10 @@ protected void onCreate(final Bundle savedInstanceState) { DebugLog.init(getExternalFilesDir(null), this); setContentView(R.layout.activity_main); + layout = (ConstraintLayout) findViewById(R.id.content); menu = new Menu(this); client = new StendhalWebView(this); + layout.addView(DPad.get().getLayout()); // initialize d-pad } catch (final Exception e) { e.printStackTrace(); DebugLog.error(e.toString()); @@ -99,6 +104,20 @@ public void onClick(final DialogInterface dialog, final int id) { confirmQuit.show(); } + @Override + protected void onResume() { + super.onResume(); + + DPad.get().onRefreshView(); + } + + @Override + public void onConfigurationChanged(final Configuration config) { + super.onConfigurationChanged(config); + + DPad.get().onRefreshView(); + } + @Override public void finish() { DebugLog.debug(MainActivity.class.getName() + ".finish() called"); diff --git a/android/app/src/main/java/org/arianne/stendhal/client/PreferencesActivity.java b/android/app/src/main/java/org/arianne/stendhal/client/PreferencesActivity.java index ce7b59465d8..e8a9e0b408a 100644 --- a/android/app/src/main/java/org/arianne/stendhal/client/PreferencesActivity.java +++ b/android/app/src/main/java/org/arianne/stendhal/client/PreferencesActivity.java @@ -11,16 +11,28 @@ ***************************************************************************/ package org.arianne.stendhal.client; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; import androidx.appcompat.app.AppCompatActivity; public class PreferencesActivity extends AppCompatActivity { + private static PreferencesActivity instance; + + + public static PreferencesActivity get() { + return instance; + } + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + instance = this; + setContentView(R.layout.activity_preferences); if (findViewById(R.id.preferencesFrame) != null) { @@ -28,15 +40,42 @@ protected void onCreate(final Bundle savedInstanceState) { return; } - final PreferenceFragment frag = new PreferenceFragment() { - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.preferences); - } - }; + getFragmentManager().beginTransaction().add(R.id.preferencesFrame, + new PFragment()).commit(); + } + } + + public static SharedPreferences getSharedPreferences() { + return PreferenceManager.getDefaultSharedPreferences(MainActivity.get()); + } + + + private static class PFragment extends PreferenceFragment + implements OnSharedPreferenceChangeListener { + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.preferences); + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences sp, final String key) { + if (key.equals("show_dpad")) { + DPad.get().onRefreshView(); + } + } + + @Override + public void onResume() { + super.onResume(); + getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + } - getFragmentManager().beginTransaction().add(R.id.preferencesFrame, frag).commit(); + @Override + public void onPause() { + getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); + super.onPause(); } } } diff --git a/android/app/src/main/res/drawable/dpad_arrow_down.png b/android/app/src/main/res/drawable/dpad_arrow_down.png new file mode 100644 index 0000000000000000000000000000000000000000..bd006919696d09be52714b81e22eaa74a18be381 GIT binary patch literal 267 zcmV+m0rdWfP)liR3gtJASBHR1~z7<;oOH@U{LR#waM1U{Y}> z=n#7`SVtomhAhAxpi}J0V4Z~;Q{p)Rtd{g;j%S5nwZ=qvx)y-dlV{=Wo(Oh9L)av+ z8ydzgfYP)PJ?h~bBZe#h;S!NeM7I0=!hit$TF*)r6pD?Y zFl+`2!Db;hrh#0T7BXWN$b?xTHRgc;U?D@R8uPx<0hc6b{LLbvE*1^FVhpH>v7iUW zgf18xIN?lr%v>uMNLQ_zh7fg9>C} zDv*gif^2LFW?;8q7B&Slu?v`uoxlq03szx!@4cZ4s#wO;CXFg66Pq48sDd)E5AL2Cq-J;ttYg?=u6ahN8=8f+Cn1@?tK?gSjCq7J@8T7}8=CNQ2R!CyWZMFghe6 Z@&;|vb<<`ALN5RS002ovPDHLkV1lmxW~~4K literal 0 HcmV?d00001 diff --git a/android/app/src/main/res/drawable/dpad_arrow_up.png b/android/app/src/main/res/drawable/dpad_arrow_up.png new file mode 100644 index 0000000000000000000000000000000000000000..077b92b0345c7012d4d1315654c274c761034cc6 GIT binary patch literal 217 zcmeAS@N?(olHy`uVBq!ia0vp^+CVJL!3HFss+Z*gsRf=cjv*0;?_TrfYgOQBO{{x- zBX?ukwx?_*OLm&?3y8L;aO3ryoqu6T%YzA1E}vYG^h8d9<@Wi1PLwQMXwv%Dw)~8hv5|Voz76M< RMS*T*@O1TaS?83{1ON$8P7VM7 literal 0 HcmV?d00001