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 state = new HashMap(); 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 bcode = new HashMap(); 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 dcode = new HashMap(); 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); } } } }); } }