2.5: added support for setting RNA properties in keymap item,
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Mon, 15 Dec 2008 13:10:29 +0000 (13:10 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Mon, 15 Dec 2008 13:10:29 +0000 (13:10 +0000)
which will then be set when the operator is called, example:

kmi= WM_keymap_add_item(keymap, "ED_SCR_OT_region_split", SKEY, KM_PRESS, 0, 0);
RNA_enum_set(kmi->ptr, "dir", 'h');

kmi= WM_keymap_add_item(keymap, "ED_SCR_OT_region_split", SKEY, KM_PRESS, KM_SHIFT, 0);
RNA_enum_set(kmi->ptr, "dir", 'v');

There is a hack I had to do here, since properties are defined
as member of wmOperator, will try to fix later, committing now
so it can be used already.

source/blender/editors/screen/screen_ops.c
source/blender/makesdna/DNA_windowmanager_types.h
source/blender/makesrna/intern/rna_wm.c
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm.c
source/blender/windowmanager/intern/wm_event_system.c
source/blender/windowmanager/intern/wm_keymap.c

index a818c47bbbdbc657df30767b33395a1c06afb518..1f7563b4824173de5588ec6d86149f84223103bb 100644 (file)
@@ -1226,13 +1226,6 @@ static int region_split_exec(bContext *C, wmOperator *op)
 
 static int region_split_invoke(bContext *C, wmOperator *op, wmEvent *evt)
 {
-       
-       /* can't do menu, so event is checked manually */
-       if(evt->shift)
-               RNA_enum_set(op->ptr, "dir", 'v');
-       else
-               RNA_enum_set(op->ptr, "dir", 'h');
-
        return region_split_exec(C, op);
 }
 
@@ -1374,6 +1367,7 @@ void ED_operatortypes_screen(void)
 void ED_keymap_screen(wmWindowManager *wm)
 {
        ListBase *keymap= WM_keymap_listbase(wm, "Screen", 0, 0);
+       wmKeymapItem *kmi;
        
        WM_keymap_verify_item(keymap, "ED_SCR_OT_cursor_type", MOUSEMOVE, 0, 0, 0);
        WM_keymap_verify_item(keymap, "ED_SCR_OT_actionzone", LEFTMOUSE, KM_PRESS, 0, 0);
@@ -1384,8 +1378,10 @@ void ED_keymap_screen(wmWindowManager *wm)
        WM_keymap_verify_item(keymap, "ED_SCR_OT_area_rip", RKEY, KM_PRESS, KM_ALT, 0);
 
         /* tests */
-       WM_keymap_add_item(keymap, "ED_SCR_OT_region_split", SKEY, KM_PRESS, 0, 0);
-       WM_keymap_add_item(keymap, "ED_SCR_OT_region_split", SKEY, KM_PRESS, KM_SHIFT, 0);
+       kmi= WM_keymap_add_item(keymap, "ED_SCR_OT_region_split", SKEY, KM_PRESS, 0, 0);
+       RNA_enum_set(kmi->ptr, "dir", 'h');
+       kmi= WM_keymap_add_item(keymap, "ED_SCR_OT_region_split", SKEY, KM_PRESS, KM_SHIFT, 0);
+       RNA_enum_set(kmi->ptr, "dir", 'v');
        WM_keymap_add_item(keymap, "ED_SCR_OT_region_flip", F5KEY, KM_PRESS, 0, 0);
        WM_keymap_verify_item(keymap, "ED_SCR_OT_repeat_last", F4KEY, KM_PRESS, 0, 0);
 }
index 867007032b6ca3388514e65d062ee4a4877c0a1b..e50aaee3c0ea70bbdea8625b37f0db4d6f3550d2 100644 (file)
@@ -142,7 +142,10 @@ typedef struct wmOperatorType {
 typedef struct wmKeymapItem {
        struct wmKeymapItem *next, *prev;
        
+       /* these are same order as wmOperator because RNA depends on it, fixme XXX */
        char idname[64];                                /* used to retrieve operator type pointer */
+       IDProperty *properties;                 /* default operator properties */
+       struct PointerRNA *ptr;                 /* rna pointer to access properties */
        
        short type;                                             /* event code itself */
        short val;                                              /* 0=any, 1=click, 2=release, or wheelvalue, or... */
@@ -170,18 +173,15 @@ typedef struct wmKeyMap {
 /* operator + operatortype should be able to redo entirely, but for different contextes */
 typedef struct wmOperator {
        struct wmOperator *next, *prev;
-       
-       wmOperatorType *type;
-       char idname[64];                /* used to retrieve type pointer */
-       
-       /* custom storage, only while operator runs, not saved */
-       void *customdata; 
-       /* IDproperty list */
-       IDProperty *properties;
 
+       /* saved */
+       char idname[64];                        /* used to retrieve type pointer */
+       IDProperty *properties;         /* saved, user-settable properties */
+       
        /* runtime */
-       struct PointerRNA *ptr;
-       ListBase *modallist;
+       wmOperatorType *type;           /* operator type definition from idname */
+       void *customdata;                       /* custom storage, only while operator runs */
+       struct PointerRNA *ptr;         /* rna pointer to access properties */
 } wmOperator;
 
 /* operator type exec(), invoke() modal(), return values */
index 0361cff164cb2d6bfad732f5876fabd25450ce87..11dfcb6a21007058495fb0a1c09a9af3bb665d28 100644 (file)
@@ -56,18 +56,10 @@ static int rna_Operator_name_length(PointerRNA *ptr)
 static void rna_def_operator(BlenderRNA *brna)
 {
        StructRNA *srna;
-       PropertyRNA *prop;
 
        srna= RNA_def_struct(brna, "Operator", NULL, "Operator");
        RNA_def_struct_sdna(srna, "wmOperator");
        RNA_def_struct_funcs(srna, NULL, "rna_Operator_refine");
-
-       prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
-       RNA_def_property_flag(prop, PROP_NOT_EDITABLE);
-       RNA_def_property_ui_text(prop, "Name", "Operator name.");
-       RNA_def_property_string_funcs(prop, "rna_Operator_name_get", "rna_Operator_name_length", NULL);
-
-       RNA_def_struct_name_property(srna, prop);
 }
 
 static void rna_def_windowmanager(BlenderRNA *brna)
index fd9e05fa107c09668e2563f2e75c91783353d6e6..b278f83706a566291ea240f1192bafe4c2abc612 100644 (file)
@@ -67,7 +67,6 @@ wmKeymapItem *WM_keymap_verify_item(ListBase *lb, char *idname, short type,
                                                                 short val, int modifier, short keymodifier);
 wmKeymapItem *WM_keymap_add_item       (ListBase *lb, char *idname, short type, 
                                                                 short val, int modifier, short keymodifier);
-void           WM_keymap_property_set(wmKeymapItem *km, const char *propname, const char *propval);
 ListBase       *WM_keymap_listbase     (wmWindowManager *wm, const char *nameid, 
                                                                 int spaceid, int regionid);
 
@@ -105,7 +104,6 @@ int                 WM_operator_winactive   (struct bContext *C);
 wmOperatorType *WM_operatortype_find(const char *idname);
 void           WM_operatortype_append  (void (*opfunc)(wmOperatorType*));
 
-int                    WM_operator_invoke              (struct bContext *C, wmOperatorType *ot, struct wmEvent *event);
 int         WM_operator_call           (struct bContext *C, const char *opstring);
 int         WM_operator_call_rwin      (struct bContext *C, const char *opstring);
 
index 9e0408097d9d434742c6f3f6d1523b1f39689774..d37857ebfa0f08a7bd38b08edc6243fce831d0ba 100644 (file)
@@ -132,6 +132,7 @@ void wm_close_and_free(bContext *C, wmWindowManager *wm)
        wmOperator *op;
        wmReport *report;
        wmKeyMap *km;
+       wmKeymapItem *kmi;
        
        while((win= wm->windows.first)) {
                BLI_remlink(&wm->windows, win);
@@ -149,6 +150,16 @@ void wm_close_and_free(bContext *C, wmWindowManager *wm)
        }
 
        while((km= wm->keymaps.first)) {
+               for(kmi=km->keymap.first; kmi; kmi=kmi->next) {
+                       if(kmi->ptr)
+                               MEM_freeN(kmi->ptr);
+
+                       if(kmi->properties) {
+                               IDP_FreeProperty(kmi->properties);
+                               MEM_freeN(kmi->properties);
+                       }
+               }
+
                BLI_freelistN(&km->keymap);
                BLI_remlink(&wm->keymaps, km);
                MEM_freeN(km);
index 100bdbc6d2b26dcb09f3d7ee866de91153ccb68f..e418110ce175a4280ca203eb5997ec3172c2e1a3 100644 (file)
@@ -41,6 +41,7 @@
 #include "BLI_blenlib.h"
 
 #include "BKE_blender.h"
+#include "BKE_idprop.h"
 #include "BKE_global.h"
 
 #include "ED_screen.h"
@@ -275,13 +276,16 @@ void wm_draw_update(bContext *C)
 /* ********************* operators ******************* */
 
 
-int WM_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event)
+static int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, IDProperty *properties)
 {
        int retval= OPERATOR_PASS_THROUGH;
 
        if(ot->poll==NULL || ot->poll(C)) {
                wmOperator *op= MEM_callocN(sizeof(wmOperator), "wmOperator");
 
+               if(properties)
+                       op->properties= IDP_CopyProperty(properties);
+
                /* XXX adding new operator could be function, only happens here now */
                op->type= ot;
                BLI_strncpy(op->idname, ot->idname, OP_MAX_TYPENAME);
@@ -324,7 +328,7 @@ int WM_operator_call_rwin(bContext *C, const char *opstring)
                                C->region= ar1;
                }
                
-               retval= WM_operator_invoke(C, ot, C->window->eventstate);
+               retval= wm_operator_invoke(C, ot, C->window->eventstate, NULL);
                
                /* set region back */
                C->region= ar;
@@ -342,7 +346,7 @@ int WM_operator_call(bContext *C, const char *opstring)
 
        /* dummie test */
        if(ot && C && C->window) {
-               return WM_operator_invoke(C, ot, C->window->eventstate);
+               return wm_operator_invoke(C, ot, C->window->eventstate, NULL);
        }
        
        return 0;
@@ -420,7 +424,7 @@ static int wm_eventmatch(wmEvent *winevent, wmKeymapItem *kmi)
 }
 
 /* Warning: this function removes a modal handler, when finished */
-static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHandler *handler, wmEvent *event)
+static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHandler *handler, wmEvent *event, IDProperty *properties)
 {
        int retval= OPERATOR_PASS_THROUGH;
        
@@ -471,7 +475,7 @@ static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHand
                wmOperatorType *ot= WM_operatortype_find(event->keymap_idname);
 
                if(ot)
-                       retval= WM_operator_invoke(C, ot, event);
+                       retval= wm_operator_invoke(C, ot, event, properties);
        }
 
        if(retval & OPERATOR_PASS_THROUGH)
@@ -550,7 +554,7 @@ static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers)
                                                
                                                event->keymap_idname= kmi->idname;      /* weak, but allows interactive callback to not use rawkey */
                                                
