[Android] Change how the onTouchEvent native method works. Just pass in the correct Button ID and Action and it'll be pressed or not. Not actually rigged up to the Java code yet. Doesn't support anything with an Axis yet so no C stick, main stick, L and R triggers

This commit is contained in:
Ryan Houdek 2013-10-26 05:36:20 -05:00
parent c3e4aa1f35
commit c24dfe559b
5 changed files with 25 additions and 80 deletions

View File

@ -17,12 +17,11 @@ public final class NativeLibrary
{
/**
* Handles touch events.
*
*
* @param Button Key code identifying which button was pressed.
* @param Action Mask for the action being performed.
* @param X Location on the screen's X-axis that the touch event occurred.
* @param Y Location on the screen's Y-axis that the touch event occurred.
*/
public static native void onTouchEvent(int Action, float X, float Y);
public static native void onTouchEvent(int Button, int Action);
/**
* Handles button press events for a gamepad.

View File

@ -135,22 +135,6 @@ public final class EmulationActivity extends Activity
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float X = event.getX();
float Y = event.getY();
int Action = event.getActionMasked();
// Converts button locations 0 - 1 to OGL screen coords -1.0 - 1.0
float ScreenX = ((X / screenWidth) * 2.0f) - 1.0f;
float ScreenY = ((Y / screenHeight) * -2.0f) + 1.0f;
NativeLibrary.onTouchEvent(Action, ScreenX, ScreenY);
return false;
}
@Override
public void onBackPressed()
{

View File

@ -15,7 +15,7 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <vector>
#include <unordered_map>
#include "GLInterface.h"
#include "Android/TextureLoader.h"
#include "Android/ButtonManager.h"
@ -24,16 +24,8 @@ extern void DrawButton(GLuint tex, float *coords);
namespace ButtonManager
{
std::vector<Button*> m_buttons;
std::map<std::string, InputDevice*> m_controllers;
// XXX: This needs to not be here so we can load the locations from file
// This will allow customizable button locations in the future
// These are the OpenGL on screen coordinates
float m_coords[][8] = { // X, Y, X, EY, EX, EY, EX, Y
{0.75f, -1.0f, 0.75f, -0.75f, 1.0f, -0.75f, 1.0f, -1.0f}, // A
{0.50f, -1.0f, 0.50f, -0.75f, 0.75f, -0.75f, 0.75f, -1.0f}, // B
{-0.10f, -1.0f, -0.10f, -0.80f, 0.10f, -0.80f, 0.10f, -1.0f}, // Start
};
std::unordered_map<int, Button*> m_buttons;
std::unordered_map<std::string, InputDevice*> m_controllers;
const char *configStrings[] = { "InputA",
"InputB",
"InputStart",
@ -71,9 +63,17 @@ namespace ButtonManager
void Init()
{
// Initialize our touchscreen buttons
m_buttons.push_back(new Button("ButtonA.png", BUTTON_A, m_coords[0]));
m_buttons.push_back(new Button("ButtonB.png", BUTTON_B, m_coords[1]));
m_buttons.push_back(new Button("ButtonStart.png", BUTTON_START, m_coords[2]));
m_buttons[BUTTON_A] = new Button();
m_buttons[BUTTON_B] = new Button();
m_buttons[BUTTON_START] = new Button();
m_buttons[BUTTON_X] = new Button();
m_buttons[BUTTON_Y] = new Button();
m_buttons[BUTTON_Z] = new Button();
m_buttons[BUTTON_UP] = new Button();
m_buttons[BUTTON_DOWN] = new Button();
m_buttons[BUTTON_LEFT] = new Button();
m_buttons[BUTTON_RIGHT] = new Button();
// Init our controller bindings
IniFile ini;
@ -109,9 +109,7 @@ namespace ButtonManager
bool GetButtonPressed(ButtonType button)
{
bool pressed = false;
for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
if ((*it)->GetButtonType() == button)
pressed = (*it)->Pressed();
pressed = m_buttons[button]->Pressed();
for (auto it = m_controllers.begin(); it != m_controllers.end(); ++it)
pressed |= it->second->ButtonValue(button);
@ -125,28 +123,13 @@ namespace ButtonManager
return 0.0f;
return it->second->AxisValue(axis);
}
void TouchEvent(int action, float x, float y)
void TouchEvent(int button, int action)
{
// Actions
// 0 is press
// 1 is let go
// 2 is move
for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
{
float *coords = (*it)->GetCoords();
if ( x >= coords[0] &&
x <= coords[4] &&
y >= coords[1] &&
y <= coords[3])
{
if (action == 0)
(*it)->SetState(BUTTON_PRESSED);
if (action == 1)
(*it)->SetState(BUTTON_RELEASED);
if (action == 2)
; // XXX: Be used later for analog stick
}
}
m_buttons[button]->SetState(action ? BUTTON_RELEASED : BUTTON_PRESSED);
}
void GamepadEvent(std::string dev, int button, int action)
@ -174,7 +157,7 @@ namespace ButtonManager
void Shutdown()
{
for(auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
delete *it;
delete it->second;
for (auto it = m_controllers.begin(); it != m_controllers.end(); ++it)
delete it->second;
m_controllers.clear();

View File

@ -61,32 +61,11 @@ namespace ButtonManager
class Button
{
private:
int m_tex;
ButtonType m_button;
ButtonState m_state;
float m_coords[8];
public:
Button(std::string filename, ButtonType button, float *coords)
{
u32 width, height;
char *image;
// image = LoadPNG((std::string(DOLPHIN_DATA_DIR "/") + filename).c_str(), width, height);
// XXX: Make platform specific drawing
m_button = button;
memcpy(m_coords, coords, sizeof(float) * 8);
m_state = BUTTON_RELEASED;
}
Button(ButtonType button)
{
m_button = button;
m_state = BUTTON_RELEASED;
}
Button() : m_state(BUTTON_RELEASED) {}
void SetState(ButtonState state) { m_state = state; }
bool Pressed() { return m_state == BUTTON_PRESSED; }
ButtonType GetButtonType() { return m_button; }
GLuint GetTexture() { return m_tex; }
float *GetCoords() { return m_coords; }
~Button() { }
};
@ -131,7 +110,7 @@ namespace ButtonManager
void DrawButtons();
bool GetButtonPressed(ButtonType button);
float GetAxisValue(ButtonType axis);
void TouchEvent(int action, float x, float y);
void TouchEvent(int button, int action);
void GamepadEvent(std::string dev, int button, int action);
void GamepadAxisEvent(std::string dev, int axis, float value);
void Shutdown();

View File

@ -237,9 +237,9 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_StopEmulatio
Core::Stop();
updateMainFrameEvent.Set(); // Kick the waiting event
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_onTouchEvent(JNIEnv *env, jobject obj, jint Action, jfloat X, jfloat Y)
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_onTouchEvent(JNIEnv *env, jobject obj, jint Button, jint Action)
{
ButtonManager::TouchEvent(Action, X, Y);
ButtonManager::TouchEvent(Button, Action);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_onGamePadEvent(JNIEnv *env, jobject obj, jstring jDevice, jint Button, jint Action)
{