code cleanup: use const events for modal and invoke operators.
[blender.git] / source / blender / editors / mesh / editmesh_loopcut.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Joseph Eagar, Joshua Leung
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/mesh/editmesh_loopcut.c
28  *  \ingroup edmesh
29  */
30
31 #include "DNA_object_types.h"
32 #include "DNA_scene_types.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_array.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39
40 #include "BLF_translation.h"
41
42 #include "BKE_context.h"
43 #include "BKE_modifier.h"
44 #include "BKE_report.h"
45 #include "BKE_tessmesh.h"
46
47 #include "BIF_gl.h"
48
49 #include "ED_screen.h"
50 #include "ED_space_api.h"
51 #include "ED_view3d.h"
52 #include "ED_mesh.h"
53 #include "ED_numinput.h"
54
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "mesh_intern.h"
62
63 /* ringsel operator */
64
65 /* struct for properties used while drawing */
66 typedef struct RingSelOpData {
67         ARegion *ar;        /* region that ringsel was activated in */
68         void *draw_handle;  /* for drawing preview loop */
69         
70         float (*edges)[2][3];
71         int totedge;
72
73         ViewContext vc;
74
75         Object *ob;
76         BMEditMesh *em;
77         BMEdge *eed;
78         NumInput num;
79
80         int extend;
81         int do_cut;
82 } RingSelOpData;
83
84 /* modal loop selection drawing callback */
85 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
86 {
87         View3D *v3d = CTX_wm_view3d(C);
88         RingSelOpData *lcd = arg;
89         int i;
90         
91         if (lcd->totedge > 0) {
92                 if (v3d && v3d->zbuf)
93                         glDisable(GL_DEPTH_TEST);
94
95                 glPushMatrix();
96                 glMultMatrixf(lcd->ob->obmat);
97
98                 glColor3ub(255, 0, 255);
99                 glBegin(GL_LINES);
100                 for (i = 0; i < lcd->totedge; i++) {
101                         glVertex3fv(lcd->edges[i][0]);
102                         glVertex3fv(lcd->edges[i][1]);
103                 }
104                 glEnd();
105
106                 glPopMatrix();
107                 if (v3d && v3d->zbuf)
108                         glEnable(GL_DEPTH_TEST);
109         }
110 }
111
112 /* given two opposite edges in a face, finds the ordering of their vertices so
113  * that cut preview lines won't cross each other */
114 static void edgering_find_order(BMEdge *lasteed, BMEdge *eed,
115                                 BMVert *lastv1, BMVert *v[2][2])
116 {
117         BMIter liter;
118         BMLoop *l, *l2;
119         int rev;
120
121         l = eed->l;
122
123         /* find correct order for v[1] */
124         if (!(BM_edge_in_face(l->f, eed) && BM_edge_in_face(l->f, lasteed))) {
125                 BM_ITER_ELEM (l, &liter, l, BM_LOOPS_OF_LOOP) {
126                         if (BM_edge_in_face(l->f, eed) && BM_edge_in_face(l->f, lasteed))
127                                 break;
128                 }
129         }
130         
131         /* this should never happen */
132         if (!l) {
133                 v[0][0] = eed->v1;
134                 v[0][1] = eed->v2;
135                 v[1][0] = lasteed->v1;
136                 v[1][1] = lasteed->v2;
137                 return;
138         }
139         
140         l2 = BM_face_other_edge_loop(l->f, l->e, eed->v1);
141         rev = (l2 == l->prev);
142         while (l2->v != lasteed->v1 && l2->v != lasteed->v2) {
143                 l2 = rev ? l2->prev : l2->next;
144         }
145
146         if (l2->v == lastv1) {
147                 v[0][0] = eed->v1;
148                 v[0][1] = eed->v2;
149         }
150         else {
151                 v[0][0] = eed->v2;
152                 v[0][1] = eed->v1;
153         }
154 }
155
156 static void edgering_sel(RingSelOpData *lcd, int previewlines, int select)
157 {
158         BMEditMesh *em = lcd->em;
159         BMEdge *eed_start = lcd->eed;
160         BMEdge *eed, *eed_last;
161         BMVert *v[2][2], *v_last;
162         BMWalker walker;
163         float (*edges)[2][3] = NULL;
164         BLI_array_declare(edges);
165         int i, tot = 0;
166         
167         memset(v, 0, sizeof(v));
168         
169         if (!eed_start)
170                 return;
171
172         if (lcd->edges) {
173                 MEM_freeN(lcd->edges);
174                 lcd->edges = NULL;
175                 lcd->totedge = 0;
176         }
177
178         if (!lcd->extend) {
179                 EDBM_flag_disable_all(lcd->em, BM_ELEM_SELECT);
180         }
181
182         if (select) {
183                 BMW_init(&walker, em->bm, BMW_EDGERING,
184                          BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
185                          BMW_FLAG_TEST_HIDDEN,
186                          BMW_NIL_LAY);
187
188                 for (eed = BMW_begin(&walker, eed_start); eed; eed = BMW_step(&walker)) {
189                         BM_edge_select_set(em->bm, eed, TRUE);
190                 }
191                 BMW_end(&walker);
192
193                 return;
194         }
195
196         BMW_init(&walker, em->bm, BMW_EDGERING,
197                  BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
198                  BMW_FLAG_TEST_HIDDEN,
199                  BMW_NIL_LAY);
200
201         v_last   = NULL;
202         eed_last = NULL;
203
204         for (eed = eed_start = BMW_begin(&walker, eed_start); eed; eed = BMW_step(&walker)) {
205                 if (eed_last) {
206                         if (v_last) {
207                                 v[1][0] = v[0][0];
208                                 v[1][1] = v[0][1];
209                         }
210                         else {
211                                 v[1][0] = eed_last->v1;
212                                 v[1][1] = eed_last->v2;
213                                 v_last  = eed_last->v1;
214                         }
215
216                         edgering_find_order(eed_last, eed, v_last, v);
217                         v_last = v[0][0];
218
219                         BLI_array_grow_items(edges, previewlines);
220
221                         for (i = 1; i <= previewlines; i++) {
222                                 const float fac = (i / ((float)previewlines + 1));
223                                 interp_v3_v3v3(edges[tot][0], v[0][0]->co, v[0][1]->co, fac);
224                                 interp_v3_v3v3(edges[tot][1], v[1][0]->co, v[1][1]->co, fac);
225                                 tot++;
226                         }
227                 }
228                 eed_last = eed;
229         }
230         
231 #ifdef BMW_EDGERING_NGON
232         if (lasteed != startedge && BM_edge_share_face_check(lasteed, startedge)) {
233 #else
234         if (eed_last != eed_start && BM_edge_share_quad_check(eed_last, eed_start)) {
235 #endif
236                 v[1][0] = v[0][0];
237                 v[1][1] = v[0][1];
238
239                 edgering_find_order(eed_last, eed_start, v_last, v);
240                 
241                 BLI_array_grow_items(edges, previewlines);
242
243                 for (i = 1; i <= previewlines; i++) {
244                         const float fac = (i / ((float)previewlines + 1));
245
246                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1]) {
247                                 continue;
248                         }
249
250                         interp_v3_v3v3(edges[tot][0], v[0][0]->co, v[0][1]->co, fac);
251                         interp_v3_v3v3(edges[tot][1], v[1][0]->co, v[1][1]->co, fac);
252                         tot++;
253                 }
254         }
255
256         BMW_end(&walker);
257         lcd->edges = edges;
258         lcd->totedge = tot;
259 }
260
261 static void ringsel_find_edge(RingSelOpData *lcd, int cuts)
262 {
263         if (lcd->eed) {
264                 edgering_sel(lcd, cuts, 0);
265         }
266         else if (lcd->edges) {
267                 MEM_freeN(lcd->edges);
268                 lcd->edges = NULL;
269                 lcd->totedge = 0;
270         }
271 }
272
273 static void ringsel_finish(bContext *C, wmOperator *op)
274 {
275         RingSelOpData *lcd = op->customdata;
276         const int cuts = RNA_int_get(op->ptr, "number_cuts");
277         const float smoothness = 0.292f * RNA_float_get(op->ptr, "smoothness");
278 #ifdef BMW_EDGERING_NGON
279         const int use_only_quads = FALSE;
280 #else
281         const int use_only_quads = TRUE;
282 #endif
283
284         if (lcd->eed) {
285                 BMEditMesh *em = lcd->em;
286
287                 edgering_sel(lcd, cuts, 1);
288                 
289                 if (lcd->do_cut) {
290                         /* Enable gridfill, so that intersecting loopcut works as one would expect.
291                          * Note though that it will break edgeslide in this specific case.
292                          * See [#31939]. */
293                         BM_mesh_esubdivide(em->bm, BM_ELEM_SELECT,
294                                            smoothness, 0.0f, 0.0f,
295                                            cuts,
296                                            SUBDIV_SELECT_LOOPCUT, SUBD_PATH, 0, TRUE,
297                                            use_only_quads, 0);
298
299                         /* tessface is already re-recalculated */
300                         EDBM_update_generic(em, FALSE, TRUE);
301
302                         /* force edge slide to edge select mode in in face select mode */
303                         if (em->selectmode & SCE_SELECT_FACE) {
304                                 if (em->selectmode == SCE_SELECT_FACE)
305                                         em->selectmode = SCE_SELECT_EDGE;
306                                 else
307                                         em->selectmode &= ~SCE_SELECT_FACE;
308                                 CTX_data_tool_settings(C)->selectmode = em->selectmode;
309                                 EDBM_selectmode_set(em);
310
311                                 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, CTX_data_scene(C));
312                         }
313                         else {
314                                 EDBM_selectmode_flush(lcd->em);
315                         }
316                 }
317                 else {
318                         /* XXX Is this piece of code ever used now? Simple loop select is now
319                          *     in editmesh_select.c (around line 1000)... */
320                         /* sets as active, useful for other tools */
321                         if (em->selectmode & SCE_SELECT_VERTEX)
322                                 BM_select_history_store(em->bm, lcd->eed->v1);  /* low priority TODO, get vertrex close to mouse */
323                         if (em->selectmode & SCE_SELECT_EDGE)
324                                 BM_select_history_store(em->bm, lcd->eed);
325                         
326                         EDBM_selectmode_flush(lcd->em);
327                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, lcd->ob->data);
328                 }
329         }
330 }
331
332 /* called when modal loop selection is done... */
333 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
334 {
335         RingSelOpData *lcd = op->customdata;
336
337         /* deactivate the extra drawing stuff in 3D-View */
338         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
339         
340         if (lcd->edges)
341                 MEM_freeN(lcd->edges);
342
343         ED_region_tag_redraw(lcd->ar);
344
345         /* free the custom data */
346         MEM_freeN(lcd);
347         op->customdata = NULL;
348 }
349
350
351 /* called when modal loop selection gets set up... */
352 static int ringsel_init(bContext *C, wmOperator *op, int do_cut)
353 {
354         RingSelOpData *lcd;
355
356         /* alloc new customdata */
357         lcd = op->customdata = MEM_callocN(sizeof(RingSelOpData), "ringsel Modal Op Data");
358         
359         /* assign the drawing handle for drawing preview line... */
360         lcd->ar = CTX_wm_region(C);
361         lcd->draw_handle = ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
362         lcd->ob = CTX_data_edit_object(C);
363         lcd->em = BMEdit_FromObject(lcd->ob);
364         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
365         lcd->do_cut = do_cut;
366         
367         initNumInput(&lcd->num);
368         lcd->num.idx_max = 0;
369         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
370
371         /* XXX, temp, workaround for [# ] */
372         EDBM_mesh_ensure_valid_dm_hack(CTX_data_scene(C), lcd->em);
373
374         em_setup_viewcontext(C, &lcd->vc);
375
376         ED_region_tag_redraw(lcd->ar);
377
378         return 1;
379 }
380
381 static int ringcut_cancel(bContext *C, wmOperator *op)
382 {
383         /* this is just a wrapper around exit() */
384         ringsel_exit(C, op);
385         return OPERATOR_CANCELLED;
386 }
387
388 static int ringcut_invoke(bContext *C, wmOperator *op, const wmEvent *event)
389 {
390         ScrArea *sa = CTX_wm_area(C);
391         Object *obedit = CTX_data_edit_object(C);
392         RingSelOpData *lcd;
393         BMEdge *edge;
394         float dist = 75.0f;
395
396         if (modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
397                 BKE_report(op->reports, RPT_WARNING, "Loop cut does not work well on deformed edit mesh display");
398         
399         view3d_operator_needs_opengl(C);
400
401         if (!ringsel_init(C, op, 1))
402                 return OPERATOR_CANCELLED;
403         
404         /* add a modal handler for this operator - handles loop selection */
405         WM_event_add_modal_handler(C, op);
406
407         lcd = op->customdata;
408         copy_v2_v2_int(lcd->vc.mval, event->mval);
409         
410         edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
411         if (edge != lcd->eed) {
412                 lcd->eed = edge;
413                 ringsel_find_edge(lcd, 1);
414         }
415         ED_area_headerprint(sa, IFACE_("Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts, "
416                                        "hold Alt for smooth"));
417         
418         return OPERATOR_RUNNING_MODAL;
419 }
420
421 static int loopcut_modal(bContext *C, wmOperator *op, const wmEvent *event)
422 {
423         float smoothness = RNA_float_get(op->ptr, "smoothness");
424         int cuts = RNA_int_get(op->ptr, "number_cuts");
425         RingSelOpData *lcd = op->customdata;
426         int show_cuts = 0;
427
428         view3d_operator_needs_opengl(C);
429
430         switch (event->type) {
431                 case RETKEY:
432                 case PADENTER:
433                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
434                         if (event->val == KM_PRESS) {
435                                 /* finish */
436                                 ED_region_tag_redraw(lcd->ar);
437                                 
438                                 ringsel_finish(C, op);
439                                 ringsel_exit(C, op);
440                                 
441                                 ED_area_headerprint(CTX_wm_area(C), NULL);
442                                 
443                                 return OPERATOR_FINISHED;
444                         }
445                         
446                         ED_region_tag_redraw(lcd->ar);
447                         break;
448                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
449                         ED_region_tag_redraw(lcd->ar);
450                         ringsel_exit(C, op);
451                         ED_area_headerprint(CTX_wm_area(C), NULL);
452
453                         return OPERATOR_FINISHED;
454                 case ESCKEY:
455                         if (event->val == KM_RELEASE) {
456                                 /* cancel */
457                                 ED_region_tag_redraw(lcd->ar);
458                                 ED_area_headerprint(CTX_wm_area(C), NULL);
459                                 
460                                 return ringcut_cancel(C, op);
461                         }
462                         
463                         ED_region_tag_redraw(lcd->ar);
464                         break;
465                 case PADPLUSKEY:
466                 case PAGEUPKEY:
467                 case WHEELUPMOUSE:  /* change number of cuts */
468                         if (event->val == KM_RELEASE)
469                                 break;
470                         if (event->alt == 0) {
471                                 cuts++;
472                                 RNA_int_set(op->ptr, "number_cuts", cuts);
473                                 ringsel_find_edge(lcd, cuts);
474                                 show_cuts = TRUE;
475                         }
476                         else {
477                                 smoothness = min_ff(smoothness + 0.05f, 4.0f);
478                                 RNA_float_set(op->ptr, "smoothness", smoothness);
479                                 show_cuts = TRUE;
480                         }
481                         
482                         ED_region_tag_redraw(lcd->ar);
483                         break;
484                 case PADMINUS:
485                 case PAGEDOWNKEY:
486                 case WHEELDOWNMOUSE:  /* change number of cuts */
487                         if (event->val == KM_RELEASE)
488                                 break;
489
490                         if (event->alt == 0) {
491                                 cuts = max_ii(cuts - 1, 0);
492                                 RNA_int_set(op->ptr, "number_cuts", cuts);
493                                 ringsel_find_edge(lcd, cuts);
494                                 show_cuts = TRUE;
495                         }
496                         else {
497                                 smoothness = max_ff(smoothness - 0.05f, 0.0f);
498                                 RNA_float_set(op->ptr, "smoothness", smoothness);
499                                 show_cuts = TRUE;
500                         }
501                         
502                         ED_region_tag_redraw(lcd->ar);
503                         break;
504                 case MOUSEMOVE:  /* mouse moved somewhere to select another loop */
505                 {
506                         float dist = 75.0f;
507                         BMEdge *edge;
508
509                         lcd->vc.mval[0] = event->mval[0];
510                         lcd->vc.mval[1] = event->mval[1];
511                         edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
512
513                         if (edge != lcd->eed) {
514                                 lcd->eed = edge;
515                                 ringsel_find_edge(lcd, cuts);
516                         }
517
518                         ED_region_tag_redraw(lcd->ar);
519                         break;
520                 }
521         }
522         
523         /* using the keyboard to input the number of cuts */
524         if (event->val == KM_PRESS) {
525                 /* init as zero so backspace clears */
526                 
527                 if (handleNumInput(&lcd->num, event)) {
528                         float value = RNA_int_get(op->ptr, "number_cuts");
529                         applyNumInput(&lcd->num, &value);
530                         
531                         /* allow zero so you can backspace and type in a value
532                          * otherwise 1 as minimum would make more sense */
533                         cuts = CLAMPIS(value, 0, 130);
534                         
535                         RNA_int_set(op->ptr, "number_cuts", cuts);
536                         ringsel_find_edge(lcd, cuts);
537                         show_cuts = TRUE;
538                         
539                         ED_region_tag_redraw(lcd->ar);
540                 }
541         }
542         
543         if (show_cuts) {
544                 char buf[64];
545                 BLI_snprintf(buf, sizeof(buf), IFACE_("Number of Cuts: %d, Smooth: %.2f (Alt)"), cuts, smoothness);
546                 ED_area_headerprint(CTX_wm_area(C), buf);
547         }
548         
549         /* keep going until the user confirms */
550         return OPERATOR_RUNNING_MODAL;
551 }
552
553 /* for bmesh this tool is in bmesh_select.c */
554 #if 0
555
556 void MESH_OT_edgering_select(wmOperatorType *ot)
557 {
558         /* description */
559         ot->name = "Edge Ring Select";
560         ot->idname = "MESH_OT_edgering_select";
561         ot->description = "Select an edge ring";
562         
563         /* callbacks */
564         ot->invoke = ringsel_invoke;
565         ot->poll = ED_operator_editmesh_region_view3d; 
566         
567         /* flags */
568         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
569
570         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
571 }
572
573 #endif
574
575 void MESH_OT_loopcut(wmOperatorType *ot)
576 {
577         PropertyRNA *prop;
578
579         /* description */
580         ot->name = "Loop Cut";
581         ot->idname = "MESH_OT_loopcut";
582         ot->description = "Add a new loop between existing loops";
583         
584         /* callbacks */
585         ot->invoke = ringcut_invoke;
586         ot->modal = loopcut_modal;
587         ot->cancel = ringcut_cancel;
588         ot->poll = ED_operator_editmesh_region_view3d;
589         
590         /* flags */
591         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
592
593         /* properties */
594         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
595         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
596         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
597
598         prop = RNA_def_float(ot->srna, "smoothness", 0.0f, 0.0f, FLT_MAX, "Smoothness", "Smoothness factor", 0.0f, 4.0f);
599         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
600 }