-                                               action= wm_handler_operator_call(C, handlers, handler, event);
+                                               action= wm_handler_operator_call(C, handlers, handler, event, kmi->properties);
                                                if(action==WM_HANDLER_BREAK)  /* not wm_event_always_pass(event) here, it denotes removed handler */
                                                        break;
                                        }
@@ -561,7 +565,7 @@ static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers)
                        }
                        else {
                                /* modal, swallows all */
-                               action= wm_handler_operator_call(C, handlers, handler, event);
+                               action= wm_handler_operator_call(C, handlers, handler, event, NULL);
                        }
 
                        if(!wm_event_always_pass(event) && action==WM_HANDLER_BREAK)
index f01ca55c31999dff203844c4eecd405d0583c3d6..0e962408b553bbc647d6bc4a4baaa547548d2bec 100644 (file)
@@ -39,6 +39,9 @@
 #include "BKE_library.h"
 #include "BKE_main.h"
 
+#include "RNA_access.h"
+#include "RNA_types.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 #include "wm_window.h"
@@ -47,7 +50,7 @@
 
 /* ***************** generic call, exported **************** */
 
-static void keymap_set(wmKeymapItem *kmi, short type, short val, int modifier, short keymodifier)
+static void keymap_event_set(wmKeymapItem *kmi, short type, short val, int modifier, short keymodifier)
 {
        kmi->type= type;
        kmi->val= val;
@@ -71,6 +74,20 @@ static void keymap_set(wmKeymapItem *kmi, short type, short val, int modifier, s
                kmi->oskey= 2;  
 }
 
+static void keymap_properties_set(wmKeymapItem *kmi)
+{
+       wmOperatorType *ot;
+       
+       if(!kmi->ptr) {
+               ot= WM_operatortype_find(kmi->idname);
+
+               if(ot) {
+                       kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
+                       RNA_pointer_create(NULL, NULL, ot->srna, kmi, kmi->ptr);
+               }
+       }
+}
+
 /* if item was added, then bail out */
 wmKeymapItem *WM_keymap_verify_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
 {
@@ -85,7 +102,8 @@ wmKeymapItem *WM_keymap_verify_item(ListBase *lb, char *idname, short type, shor
                BLI_addtail(lb, kmi);
                BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
                
-               keymap_set(kmi, type, val, modifier, keymodifier);
+               keymap_event_set(kmi, type, val, modifier, keymodifier);
+               keymap_properties_set(kmi);
        }
        return kmi;
 }
@@ -104,7 +122,8 @@ wmKeymapItem *WM_keymap_set_item(ListBase *lb, char *idname, short type, short v
                BLI_addtail(lb, kmi);
                BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
        }
-       keymap_set(kmi, type, val, modifier, keymodifier);
+       keymap_event_set(kmi, type, val, modifier, keymodifier);
+       keymap_properties_set(kmi);
        return kmi;
 }
 
@@ -116,15 +135,11 @@ wmKeymapItem *WM_keymap_add_item(ListBase *lb, char *idname, short type, short v
        BLI_addtail(lb, kmi);
        BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
 
-       keymap_set(kmi, type, val, modifier, keymodifier);
+       keymap_event_set(kmi, type, val, modifier, keymodifier);
+       keymap_properties_set(kmi);
        return kmi;
 }
 
-void WM_keymap_property_set(wmKeymapItem *km, const char *propname, const char *propval)
-{
-       /* todo */
-}
-
 /* ****************** storage in WM ************ */
 
 /* name id's are for storing general or multiple keymaps,