=bmesh=
authorJoseph Eagar <joeedh@gmail.com>
Mon, 9 May 2011 21:38:55 +0000 (21:38 +0000)
committerJoseph Eagar <joeedh@gmail.com>
Mon, 9 May 2011 21:38:55 +0000 (21:38 +0000)
added modal keymap for knife tool.
I'm not sure how to handle the panning
pass-through, it's still hardcoded to
happen on MIDDLEMOUSE.

source/blender/editors/mesh/knifetool.c
source/blender/editors/mesh/mesh_intern.h
source/blender/editors/mesh/mesh_ops.c
source/blender/editors/transform/transform.c

index 0315fa805067792e9acf4beecd9dcdbc1209a681..e010498391a444678b3140560541b86c5b7314bb 100755 (executable)
@@ -1750,6 +1750,61 @@ static int knifetool_invoke (bContext *C, wmOperator *op, wmEvent *evt)
        return OPERATOR_RUNNING_MODAL;
 }
 
+enum {
+       KNF_MODAL_CANCEL=1,
+       KNF_MODAL_CONFIRM,
+       KNF_MODAL_MIDPOINT_ON,
+       KNF_MODAL_MIDPOINT_OFF,
+       KNF_MODAL_NEW_CUT,
+       KNF_MODEL_IGNORE_SNAP_ON,
+       KNF_MODEL_IGNORE_SNAP_OFF,
+       KNF_MODAL_ADD_CUT,
+};
+
+wmKeyMap* knifetool_modal_keymap(wmKeyConfig *keyconf)
+{
+       static EnumPropertyItem modal_items[] = {
+       {KNF_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
+       {KNF_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
+       {KNF_MODAL_MIDPOINT_ON, "SNAP_MIDPOINTS_ON", 0, "Snap To Midpoints On", ""},
+       {KNF_MODAL_MIDPOINT_OFF, "SNAP_MIDPOINTS_OFF", 0, "Snap To Midpoints Off", ""},
+       {KNF_MODEL_IGNORE_SNAP_ON, "IGNORE_SNAP_ON", 0, "Ignore Snapping On", ""},
+       {KNF_MODEL_IGNORE_SNAP_OFF, "IGNORE_SNAP_OFF", 0, "Ignore Snapping Off", ""},
+       {KNF_MODAL_NEW_CUT, "NEW_CUT", 0, "End Current Cut", ""},
+       {KNF_MODAL_ADD_CUT, "ADD_CUT", 0, "Add Cut", ""},
+
+       {0, NULL, 0, NULL, NULL}};
+       
+       wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Knife Tool Modal Map");
+       
+       /* this function is called for each spacetype, only needs to add map once */
+       if(keymap) return NULL;
+       
+       keymap= WM_modalkeymap_add(keyconf, "Transform Modal Map", modal_items);
+       
+       /* items for modal map */
+       WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, KNF_MODAL_CONFIRM);
+       WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, KNF_MODAL_ADD_CUT);
+       WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_PRESS, KM_ANY, 0, KNF_MODAL_CONFIRM);
+       WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, KNF_MODAL_CONFIRM);
+       WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, KNF_MODAL_CONFIRM);
+       WM_modalkeymap_add_item(keymap, EKEY, KM_PRESS, 0, 0, KNF_MODAL_NEW_CUT);
+
+       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, KNF_MODAL_MIDPOINT_ON);
+       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, KNF_MODAL_MIDPOINT_OFF);
+       WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_PRESS, KM_ANY, 0, KNF_MODAL_MIDPOINT_ON);
+       WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_RELEASE, KM_ANY, 0, KNF_MODAL_MIDPOINT_OFF);
+
+       WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, KNF_MODEL_IGNORE_SNAP_ON);
+       WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, KNF_MODEL_IGNORE_SNAP_OFF);
+       WM_modalkeymap_add_item(keymap, RIGHTSHIFTKEY, KM_PRESS, KM_ANY, 0, KNF_MODEL_IGNORE_SNAP_ON);
+       WM_modalkeymap_add_item(keymap, RIGHTSHIFTKEY, KM_RELEASE, KM_ANY, 0, KNF_MODEL_IGNORE_SNAP_OFF);
+       
+       WM_modalkeymap_assign(keymap, "MESH_OT_knifetool");
+       
+       return keymap;
+}
+
 static int knifetool_modal (bContext *C, wmOperator *op, wmEvent *event)
 {
        Object *obedit;
@@ -1768,84 +1823,91 @@ static int knifetool_modal (bContext *C, wmOperator *op, wmEvent *event)
        if (kcd->mode == MODE_PANNING)
                kcd->mode = kcd->prevmode;
        
-       kcd->snap_midpoints = event->ctrl;
-       kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = event->shift;
-       
-       switch (event->type) {
-               case ESCKEY:
-               case RETKEY: /* confirm */ // XXX hardcoded
-                       if (event->val == KM_RELEASE) {
-                               if (kcd->mode == MODE_DRAGGING && event->type == ESCKEY) {
-                                       kcd->mode = MODE_IDLE;
-                                       ED_region_tag_redraw(kcd->ar);                                  
-                               } else {                                
-                                       /* finish */
-                                       ED_region_tag_redraw(kcd->ar);
-                                       
-                                       knifetool_finish(C, op);
-                                       knifetool_exit(C, op);
-                                       
-                                       return OPERATOR_FINISHED;
-                               }
-                       }
-                       
-                       ED_region_tag_redraw(kcd->ar);
-                       return OPERATOR_RUNNING_MODAL;
-               
-               case WHEELUPMOUSE:
-               case WHEELDOWNMOUSE:
-                       return OPERATOR_PASS_THROUGH;
-               case MIDDLEMOUSE:
-                       if (event->val != KM_RELEASE) {
-                               if (kcd->mode != MODE_PANNING)
-                                       kcd->prevmode = kcd->mode;
-                               kcd->mode = MODE_PANNING;
-                       } else {
-                               kcd->mode = kcd->prevmode;
-                       }
-                       
-                       ED_region_tag_redraw(kcd->ar);
-                       return OPERATOR_PASS_THROUGH;
-                       
-               case LEFTMOUSE:
-                       knife_recalc_projmat(kcd);
-                       if (event->val != KM_RELEASE)
+       /* handle modal keymap */
+       if (event->type == EVT_MODAL_MAP) {
+               switch (event->val) {
+                       case KNF_MODAL_CANCEL:
+                               /* finish */
+                               ED_region_tag_redraw(kcd->ar);
+                               
+                               knifetool_exit(C, op);
+                               
+                               return OPERATOR_CANCELLED;
+                       case KNF_MODAL_CONFIRM:
+                               /* finish */
+                               ED_region_tag_redraw(kcd->ar);
+                               
+                               knifetool_finish(C, op);
+                               knifetool_exit(C, op);
+                               
+                               return OPERATOR_FINISHED;
+                       case KNF_MODAL_MIDPOINT_ON:
+                               ED_region_tag_redraw(kcd->ar);
+                               kcd->snap_midpoints = 1;
                                break;
-                       
-                       if (kcd->mode == MODE_DRAGGING) {
-                               knife_add_cut(kcd);
-                               if (!kcd->extend) {
-                                       knife_finish_cut(kcd);
-                                       kcd->mode = MODE_IDLE;
-                               }
-                       } else if (kcd->mode != MODE_PANNING) {
-                               knife_start_cut(kcd);
-                               kcd->mode = MODE_DRAGGING;
-                       }
-
-                       ED_region_tag_redraw(kcd->ar);
-                       return OPERATOR_RUNNING_MODAL;
-                       
-               case EKEY:
-                       kcd->extend = event->val==KM_RELEASE;
-                       if (event->val == KM_RELEASE) {
+                       case KNF_MODAL_MIDPOINT_OFF:
+                               ED_region_tag_redraw(kcd->ar);
+                               kcd->snap_midpoints = 0;
+                               break;
+                       case KNF_MODEL_IGNORE_SNAP_ON:
+                               ED_region_tag_redraw(kcd->ar);
+                               kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = 1;
+                               break;
+                       case KNF_MODEL_IGNORE_SNAP_OFF:
+                               ED_region_tag_redraw(kcd->ar);
+                               kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = 0;
+                               break;
+                       case KNF_MODAL_NEW_CUT:
+                               ED_region_tag_redraw(kcd->ar);
                                knife_finish_cut(kcd);
                                kcd->mode = MODE_IDLE;
-                       }
-                       return OPERATOR_RUNNING_MODAL;
-               case LEFTCTRLKEY:
-               case RIGHTCTRLKEY:
-               case MOUSEMOVE:  /* mouse moved somewhere to select another loop */
-                       if (kcd->mode != MODE_PANNING) {
+                               break;
+                       case KNF_MODAL_ADD_CUT:
                                knife_recalc_projmat(kcd);
-                               kcd->vc.mval[0] = event->mval[0];
-                               kcd->vc.mval[1] = event->mval[1];
-                               
-                               if (knife_update_active(kcd))                                   
-                                       ED_region_tag_redraw(kcd->ar);
-                       }
 
-                       break;
+                               if (kcd->mode == MODE_DRAGGING) {
+                                       knife_add_cut(kcd);
+                                       if (!kcd->extend) {
+                                               knife_finish_cut(kcd);
+                                               kcd->mode = MODE_IDLE;
+                                       }
+                               } else if (kcd->mode != MODE_PANNING) {
+                                       knife_start_cut(kcd);
+                                       kcd->mode = MODE_DRAGGING;
+                               }
+               
+                               ED_region_tag_redraw(kcd->ar);
+                               break;
+                       }
+       } else { /*non-modal-mapped events*/
+               switch (event->type) {
+                       case WHEELUPMOUSE:
+                       case WHEELDOWNMOUSE:
+                               return OPERATOR_PASS_THROUGH;
+                       case MIDDLEMOUSE:
+                               if (event->val != KM_RELEASE) {
+                                       if (kcd->mode != MODE_PANNING)
+                                               kcd->prevmode = kcd->mode;
+                                       kcd->mode = MODE_PANNING;
+                               } else {
+                                       kcd->mode = kcd->prevmode;
+                               }
+                               
+                               ED_region_tag_redraw(kcd->ar);
+                               return OPERATOR_PASS_THROUGH;
+                               
+                       case MOUSEMOVE:  /* mouse moved somewhere to select another loop */
+                               if (kcd->mode != MODE_PANNING) {
+                                       knife_recalc_projmat(kcd);
+                                       kcd->vc.mval[0] = event->mval[0];
+                                       kcd->vc.mval[1] = event->mval[1];
+                                       
+                                       if (knife_update_active(kcd))                                   
+                                               ED_region_tag_redraw(kcd->ar);
+                               }
+       
+                               break;
+               }
        }
        
        /* keep going until the user confirms */
index 3af641c222d11d7e41ea08b60b7400428e999d3d..8415ecf9410d5da0c7040fc222e0e887e42e3a7f 100644 (file)
@@ -192,6 +192,8 @@ extern struct EditFace *EM_face_from_faces(EditMesh *em, struct EditFace *efa1,
 
 extern int EM_view3d_poll(struct bContext *C);
 
+struct wmKeyMap* knifetool_modal_keymap(struct wmKeyConfig *keyconf);
+
 /* ******************* knifetool.c */
 
 void MESH_OT_knife_cut(struct wmOperatorType *ot);
index 38e57e0233943471b98ef2116e0d17c2a0ee57fa..94c9dc87760f4dd6e238272b17c76859661412bc 100644 (file)
@@ -338,5 +338,6 @@ void ED_keymap_mesh(wmKeyConfig *keyconf)
        }
        
        ED_object_generic_keymap(keyconf, keymap, 3);
+       knifetool_modal_keymap(keyconf);
 }
 
index a9792ebaca3bcb668a4d5d4c8854024d172ce330..c87bdae3029f967ac6e5c4c9a2ff21cab91b0118 100644 (file)
@@ -1577,21 +1577,22 @@ int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int
        {
                wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
                wmKeyMapItem *kmi;
-
-               for (kmi = keymap->items.first; kmi; kmi = kmi->next)
-               {
-                       if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS)
+               
+               if (keymap) {
+                       for (kmi = keymap->items.first; kmi; kmi = kmi->next)
                        {
-                               if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
-                                       (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
-                                       (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
-                                       (kmi->type == OSKEY && event->oskey)) {
-                                       t->modifiers |= MOD_SNAP_INVERT;
+                               if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS)
+                               {
+                                       if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
+                                               (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
+                                               (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
+                                               (kmi->type == OSKEY && event->oskey)) {
+                                               t->modifiers |= MOD_SNAP_INVERT;
+                                       }
+                                       break;
                                }
-                               break;
                        }
                }
-
        }
 
        initSnapping(t, op); // Initialize snapping data AFTER mode flags