Skip to content

Commit

Permalink
Create event functions for precise input handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteMasterEric authored and ninjamuffin99 committed Nov 1, 2024
1 parent 21c92d3 commit 9821208
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 10 deletions.
1 change: 1 addition & 0 deletions project/include/system/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace lime {
static void GCExitBlocking ();
static void GCTryEnterBlocking ();
static void GCTryExitBlocking ();
static int GetTicks ();
static bool GetAllowScreenTimeout ();
static std::wstring* GetDeviceModel ();
static std::wstring* GetDeviceVendor ();
Expand Down
1 change: 1 addition & 0 deletions project/include/ui/GamepadEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace lime {
int id;
GamepadEventType type;
double axisValue;
int timestamp;

static ValuePointer* callback;
static ValuePointer* eventObject;
Expand Down
1 change: 1 addition & 0 deletions project/include/ui/KeyEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace lime {
int modifier;
KeyEventType type;
int windowID;
int timestamp;

static ValuePointer* callback;
static ValuePointer* eventObject;
Expand Down
18 changes: 16 additions & 2 deletions project/src/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,18 @@ namespace lime {

}

int lime_sdl_get_ticks () {

return System::GetTicks();

}


HL_PRIM int HL_NAME(hl_sdl_get_ticks) () {

return System::GetTicks();

}

bool lime_system_get_allow_screen_timeout () {

Expand Down Expand Up @@ -4011,6 +4023,7 @@ namespace lime {
DEFINE_PRIME3 (lime_png_decode_file);
DEFINE_PRIME2v (lime_render_event_manager_register);
DEFINE_PRIME2v (lime_sensor_event_manager_register);
DEFINE_PRIME0 (lime_sdl_get_ticks);
DEFINE_PRIME0 (lime_system_get_allow_screen_timeout);
DEFINE_PRIME0 (lime_system_get_device_model);
DEFINE_PRIME0 (lime_system_get_device_vendor);
Expand Down Expand Up @@ -4083,9 +4096,9 @@ namespace lime {
#define _TCLIPBOARD_EVENT _OBJ (_I32)
#define _TDISPLAYMODE _OBJ (_I32 _I32 _I32 _I32)
#define _TDROP_EVENT _OBJ (_BYTES _I32)
#define _TGAMEPAD_EVENT _OBJ (_I32 _I32 _I32 _I32 _F64)
#define _TGAMEPAD_EVENT _OBJ (_I32 _I32 _I32 _I32 _F64 _I32)
#define _TJOYSTICK_EVENT _OBJ (_I32 _I32 _I32 _I32 _F64 _F64)
#define _TKEY_EVENT _OBJ (_F64 _I32 _I32 _I32)
#define _TKEY_EVENT _OBJ (_F64 _I32 _I32 _I32 _I32)
#define _TMOUSE_EVENT _OBJ (_I32 _F64 _F64 _I32 _I32 _F64 _F64 _I32)
#define _TRECTANGLE _OBJ (_F64 _F64 _F64 _F64)
#define _TRENDER_EVENT _OBJ (_I32)
Expand Down Expand Up @@ -4199,6 +4212,7 @@ namespace lime {
DEFINE_HL_PRIM (_TIMAGEBUFFER, hl_png_decode_file, _STRING _BOOL _TIMAGEBUFFER);
DEFINE_HL_PRIM (_VOID, hl_render_event_manager_register, _FUN (_VOID, _NO_ARG) _TRENDER_EVENT);
DEFINE_HL_PRIM (_VOID, hl_sensor_event_manager_register, _FUN (_VOID, _NO_ARG) _TSENSOR_EVENT);
DEFINE_HL_PRIM (_I32, hl_sdl_get_ticks, _NO_ARG);
DEFINE_HL_PRIM (_BOOL, hl_system_get_allow_screen_timeout, _NO_ARG);
DEFINE_HL_PRIM (_BYTES, hl_system_get_device_model, _NO_ARG);
DEFINE_HL_PRIM (_BYTES, hl_system_get_device_vendor, _NO_ARG);
Expand Down
6 changes: 6 additions & 0 deletions project/src/backend/sdl/SDLApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ namespace lime {

gamepadsAxisMap[event->caxis.which][event->caxis.axis] = event->caxis.value;
gamepadEvent.axisValue = event->caxis.value / (event->caxis.value > 0 ? 32767.0 : 32768.0);
gamepadEvent.timestamp = event->common.timestamp;

GamepadEvent::Dispatch (&gamepadEvent);
break;
Expand All @@ -410,6 +411,7 @@ namespace lime {
gamepadEvent.type = GAMEPAD_BUTTON_DOWN;
gamepadEvent.button = event->cbutton.button;
gamepadEvent.id = event->cbutton.which;
gamepadEvent.timestamp = event->common.timestamp;

GamepadEvent::Dispatch (&gamepadEvent);
break;
Expand All @@ -419,6 +421,7 @@ namespace lime {
gamepadEvent.type = GAMEPAD_BUTTON_UP;
gamepadEvent.button = event->cbutton.button;
gamepadEvent.id = event->cbutton.which;
gamepadEvent.timestamp = event->common.timestamp;

GamepadEvent::Dispatch (&gamepadEvent);
break;
Expand All @@ -429,6 +432,7 @@ namespace lime {

gamepadEvent.type = GAMEPAD_CONNECT;
gamepadEvent.id = SDLGamepad::GetInstanceID (event->cdevice.which);
gamepadEvent.timestamp = event->common.timestamp;

GamepadEvent::Dispatch (&gamepadEvent);

Expand All @@ -440,6 +444,7 @@ namespace lime {

gamepadEvent.type = GAMEPAD_DISCONNECT;
gamepadEvent.id = event->cdevice.which;
gamepadEvent.timestamp = event->common.timestamp;

GamepadEvent::Dispatch (&gamepadEvent);
SDLGamepad::Disconnect (event->cdevice.which);
Expand Down Expand Up @@ -561,6 +566,7 @@ namespace lime {
keyEvent.keyCode = event->key.keysym.sym;
keyEvent.modifier = event->key.keysym.mod;
keyEvent.windowID = event->key.windowID;
keyEvent.timestamp = event->common.timestamp;

if (keyEvent.type == KEY_DOWN) {

Expand Down
7 changes: 7 additions & 0 deletions project/src/backend/sdl/SDLSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ namespace lime {
}


int System::GetTicks () {

return SDL_GetTicks ();

}


bool System::GetAllowScreenTimeout () {

return SDL_IsScreenSaverEnabled ();
Expand Down
5 changes: 5 additions & 0 deletions project/src/ui/GamepadEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace lime {
static int id_id;
static int id_type;
static int id_value;
static int id_timestamp;
static bool init = false;


Expand All @@ -23,6 +24,7 @@ namespace lime {
button = 0;
id = 0;
type = GAMEPAD_AXIS_MOVE;
timestamp = 0;

}

Expand All @@ -40,6 +42,7 @@ namespace lime {
id_id = val_id ("id");
id_type = val_id ("type");
id_value = val_id ("axisValue");
id_timestamp = val_id ("timestamp");
init = true;

}
Expand All @@ -51,6 +54,7 @@ namespace lime {
alloc_field (object, id_id, alloc_int (event->id));
alloc_field (object, id_type, alloc_int (event->type));
alloc_field (object, id_value, alloc_float (event->axisValue));
alloc_field (object, id_timestamp, alloc_int (event->timestamp));

} else {

Expand All @@ -61,6 +65,7 @@ namespace lime {
eventObject->id = event->id;
eventObject->type = event->type;
eventObject->axisValue = event->axisValue;
eventObject->timestamp = event->timestamp;

}

Expand Down
4 changes: 4 additions & 0 deletions project/src/ui/KeyEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace lime {
static int id_modifier;
static int id_type;
static int id_windowID;
static int id_timestamp;
static bool init = false;


Expand All @@ -21,6 +22,7 @@ namespace lime {
modifier = 0;
type = KEY_DOWN;
windowID = 0;
timestamp = 0;

}

Expand All @@ -37,6 +39,7 @@ namespace lime {
id_modifier = val_id ("modifier");
id_type = val_id ("type");
id_windowID = val_id ("windowID");
id_timestamp = val_id ("timestamp");
init = true;

}
Expand All @@ -47,6 +50,7 @@ namespace lime {
alloc_field (object, id_modifier, alloc_int (event->modifier));
alloc_field (object, id_type, alloc_int (event->type));
alloc_field (object, id_windowID, alloc_int (event->windowID));
alloc_field (object, id_timestamp, alloc_int (event->timestamp));

} else {

Expand Down
11 changes: 9 additions & 2 deletions src/lime/_internal/backend/html5/HTML5Application.hx
Original file line number Diff line number Diff line change
Expand Up @@ -412,21 +412,24 @@ class HTML5Application

var keyCode = cast convertKeyCode(event.keyCode != null ? event.keyCode : event.which);
var modifier = (event.shiftKey ? (KeyModifier.SHIFT) : 0) | (event.ctrlKey ? (KeyModifier.CTRL) : 0) | (event.altKey ? (KeyModifier.ALT) : 0) | (event.metaKey ? (KeyModifier.META) : 0);
var timestamp = haxe.Int64.fromFloat(event.timeStamp);

if (event.type == "keydown")
{
parent.window.onKeyDown.dispatch(keyCode, modifier);
parent.window.onKeyDownPrecise.dispatch(keyCode, modifier, timestamp);

if (parent.window.onKeyDown.canceled && event.cancelable)
if ((parent.window.onKeyDown.canceled || parent.window.onKeyDownPrecise.canceled) && event.cancelable)
{
event.preventDefault();
}
}
else
{
parent.window.onKeyUp.dispatch(keyCode, modifier);
parent.window.onKeyUpPrecise.dispatch(keyCode, modifier, timestamp);

if (parent.window.onKeyUp.canceled && event.cancelable)
if ((parent.window.onKeyUp.canceled || parent.window.onKeyUpPrecise.canceled) && event.cancelable)
{
event.preventDefault();
}
Expand Down Expand Up @@ -615,13 +618,17 @@ class HTML5Application
default: continue;
}

var timestamp = haxe.Int64.fromFloat(js.Browser.window.performance.now());

if (value > 0)
{
gamepad.onButtonDown.dispatch(button);
gamepad.onButtonDownPrecise.dispatch(button, timestamp);
}
else
{
gamepad.onButtonUp.dispatch(button);
gamepad.onButtonUpPrecise.dispatch(button, timestamp);
}
}
}
Expand Down
29 changes: 23 additions & 6 deletions src/lime/_internal/backend/native/NativeApplication.hx
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,24 @@ class NativeApplication
{
case AXIS_MOVE:
var gamepad = Gamepad.devices.get(gamepadEventInfo.id);
if (gamepad != null) gamepad.onAxisMove.dispatch(gamepadEventInfo.axis, gamepadEventInfo.axisValue);
if (gamepad != null) {
gamepad.onAxisMove.dispatch(gamepadEventInfo.axis, gamepadEventInfo.axisValue);
gamepad.onAxisMovePrecise.dispatch(gamepadEventInfo.axis, gamepadEventInfo.axisValue, gamepadEventInfo.timestamp);
}

case BUTTON_DOWN:
var gamepad = Gamepad.devices.get(gamepadEventInfo.id);
if (gamepad != null) gamepad.onButtonDown.dispatch(gamepadEventInfo.button);
if (gamepad != null) {
gamepad.onButtonDown.dispatch(gamepadEventInfo.button);
gamepad.onButtonDownPrecise.dispatch(gamepadEventInfo.button, gamepadEventInfo.timestamp);
}

case BUTTON_UP:
var gamepad = Gamepad.devices.get(gamepadEventInfo.id);
if (gamepad != null) gamepad.onButtonUp.dispatch(gamepadEventInfo.button);
if (gamepad != null) {
gamepad.onButtonUp.dispatch(gamepadEventInfo.button);
gamepad.onButtonUpPrecise.dispatch(gamepadEventInfo.button, gamepadEventInfo.timestamp);
}

case CONNECT:
Gamepad.__connect(gamepadEventInfo.id);
Expand Down Expand Up @@ -696,13 +705,17 @@ class NativeApplication
public var type:GamepadEventType;
public var axisValue:Float;

public function new(type:GamepadEventType = null, id:Int = 0, button:Int = 0, axis:Int = 0, value:Float = 0)
// TODO: This should probably be an Int64
public var timestamp:Int = 0;

public function new(type:GamepadEventType = null, id:Int = 0, button:Int = 0, axis:Int = 0, value:Float = 0, timestamp:Int = 0)
{
this.type = type;
this.id = id;
this.button = button;
this.axis = axis;
this.axisValue = value;
this.timestamp = timestamp;
}

public function clone():GamepadEventInfo
Expand Down Expand Up @@ -762,17 +775,21 @@ class NativeApplication
public var type:KeyEventType;
public var windowID:Int;

public function new(type:KeyEventType = null, windowID:Int = 0, keyCode: Float = 0, modifier:Int = 0)
// TODO: This should probably be an Int64
public var timestamp:Int = 0;

public function new(type:KeyEventType = null, windowID:Int = 0, keyCode: Float = 0, modifier:Int = 0, timestamp:Int = 0)
{
this.type = type;
this.windowID = windowID;
this.keyCode = keyCode;
this.modifier = modifier;
this.timestamp = timestamp;
}

public function clone():KeyEventInfo
{
return new KeyEventInfo(type, windowID, keyCode, modifier);
return new KeyEventInfo(type, windowID, keyCode, modifier, timestamp);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/lime/_internal/backend/native/NativeCFFI.hx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class NativeCFFI

@:cffi private static function lime_sensor_event_manager_register(callback:Dynamic, eventObject:Dynamic):Void;

@:cffi private static function lime_sdl_get_ticks():Int;

@:cffi private static function lime_system_get_allow_screen_timeout():Bool;

@:cffi private static function lime_system_set_allow_screen_timeout(value:Bool):Bool;
Expand Down Expand Up @@ -520,6 +522,7 @@ class NativeCFFI
"lime_render_event_manager_register", "oov", false));
private static var lime_sensor_event_manager_register = new cpp.Callable<cpp.Object->cpp.Object->cpp.Void>(cpp.Prime._loadPrime("lime",
"lime_sensor_event_manager_register", "oov", false));
private static var lime_sdl_get_ticks = new cpp.Callable<Void->Int>(cpp.Prime._loadPrime("lime", "lime_sdl_get_ticks", "i", false));
private static var lime_system_get_allow_screen_timeout = new cpp.Callable<Void->Bool>(cpp.Prime._loadPrime("lime",
"lime_system_get_allow_screen_timeout", "b", false));
private static var lime_system_set_allow_screen_timeout = new cpp.Callable<Bool->Bool>(cpp.Prime._loadPrime("lime",
Expand Down Expand Up @@ -716,6 +719,7 @@ class NativeCFFI
private static var lime_png_decode_file = CFFI.load("lime", "lime_png_decode_file", 3);
private static var lime_render_event_manager_register = CFFI.load("lime", "lime_render_event_manager_register", 2);
private static var lime_sensor_event_manager_register = CFFI.load("lime", "lime_sensor_event_manager_register", 2);
private static var lime_sdl_get_ticks = CFFI.load("lime", "lime_sdl_get_ticks", 0);
private static var lime_system_get_allow_screen_timeout = CFFI.load("lime", "lime_system_get_allow_screen_timeout", 0);
private static var lime_system_set_allow_screen_timeout = CFFI.load("lime", "lime_system_set_allow_screen_timeout", 1);
private static var lime_system_get_device_model = CFFI.load("lime", "lime_system_get_device_model", 0);
Expand Down Expand Up @@ -1164,6 +1168,11 @@ class NativeCFFI
@:hlNative("lime", "hl_sensor_event_manager_register") private static function lime_sensor_event_manager_register(callback:Void->Void,
eventObject:SensorEventInfo):Void {}

@:hlNative("lime", "hl_sdl_get_ticks") private static function lime_sdl_get_ticks():Int
{
return 0;
}

@:hlNative("lime", "hl_system_get_allow_screen_timeout") private static function lime_system_get_allow_screen_timeout():Bool
{
return false;
Expand Down
Loading

0 comments on commit 9821208

Please sign in to comment.