WM: add utility to lookup a keymap item from an event
authorCampbell Barton <ideasman42@gmail.com>
Sun, 27 Oct 2019 17:53:44 +0000 (04:53 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 27 Oct 2019 17:59:35 +0000 (04:59 +1100)
Useful for modal operators to be able to match events
against regular keymaps.

source/blender/makesrna/intern/rna_wm_api.c
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_event_system.c

index 886258ee45fbeef383f1a1140bb7df09f63a95bb..1330250edab310cf1691dff4b90212df4bc52879 100644 (file)
@@ -366,6 +366,14 @@ static PointerRNA rna_KeyMap_item_find_from_operator(ID *id,
   return kmi_ptr;
 }
 
+static PointerRNA rna_KeyMap_item_match_event(ID *id, wmKeyMap *km, bContext *C, wmEvent *event)
+{
+  wmKeyMapItem *kmi = WM_event_match_keymap_item(C, km, event);
+  PointerRNA kmi_ptr;
+  RNA_pointer_create(id, &RNA_KeyMapItem, kmi, &kmi_ptr);
+  return kmi_ptr;
+}
+
 static wmKeyMap *rna_keymap_new(
     wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid, bool modal, bool tool)
 {
@@ -1128,6 +1136,14 @@ void RNA_api_keymapitems(StructRNA *srna)
   parm = RNA_def_pointer(func, "item", "KeyMapItem", "", "");
   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
   RNA_def_function_return(func, parm);
+
+  func = RNA_def_function(srna, "match_event", "rna_KeyMap_item_match_event");
+  RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_CONTEXT);
+  parm = RNA_def_pointer(func, "event", "Event", "", "");
+  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+  parm = RNA_def_pointer(func, "item", "KeyMapItem", "", "");
+  RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
+  RNA_def_function_return(func, parm);
 }
 
 void RNA_api_keymaps(StructRNA *srna)
index f8b6b5171daeeab6b2f1ef2feec911c855fe80ee..f7abfaf565ea3ddad74a9d942b9b4cc5f18d5390 100644 (file)
@@ -243,6 +243,10 @@ void WM_event_set_keymap_handler_post_callback(struct wmEventHandler_Keymap *han
 wmKeyMap *WM_event_get_keymap_from_handler(wmWindowManager *wm,
                                            struct wmEventHandler_Keymap *handler);
 
+wmKeyMapItem *WM_event_match_keymap_item(struct bContext *C,
+                                         wmKeyMap *keymap,
+                                         const struct wmEvent *event);
+
 typedef int (*wmUIHandlerFunc)(struct bContext *C, const struct wmEvent *event, void *userdata);
 typedef void (*wmUIHandlerRemoveFunc)(struct bContext *C, void *userdata);
 
index 1dd37dd28432e8d82c0ecf9ecdc37f221d8a8114..f52e958c4ca5f81930388a0f48fb6262b5361cfe 100644 (file)
@@ -5002,6 +5002,19 @@ wmKeyMap *WM_event_get_keymap_from_handler(wmWindowManager *wm, wmEventHandler_K
   return keymap;
 }
 
+wmKeyMapItem *WM_event_match_keymap_item(bContext *C, wmKeyMap *keymap, const wmEvent *event)
+{
+  for (wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
+    if (wm_eventmatch(event, kmi)) {
+      wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
+      if (WM_operator_poll_context(C, ot, WM_OP_INVOKE_DEFAULT)) {
+        return kmi;
+      }
+    }
+  }
+  return NULL;
+}
+
 static wmKeyMapItem *wm_kmi_from_event(bContext *C,
                                        wmWindowManager *wm,
                                        ListBase *handlers,
@@ -5017,13 +5030,9 @@ static wmKeyMapItem *wm_kmi_from_event(bContext *C,
         wmEventHandler_Keymap *handler = (wmEventHandler_Keymap *)handler_base;
         wmKeyMap *keymap = WM_event_get_keymap_from_handler(wm, handler);
         if (keymap && WM_keymap_poll(C, keymap)) {
-          for (wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
-            if (wm_eventmatch(event, kmi)) {
-              wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
-              if (WM_operator_poll_context(C, ot, WM_OP_INVOKE_DEFAULT)) {
-                return kmi;
-              }
-            }
+          wmKeyMapItem *kmi = WM_event_match_keymap_item(C, keymap, event);
+          if (kmi != NULL) {
+            return kmi;
           }
         }
       }