From 3e8bbdbeb26e452b5ca685f0dd71da9ec5f4fc45 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 12 Mar 2024 09:01:52 -0500 Subject: [PATCH] Add touch events, clipboard, virtual keyboard --- scripts/engine.js | 11 +++++++++ scripts/input.js | 46 ++++++++++++++++++++++++------------- scripts/std.js | 12 +++++++++- source/engine/input.c | 51 +++++++++++++++++++++++++++++++++++++----- source/engine/input.h | 6 +++++ source/engine/jsffi.c | 6 +++++ source/engine/window.c | 10 ++++----- source/engine/yugine.c | 23 ++++++++++++++++++- 8 files changed, 136 insertions(+), 29 deletions(-) diff --git a/scripts/engine.js b/scripts/engine.js index d571935..405b4ac 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -215,6 +215,17 @@ Range is given by a semantic versioning number, prefixed with nothing, a ~, or a ~ means that MAJOR and MINOR must match exactly, but any PATCH greater or equal is valid. ^ means that MAJOR must match exactly, but any MINOR and PATCH greater or equal is valid.`; +prosperon.iconified = function(icon) {}; +prosperon.focus = function(focus) {}; +prosperon.resize = function(dimensions) {}; +prosperon.suspended = function(sus) {}; +prosperon.mouseenter = function(){}; +prosperon.mouseleave = function(){}; +prosperon.touchpress = function(touches){}; +prosperon.touchrelease = function(touches){}; +prosperon.touchmove = function(touches){}; +prosperon.clipboardpaste = function(str){}; + global.mixin("scripts/input.js"); global.mixin("scripts/std.js"); console.level = 1; diff --git a/scripts/input.js b/scripts/input.js index 63705f6..01fb988 100644 --- a/scripts/input.js +++ b/scripts/input.js @@ -1,22 +1,26 @@ var keycodes = { - 0x00E: "back", - 0x00F: "tab", - 0x01C: "enter", - 0x001: "esc", - 0x039: "space", - 0x149: "pgup", - 0x151: "pgdown", - 0x14F: "end", - 0x147: "home", - 0x14B: "left", - 0x148: "up", - 0x14D: "right", - 0x150: "down", - 0x152: "insert", - 0x153: "delete", + 259: "back", + 258: "tab", + 257: "enter", + 1: "escape", + 32: "space", + 266: "pgup", + 267: "pgdown", + 268: "home", + 269: "end", + 263: "left", + 265: "up", + 262: "right", + 265: "down", + 260: "insert", + 261: "delete", 45: "minus", }; +var codekeys = {}; +for (var code in keycodes) + codekeys[keycodes[code]] = code; + var mod = { shift: 0, ctrl: 0, @@ -63,6 +67,7 @@ function modstr() prosperon.keydown = function(key, repeat) { + prosperon.keys[key] = true; if (key == 341 || key == 345) @@ -232,6 +237,13 @@ input.action = { actions: [], }; +input.keyboard_show = function(show) +{ + cmd(250,show); +} + +input.keyboard_shown = function() { return cmd(248); } + /* May be a human player; may be an AI player */ var Player = { players: [], @@ -364,5 +376,7 @@ return { Keys, input, Player, - player + player, + keycodes, + codekeys }; diff --git a/scripts/std.js b/scripts/std.js index 9b0f46c..bbba334 100644 --- a/scripts/std.js +++ b/scripts/std.js @@ -1,11 +1,21 @@ os.cwd.doc = "Get the absolute path of the current working directory."; os.env.doc = "Return the value of the environment variable v."; os.platform = "steam"; -if (os.sys === 'windows') +if (os.sys() === 'windows') os.user = os.env("USERNAME"); else os.user = os.env("USER"); + + +var appy = {}; +appy.inputs = {}; +if (os.sys() === 'macos') { + appy.inputs['S-q'] = function() { Game.quit(); }; + appy.inputs['S-h'] = function() { }; +} +player[0].control(appy); + var steam = {}; steam.appid = 480; steam.userid = 8437843; diff --git a/source/engine/input.c b/source/engine/input.c index ac27c25..64f310c 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -10,12 +10,14 @@ #include "resources.h" #include "jsffi.h" -static struct callee pawn_callee; -static struct callee gamepad_callee; - void input_dropped_files(int n) { - script_evalf("prosperon.droppedfile('%s');", sapp_get_dropped_file_path(0)); + script_evalf("prosperon.droppedfile(`%s`);", sapp_get_dropped_file_path(0)); +} + +void input_clipboard_paste(char *str) +{ + script_evalf("prosperon.clipboardpaste(`%s`);", sapp_get_clipboard_string()); } /* @@ -29,9 +31,46 @@ void cursor_show() { sapp_show_mouse(1); } void cursor_img(const char *path) { -/* NSString *str = [NSString stringWithUTF8String:path]; - NSImage *img = [[NSImage alloc] initWithContentsOfFile:str]; +/* NSdesting *dest = [NSdesting destingWithUTF8desting:path]; + NSImage *img = [[NSImage alloc] initWithContentsOfFile:dest]; NSCursor *custom = [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(0,0)]; [custom set]; */ } + +static char *touch_jstrn(char *dest, int len, sapp_touchpoint *touch, int n) +{ + dest[0] = 0; + char touchdest[512] = {0}; + strncat(dest,"[", 512); + for (int i = 0; i < n; i++) { + snprintf(touchdest, 512, "{id:%p, x:%g, y:%g},", touch[i].identifier, touch[i].pos_x, touch[i].pos_y); + strncat(dest,touchdest,512); + } + strncat(dest,"]", 512); + return dest; +} + +void touch_start(sapp_touchpoint *touch, int n) +{ + char dest[512] = {0}; + script_evalf("prosperon.touchpress(%s);", touch_jstrn(dest, 512, touch, n)); +} + +void touch_move(sapp_touchpoint *touch, int n) +{ + char dest[512] = {0}; + script_evalf("prosperon.touchmove(%s);", touch_jstrn(dest,512,touch,n)); +} + +void touch_end(sapp_touchpoint *touch, int n) +{ + char dest[512] = {0}; + script_evalf("prosperon.touchend(%s);", touch_jstrn(dest,512,touch,n)); +} + +void touch_cancelled(sapp_touchpoint *touch, int n) +{ + char dest[512] = {0}; + script_evalf("prosperon.touchend(%s);", touch_jstrn(dest,512,touch,n)); +} diff --git a/source/engine/input.h b/source/engine/input.h index 361ad9b..30229be 100644 --- a/source/engine/input.h +++ b/source/engine/input.h @@ -4,13 +4,19 @@ #include "script.h" #include #include "HandmadeMath.h" +#include "sokol_app.h" void cursor_hide(); void cursor_show(); void cursor_img(const char *path); void set_mouse_mode(int mousemode); +void touch_start(sapp_touchpoint *touch, int n); +void touch_move(sapp_touchpoint *touch, int n); +void touch_end(sapp_touchpoint *touch, int n); +void touch_cancelled(sapp_touchpoint *touch, int n); void input_dropped_files(int n); +void input_clipboard_paste(char *str); const char *keyname_extd(int key); diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index 3fe48db..f056fc1 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -1401,10 +1401,16 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 234: ret = emitter2js(make_emitter()); break; + case 248: + ret = bool2js(sapp_keyboard_shown()); + break; case 249: str = JS_ToCString(js,argv[2]); js2emitter(argv[1])->texture = texture_pullfromfile(str); break; + case 250: + sapp_show_keyboard(js2bool(argv[1])); + break; case 251: js2gameobject(argv[1])->warp_filter = js2bitmask(argv[2]); break; diff --git a/source/engine/window.c b/source/engine/window.c index 9714526..63d7a79 100644 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -30,25 +30,25 @@ void window_resize(int width, int height) float raspect = mainwin.rwidth/mainwin.rheight; mainwin.pheight = mainwin.rheight; mainwin.pwidth = mainwin.rwidth*aspect/raspect; - - JSValue vals[2] = { int2js(width), int2js(height) }; - send_signal("window_resize", 2, vals); + + script_evalf("prosperon.resize([%d,%d]);", width,height); } void window_focused(int focus) { mainwin.focus = focus; + script_evalf("prosperon.focus(%d);", focus); } void window_iconified(int s) { mainwin.iconified = s; + script_evalf("prosperon.iconified(%d);", s); } - void window_suspended(int s) { - + script_evalf("prosperon.suspended(%d);", s); } void window_set_icon(const char *png) { diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 6e7730d..df79855 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -232,9 +232,30 @@ void c_event(const sapp_event *e) break; case SAPP_EVENTTYPE_FILES_DROPPED: -// input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy, e->modifiers); input_dropped_files(sapp_get_num_dropped_files()); break; + + case SAPP_EVENTTYPE_MOUSE_ENTER: + script_evalf("prosperon.mouseenter();"); + break; + case SAPP_EVENTTYPE_MOUSE_LEAVE: + script_evalf("prosperon.mouseleave();"); + break; + case SAPP_EVENTTYPE_TOUCHES_BEGAN: + touch_start(e->touches, e->num_touches); + break; + case SAPP_EVENTTYPE_TOUCHES_MOVED: + touch_move(e->touches, e->num_touches); + break; + case SAPP_EVENTTYPE_TOUCHES_ENDED: + touch_end(e->touches, e->num_touches); + break; + case SAPP_EVENTTYPE_TOUCHES_CANCELLED: + touch_cancelled(e->touches, e->num_touches); + break; + case SAPP_EVENTTYPE_CLIPBOARD_PASTED: + input_clipboard_paste(sapp_get_clipboard_string()); + break; default: break; }