Accelerometer events
This commit is contained in:
@@ -12,6 +12,8 @@ import lime.graphics.Renderer;
|
||||
import lime.math.Rectangle;
|
||||
import lime.system.Display;
|
||||
import lime.system.DisplayMode;
|
||||
import lime.system.Sensor;
|
||||
import lime.system.SensorType;
|
||||
import lime.system.System;
|
||||
import lime.ui.Gamepad;
|
||||
import lime.ui.Touch;
|
||||
@@ -25,6 +27,7 @@ import lime.ui.Window;
|
||||
@:access(lime._backend.native.NativeRenderer)
|
||||
@:access(lime.app.Application)
|
||||
@:access(lime.graphics.Renderer)
|
||||
@:access(lime.system.Sensor)
|
||||
@:access(lime.ui.Gamepad)
|
||||
@:access(lime.ui.Window)
|
||||
|
||||
@@ -38,6 +41,7 @@ class NativeApplication {
|
||||
private var keyEventInfo = new KeyEventInfo ();
|
||||
private var mouseEventInfo = new MouseEventInfo ();
|
||||
private var renderEventInfo = new RenderEventInfo (RENDER);
|
||||
private var sensorEventInfo = new SensorEventInfo ();
|
||||
private var textEventInfo = new TextEventInfo ();
|
||||
private var touchEventInfo = new TouchEventInfo ();
|
||||
private var unusedTouchesPool = new List<Touch> ();
|
||||
@@ -81,6 +85,11 @@ class NativeApplication {
|
||||
lime_touch_event_manager_register (handleTouchEvent, touchEventInfo);
|
||||
lime_window_event_manager_register (handleWindowEvent, windowEventInfo);
|
||||
|
||||
#if (ios || android)
|
||||
Sensor.registerSensor (SensorType.ACCELEROMETER, 0);
|
||||
lime_sensor_event_manager_register (handleSensorEvent, sensorEventInfo);
|
||||
#end
|
||||
|
||||
#if nodejs
|
||||
|
||||
lime_application_init (handle);
|
||||
@@ -292,6 +301,19 @@ class NativeApplication {
|
||||
}
|
||||
|
||||
|
||||
private function handleSensorEvent ():Void {
|
||||
|
||||
var sensor = Sensor.sensorByID.get (sensorEventInfo.id);
|
||||
|
||||
if (sensor != null) {
|
||||
|
||||
sensor.onUpdate.dispatch (sensorEventInfo.x, sensorEventInfo.y, sensorEventInfo.z);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function handleTextEvent ():Void {
|
||||
|
||||
var window = parent.windowByID.get (textEventInfo.windowID);
|
||||
@@ -516,6 +538,7 @@ class NativeApplication {
|
||||
@:cffi private static function lime_key_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_mouse_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_render_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_sensor_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_text_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_touch_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_window_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@@ -717,6 +740,44 @@ private class RenderEventInfo {
|
||||
}
|
||||
|
||||
|
||||
private class SensorEventInfo {
|
||||
|
||||
|
||||
public var id:Int;
|
||||
public var x:Float;
|
||||
public var y:Float;
|
||||
public var z:Float;
|
||||
public var type:SensorEventType;
|
||||
|
||||
|
||||
public function new (type:SensorEventType = null, id:Int = 0, x:Float = 0, y:Float = 0, z:Float = 0) {
|
||||
|
||||
this.type = type;
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function clone ():SensorEventInfo {
|
||||
|
||||
return new SensorEventInfo (type, id, x, y, z);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:enum private abstract SensorEventType(Int) {
|
||||
|
||||
var ACCELEROMETER = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private class TextEventInfo {
|
||||
|
||||
|
||||
|
||||
63
lime/system/Sensor.hx
Normal file
63
lime/system/Sensor.hx
Normal file
@@ -0,0 +1,63 @@
|
||||
package lime.system;
|
||||
|
||||
|
||||
import lime.app.Event;
|
||||
|
||||
|
||||
class Sensor {
|
||||
|
||||
|
||||
private static var sensorByID = new Map<Int, Sensor> ();
|
||||
private static var sensors = new Array<Sensor> ();
|
||||
|
||||
public var id:Int;
|
||||
public var onUpdate = new Event<Float->Float->Float->Void> ();
|
||||
public var type:SensorType;
|
||||
|
||||
|
||||
private function new (type:SensorType, id:Int) {
|
||||
|
||||
this.type = type;
|
||||
this.id = id;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function getSensors (type:SensorType = null):Array<Sensor> {
|
||||
|
||||
if (type == null) {
|
||||
|
||||
return sensors.copy ();
|
||||
|
||||
} else {
|
||||
|
||||
var result = [];
|
||||
|
||||
for (sensor in sensors) {
|
||||
|
||||
if (sensor.type == type) {
|
||||
|
||||
result.push (sensor);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static function registerSensor (type:SensorType, id:Int):Void {
|
||||
|
||||
var sensor = new Sensor (type, id);
|
||||
|
||||
sensors.push (sensor);
|
||||
sensorByID.set (id, sensor);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
8
lime/system/SensorType.hx
Normal file
8
lime/system/SensorType.hx
Normal file
@@ -0,0 +1,8 @@
|
||||
package lime.system;
|
||||
|
||||
|
||||
enum SensorType {
|
||||
|
||||
ACCELEROMETER;
|
||||
|
||||
}
|
||||
@@ -216,6 +216,7 @@
|
||||
<file name="src/math/Rectangle.cpp" />
|
||||
<file name="src/math/Vector2.cpp" />
|
||||
<file name="src/system/JNI.cpp" if="android" />
|
||||
<file name="src/system/SensorEvent.cpp" />
|
||||
<file name="src/ui/GamepadEvent.cpp" />
|
||||
<file name="src/ui/KeyEvent.cpp" />
|
||||
<file name="src/ui/MouseEvent.cpp" />
|
||||
|
||||
42
project/include/system/SensorEvent.h
Normal file
42
project/include/system/SensorEvent.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef LIME_SYSTEM_SENSOR_EVENT_H
|
||||
#define LIME_SYSTEM_SENSOR_EVENT_H
|
||||
|
||||
|
||||
#include <hx/CFFI.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
enum SensorEventType {
|
||||
|
||||
SENSOR_ACCELEROMETER
|
||||
|
||||
};
|
||||
|
||||
|
||||
class SensorEvent {
|
||||
|
||||
public:
|
||||
|
||||
static AutoGCRoot* callback;
|
||||
static AutoGCRoot* eventObject;
|
||||
|
||||
SensorEvent ();
|
||||
|
||||
static void Dispatch (SensorEvent* event);
|
||||
|
||||
int id;
|
||||
SensorEventType type;
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
60
project/src/system/SensorEvent.cpp
Normal file
60
project/src/system/SensorEvent.cpp
Normal 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 ());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user