operator.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. let operator_ops: map_t<string, any> = map_create();
  2. function operator_register(name: string, call: any) {
  3. map_set(operator_ops, name, call);
  4. }
  5. function operator_run(name: string) {
  6. if (map_get(operator_ops, name) != null) {
  7. map_get(operator_ops, name)();
  8. }
  9. }
  10. function operator_update() {
  11. if (mouse_started_any() || keyboard_started_any()) {
  12. let keys: string[] = map_keys(config_keymap);
  13. for (let i: i32 = 0; i < keys.length; ++i) {
  14. let op: string = keys[i];
  15. if (operator_shortcut(map_get(config_keymap, op))) {
  16. operator_run(op);
  17. }
  18. }
  19. }
  20. }
  21. function operator_shortcut(s: string, type: shortcut_type_t = shortcut_type_t.STARTED): bool {
  22. if (s == "") {
  23. return false;
  24. }
  25. let shift: bool = string_index_of(s, "shift") >= 0;
  26. let ctrl: bool = string_index_of(s, "ctrl") >= 0;
  27. let alt: bool = string_index_of(s, "alt") >= 0;
  28. let flag: bool = shift == keyboard_down("shift") && ctrl == keyboard_down("control") && alt == keyboard_down("alt");
  29. if (string_index_of(s, "+") > 0) {
  30. s = substring(s, string_last_index_of(s, "+") + 1, s.length);
  31. if (s == "number") {
  32. return flag;
  33. }
  34. }
  35. else if (shift || ctrl || alt) {
  36. return flag;
  37. }
  38. let key: bool = (s == "left" || s == "right" || s == "middle") ?
  39. // Mouse
  40. (type == shortcut_type_t.DOWN ? mouse_down(s) : mouse_started(s)) :
  41. // Keyboard
  42. (type == shortcut_type_t.REPEAT ? keyboard_repeat(s) : type == shortcut_type_t.DOWN ? keyboard_down(s) :
  43. type == shortcut_type_t.RELEASED ? keyboard_released(s) : keyboard_started(s));
  44. return flag && key;
  45. }
  46. enum shortcut_type_t {
  47. STARTED,
  48. REPEAT,
  49. DOWN,
  50. RELEASED,
  51. }