Add touch events, clipboard, virtual keyboard

This commit is contained in:
John Alanbrook 2024-03-12 09:01:52 -05:00
parent 4843981527
commit 3e8bbdbeb2
8 changed files with 136 additions and 29 deletions

View file

@ -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 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.`; ^ 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/input.js");
global.mixin("scripts/std.js"); global.mixin("scripts/std.js");
console.level = 1; console.level = 1;

View file

@ -1,22 +1,26 @@
var keycodes = { var keycodes = {
0x00E: "back", 259: "back",
0x00F: "tab", 258: "tab",
0x01C: "enter", 257: "enter",
0x001: "esc", 1: "escape",
0x039: "space", 32: "space",
0x149: "pgup", 266: "pgup",
0x151: "pgdown", 267: "pgdown",
0x14F: "end", 268: "home",
0x147: "home", 269: "end",
0x14B: "left", 263: "left",
0x148: "up", 265: "up",
0x14D: "right", 262: "right",
0x150: "down", 265: "down",
0x152: "insert", 260: "insert",
0x153: "delete", 261: "delete",
45: "minus", 45: "minus",
}; };
var codekeys = {};
for (var code in keycodes)
codekeys[keycodes[code]] = code;
var mod = { var mod = {
shift: 0, shift: 0,
ctrl: 0, ctrl: 0,
@ -63,6 +67,7 @@ function modstr()
prosperon.keydown = function(key, repeat) prosperon.keydown = function(key, repeat)
{ {
prosperon.keys[key] = true; prosperon.keys[key] = true;
if (key == 341 || key == 345) if (key == 341 || key == 345)
@ -232,6 +237,13 @@ input.action = {
actions: [], 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 */ /* May be a human player; may be an AI player */
var Player = { var Player = {
players: [], players: [],
@ -364,5 +376,7 @@ return {
Keys, Keys,
input, input,
Player, Player,
player player,
keycodes,
codekeys
}; };

View file

@ -1,11 +1,21 @@
os.cwd.doc = "Get the absolute path of the current working directory."; os.cwd.doc = "Get the absolute path of the current working directory.";
os.env.doc = "Return the value of the environment variable v."; os.env.doc = "Return the value of the environment variable v.";
os.platform = "steam"; os.platform = "steam";
if (os.sys === 'windows') if (os.sys() === 'windows')
os.user = os.env("USERNAME"); os.user = os.env("USERNAME");
else else
os.user = os.env("USER"); 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 = {}; var steam = {};
steam.appid = 480; steam.appid = 480;
steam.userid = 8437843; steam.userid = 8437843;

View file

@ -10,12 +10,14 @@
#include "resources.h" #include "resources.h"
#include "jsffi.h" #include "jsffi.h"
static struct callee pawn_callee;
static struct callee gamepad_callee;
void input_dropped_files(int n) 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) void cursor_img(const char *path)
{ {
/* NSString *str = [NSString stringWithUTF8String:path]; /* NSdesting *dest = [NSdesting destingWithUTF8desting:path];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:str]; NSImage *img = [[NSImage alloc] initWithContentsOfFile:dest];
NSCursor *custom = [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(0,0)]; NSCursor *custom = [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(0,0)];
[custom set]; [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));
}

View file

@ -4,13 +4,19 @@
#include "script.h" #include "script.h"
#include <stdint.h> #include <stdint.h>
#include "HandmadeMath.h" #include "HandmadeMath.h"
#include "sokol_app.h"
void cursor_hide(); void cursor_hide();
void cursor_show(); void cursor_show();
void cursor_img(const char *path); void cursor_img(const char *path);
void set_mouse_mode(int mousemode); 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_dropped_files(int n);
void input_clipboard_paste(char *str);
const char *keyname_extd(int key); const char *keyname_extd(int key);

View file

@ -1401,10 +1401,16 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 234: case 234:
ret = emitter2js(make_emitter()); ret = emitter2js(make_emitter());
break; break;
case 248:
ret = bool2js(sapp_keyboard_shown());
break;
case 249: case 249:
str = JS_ToCString(js,argv[2]); str = JS_ToCString(js,argv[2]);
js2emitter(argv[1])->texture = texture_pullfromfile(str); js2emitter(argv[1])->texture = texture_pullfromfile(str);
break; break;
case 250:
sapp_show_keyboard(js2bool(argv[1]));
break;
case 251: case 251:
js2gameobject(argv[1])->warp_filter = js2bitmask(argv[2]); js2gameobject(argv[1])->warp_filter = js2bitmask(argv[2]);
break; break;

View file

@ -31,24 +31,24 @@ void window_resize(int width, int height)
mainwin.pheight = mainwin.rheight; mainwin.pheight = mainwin.rheight;
mainwin.pwidth = mainwin.rwidth*aspect/raspect; mainwin.pwidth = mainwin.rwidth*aspect/raspect;
JSValue vals[2] = { int2js(width), int2js(height) }; script_evalf("prosperon.resize([%d,%d]);", width,height);
send_signal("window_resize", 2, vals);
} }
void window_focused(int focus) void window_focused(int focus)
{ {
mainwin.focus = focus; mainwin.focus = focus;
script_evalf("prosperon.focus(%d);", focus);
} }
void window_iconified(int s) void window_iconified(int s)
{ {
mainwin.iconified = s; mainwin.iconified = s;
script_evalf("prosperon.iconified(%d);", s);
} }
void window_suspended(int s) void window_suspended(int s)
{ {
script_evalf("prosperon.suspended(%d);", s);
} }
void window_set_icon(const char *png) { void window_set_icon(const char *png) {

View file

@ -232,9 +232,30 @@ void c_event(const sapp_event *e)
break; break;
case SAPP_EVENTTYPE_FILES_DROPPED: 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()); input_dropped_files(sapp_get_num_dropped_files());
break; 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: default:
break; break;
} }