Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create event functions for precise input handling. #1802

Open
wants to merge 2 commits into
base: 8.2.0-Dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2648,6 +2648,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 @@ -3997,6 +4009,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 @@ -4069,9 +4082,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 @@ -4185,6 +4198,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
32 changes: 26 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 @@ -254,14 +263,17 @@ class NativeApplication
var int32:Float = keyEventInfo.keyCode;
var keyCode:KeyCode = Std.int(int32);
var modifier:KeyModifier = keyEventInfo.modifier;
var timestamp = keyEventInfo.timestamp;

switch (type)
{
case KEY_DOWN:
window.onKeyDown.dispatch(keyCode, modifier);
window.onKeyDownPrecise.dispatch(keyCode, modifier, timestamp);

case KEY_UP:
window.onKeyUp.dispatch(keyCode, modifier);
window.onKeyUpPrecise.dispatch(keyCode, modifier, timestamp);
}

#if (windows || linux)
Expand Down Expand Up @@ -696,13 +708,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 +778,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
Loading