Accelerometer events

This commit is contained in:
Joshua Granick
2015-09-15 16:30:15 -07:00
parent 7473f7d1d0
commit 1170702692
9 changed files with 284 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
#include <graphics/RenderEvent.h>
#include <system/Clipboard.h>
#include <system/JNI.h>
#include <system/SensorEvent.h>
#include <system/System.h>
#include <text/Font.h>
#include <text/TextLayout.h>
@@ -958,6 +959,14 @@ namespace lime {
}
void lime_sensor_event_manager_register (value callback, value eventObject) {
SensorEvent::callback = new AutoGCRoot (callback);
SensorEvent::eventObject = new AutoGCRoot (eventObject);
}
value lime_system_get_directory (int type, HxString company, HxString title) {
const char* path = System::GetDirectory ((SystemDirectory)type, company.__s, title.__s);
@@ -1298,6 +1307,7 @@ namespace lime {
DEFINE_PRIME1v (lime_renderer_make_current);
DEFINE_PRIME1v (lime_renderer_unlock);
DEFINE_PRIME2v (lime_render_event_manager_register);
DEFINE_PRIME2v (lime_sensor_event_manager_register);
DEFINE_PRIME3 (lime_system_get_directory);
DEFINE_PRIME1 (lime_system_get_display);
DEFINE_PRIME0 (lime_system_get_num_displays);

View File

@@ -18,6 +18,7 @@ namespace lime {
std::map<int, std::map<int, int> > gamepadsAxisMap;
const int analogAxisDeadZone = 1000;
SDLApplication::SDLApplication () {
if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) != 0) {
@@ -45,10 +46,15 @@ namespace lime {
KeyEvent keyEvent;
MouseEvent mouseEvent;
RenderEvent renderEvent;
SensorEvent sensorEvent;
TextEvent textEvent;
TouchEvent touchEvent;
WindowEvent windowEvent;
#if defined(IOS) || defined(ANDROID)
SDL_JoystickOpen (0);
#endif
#ifdef HX_MACOS
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL (CFBundleGetMainBundle ());
char path[PATH_MAX];
@@ -145,6 +151,16 @@ namespace lime {
break;
case SDL_JOYAXISMOTION:
#if defined(IOS) || defined(ANDROID)
if (event->jaxis.which == 0) {
ProcessSensorEvent (event);
}
#endif
break;
case SDL_JOYBALLMOTION:
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
@@ -392,6 +408,26 @@ namespace lime {
}
void SDLApplication::ProcessSensorEvent (SDL_Event* event) {
if (SensorEvent::callback) {
switch (event->jaxis.axis) {
case 0: sensorEvent.x = event->jaxis.value; break;
case 1: sensorEvent.y = event->jaxis.value; break;
case 2: sensorEvent.z = event->jaxis.value; break;
default: break;
}
SensorEvent::Dispatch (&sensorEvent);
}
}
void SDLApplication::ProcessTextEvent (SDL_Event* event) {
if (TextEvent::callback) {

View File

@@ -6,6 +6,7 @@
#include <app/Application.h>
#include <app/ApplicationEvent.h>
#include <graphics/RenderEvent.h>
#include <system/SensorEvent.h>
#include <ui/GamepadEvent.h>
#include <ui/KeyEvent.h>
#include <ui/MouseEvent.h>
@@ -39,6 +40,7 @@ namespace lime {
void ProcessGamepadEvent (SDL_Event* event);
void ProcessKeyEvent (SDL_Event* event);
void ProcessMouseEvent (SDL_Event* event);
void ProcessSensorEvent (SDL_Event* event);
void ProcessTextEvent (SDL_Event* event);
void ProcessTouchEvent (SDL_Event* event);
void ProcessWindowEvent (SDL_Event* event);
@@ -58,6 +60,7 @@ namespace lime {
MouseEvent mouseEvent;
double nextUpdate;
RenderEvent renderEvent;
SensorEvent sensorEvent;
TextEvent textEvent;
TouchEvent touchEvent;
WindowEvent windowEvent;

View File

@@ -0,0 +1,60 @@
#include <hx/CFFI.h>
#include <system/SensorEvent.h>
namespace lime {
AutoGCRoot* SensorEvent::callback = 0;
AutoGCRoot* SensorEvent::eventObject = 0;
static int id_id;
static int id_type;
static int id_x;
static int id_y;
static int id_z;
static bool init = false;
SensorEvent::SensorEvent () {
type = SENSOR_ACCELEROMETER;
id = 0;
x = 0;
y = 0;
z = 0;
}
void SensorEvent::Dispatch (SensorEvent* event) {
if (SensorEvent::callback) {
if (!init) {
id_id = val_id ("id");
id_type = val_id ("type");
id_x = val_id ("x");
id_y = val_id ("y");
id_z = val_id ("z");
init = true;
}
value object = (SensorEvent::eventObject ? SensorEvent::eventObject->get () : alloc_empty_object ());
alloc_field (object, id_id, alloc_int (event->id));
alloc_field (object, id_type, alloc_int (event->type));
alloc_field (object, id_x, alloc_float (event->x));
alloc_field (object, id_y, alloc_float (event->y));
alloc_field (object, id_z, alloc_float (event->z));
val_call0 (SensorEvent::callback->get ());
}
}
}