[Android] Gamepad input. Refactor JNI native functions to all pull from a single class instead of everywhere willy-nilly

This commit is contained in:
Ryan Houdek
2013-06-18 07:09:20 -05:00
parent a518a1cbdc
commit 7223778520
19 changed files with 946 additions and 395 deletions

View File

@ -23,8 +23,7 @@ elseif(X11_FOUND)
Src/ControllerInterface/Xlib/Xlib.cpp)
elseif(ANDROID)
set(SRCS ${SRCS}
Src/ControllerInterface/Android/Android.cpp
Src/Android/ButtonManager.cpp)
Src/ControllerInterface/Android/Android.cpp)
endif()
add_library(inputcommon ${SRCS})

View File

@ -1,92 +0,0 @@
// Copyright (C) 2003 Dolphin Project.
// 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, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <vector>
#include "GLInterface.h"
#include "Android/TextureLoader.h"
#include "Android/ButtonManager.h"
extern void DrawButton(GLuint tex, float *coords);
namespace ButtonManager
{
std::vector<Button*> m_buttons;
// 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
};
void Init()
{
// Initialize our 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]));
}
bool GetButtonPressed(ButtonType button)
{
for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
if ((*it)->GetButtonType() == button)
return (*it)->Pressed();
return false;
}
void TouchEvent(int action, float x, float y)
{
// 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
}
}
}
void Shutdown()
{
for(auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
delete *it;
}
void DrawButtons()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for(auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
DrawButton((*it)->GetTexture(), (*it)->GetCoords());
glDisable(GL_BLEND);
}
}

View File

@ -1,63 +0,0 @@
// Copyright (C) 2003 Dolphin Project.
// 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, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <string>
#include "CommonPaths.h"
#include "Android/TextureLoader.h"
namespace ButtonManager
{
enum ButtonType
{
BUTTON_A = 0,
BUTTON_B,
BUTTON_START,
};
enum ButtonState
{
BUTTON_RELEASED = 0,
BUTTON_PRESSED = 1
};
class Button
{
private:
GLuint m_tex;
ButtonType m_button;
ButtonState m_state;
float m_coords[8];
public:
Button(std::string filename, ButtonType button, float *coords)
{
m_tex = LoadPNG((std::string(DOLPHIN_DATA_DIR "/") + filename).c_str());
m_button = button;
memcpy(m_coords, coords, sizeof(float) * 8);
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() { glDeleteTextures(1, &m_tex); }
};
void Init();
void DrawButtons();
bool GetButtonPressed(ButtonType button);
void TouchEvent(int action, float x, float y);
void Shutdown();
}

View File

@ -48,6 +48,20 @@ Touchscreen::Touchscreen()
AddInput(new Button(ButtonManager::BUTTON_A));
AddInput(new Button(ButtonManager::BUTTON_B));
AddInput(new Button(ButtonManager::BUTTON_START));
AddInput(new Button(ButtonManager::BUTTON_X));
AddInput(new Button(ButtonManager::BUTTON_Y));
AddInput(new Button(ButtonManager::BUTTON_Z));
AddInput(new Button(ButtonManager::BUTTON_UP));
AddInput(new Button(ButtonManager::BUTTON_DOWN));
AddInput(new Button(ButtonManager::BUTTON_LEFT));
AddInput(new Button(ButtonManager::BUTTON_RIGHT));
AddAnalogInputs(new Axis(ButtonManager::STICK_MAIN_LEFT), new Axis(ButtonManager::STICK_MAIN_RIGHT));
AddAnalogInputs(new Axis(ButtonManager::STICK_MAIN_UP), new Axis(ButtonManager::STICK_MAIN_DOWN));
AddAnalogInputs(new Axis(ButtonManager::STICK_C_UP), new Axis(ButtonManager::STICK_C_DOWN));
AddAnalogInputs(new Axis(ButtonManager::STICK_C_LEFT), new Axis(ButtonManager::STICK_C_RIGHT));
AddAnalogInputs(new Axis(ButtonManager::TRIGGER_L), new Axis(ButtonManager::TRIGGER_L));
AddAnalogInputs(new Axis(ButtonManager::TRIGGER_R), new Axis(ButtonManager::TRIGGER_R));
}
// Buttons and stuff
@ -62,6 +76,17 @@ ControlState Touchscreen::Button::GetState() const
{
return ButtonManager::GetButtonPressed(m_index);
}
std::string Touchscreen::Axis::GetName() const
{
std::ostringstream ss;
ss << "Axis " << (int)m_index;
return ss.str();
}
ControlState Touchscreen::Axis::GetState() const
{
return ButtonManager::GetAxisValue(m_index);
}
}
}

View File

@ -36,7 +36,16 @@ private:
Button(ButtonManager::ButtonType index) : m_index(index) {}
ControlState GetState() const;
private:
const ButtonManager::ButtonType m_index;
const ButtonManager::ButtonType m_index;
};
class Axis : public Input
{
public:
std::string GetName() const;
Axis(ButtonManager::ButtonType index) : m_index(index) {}
ControlState GetState() const;
private:
const ButtonManager::ButtonType m_index;
};
public: