Add lime.ui.Joystick and dispatch joystick events
This commit is contained in:
@@ -16,6 +16,8 @@ import lime.system.Sensor;
|
||||
import lime.system.SensorType;
|
||||
import lime.system.System;
|
||||
import lime.ui.Gamepad;
|
||||
import lime.ui.Joystick;
|
||||
import lime.ui.JoystickHatPosition;
|
||||
import lime.ui.Touch;
|
||||
import lime.ui.Window;
|
||||
|
||||
@@ -29,6 +31,7 @@ import lime.ui.Window;
|
||||
@:access(lime.graphics.Renderer)
|
||||
@:access(lime.system.Sensor)
|
||||
@:access(lime.ui.Gamepad)
|
||||
@:access(lime.ui.Joystick)
|
||||
@:access(lime.ui.Window)
|
||||
|
||||
|
||||
@@ -38,6 +41,7 @@ class NativeApplication {
|
||||
private var applicationEventInfo = new ApplicationEventInfo (UPDATE);
|
||||
private var currentTouches = new Map<Int, Touch> ();
|
||||
private var gamepadEventInfo = new GamepadEventInfo ();
|
||||
private var joystickEventInfo = new JoystickEventInfo ();
|
||||
private var keyEventInfo = new KeyEventInfo ();
|
||||
private var mouseEventInfo = new MouseEventInfo ();
|
||||
private var renderEventInfo = new RenderEventInfo (RENDER);
|
||||
@@ -82,6 +86,7 @@ class NativeApplication {
|
||||
|
||||
lime_application_event_manager_register (handleApplicationEvent, applicationEventInfo);
|
||||
lime_gamepad_event_manager_register (handleGamepadEvent, gamepadEventInfo);
|
||||
lime_joystick_event_manager_register (handleJoystickEvent, joystickEventInfo);
|
||||
lime_key_event_manager_register (handleKeyEvent, keyEventInfo);
|
||||
lime_mouse_event_manager_register (handleMouseEvent, mouseEventInfo);
|
||||
lime_render_event_manager_register (handleRenderEvent, renderEventInfo);
|
||||
@@ -203,6 +208,57 @@ class NativeApplication {
|
||||
}
|
||||
|
||||
|
||||
private function handleJoystickEvent ():Void {
|
||||
|
||||
switch (joystickEventInfo.type) {
|
||||
|
||||
case AXIS_MOVE:
|
||||
|
||||
var joystick = Joystick.devices.get (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.onAxisMove.dispatch (joystickEventInfo.index, joystickEventInfo.value);
|
||||
|
||||
case HAT_MOVE:
|
||||
|
||||
var joystick = Joystick.devices.get (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.onHatMove.dispatch (joystickEventInfo.index, joystickEventInfo.x);
|
||||
|
||||
case TRACKBALL_MOVE:
|
||||
|
||||
var joystick = Joystick.devices.get (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.onTrackballMove.dispatch (joystickEventInfo.index, joystickEventInfo.value);
|
||||
|
||||
case BUTTON_DOWN:
|
||||
|
||||
var joystick = Joystick.devices.get (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.onButtonDown.dispatch (joystickEventInfo.index);
|
||||
|
||||
case BUTTON_UP:
|
||||
|
||||
var joystick = Joystick.devices.get (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.onButtonUp.dispatch (joystickEventInfo.index);
|
||||
|
||||
case CONNECT:
|
||||
|
||||
if (!Joystick.devices.exists (joystickEventInfo.id)) {
|
||||
|
||||
var joystick = new Joystick (joystickEventInfo.id);
|
||||
Joystick.devices.set (joystickEventInfo.id, joystick);
|
||||
Joystick.onConnect.dispatch (joystick);
|
||||
|
||||
}
|
||||
|
||||
case DISCONNECT:
|
||||
|
||||
var joystick = Joystick.devices.get (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.connected = false;
|
||||
Joystick.devices.remove (joystickEventInfo.id);
|
||||
if (joystick != null) joystick.onDisconnect.dispatch ();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function handleKeyEvent ():Void {
|
||||
|
||||
var window = parent.windowByID.get (keyEventInfo.windowID);
|
||||
@@ -538,6 +594,7 @@ class NativeApplication {
|
||||
@:cffi private static function lime_application_set_frame_rate (handle:Dynamic, value:Float):Void;
|
||||
@:cffi private static function lime_application_update (handle:Dynamic):Bool;
|
||||
@:cffi private static function lime_gamepad_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@:cffi private static function lime_joystick_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void;
|
||||
@: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;
|
||||
@@ -626,6 +683,52 @@ private class GamepadEventInfo {
|
||||
}
|
||||
|
||||
|
||||
private class JoystickEventInfo {
|
||||
|
||||
|
||||
public var id:Int;
|
||||
public var index:Int;
|
||||
public var type:JoystickEventType;
|
||||
public var value:Float;
|
||||
public var x:Int;
|
||||
public var y:Int;
|
||||
|
||||
|
||||
public function new (type:JoystickEventType = null, id:Int = 0, index:Int = 0, value:Float = 0, x:Int = 0, y:Int = 0) {
|
||||
|
||||
this.type = type;
|
||||
this.id = id;
|
||||
this.index = index;
|
||||
this.value = value;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function clone ():JoystickEventInfo {
|
||||
|
||||
return new JoystickEventInfo (type, id, index, value, x, y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:enum private abstract JoystickEventType(Int) {
|
||||
|
||||
var AXIS_MOVE = 0;
|
||||
var HAT_MOVE = 1;
|
||||
var TRACKBALL_MOVE = 2;
|
||||
var BUTTON_DOWN = 3;
|
||||
var BUTTON_UP = 4;
|
||||
var CONNECT = 5;
|
||||
var DISCONNECT = 6;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private class KeyEventInfo {
|
||||
|
||||
|
||||
|
||||
131
lime/ui/Joystick.hx
Normal file
131
lime/ui/Joystick.hx
Normal file
@@ -0,0 +1,131 @@
|
||||
package lime.ui;
|
||||
|
||||
|
||||
import lime.app.Event;
|
||||
|
||||
#if !macro
|
||||
@:build(lime.system.CFFI.build())
|
||||
#end
|
||||
|
||||
|
||||
class Joystick {
|
||||
|
||||
|
||||
public static var devices = new Map<Int, Joystick> ();
|
||||
public static var onConnect = new Event<Joystick->Void> ();
|
||||
|
||||
public var connected (default, null):Bool;
|
||||
public var guid (get, never):String;
|
||||
public var id (default, null):Int;
|
||||
public var name (get, never):String;
|
||||
public var numAxes (get, never):Int;
|
||||
public var numButtons (get, never):Int;
|
||||
public var numHats (get, never):Int;
|
||||
public var numTrackballs (get, never):Int;
|
||||
public var onAxisMove = new Event<Int->Float->Void> ();
|
||||
public var onButtonDown = new Event<Int->Void> ();
|
||||
public var onButtonUp = new Event<Int->Void> ();
|
||||
public var onDisconnect = new Event<Void->Void> ();
|
||||
public var onHatMove = new Event<Int->JoystickHatPosition->Void> ();
|
||||
public var onTrackballMove = new Event<Int->Float->Void> ();
|
||||
|
||||
|
||||
public function new (id:Int) {
|
||||
|
||||
this.id = id;
|
||||
connected = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Get & Set Methods
|
||||
|
||||
|
||||
|
||||
|
||||
@:noCompletion private inline function get_guid ():String {
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
return lime_joystick_get_device_guid (this.id);
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:noCompletion private inline function get_name ():String {
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
return lime_joystick_get_device_name (this.id);
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:noCompletion private inline function get_numAxes ():Int {
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
return lime_joystick_get_num_axes (this.id);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:noCompletion private inline function get_numButtons ():Int {
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
return lime_joystick_get_num_buttons (this.id);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:noCompletion private inline function get_numHats ():Int {
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
return lime_joystick_get_num_hats (this.id);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:noCompletion private inline function get_numTrackballs ():Int {
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
return lime_joystick_get_num_trackballs (this.id);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Native Methods
|
||||
|
||||
|
||||
|
||||
|
||||
#if ((cpp || neko || nodejs) && !macro)
|
||||
@:cffi private static function lime_joystick_get_device_guid (id:Int):Dynamic;
|
||||
@:cffi private static function lime_joystick_get_device_name (id:Int):Dynamic;
|
||||
@:cffi private static function lime_joystick_get_num_axes (id:Int):Int;
|
||||
@:cffi private static function lime_joystick_get_num_buttons (id:Int):Int;
|
||||
@:cffi private static function lime_joystick_get_num_hats (id:Int):Int;
|
||||
@:cffi private static function lime_joystick_get_num_trackballs (id:Int):Int;
|
||||
#end
|
||||
|
||||
|
||||
}
|
||||
148
lime/ui/JoystickHatPosition.hx
Normal file
148
lime/ui/JoystickHatPosition.hx
Normal file
@@ -0,0 +1,148 @@
|
||||
package lime.ui;
|
||||
|
||||
|
||||
abstract JoystickHatPosition(Int) from Int to Int from UInt to UInt {
|
||||
|
||||
|
||||
public static inline var CENTER:JoystickHatPosition = 0x00;
|
||||
public static inline var DOWN:JoystickHatPosition = 0x04;
|
||||
public static inline var LEFT:JoystickHatPosition = 0x08;
|
||||
public static inline var RIGHT:JoystickHatPosition = 0x02;
|
||||
public static inline var UP:JoystickHatPosition = 0x01;
|
||||
|
||||
public static inline var DOWN_LEFT:JoystickHatPosition = (0x04 | 0x08);
|
||||
public static inline var DOWN_RIGHT:JoystickHatPosition = (0x04 | 0x02);
|
||||
public static inline var UP_LEFT:JoystickHatPosition = (0x01 | 0x08);
|
||||
public static inline var UP_RIGHT:JoystickHatPosition = (0x01 | 0x02);
|
||||
|
||||
public var center (get, set):Bool;
|
||||
public var down (get, set):Bool;
|
||||
public var left (get, set):Bool;
|
||||
public var right (get, set):Bool;
|
||||
public var up (get, set):Bool;
|
||||
|
||||
|
||||
public function new (value:Int) {
|
||||
|
||||
this = value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function get_center ():Bool {
|
||||
|
||||
return (this == 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private inline function set_center (value:Bool):Bool {
|
||||
|
||||
if (value) {
|
||||
|
||||
this = 0;
|
||||
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function get_down ():Bool {
|
||||
|
||||
return (this & DOWN > 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private inline function set_down (value:Bool):Bool {
|
||||
|
||||
if (value) {
|
||||
|
||||
this |= DOWN;
|
||||
|
||||
} else {
|
||||
|
||||
this &= 0xFFFFFFF - DOWN;
|
||||
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function get_left ():Bool {
|
||||
|
||||
return (this & LEFT > 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private inline function set_left (value:Bool):Bool {
|
||||
|
||||
if (value) {
|
||||
|
||||
this |= LEFT;
|
||||
|
||||
} else {
|
||||
|
||||
this &= 0xFFFFFFF - LEFT;
|
||||
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function get_right ():Bool {
|
||||
|
||||
return (this & RIGHT > 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private inline function set_right (value:Bool):Bool {
|
||||
|
||||
if (value) {
|
||||
|
||||
this |= RIGHT;
|
||||
|
||||
} else {
|
||||
|
||||
this &= 0xFFFFFFF - RIGHT;
|
||||
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function get_up ():Bool {
|
||||
|
||||
return (this & UP > 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private inline function set_up (value:Bool):Bool {
|
||||
|
||||
if (value) {
|
||||
|
||||
this |= UP;
|
||||
|
||||
} else {
|
||||
|
||||
this &= 0xFFFFFFF - UP;
|
||||
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -196,6 +196,7 @@
|
||||
<file name="src/backend/sdl/SDLWindow.cpp" />
|
||||
<file name="src/backend/sdl/SDLRenderer.cpp" />
|
||||
<file name="src/backend/sdl/SDLGamepad.cpp" />
|
||||
<file name="src/backend/sdl/SDLJoystick.cpp" />
|
||||
<file name="src/backend/sdl/SDLMouse.cpp" />
|
||||
<file name="src/backend/sdl/SDLSystem.cpp" />
|
||||
|
||||
@@ -224,6 +225,7 @@
|
||||
<file name="src/system/JNI.cpp" if="android" />
|
||||
<file name="src/system/SensorEvent.cpp" />
|
||||
<file name="src/ui/GamepadEvent.cpp" />
|
||||
<file name="src/ui/JoystickEvent.cpp" />
|
||||
<file name="src/ui/KeyEvent.cpp" />
|
||||
<file name="src/ui/MouseEvent.cpp" />
|
||||
<file name="src/ui/TextEvent.cpp" />
|
||||
|
||||
@@ -10,11 +10,11 @@ namespace lime {
|
||||
|
||||
enum GamepadEventType {
|
||||
|
||||
AXIS_MOVE,
|
||||
BUTTON_DOWN,
|
||||
BUTTON_UP,
|
||||
CONNECT,
|
||||
DISCONNECT
|
||||
GAMEPAD_AXIS_MOVE,
|
||||
GAMEPAD_BUTTON_DOWN,
|
||||
GAMEPAD_BUTTON_UP,
|
||||
GAMEPAD_CONNECT,
|
||||
GAMEPAD_DISCONNECT
|
||||
|
||||
};
|
||||
|
||||
|
||||
25
project/include/ui/Joystick.h
Normal file
25
project/include/ui/Joystick.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef LIME_UI_JOYSTICK_H
|
||||
#define LIME_UI_JOYSTICK_H
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
class Joystick {
|
||||
|
||||
public:
|
||||
|
||||
static const char* GetDeviceGUID (int id);
|
||||
static const char* GetDeviceName (int id);
|
||||
static int GetNumAxes (int id);
|
||||
static int GetNumButtons (int id);
|
||||
static int GetNumHats (int id);
|
||||
static int GetNumTrackballs (int id);
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
48
project/include/ui/JoystickEvent.h
Normal file
48
project/include/ui/JoystickEvent.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef LIME_UI_JOYSTICK_EVENT_H
|
||||
#define LIME_UI_JOYSTICK_EVENT_H
|
||||
|
||||
|
||||
#include <hx/CFFI.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
enum JoystickEventType {
|
||||
|
||||
JOYSTICK_AXIS_MOVE,
|
||||
JOYSTICK_HAT_MOVE,
|
||||
JOYSTICK_TRACKBALL_MOVE,
|
||||
JOYSTICK_BUTTON_DOWN,
|
||||
JOYSTICK_BUTTON_UP,
|
||||
JOYSTICK_CONNECT,
|
||||
JOYSTICK_DISCONNECT
|
||||
|
||||
};
|
||||
|
||||
|
||||
class JoystickEvent {
|
||||
|
||||
public:
|
||||
|
||||
static AutoGCRoot* callback;
|
||||
static AutoGCRoot* eventObject;
|
||||
|
||||
JoystickEvent ();
|
||||
|
||||
static void Dispatch (JoystickEvent* event);
|
||||
|
||||
double eventValue;
|
||||
int id;
|
||||
int index;
|
||||
JoystickEventType type;
|
||||
int x;
|
||||
int y;
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -31,6 +31,8 @@
|
||||
#include <ui/FileDialog.h>
|
||||
#include <ui/Gamepad.h>
|
||||
#include <ui/GamepadEvent.h>
|
||||
#include <ui/Joystick.h>
|
||||
#include <ui/JoystickEvent.h>
|
||||
#include <ui/KeyEvent.h>
|
||||
#include <ui/Mouse.h>
|
||||
#include <ui/MouseCursor.h>
|
||||
@@ -784,6 +786,58 @@ namespace lime {
|
||||
}
|
||||
|
||||
|
||||
void lime_joystick_event_manager_register (value callback, value eventObject) {
|
||||
|
||||
JoystickEvent::callback = new AutoGCRoot (callback);
|
||||
JoystickEvent::eventObject = new AutoGCRoot (eventObject);
|
||||
|
||||
}
|
||||
|
||||
|
||||
value lime_joystick_get_device_guid (int id) {
|
||||
|
||||
const char* guid = Joystick::GetDeviceGUID (id);
|
||||
return guid ? alloc_string (guid) : alloc_null ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
value lime_joystick_get_device_name (int id) {
|
||||
|
||||
const char* name = Joystick::GetDeviceName (id);
|
||||
return name ? alloc_string (name) : alloc_null ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
int lime_joystick_get_num_axes (int id) {
|
||||
|
||||
return Joystick::GetNumAxes (id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int lime_joystick_get_num_buttons (int id) {
|
||||
|
||||
return Joystick::GetNumButtons (id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int lime_joystick_get_num_hats (int id) {
|
||||
|
||||
return Joystick::GetNumHats (id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int lime_joystick_get_num_trackballs (int id) {
|
||||
|
||||
return Joystick::GetNumTrackballs (id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
value lime_jpeg_decode_bytes (value data, bool decodeData) {
|
||||
|
||||
ImageBuffer imageBuffer;
|
||||
@@ -1358,6 +1412,13 @@ namespace lime {
|
||||
DEFINE_PRIME3 (lime_image_encode);
|
||||
DEFINE_PRIME1 (lime_image_load);
|
||||
DEFINE_PRIME0 (lime_jni_getenv);
|
||||
DEFINE_PRIME2v (lime_joystick_event_manager_register);
|
||||
DEFINE_PRIME1 (lime_joystick_get_device_guid);
|
||||
DEFINE_PRIME1 (lime_joystick_get_device_name);
|
||||
DEFINE_PRIME1 (lime_joystick_get_num_axes);
|
||||
DEFINE_PRIME1 (lime_joystick_get_num_buttons);
|
||||
DEFINE_PRIME1 (lime_joystick_get_num_hats);
|
||||
DEFINE_PRIME1 (lime_joystick_get_num_trackballs);
|
||||
DEFINE_PRIME2 (lime_jpeg_decode_bytes);
|
||||
DEFINE_PRIME2 (lime_jpeg_decode_file);
|
||||
DEFINE_PRIME2v (lime_key_event_manager_register);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "SDLApplication.h"
|
||||
#include "SDLGamepad.h"
|
||||
#include "SDLJoystick.h"
|
||||
|
||||
#ifdef HX_MACOS
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
@@ -16,8 +17,6 @@ namespace lime {
|
||||
AutoGCRoot* Application::callback = 0;
|
||||
SDLApplication* SDLApplication::currentApplication = 0;
|
||||
|
||||
static SDL_Joystick* accelerometer = 0;
|
||||
static SDL_JoystickID accelerometerID = 0;
|
||||
const int analogAxisDeadZone = 1000;
|
||||
std::map<int, std::map<int, int> > gamepadsAxisMap;
|
||||
|
||||
@@ -46,6 +45,7 @@ namespace lime {
|
||||
|
||||
ApplicationEvent applicationEvent;
|
||||
GamepadEvent gamepadEvent;
|
||||
JoystickEvent joystickEvent;
|
||||
KeyEvent keyEvent;
|
||||
MouseEvent mouseEvent;
|
||||
RenderEvent renderEvent;
|
||||
@@ -54,18 +54,7 @@ namespace lime {
|
||||
TouchEvent touchEvent;
|
||||
WindowEvent windowEvent;
|
||||
|
||||
#if defined(IOS) || defined(ANDROID) || defined(TVOS)
|
||||
for (int i = 0; i < SDL_NumJoysticks (); i++) {
|
||||
|
||||
if (strstr (SDL_JoystickNameForIndex (i), "Accelerometer")) {
|
||||
|
||||
accelerometer = SDL_JoystickOpen (i);
|
||||
accelerometerID = SDL_JoystickInstanceID (accelerometer);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
SDLJoystick::Init ();
|
||||
|
||||
#ifdef HX_MACOS
|
||||
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL (CFBundleGetMainBundle ());
|
||||
@@ -166,13 +155,16 @@ namespace lime {
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
|
||||
#if defined(IOS) || defined(ANDROID) || defined(TVOS)
|
||||
if (event->jaxis.which == accelerometerID) {
|
||||
if (SDLJoystick::IsAccelerometer (event->jaxis.which)) {
|
||||
|
||||
ProcessSensorEvent (event);
|
||||
|
||||
} else {
|
||||
|
||||
ProcessJoystickEvent (event);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
break;
|
||||
|
||||
case SDL_JOYBALLMOTION:
|
||||
@@ -182,7 +174,7 @@ namespace lime {
|
||||
case SDL_JOYDEVICEADDED:
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
|
||||
//joy
|
||||
ProcessJoystickEvent (event);
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
@@ -279,7 +271,7 @@ namespace lime {
|
||||
|
||||
}
|
||||
|
||||
gamepadEvent.type = AXIS_MOVE;
|
||||
gamepadEvent.type = GAMEPAD_AXIS_MOVE;
|
||||
gamepadEvent.axis = event->caxis.axis;
|
||||
gamepadEvent.id = event->caxis.which;
|
||||
|
||||
@@ -305,7 +297,7 @@ namespace lime {
|
||||
|
||||
case SDL_CONTROLLERBUTTONDOWN:
|
||||
|
||||
gamepadEvent.type = BUTTON_DOWN;
|
||||
gamepadEvent.type = GAMEPAD_BUTTON_DOWN;
|
||||
gamepadEvent.button = event->cbutton.button;
|
||||
gamepadEvent.id = event->cbutton.which;
|
||||
|
||||
@@ -314,7 +306,7 @@ namespace lime {
|
||||
|
||||
case SDL_CONTROLLERBUTTONUP:
|
||||
|
||||
gamepadEvent.type = BUTTON_UP;
|
||||
gamepadEvent.type = GAMEPAD_BUTTON_UP;
|
||||
gamepadEvent.button = event->cbutton.button;
|
||||
gamepadEvent.id = event->cbutton.which;
|
||||
|
||||
@@ -325,7 +317,7 @@ namespace lime {
|
||||
|
||||
if (SDLGamepad::Connect (event->cdevice.which)) {
|
||||
|
||||
gamepadEvent.type = CONNECT;
|
||||
gamepadEvent.type = GAMEPAD_CONNECT;
|
||||
gamepadEvent.id = SDLGamepad::GetInstanceID (event->cdevice.which);
|
||||
|
||||
GamepadEvent::Dispatch (&gamepadEvent);
|
||||
@@ -336,7 +328,7 @@ namespace lime {
|
||||
|
||||
case SDL_CONTROLLERDEVICEREMOVED: {
|
||||
|
||||
gamepadEvent.type = DISCONNECT;
|
||||
gamepadEvent.type = GAMEPAD_DISCONNECT;
|
||||
gamepadEvent.id = event->cdevice.which;
|
||||
|
||||
GamepadEvent::Dispatch (&gamepadEvent);
|
||||
@@ -352,6 +344,113 @@ namespace lime {
|
||||
}
|
||||
|
||||
|
||||
void SDLApplication::ProcessJoystickEvent (SDL_Event* event) {
|
||||
|
||||
if (JoystickEvent::callback) {
|
||||
|
||||
switch (event->type) {
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
|
||||
if (!SDLJoystick::IsAccelerometer (event->jaxis.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_AXIS_MOVE;
|
||||
joystickEvent.index = event->jaxis.axis;
|
||||
joystickEvent.eventValue = event->jaxis.value / (event->jaxis.value > 0 ? 32767.0 : 32768.0);
|
||||
joystickEvent.id = event->jaxis.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYBALLMOTION:
|
||||
|
||||
if (!SDLJoystick::IsAccelerometer (event->jball.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_TRACKBALL_MOVE;
|
||||
joystickEvent.index = event->jball.ball;
|
||||
joystickEvent.x = event->jball.xrel;
|
||||
joystickEvent.y = event->jball.yrel;
|
||||
joystickEvent.id = event->jball.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
|
||||
if (!SDLJoystick::IsAccelerometer (event->jbutton.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_BUTTON_DOWN;
|
||||
joystickEvent.index = event->jbutton.button;
|
||||
joystickEvent.id = event->jbutton.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYBUTTONUP:
|
||||
|
||||
if (!SDLJoystick::IsAccelerometer (event->jbutton.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_BUTTON_UP;
|
||||
joystickEvent.index = event->jbutton.button;
|
||||
joystickEvent.id = event->jbutton.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYHATMOTION:
|
||||
|
||||
if (!SDLJoystick::IsAccelerometer (event->jhat.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_HAT_MOVE;
|
||||
joystickEvent.index = event->jhat.hat;
|
||||
joystickEvent.x = event->jhat.value;
|
||||
joystickEvent.id = event->jhat.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYDEVICEADDED:
|
||||
|
||||
if (SDLJoystick::Connect (event->jdevice.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_CONNECT;
|
||||
joystickEvent.id = event->jdevice.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
|
||||
if (!SDLJoystick::IsAccelerometer (event->jdevice.which)) {
|
||||
|
||||
joystickEvent.type = JOYSTICK_DISCONNECT;
|
||||
joystickEvent.id = event->jdevice.which;
|
||||
|
||||
JoystickEvent::Dispatch (&joystickEvent);
|
||||
SDLJoystick::Disconnect (event->jdevice.which);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SDLApplication::ProcessKeyEvent (SDL_Event* event) {
|
||||
|
||||
if (KeyEvent::callback) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <graphics/RenderEvent.h>
|
||||
#include <system/SensorEvent.h>
|
||||
#include <ui/GamepadEvent.h>
|
||||
#include <ui/JoystickEvent.h>
|
||||
#include <ui/KeyEvent.h>
|
||||
#include <ui/MouseEvent.h>
|
||||
#include <ui/TextEvent.h>
|
||||
@@ -38,6 +39,7 @@ namespace lime {
|
||||
|
||||
void HandleEvent (SDL_Event* event);
|
||||
void ProcessGamepadEvent (SDL_Event* event);
|
||||
void ProcessJoystickEvent (SDL_Event* event);
|
||||
void ProcessKeyEvent (SDL_Event* event);
|
||||
void ProcessMouseEvent (SDL_Event* event);
|
||||
void ProcessSensorEvent (SDL_Event* event);
|
||||
@@ -55,6 +57,7 @@ namespace lime {
|
||||
Uint32 currentUpdate;
|
||||
double framePeriod;
|
||||
GamepadEvent gamepadEvent;
|
||||
JoystickEvent joystickEvent;
|
||||
KeyEvent keyEvent;
|
||||
Uint32 lastUpdate;
|
||||
MouseEvent mouseEvent;
|
||||
|
||||
118
project/src/backend/sdl/SDLJoystick.cpp
Normal file
118
project/src/backend/sdl/SDLJoystick.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
#include "SDLJoystick.h"
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
static SDL_Joystick* accelerometer = 0;
|
||||
static SDL_JoystickID accelerometerID = -1;
|
||||
std::map<int, SDL_Joystick*> joysticks = std::map<int, SDL_Joystick*> ();
|
||||
|
||||
|
||||
bool SDLJoystick::Connect (int id) {
|
||||
|
||||
if (id != accelerometerID) {
|
||||
|
||||
SDL_Joystick* joystick = SDL_JoystickOpen (id);
|
||||
|
||||
if (joystick) {
|
||||
|
||||
joysticks[id] = joystick;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool SDLJoystick::Disconnect (int id) {
|
||||
|
||||
if (joysticks.find (id) != joysticks.end ()) {
|
||||
|
||||
SDL_Joystick* joystick = joysticks[id];
|
||||
SDL_JoystickClose (joystick);
|
||||
joysticks.erase (id);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SDLJoystick::Init () {
|
||||
|
||||
#if defined(IOS) || defined(ANDROID) || defined(TVOS)
|
||||
for (int i = 0; i < SDL_NumJoysticks (); i++) {
|
||||
|
||||
if (strstr (SDL_JoystickNameForIndex (i), "Accelerometer")) {
|
||||
|
||||
accelerometer = SDL_JoystickOpen (i);
|
||||
accelerometerID = SDL_JoystickInstanceID (accelerometer);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool SDLJoystick::IsAccelerometer (int id) {
|
||||
|
||||
return (id == accelerometerID);
|
||||
|
||||
}
|
||||
|
||||
|
||||
const char* Joystick::GetDeviceGUID (int id) {
|
||||
|
||||
char* guid = new char[64];
|
||||
SDL_JoystickGetGUIDString (SDL_JoystickGetDeviceGUID (id), guid, 64);
|
||||
return guid;
|
||||
|
||||
}
|
||||
|
||||
|
||||
const char* Joystick::GetDeviceName (int id) {
|
||||
|
||||
return SDL_JoystickName (joysticks[id]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int Joystick::GetNumAxes (int id) {
|
||||
|
||||
return SDL_JoystickNumAxes (joysticks[id]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int Joystick::GetNumButtons (int id) {
|
||||
|
||||
return SDL_JoystickNumButtons (joysticks[id]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int Joystick::GetNumHats (int id) {
|
||||
|
||||
return SDL_JoystickNumHats (joysticks[id]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int Joystick::GetNumTrackballs (int id) {
|
||||
|
||||
return SDL_JoystickNumBalls (joysticks[id]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
project/src/backend/sdl/SDLJoystick.h
Normal file
28
project/src/backend/sdl/SDLJoystick.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef LIME_SDL_JOYSTICK_H
|
||||
#define LIME_SDL_JOYSTICK_H
|
||||
|
||||
|
||||
#include <SDL.h>
|
||||
#include <ui/Joystick.h>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
class SDLJoystick {
|
||||
|
||||
public:
|
||||
|
||||
static bool Connect (int id);
|
||||
static bool Disconnect (int id);
|
||||
static void Init ();
|
||||
static bool IsAccelerometer (int id);
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -22,7 +22,7 @@ namespace lime {
|
||||
axisValue = 0;
|
||||
button = 0;
|
||||
id = 0;
|
||||
type = AXIS_MOVE;
|
||||
type = GAMEPAD_AXIS_MOVE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
64
project/src/ui/JoystickEvent.cpp
Normal file
64
project/src/ui/JoystickEvent.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <hx/CFFI.h>
|
||||
#include <ui/JoystickEvent.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
AutoGCRoot* JoystickEvent::callback = 0;
|
||||
AutoGCRoot* JoystickEvent::eventObject = 0;
|
||||
|
||||
static int id_id;
|
||||
static int id_index;
|
||||
static int id_type;
|
||||
static int id_value;
|
||||
static int id_x;
|
||||
static int id_y;
|
||||
static bool init = false;
|
||||
|
||||
|
||||
JoystickEvent::JoystickEvent () {
|
||||
|
||||
id = 0;
|
||||
index = 0;
|
||||
eventValue = 0;
|
||||
x = 0;
|
||||
y = 0;
|
||||
type = JOYSTICK_AXIS_MOVE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void JoystickEvent::Dispatch (JoystickEvent* event) {
|
||||
|
||||
if (JoystickEvent::callback) {
|
||||
|
||||
if (!init) {
|
||||
|
||||
id_id = val_id ("id");
|
||||
id_index = val_id ("index");
|
||||
id_type = val_id ("type");
|
||||
id_value = val_id ("value");
|
||||
id_x = val_id ("x");
|
||||
id_y = val_id ("y");
|
||||
init = true;
|
||||
|
||||
}
|
||||
|
||||
value object = (JoystickEvent::eventObject ? JoystickEvent::eventObject->get () : alloc_empty_object ());
|
||||
|
||||
alloc_field (object, id_id, alloc_int (event->id));
|
||||
alloc_field (object, id_index, alloc_int (event->index));
|
||||
alloc_field (object, id_type, alloc_int (event->type));
|
||||
alloc_field (object, id_value, alloc_float (event->eventValue));
|
||||
alloc_field (object, id_x, alloc_int (event->x));
|
||||
alloc_field (object, id_y, alloc_int (event->y));
|
||||
|
||||
val_call0 (JoystickEvent::callback->get ());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user