Files
armorpaint/paint/sources/operator.c
T

72 lines
1.7 KiB
C
Raw Normal View History

2026-03-09 13:17:15 +01:00
#include "global.h"
2026-03-07 21:12:59 +01:00
void operator_register(char *name, void (*call)(void)) {
2026-06-08 15:35:04 +02:00
any_map_set(g_operators, name, call);
2026-03-06 12:56:38 +01:00
}
2026-03-07 21:12:59 +01:00
void operator_run(char *name) {
2026-06-08 15:35:04 +02:00
if (any_map_get(g_operators, name) != NULL) {
void (*cb)(void) = any_map_get(g_operators, name);
2026-03-06 12:56:38 +01:00
cb();
}
}
void operator_update() {
if (mouse_started_any() || keyboard_started_any()) {
2026-06-08 11:31:49 +02:00
string_array_t *keys = map_keys(g_keymap);
2026-03-06 12:56:38 +01:00
for (i32 i = 0; i < keys->length; ++i) {
2026-03-07 21:12:59 +01:00
char *op = keys->buffer[i];
2026-06-08 11:31:49 +02:00
if (operator_shortcut(any_map_get(g_keymap, op), SHORTCUT_TYPE_STARTED)) {
2026-03-06 12:56:38 +01:00
operator_run(op);
}
}
2026-08-21 22:35:41 +02:00
array_free(keys);
free(keys);
2026-03-06 12:56:38 +01:00
}
}
2026-03-07 21:12:59 +01:00
bool operator_shortcut(char *s, shortcut_type_t type) {
2026-05-05 17:49:31 +02:00
if (string_equals(s, "") || g_config->workspace == WORKSPACE_PLAYER) {
2026-03-06 12:56:38 +01:00
return false;
}
bool shift = string_index_of(s, "shift") >= 0;
bool ctrl = string_index_of(s, "ctrl") >= 0;
bool alt = string_index_of(s, "alt") >= 0;
bool flag = shift == keyboard_down("shift") && ctrl == keyboard_down("control") && alt == keyboard_down("alt");
if (string_index_of(s, "+") > 0) {
2026-08-21 22:35:41 +02:00
s = s + string_last_index_of(s, "+") + 1;
2026-03-06 12:56:38 +01:00
if (string_equals(s, "number")) {
return flag;
}
}
else if (shift || ctrl || alt) {
return flag;
}
bool key = false;
if (string_equals(s, "left") || string_equals(s, "right") || string_equals(s, "middle")) {
if (type == SHORTCUT_TYPE_DOWN) {
key = mouse_down(s);
}
else {
key = mouse_started(s);
}
}
else if (type == SHORTCUT_TYPE_REPEAT) {
key = keyboard_repeat(s);
}
else if (type == SHORTCUT_TYPE_DOWN) {
key = keyboard_down(s);
}
else if (type == SHORTCUT_TYPE_RELEASED) {
key = keyboard_released(s);
}
else {
key = keyboard_started(s);
}
return flag && key;
}