summaryrefslogtreecommitdiff
path: root/src/pw/cloudef/rpg/Base.java
blob: ed56bda9073685a785ba65cc3438a10bac63b5e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package pw.cloudef.rpg;
import org.libsdl.app.SDLActivity;
import java.util.Map;
import java.util.HashMap;
import java.lang.Integer;
import java.lang.Boolean;
import java.lang.String;
import android.os.Bundle;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.view.KeyEvent;
import android.view.InputDevice;
import com.andretietz.android.controller.ActionView;
import com.andretietz.android.controller.InputView;
import android.system.Os;

public class Base extends SDLActivity {
   private boolean gamepadHidden;
   private ActionView av;
   private ActionView dv;
   private Button hider;

   protected int mainLayout() {
      return R.layout.base;
   }

   protected boolean hideFully() {
      return false;
   }

   protected String gameId() {
      return getIntent().getExtras().getString("id");
   }

   protected String baseDir() {
      return getApplicationContext().getFilesDir().toPath().toString() + "/data/" + gameId();
   }

   protected void onCreate(Bundle savedInstanceState) {
      try { Os.setenv("COMPAT_CHDIR",  baseDir(), true); } catch (Exception e) {}
      super.onCreate(savedInstanceState);
      final LayoutInflater li = LayoutInflater.from(getApplicationContext());
      mLayout.addView((ViewGroup)li.inflate(mainLayout(), mLayout, false));

      final Map<Integer, Boolean> state = new HashMap<Integer, Boolean>();
      state.put(KeyEvent.KEYCODE_C, false);
      state.put(KeyEvent.KEYCODE_CTRL_LEFT, false);
      state.put(KeyEvent.KEYCODE_X, false);
      state.put(KeyEvent.KEYCODE_Z, false);
      state.put(KeyEvent.KEYCODE_DPAD_RIGHT, false);
      state.put(KeyEvent.KEYCODE_DPAD_LEFT, false);
      state.put(KeyEvent.KEYCODE_DPAD_DOWN, false);
      state.put(KeyEvent.KEYCODE_DPAD_UP, false);

      final Map<Integer, Integer> bcode = new HashMap<Integer, Integer>();
      bcode.put(0, KeyEvent.KEYCODE_Z);
      bcode.put(1, KeyEvent.KEYCODE_X);
      bcode.put(2, KeyEvent.KEYCODE_CTRL_LEFT);
      bcode.put(3, KeyEvent.KEYCODE_C);

      av = (ActionView)findViewById(R.id.viewAction);
      av.setOnButtonListener(new InputView.InputEventListener() {
         @Override public void onInputEvent(View view, int buttons) {
            for (int i = 0; i < 4; ++i) {
               if (((0x01 << i) & buttons) > 0 && !state.get(bcode.get(i))) {
                  dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, bcode.get(i)));
                  state.put(bcode.get(i), true);
               } else if (state.get(bcode.get(i))) {
                  dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, bcode.get(i)));
                  state.put(bcode.get(i), false);
               }
            }
         }
      });

      final Map<Integer, Integer> dcode = new HashMap<Integer, Integer>();
      dcode.put(0, KeyEvent.KEYCODE_DPAD_RIGHT);
      dcode.put(1, KeyEvent.KEYCODE_DPAD_DOWN);
      dcode.put(2, KeyEvent.KEYCODE_DPAD_LEFT);
      dcode.put(3, KeyEvent.KEYCODE_DPAD_UP);

      dv = (ActionView)findViewById(R.id.viewDirection);
      dv.setOnButtonListener(new InputView.InputEventListener() {
         @Override public void onInputEvent(View view, int buttons) {
            for (int i = 0; i < 4; ++i) {
               if (((0x01 << i) & buttons) > 0 && !state.get(dcode.get(i))) {
                  dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, dcode.get(i)));
                  state.put(dcode.get(i), true);
               } else if (state.get(dcode.get(i))) {
                  dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, dcode.get(i)));
                  state.put(dcode.get(i), false);
               }
            }
         }
      });

      hider = (Button)findViewById(R.id.hider);
      hider.setOnButtonListener(new InputView.InputEventListener() {
         @Override public void onInputEvent(View view, int buttons) {
            if (((0x01 << 0) & buttons) > 0) {
               gamepadHidden = !gamepadHidden;
               if (gamepadHidden) {
                  if (hideFully()) {
                     dv.setVisibility(View.GONE);
                     av.setVisibility(View.GONE);
                  } else {
                     dv.setAlpha(0.f);
                     av.setAlpha(0.f);
                  }
                  hider.setAlpha((hideFully() ? 0.f : 0.25f));
               } else {
                  if (hideFully()) {
                     dv.setVisibility(View.VISIBLE);
                     av.setVisibility(View.VISIBLE);
                  } else {
                     dv.setAlpha(0.5f);
                     av.setAlpha(0.5f);
                  }
                  hider.setAlpha(0.5f);
               }
            }
         }
      });
   }
}