Files
armorpaint/base/sources/operator.ts
T

78 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-03-09 14:52:33 +01:00
2024-05-03 21:29:39 +02:00
let operator_ops: map_t<string, ()=>void> = map_create();
function operator_register(name: string, call: ()=>void) {
2024-03-20 16:13:54 +01:00
map_set(operator_ops, name, call);
2024-03-09 14:52:33 +01:00
}
function operator_run(name: string) {
2024-03-20 16:13:54 +01:00
if (map_get(operator_ops, name) != null) {
2024-05-03 21:29:39 +02:00
let cb: ()=>void = map_get(operator_ops, name);
cb();
2024-03-09 14:52:33 +01:00
}
}
function operator_update() {
if (mouse_started_any() || keyboard_started_any()) {
2024-04-12 12:51:40 +02:00
let keys: string[] = map_keys(config_keymap);
2024-04-08 17:40:32 +02:00
for (let i: i32 = 0; i < keys.length; ++i) {
let op: string = keys[i];
if (operator_shortcut(map_get(config_keymap, op))) {
2024-03-09 14:52:33 +01:00
operator_run(op);
}
}
}
}
2024-03-22 14:17:49 +01:00
function operator_shortcut(s: string, type: shortcut_type_t = shortcut_type_t.STARTED): bool {
2024-03-09 14:52:33 +01:00
if (s == "") {
return false;
}
2024-03-20 16:13:54 +01:00
let shift: bool = string_index_of(s, "shift") >= 0;
let ctrl: bool = string_index_of(s, "ctrl") >= 0;
let alt: bool = string_index_of(s, "alt") >= 0;
2024-03-09 14:52:33 +01:00
let flag: bool = shift == keyboard_down("shift") && ctrl == keyboard_down("control") && alt == keyboard_down("alt");
2024-03-20 16:13:54 +01:00
if (string_index_of(s, "+") > 0) {
s = substring(s, string_last_index_of(s, "+") + 1, s.length);
2024-03-09 14:52:33 +01:00
if (s == "number") {
return flag;
}
}
else if (shift || ctrl || alt) {
return flag;
}
2024-09-24 16:06:49 +02:00
let key: bool = false;
if (s == "left" || s == "right" || s == "middle") {
if (type == shortcut_type_t.DOWN) {
key = mouse_down(s);
}
else {
key = mouse_started(s);
}
}
else if (type == shortcut_type_t.REPEAT) {
key = keyboard_repeat(s);
}
else if (type == shortcut_type_t.DOWN) {
key = keyboard_down(s);
}
else if (type == shortcut_type_t.RELEASED) {
key = keyboard_released(s);
}
else {
key = keyboard_started(s);
}
2024-03-09 14:52:33 +01:00
return flag && key;
}
enum shortcut_type_t {
STARTED,
REPEAT,
DOWN,
RELEASED,
}