Skip to content

Commit

Permalink
WebView: add mechanism to show a D-pad
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Feb 16, 2022
1 parent 89dd013 commit 4b8514d
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 8 deletions.
120 changes: 120 additions & 0 deletions android/app/src/main/java/org/arianne/stendhal/client/DPad.java
Original file line number Diff line number Diff line change
@@ -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<ArrowView> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,71 @@
***************************************************************************/
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) {
if (savedInstanceState != null) {
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();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/app/src/main/res/drawable/dpad_arrow_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4b8514d

Please sign in to comment.