Rename stuff, generate basic pad IDs for SDL input

This commit is contained in:
Daniel Stuart Baxter 2015-05-26 23:16:29 -05:00
parent 614da6bc17
commit 0d6c90a1ea
5 changed files with 72 additions and 16 deletions

View File

@ -10,7 +10,7 @@
namespace InputCommon {
struct ControllerState {
Service::HID::PadState jpad_state;
Service::HID::PadState pad_state;
s16 circle_pad_x;
s16 circle_pad_y;
u16 touch_screen_x;

View File

@ -35,6 +35,11 @@ std::string ControllerBase::GetDeviceName() const {
return device_name;
}
const Service::HID::PadState ControllerBase::GetPadState() const {
return controller.pad_state;
}
void Init(ControllerBackends backend) {
switch(backend)
{

View File

@ -35,9 +35,15 @@ public:
///Returns internal name of currently selected device. Expose this to the UI
std::string GetDeviceName() const;
///Returns internal pad state
const Service::HID::PadState GetPadState() const;
protected:
///Internal name of currently selected device
std::string device_name;
///Internal view this specific object will have of 3DS input states
ControllerState controller;
};
///Initialize the user input system

View File

@ -8,13 +8,15 @@ namespace InputCommon {
SDLController::SDLController() {
device_name = "SDL2::NONE";
j_pad = nullptr;
jpad = nullptr;
index = 0;
controller_state.circle_pad_x = 0;
controller_state.circle_pad_y = 0;
controller_state.touch_screen_x = 0;
controller_state.touch_screen_y = 0;
controller.circle_pad_x = 0;
controller.circle_pad_y = 0;
controller.touch_screen_x = 0;
controller.touch_screen_y = 0;
Init();
}
SDLController::~SDLController() {
@ -30,9 +32,10 @@ bool SDLController::Init() {
//Attempt to open joystick with SDL
//TODO - Use DiscoverDevice to generate a list of available joysticks, then open the correct index
j_pad = SDL_JoystickOpen(index);
jpad = SDL_JoystickOpen(index);
if(j_pad == nullptr) {
if(jpad == nullptr) {
std::cout<<"What\n";
return false;
}
}
@ -41,14 +44,57 @@ void SDLController::DiscoverDevices() {}
void SDLController::ShutDown() {
//Attempt to close joystick with SDL
if(j_pad != nullptr) {
SDL_JoystickClose(j_pad);
if(jpad != nullptr) {
SDL_JoystickClose(jpad);
}
//Delete the current user input plugin
delete g_user_input;
}
void SDLController::Poll() {}
void SDLController::Poll() {
if(SDL_PollEvent(&input_event)) {
if((input_event.type == SDL_JOYBUTTONDOWN) || (input_event.type == SDL_JOYBUTTONUP)
|| (input_event.type == SDL_JOYAXISMOTION) || (input_event.type == SDL_JOYHATMOTION)) {
ProcessInput();
}
}
}
void SDLController::ProcessInput() {
//Use a unique id for joystick input
int pad_id = 0;
int val = 0;
//Pressed state, for joystick buttons only
bool pressed = false;
//Generate the id based on input from an event
switch(input_event.type) {
//Joystick button press events
case SDL_JOYBUTTONDOWN:
pad_id = 100;
pad_id += input_event.jbutton.button;
pressed = true;
break;
//Joystick button release events
case SDL_JOYBUTTONUP:
pad_id = 100;
pad_id += input_event.jbutton.button;
pressed = false;
break;
//Joystick axis motion events
case SDL_JOYAXISMOTION:
pad_id = 200;
pad_id += (input_event.jaxis.axis * 2);
val = input_event.jaxis.value;
if(val > 0) {
pad_id += 1;
}
}
}
}//namespace

View File

@ -27,18 +27,17 @@ public:
///Shuts down all joysticks opened by SDL2
void ShutDown();
///Processes input when polling
void ProcessInput();
///SDL event used for polling
SDL_Event input_event;
private:
SDL_Joystick* j_pad;
SDL_Joystick* jpad;
///Index of joystick, used for opening/closing a joystick
int index;
///Internal view this specific object will have of 3DS input states
ControllerState controller_state;
};
} //namespace