2024-03-11 22:23:02 -05:00
|
|
|
var keycodes = {
|
2024-03-12 09:01:52 -05:00
|
|
|
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",
|
2024-03-11 22:23:02 -05:00
|
|
|
45: "minus",
|
|
|
|
};
|
|
|
|
|
2024-03-12 09:01:52 -05:00
|
|
|
var codekeys = {};
|
|
|
|
for (var code in keycodes)
|
|
|
|
codekeys[keycodes[code]] = code;
|
|
|
|
|
2024-03-11 22:23:02 -05:00
|
|
|
var mod = {
|
|
|
|
shift: 0,
|
|
|
|
ctrl: 0,
|
|
|
|
alt: 0,
|
|
|
|
super: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
released
|
|
|
|
rep
|
|
|
|
pressed
|
|
|
|
pressrep
|
|
|
|
down
|
|
|
|
*/
|
|
|
|
|
|
|
|
function keyname_extd(key)
|
|
|
|
{
|
|
|
|
if (key > 289 && key < 302) {
|
|
|
|
var num = key-289;
|
|
|
|
return `f${num}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key >= 320 && key <= 329) {
|
|
|
|
var num = key-320;
|
|
|
|
return `kp${num}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keycodes[key]) return keycodes[key];
|
|
|
|
if (key >= 32 && key <= 126) return String.fromCharCode(key).lc();
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
prosperon.keys = [];
|
|
|
|
|
|
|
|
function modstr()
|
|
|
|
{
|
|
|
|
var s = "";
|
|
|
|
if (mod.ctrl) s += "C-";
|
|
|
|
if (mod.alt) s += "M-";
|
|
|
|
if (mod.super) s += "S-";
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
prosperon.keydown = function(key, repeat)
|
|
|
|
{
|
|
|
|
prosperon.keys[key] = true;
|
|
|
|
|
|
|
|
if (key == 341 || key == 345)
|
|
|
|
mod.ctrl = 1;
|
|
|
|
else if (key == 342 || key == 346)
|
|
|
|
mod.alt = 1;
|
|
|
|
else if (key == 343 || key == 347)
|
|
|
|
mod.super = 1;
|
|
|
|
else if (key == 340 || key == 344)
|
|
|
|
mod.shift = 1;
|
|
|
|
else {
|
|
|
|
var emacs = modstr() + keyname_extd(key);
|
|
|
|
player[0].raw_input(emacs, "pressrep");
|
|
|
|
if (repeat)
|
|
|
|
player[0].raw_input(emacs, "rep");
|
|
|
|
else
|
|
|
|
player[0].raw_input(emacs, "pressed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prosperon.keyup = function(key)
|
|
|
|
{
|
|
|
|
prosperon.keys[key] = false;
|
|
|
|
if (key == 341 || key == 345)
|
|
|
|
mod.ctrl = 0;
|
|
|
|
else if (key == 342 || key == 346)
|
|
|
|
mod.alt = 0;
|
|
|
|
else if (key == 343 || key == 347)
|
|
|
|
mod.super = 0;
|
|
|
|
else if (key == 340 || key == 344)
|
|
|
|
mod.shift = 0;
|
|
|
|
else {
|
|
|
|
var emacs = modstr() + keyname_extd(key);
|
|
|
|
player[0].raw_input(emacs, "released");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prosperon.droppedfile = function(path)
|
|
|
|
{
|
|
|
|
player[0].raw_input("drop", "pressed", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
var mousepos = [0,0];
|
|
|
|
|
|
|
|
prosperon.textinput = function(){};
|
|
|
|
prosperon.mousemove = function(pos, dx){
|
|
|
|
mousepos = pos;
|
|
|
|
player[0].mouse_input(modstr() + "move", pos, dx);
|
|
|
|
};
|
|
|
|
prosperon.mousescroll = function(dx){
|
|
|
|
player[0].mouse_input(modstr() + "scroll", dx);
|
|
|
|
};
|
|
|
|
prosperon.mousedown = function(b){
|
|
|
|
player[0].raw_input(modstr() + Mouse.button[b], "pressed");
|
|
|
|
};
|
|
|
|
prosperon.mouseup = function(b){
|
|
|
|
player[0].raw_input(modstr() + Mouse.button[b], "released");
|
|
|
|
};
|
|
|
|
|
2023-09-11 17:09:21 -05:00
|
|
|
var Mouse = {
|
2024-03-11 22:23:02 -05:00
|
|
|
screenpos() { return mousepos.slice(); },
|
2024-03-18 08:16:25 -05:00
|
|
|
worldpos() { return window.screen2world(mousepos); },
|
|
|
|
disabled() { input.mouse_mode(1); },
|
|
|
|
normal() { input.mouse_mode(0); },
|
2024-02-25 17:31:48 -06:00
|
|
|
|
|
|
|
mode(m) {
|
|
|
|
if (Mouse.custom[m])
|
2024-03-18 08:16:25 -05:00
|
|
|
input.cursor_img(Mouse.custom[m]);
|
2024-02-25 17:31:48 -06:00
|
|
|
else
|
2024-03-18 08:16:25 -05:00
|
|
|
input.mouse_cursor(m);
|
2024-02-25 17:31:48 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
set_custom_cursor(img, mode) {
|
|
|
|
mode ??= Mouse.cursor.default;
|
|
|
|
if (!img)
|
|
|
|
delete Mouse.custom[mode];
|
|
|
|
else {
|
2024-03-18 08:16:25 -05:00
|
|
|
input.cursor_img(img);
|
2024-02-25 17:31:48 -06:00
|
|
|
Mouse.custom[mode] = img;
|
|
|
|
}
|
|
|
|
},
|
2024-03-11 22:23:02 -05:00
|
|
|
|
|
|
|
button: { /* left, right, middle mouse */
|
|
|
|
0: "lm",
|
|
|
|
1: "rm",
|
|
|
|
2: "mm"
|
|
|
|
},
|
2024-02-25 17:31:48 -06:00
|
|
|
custom:[],
|
|
|
|
cursor: {
|
|
|
|
default: 0,
|
|
|
|
arrow: 1,
|
|
|
|
ibeam: 2,
|
|
|
|
cross: 3,
|
|
|
|
hand: 4,
|
|
|
|
ew: 5,
|
|
|
|
ns: 6,
|
|
|
|
nwse: 7,
|
|
|
|
nesw: 8,
|
|
|
|
resize: 9,
|
|
|
|
no: 10
|
|
|
|
},
|
2023-09-11 17:09:21 -05:00
|
|
|
};
|
|
|
|
|
2023-10-23 08:08:11 -05:00
|
|
|
Mouse.doc = {};
|
|
|
|
Mouse.doc.pos = "The screen position of the mouse.";
|
|
|
|
Mouse.doc.worldpos = "The position in the game world of the mouse.";
|
|
|
|
Mouse.disabled.doc = "Set the mouse to hidden. This locks it to the game and hides it, but still provides movement and click events.";
|
|
|
|
Mouse.normal.doc = "Set the mouse to show again after hiding.";
|
|
|
|
|
2023-09-11 17:09:21 -05:00
|
|
|
var Keys = {
|
2024-03-11 22:23:02 -05:00
|
|
|
down(code) {
|
|
|
|
return prosperon.keys[code];
|
|
|
|
},
|
2023-09-11 17:09:21 -05:00
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
input.state2str = function(state) {
|
2023-09-07 16:46:35 -05:00
|
|
|
if (typeof state === 'string') return state;
|
|
|
|
switch (state) {
|
|
|
|
case 0:
|
|
|
|
return "down";
|
|
|
|
case 1:
|
|
|
|
return "pressed";
|
|
|
|
case 2:
|
|
|
|
return "released";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
input.print_pawn_kbm = function(pawn) {
|
2023-09-07 16:46:35 -05:00
|
|
|
if (!('inputs' in pawn)) return;
|
|
|
|
var str = "";
|
|
|
|
for (var key in pawn.inputs) {
|
2023-09-22 09:44:58 -05:00
|
|
|
if (!pawn.inputs[key].doc) continue;
|
2023-09-07 16:46:35 -05:00
|
|
|
str += `${key} | ${pawn.inputs[key].doc}\n`;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
input.print_md_kbm = function(pawn) {
|
2023-09-07 16:46:35 -05:00
|
|
|
if (!('inputs' in pawn)) return;
|
|
|
|
|
|
|
|
var str = "";
|
|
|
|
str += "|control|description|\n|---|---|\n";
|
|
|
|
|
|
|
|
for (var key in pawn.inputs) {
|
|
|
|
str += `|${key}|${pawn.inputs[key].doc}|`;
|
|
|
|
str += "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
input.has_bind = function(pawn, bind) {
|
2023-09-07 16:46:35 -05:00
|
|
|
return (typeof pawn.inputs?.[bind] === 'function');
|
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
input.action = {
|
2023-09-07 16:46:35 -05:00
|
|
|
add_new(name) {
|
2024-02-25 17:31:48 -06:00
|
|
|
var action = Object.create(input.action);
|
2023-09-07 16:46:35 -05:00
|
|
|
action.name = name;
|
|
|
|
action.inputs = [];
|
|
|
|
this.actions.push(action);
|
|
|
|
|
|
|
|
return action;
|
|
|
|
},
|
|
|
|
actions: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
/* May be a human player; may be an AI player */
|
|
|
|
var Player = {
|
|
|
|
players: [],
|
|
|
|
input(fn, ...args) {
|
|
|
|
this.pawns.forEach(x => x[fn]?.(...args));
|
|
|
|
},
|
|
|
|
|
2023-09-12 17:19:46 -05:00
|
|
|
mouse_input(type, ...args) {
|
2023-09-27 17:40:04 -05:00
|
|
|
for (var pawn of this.pawns.reversed()) {
|
2023-09-12 17:19:46 -05:00
|
|
|
if (typeof pawn.inputs?.mouse?.[type] === 'function') {
|
|
|
|
pawn.inputs.mouse[type].call(pawn,...args);
|
2024-03-11 22:23:02 -05:00
|
|
|
pawn.inputs.post?.call(pawn);
|
|
|
|
if (!pawn.inputs.fallthru)
|
|
|
|
return;
|
2023-09-12 17:19:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-14 12:49:29 -05:00
|
|
|
char_input(c) {
|
2023-09-27 17:40:04 -05:00
|
|
|
for (var pawn of this.pawns.reversed()) {
|
2023-09-14 12:49:29 -05:00
|
|
|
if (typeof pawn.inputs?.char === 'function') {
|
|
|
|
pawn.inputs.char.call(pawn, c);
|
2024-03-11 22:23:02 -05:00
|
|
|
pawn.inputs.post?.call(pawn);
|
|
|
|
if (!pawn.inputs.fallthru)
|
|
|
|
return;
|
2023-09-14 12:49:29 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2023-09-07 16:46:35 -05:00
|
|
|
raw_input(cmd, state, ...args) {
|
2023-09-27 17:40:04 -05:00
|
|
|
for (var pawn of this.pawns.reversed()) {
|
2023-09-07 16:46:35 -05:00
|
|
|
if (typeof pawn.inputs?.any === 'function') {
|
|
|
|
pawn.inputs.any(cmd);
|
2024-03-11 22:23:02 -05:00
|
|
|
|
|
|
|
if (!pawn.inputs.fallthru)
|
2023-10-05 13:33:43 -05:00
|
|
|
return;
|
2023-09-07 16:46:35 -05:00
|
|
|
}
|
2023-10-17 12:22:06 -05:00
|
|
|
|
|
|
|
if (!pawn.inputs?.[cmd]) {
|
2023-11-07 17:24:26 -06:00
|
|
|
if (pawn.inputs?.block) return;
|
2024-03-11 22:23:02 -05:00
|
|
|
continue;
|
2023-10-17 12:22:06 -05:00
|
|
|
}
|
2023-09-07 16:46:35 -05:00
|
|
|
|
2024-03-11 22:23:02 -05:00
|
|
|
var fn = null;
|
2023-09-07 16:46:35 -05:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 'pressed':
|
2024-03-11 22:23:02 -05:00
|
|
|
fn = pawn.inputs[cmd];
|
|
|
|
break;
|
|
|
|
case 'rep':
|
|
|
|
fn = pawn.inputs[cmd].rep ? pawn.inputs[cmd] : null;
|
|
|
|
break;
|
|
|
|
case 'released':
|
|
|
|
fn = pawn.inputs[cmd].released;
|
|
|
|
break;
|
|
|
|
case 'down':
|
|
|
|
fn = pawn.inputs[cmd].down;
|
2023-09-07 16:46:35 -05:00
|
|
|
}
|
|
|
|
|
2023-09-13 16:49:22 -05:00
|
|
|
if (typeof fn === 'function') {
|
2023-09-07 16:46:35 -05:00
|
|
|
fn.call(pawn, ... args);
|
2024-03-11 22:23:02 -05:00
|
|
|
pawn.inputs.post?.call(pawn);
|
2023-09-13 16:49:22 -05:00
|
|
|
}
|
2023-10-11 17:22:41 -05:00
|
|
|
|
2023-11-20 07:49:14 -06:00
|
|
|
switch (state) {
|
|
|
|
case 'released':
|
2024-03-11 22:23:02 -05:00
|
|
|
pawn.inputs.release_post?.call(pawn);
|
|
|
|
break;
|
2023-11-20 07:49:14 -06:00
|
|
|
}
|
|
|
|
|
2023-10-11 17:22:41 -05:00
|
|
|
if (!pawn.inputs.fallthru) return;
|
2023-10-17 12:22:06 -05:00
|
|
|
if (pawn.inputs.block) return;
|
2023-09-07 16:46:35 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
do_uncontrol(pawn) {
|
2023-09-07 16:46:35 -05:00
|
|
|
this.players.forEach(function(p) {
|
|
|
|
p.pawns = p.pawns.filter(x => x !== pawn);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
obj_controlled(obj) {
|
|
|
|
for (var p in Player.players) {
|
|
|
|
if (p.pawns.contains(obj))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2023-10-16 19:59:58 -05:00
|
|
|
print_pawns() {
|
|
|
|
for (var pawn of this.pawns.reversed())
|
2024-03-14 09:33:15 -05:00
|
|
|
say(pawn.toString());
|
2023-10-16 19:59:58 -05:00
|
|
|
},
|
|
|
|
|
2023-09-07 16:46:35 -05:00
|
|
|
create() {
|
|
|
|
var n = Object.create(this);
|
|
|
|
n.pawns = [];
|
|
|
|
n.gamepads = [];
|
|
|
|
this.players.push(n);
|
2024-02-29 13:54:33 -06:00
|
|
|
this[this.players.length-1] = n;
|
2023-09-07 16:46:35 -05:00
|
|
|
return n;
|
|
|
|
},
|
2023-10-23 08:08:11 -05:00
|
|
|
|
|
|
|
pawns: [],
|
|
|
|
|
|
|
|
control(pawn) {
|
|
|
|
this.pawns.push_unique(pawn);
|
|
|
|
},
|
|
|
|
|
|
|
|
uncontrol(pawn) {
|
|
|
|
this.pawns = this.pawns.filter(x => x !== pawn);
|
|
|
|
},
|
2023-09-07 16:46:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < 4; i++) {
|
|
|
|
Player.create();
|
|
|
|
}
|
2023-10-23 08:08:11 -05:00
|
|
|
|
|
|
|
Player.control.doc = "Control a provided object, if the object has an 'inputs' object.";
|
|
|
|
Player.uncontrol.doc = "Uncontrol a previously controlled object.";
|
|
|
|
Player.print_pawns.doc = "Print out a list of the current pawn control stack.";
|
|
|
|
Player.doc = {};
|
|
|
|
Player.doc.players = "A list of current players.";
|
2024-02-27 10:09:15 -06:00
|
|
|
|
2024-02-29 13:54:33 -06:00
|
|
|
var player = Player;
|
|
|
|
|
2024-02-27 10:09:15 -06:00
|
|
|
return {
|
|
|
|
Mouse,
|
|
|
|
Keys,
|
|
|
|
input,
|
|
|
|
Player,
|
2024-03-12 09:01:52 -05:00
|
|
|
player,
|
|
|
|
keycodes,
|
|
|
|
codekeys
|
2024-02-27 10:09:15 -06:00
|
|
|
};
|