Improve accelerometer support

This commit is contained in:
Joshua Granick
2015-09-15 22:46:07 -07:00
parent 1170702692
commit 5b5528e7cb
2 changed files with 26 additions and 9 deletions

View File

@@ -60,6 +60,10 @@ class NativeApplication {
AudioManager.init (); AudioManager.init ();
#if (ios || android)
Sensor.registerSensor (SensorType.ACCELEROMETER, 0);
#end
} }
@@ -86,7 +90,6 @@ class NativeApplication {
lime_window_event_manager_register (handleWindowEvent, windowEventInfo); lime_window_event_manager_register (handleWindowEvent, windowEventInfo);
#if (ios || android) #if (ios || android)
Sensor.registerSensor (SensorType.ACCELEROMETER, 0);
lime_sensor_event_manager_register (handleSensorEvent, sensorEventInfo); lime_sensor_event_manager_register (handleSensorEvent, sensorEventInfo);
#end #end

View File

@@ -15,8 +15,11 @@ namespace lime {
AutoGCRoot* Application::callback = 0; AutoGCRoot* Application::callback = 0;
SDLApplication* SDLApplication::currentApplication = 0; SDLApplication* SDLApplication::currentApplication = 0;
std::map<int, std::map<int, int> > gamepadsAxisMap;
static SDL_Joystick* accelerometer = 0;
static SDL_JoystickID accelerometerID = 0;
const int analogAxisDeadZone = 1000; const int analogAxisDeadZone = 1000;
std::map<int, std::map<int, int> > gamepadsAxisMap;
SDLApplication::SDLApplication () { SDLApplication::SDLApplication () {
@@ -52,7 +55,16 @@ namespace lime {
WindowEvent windowEvent; WindowEvent windowEvent;
#if defined(IOS) || defined(ANDROID) #if defined(IOS) || defined(ANDROID)
SDL_JoystickOpen (0); for (int i = 0; i < SDL_NumJoysticks (); i++) {
if (strstr (SDL_JoystickNameForIndex (i), "Accelerometer")) {
accelerometer = SDL_JoystickOpen (i);
accelerometerID = SDL_JoystickInstanceID (accelerometer);
}
}
#endif #endif
#ifdef HX_MACOS #ifdef HX_MACOS
@@ -153,7 +165,7 @@ namespace lime {
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
#if defined(IOS) || defined(ANDROID) #if defined(IOS) || defined(ANDROID)
if (event->jaxis.which == 0) { if (event->jaxis.which == accelerometerID) {
ProcessSensorEvent (event); ProcessSensorEvent (event);
@@ -260,7 +272,7 @@ namespace lime {
gamepadsAxisMap[event->caxis.which][event->caxis.axis] = event->caxis.value; gamepadsAxisMap[event->caxis.which][event->caxis.axis] = event->caxis.value;
} else if (gamepadsAxisMap[event->caxis.which][event->caxis.axis] == event->caxis.value) { } else if (gamepadsAxisMap[event->caxis.which][event->caxis.axis] == event->caxis.value) {
break; break;
} }
@@ -284,7 +296,7 @@ namespace lime {
} }
gamepadsAxisMap[event->caxis.which][event->caxis.axis] = event->caxis.value; gamepadsAxisMap[event->caxis.which][event->caxis.axis] = event->caxis.value;
gamepadEvent.axisValue = event->caxis.value / (event->caxis.value>0?32767.0:32768.0); gamepadEvent.axisValue = event->caxis.value / (event->caxis.value > 0 ? 32767.0 : 32768.0);
GamepadEvent::Dispatch (&gamepadEvent); GamepadEvent::Dispatch (&gamepadEvent);
break; break;
@@ -412,11 +424,13 @@ namespace lime {
if (SensorEvent::callback) { if (SensorEvent::callback) {
double value = event->jaxis.value / (event->jaxis.value > 0 ? 32767.0 : 32768.0);
switch (event->jaxis.axis) { switch (event->jaxis.axis) {
case 0: sensorEvent.x = event->jaxis.value; break; case 0: sensorEvent.x = value; break;
case 1: sensorEvent.y = event->jaxis.value; break; case 1: sensorEvent.y = value; break;
case 2: sensorEvent.z = event->jaxis.value; break; case 2: sensorEvent.z = value; break;
default: break; default: break;
} }