Merge branch 'master' into blender2.8
[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
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_stack.h"
36 #include "BLI_string.h"
37 #include "BLI_math.h"
38
39 #include "BLT_translation.h"
40
41 #include "BKE_context.h"
42 #include "BKE_modifier.h"
43 #include "BKE_report.h"
44 #include "BKE_editmesh.h"
45 #include "BKE_DerivedMesh.h"
46 #include "BKE_unit.h"
47
48 #include "GPU_immediate.h"
49 #include "GPU_matrix.h"
50
51 #include "UI_interface.h"
52
53 #include "ED_screen.h"
54 #include "ED_space_api.h"
55 #include "ED_view3d.h"
56 #include "ED_mesh.h"
57 #include "ED_numinput.h"
58
59 #include "RNA_access.h"
60 #include "RNA_define.h"
61 #include "RNA_enum_types.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "DEG_depsgraph.h"
67
68 #include "mesh_intern.h"  /* own include */
69
70 #define SUBD_SMOOTH_MAX 4.0f
71 #define SUBD_CUTS_MAX 500
72
73 /* ringsel operator */
74
75 /* struct for properties used while drawing */
76 typedef struct RingSelOpData {
77         ARegion *ar;        /* region that ringsel was activated in */
78         void *draw_handle;  /* for drawing preview loop */
79         
80         float (*edges)[2][3];
81         int totedge;
82
83         float (*points)[3];
84         int totpoint;
85
86         ViewContext vc;
87
88         Object *ob;
89         BMEditMesh *em;
90         BMEdge *eed;
91         NumInput num;
92
93         bool extend;
94         bool do_cut;
95
96         float cuts;         /* cuts as float so smooth mouse pan works in small increments */
97         float smoothness;
98 } RingSelOpData;
99
100 /* modal loop selection drawing callback */
101 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
102 {
103         View3D *v3d = CTX_wm_view3d(C);
104         RingSelOpData *lcd = arg;
105         
106         if ((lcd->totedge > 0) || (lcd->totpoint > 0)) {
107                 if (v3d && v3d->zbuf)
108                         glDisable(GL_DEPTH_TEST);
109
110                 gpuPushMatrix();
111                 gpuMultMatrix(lcd->ob->obmat);
112
113                 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
114
115                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
116                 immUniformColor3ub(255, 0, 255);
117
118                 if (lcd->totedge > 0) {
119                         immBegin(GWN_PRIM_LINES, lcd->totedge * 2);
120
121                         for (int i = 0; i < lcd->totedge; i++) {
122                                 immVertex3fv(pos, lcd->edges[i][0]);
123                                 immVertex3fv(pos, lcd->edges[i][1]);
124                         }
125
126                         immEnd();
127                 }
128
129                 if (lcd->totpoint > 0) {
130                         glPointSize(3.0f);
131
132                         immBegin(GWN_PRIM_POINTS, lcd->totpoint);
133
134                         for (int i = 0; i < lcd->totpoint; i++) {
135                                 immVertex3fv(pos, lcd->points[i]);
136                         }
137
138                         immEnd();
139                 }
140
141                 immUnbindProgram();
142
143                 gpuPopMatrix();
144
145                 if (v3d && v3d->zbuf)
146                         glEnable(GL_DEPTH_TEST);
147         }
148 }
149
150 /* given two opposite edges in a face, finds the ordering of their vertices so
151  * that cut preview lines won't cross each other */
152 static void edgering_find_order(BMEdge *lasteed, BMEdge *eed,
153                                 BMVert *lastv1, BMVert *v[2][2])
154 {
155         BMIter liter;
156         BMLoop *l, *l2;
157         int rev;
158
159         l = eed->l;
160
161         /* find correct order for v[1] */
162         if (!(BM_edge_in_face(eed, l->f) && BM_edge_in_face(lasteed, l->f))) {
163                 BM_ITER_ELEM (l, &liter, l, BM_LOOPS_OF_LOOP) {
164                         if (BM_edge_in_face(eed, l->f) && BM_edge_in_face(lasteed, l->f))
165                                 break;
166                 }
167         }
168         
169         /* this should never happen */
170         if (!l) {
171                 v[0][0] = eed->v1;
172                 v[0][1] = eed->v2;
173                 v[1][0] = lasteed->v1;
174                 v[1][1] = lasteed->v2;
175                 return;
176         }
177         
178         l2 = BM_loop_other_edge_loop(l, eed->v1);
179         rev = (l2 == l->prev);
180         while (l2->v != lasteed->v1 && l2->v != lasteed->v2) {
181                 l2 = rev ? l2->prev : l2->next;
182         }
183
184         if (l2->v == lastv1) {
185                 v[0][0] = eed->v1;
186                 v[0][1] = eed->v2;
187         }
188         else {
189                 v[0][0] = eed->v2;
190                 v[0][1] = eed->v1;
191         }
192 }
193
194 static void edgering_vcos_get(DerivedMesh *dm, BMVert *v[2][2], float r_cos[2][2][3])
195 {
196         if (dm) {
197                 int j, k;
198                 for (j = 0; j < 2; j++) {
199                         for (k = 0; k < 2; k++) {
200                                 dm->getVertCo(dm, BM_elem_index_get(v[j][k]), r_cos[j][k]);
201                         }
202                 }
203         }
204         else {
205                 int j, k;
206                 for (j = 0; j < 2; j++) {
207                         for (k = 0; k < 2; k++) {
208                                 copy_v3_v3(r_cos[j][k], v[j][k]->co);
209                         }
210                 }
211         }
212 }
213
214 static void edgering_vcos_get_pair(DerivedMesh *dm, BMVert *v[2], float r_cos[2][3])
215 {
216         if (dm) {
217                 int j;
218                 for (j = 0; j < 2; j++) {
219                         dm->getVertCo(dm, BM_elem_index_get(v[j]), r_cos[j]);
220                 }
221         }
222         else {
223                 int j;
224                 for (j = 0; j < 2; j++) {
225                         copy_v3_v3(r_cos[j], v[j]->co);
226                 }
227         }
228 }
229
230 static void edgering_preview_free(RingSelOpData *lcd)
231 {
232         MEM_SAFE_FREE(lcd->edges);
233         lcd->totedge = 0;
234
235         MEM_SAFE_FREE(lcd->points);
236         lcd->totpoint = 0;
237 }
238
239 static void edgering_preview_calc_edges(RingSelOpData *lcd, DerivedMesh *dm, const int previewlines)
240 {
241         BMesh *bm = lcd->em->bm;
242         BMWalker walker;
243         BMEdge *eed_start = lcd->eed;
244         BMEdge *eed, *eed_last;
245         BMVert *v[2][2] = {{NULL}}, *v_last;
246         float (*edges)[2][3] = NULL;
247         BLI_Stack *edge_stack;
248
249         int i, tot = 0;
250
251         BMW_init(&walker, bm, BMW_EDGERING,
252                  BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
253                  BMW_FLAG_TEST_HIDDEN,
254                  BMW_NIL_LAY);
255
256
257         edge_stack = BLI_stack_new(sizeof(BMEdge *), __func__);
258
259         eed_last = NULL;
260         for (eed = eed_last = BMW_begin(&walker, lcd->eed); eed; eed = BMW_step(&walker)) {
261                 BLI_stack_push(edge_stack, &eed);
262         }
263         BMW_end(&walker);
264
265
266         eed_start = *(BMEdge **)BLI_stack_peek(edge_stack);
267
268         edges = MEM_mallocN(
269                 (sizeof(*edges) * (BLI_stack_count(edge_stack) + (eed_last != eed_start))) * previewlines, __func__);
270
271         v_last   = NULL;
272         eed_last = NULL;
273
274         while (!BLI_stack_is_empty(edge_stack)) {
275                 BLI_stack_pop(edge_stack, &eed);
276
277                 if (eed_last) {
278                         if (v_last) {
279                                 v[1][0] = v[0][0];
280                                 v[1][1] = v[0][1];
281                         }
282                         else {
283                                 v[1][0] = eed_last->v1;
284                                 v[1][1] = eed_last->v2;
285                                 v_last  = eed_last->v1;
286                         }
287
288                         edgering_find_order(eed_last, eed, v_last, v);
289                         v_last = v[0][0];
290
291                         for (i = 1; i <= previewlines; i++) {
292                                 const float fac = (i / ((float)previewlines + 1));
293                                 float v_cos[2][2][3];
294
295                                 edgering_vcos_get(dm, v, v_cos);
296
297                                 interp_v3_v3v3(edges[tot][0], v_cos[0][0], v_cos[0][1], fac);
298                                 interp_v3_v3v3(edges[tot][1], v_cos[1][0], v_cos[1][1], fac);
299                                 tot++;
300                         }
301                 }
302                 eed_last = eed;
303         }
304
305         if ((eed_last != eed_start) &&
306 #ifdef BMW_EDGERING_NGON
307             BM_edge_share_face_check(eed_last, eed_start)
308 #else
309             BM_edge_share_quad_check(eed_last, eed_start)
310 #endif
311             )
312         {
313                 v[1][0] = v[0][0];
314                 v[1][1] = v[0][1];
315
316                 edgering_find_order(eed_last, eed_start, v_last, v);
317
318                 for (i = 1; i <= previewlines; i++) {
319                         const float fac = (i / ((float)previewlines + 1));
320                         float v_cos[2][2][3];
321
322                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1]) {
323                                 continue;
324                         }
325
326                         edgering_vcos_get(dm, v, v_cos);
327
328                         interp_v3_v3v3(edges[tot][0], v_cos[0][0], v_cos[0][1], fac);
329                         interp_v3_v3v3(edges[tot][1], v_cos[1][0], v_cos[1][1], fac);
330                         tot++;
331                 }
332         }
333
334         BLI_stack_free(edge_stack);
335
336         lcd->edges = edges;
337         lcd->totedge = tot;
338 }
339
340 static void edgering_preview_calc_points(RingSelOpData *lcd, DerivedMesh *dm, const int previewlines)
341 {
342         float v_cos[2][3];
343         float (*points)[3];
344         int i, tot = 0;
345
346         if (dm) {
347                 BM_mesh_elem_table_ensure(lcd->em->bm, BM_VERT);
348         }
349
350         points = MEM_mallocN(sizeof(*lcd->points) * previewlines, __func__);
351
352         edgering_vcos_get_pair(dm, &lcd->eed->v1, v_cos);
353
354         for (i = 1; i <= previewlines; i++) {
355                 const float fac = (i / ((float)previewlines + 1));
356                 interp_v3_v3v3(points[tot], v_cos[0], v_cos[1], fac);
357                 tot++;
358         }
359
360         lcd->points = points;
361         lcd->totpoint = previewlines;
362 }
363
364 static void edgering_preview_calc(RingSelOpData *lcd, const int previewlines)
365 {
366         DerivedMesh *dm;
367
368         BLI_assert(lcd->eed != NULL);
369
370         edgering_preview_free(lcd);
371
372         dm = EDBM_mesh_deform_dm_get(lcd->em);
373         if (dm) {
374                 BM_mesh_elem_table_ensure(lcd->em->bm, BM_VERT);
375         }
376
377         if (BM_edge_is_wire(lcd->eed)) {
378                 edgering_preview_calc_points(lcd, dm, previewlines);
379         }
380         else {
381                 edgering_preview_calc_edges(lcd, dm, previewlines);
382         }
383 }
384
385 static void edgering_select(RingSelOpData *lcd)
386 {
387         BMEditMesh *em = lcd->em;
388         BMEdge *eed_start = lcd->eed;
389         BMWalker walker;
390         BMEdge *eed;
391         
392         if (!eed_start)
393                 return;
394
395         if (!lcd->extend) {
396                 EDBM_flag_disable_all(lcd->em, BM_ELEM_SELECT);
397         }
398
399         BMW_init(&walker, em->bm, BMW_EDGERING,
400                  BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
401                  BMW_FLAG_TEST_HIDDEN,
402                  BMW_NIL_LAY);
403
404         for (eed = BMW_begin(&walker, eed_start); eed; eed = BMW_step(&walker)) {
405                 BM_edge_select_set(em->bm, eed, true);
406         }
407         BMW_end(&walker);
408 }
409
410 static void ringsel_find_edge(RingSelOpData *lcd, const int previewlines)
411 {
412         if (lcd->eed) {
413                 edgering_preview_calc(lcd, previewlines);
414         }
415         else {
416                 edgering_preview_free(lcd);
417         }
418 }
419
420 static void ringsel_finish(bContext *C, wmOperator *op)
421 {
422         RingSelOpData *lcd = op->customdata;
423         const int cuts = RNA_int_get(op->ptr, "number_cuts");
424         const float smoothness = RNA_float_get(op->ptr, "smoothness");
425         const int smooth_falloff = RNA_enum_get(op->ptr, "falloff");
426 #ifdef BMW_EDGERING_NGON
427         const bool use_only_quads = false;
428 #else
429         const bool use_only_quads = false;
430 #endif
431
432         if (lcd->eed) {
433                 BMEditMesh *em = lcd->em;
434                 BMVert *v_eed_orig[2] = {lcd->eed->v1, lcd->eed->v2};
435
436                 edgering_select(lcd);
437                 
438                 if (lcd->do_cut) {
439                         const bool is_macro = (op->opm != NULL);
440                         /* a single edge (rare, but better support) */
441                         const bool is_single = (BM_edge_is_wire(lcd->eed));
442                         const int seltype = is_single ? SUBDIV_SELECT_INNER : SUBDIV_SELECT_LOOPCUT;
443
444                         /* Enable gridfill, so that intersecting loopcut works as one would expect.
445                          * Note though that it will break edgeslide in this specific case.
446                          * See [#31939]. */
447                         BM_mesh_esubdivide(em->bm, BM_ELEM_SELECT,
448                                            smoothness, smooth_falloff, true,
449                                            0.0f, 0.0f,
450                                            cuts, seltype, SUBD_CORNER_PATH, 0, true,
451                                            use_only_quads, 0);
452
453                         /* when used in a macro the tessfaces will be recalculated anyway,
454                          * this is needed here because modifiers depend on updated tessellation, see T45920 */
455                         EDBM_update_generic(em, true, true);
456
457                         if (is_single) {
458                                 /* de-select endpoints */
459                                 BM_vert_select_set(em->bm, v_eed_orig[0], false);
460                                 BM_vert_select_set(em->bm, v_eed_orig[1], false);
461
462                                 EDBM_selectmode_flush_ex(lcd->em, SCE_SELECT_VERTEX);
463                         }
464                         /* we cant slide multiple edges in vertex select mode */
465                         else if (is_macro && (cuts > 1) && (em->selectmode & SCE_SELECT_VERTEX)) {
466                                 EDBM_selectmode_disable(lcd->vc.scene, em, SCE_SELECT_VERTEX, SCE_SELECT_EDGE);
467                         }
468                         /* force edge slide to edge select mode in in face select mode */
469                         else if (EDBM_selectmode_disable(lcd->vc.scene, em, SCE_SELECT_FACE, SCE_SELECT_EDGE)) {
470                                 /* pass, the change will flush selection */
471                         }
472                         else {
473                                 /* else flush explicitly */
474                                 EDBM_selectmode_flush(lcd->em);
475                         }
476                 }
477                 else {
478                         /* XXX Is this piece of code ever used now? Simple loop select is now
479                          *     in editmesh_select.c (around line 1000)... */
480                         /* sets as active, useful for other tools */
481                         if (em->selectmode & SCE_SELECT_VERTEX)
482                                 BM_select_history_store(em->bm, lcd->eed->v1);  /* low priority TODO, get vertrex close to mouse */
483                         if (em->selectmode & SCE_SELECT_EDGE)
484                                 BM_select_history_store(em->bm, lcd->eed);
485                         
486                         EDBM_selectmode_flush(lcd->em);
487                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, lcd->ob->data);
488                 }
489         }
490 }
491
492 /* called when modal loop selection is done... */
493 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
494 {
495         RingSelOpData *lcd = op->customdata;
496
497         /* deactivate the extra drawing stuff in 3D-View */
498         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
499         
500         edgering_preview_free(lcd);
501
502         ED_region_tag_redraw(lcd->ar);
503
504         /* free the custom data */
505         MEM_freeN(lcd);
506         op->customdata = NULL;
507 }
508
509
510 /* called when modal loop selection gets set up... */
511 static int ringsel_init(bContext *C, wmOperator *op, bool do_cut)
512 {
513         RingSelOpData *lcd;
514         Scene *scene = CTX_data_scene(C);
515
516         /* alloc new customdata */
517         lcd = op->customdata = MEM_callocN(sizeof(RingSelOpData), "ringsel Modal Op Data");
518         
519         /* assign the drawing handle for drawing preview line... */
520         lcd->ar = CTX_wm_region(C);
521         lcd->draw_handle = ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
522         lcd->ob = CTX_data_edit_object(C);
523         lcd->em = BKE_editmesh_from_object(lcd->ob);
524         lcd->extend = do_cut ? false : RNA_boolean_get(op->ptr, "extend");
525         lcd->do_cut = do_cut;
526         lcd->cuts = RNA_int_get(op->ptr, "number_cuts");
527         lcd->smoothness = RNA_float_get(op->ptr, "smoothness");
528         
529         initNumInput(&lcd->num);
530         lcd->num.idx_max = 1;
531         lcd->num.val_flag[0] |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
532         /* No specific flags for smoothness. */
533         lcd->num.unit_sys = scene->unit.system;
534         lcd->num.unit_type[0] = B_UNIT_NONE;
535         lcd->num.unit_type[1] = B_UNIT_NONE;
536
537         em_setup_viewcontext(C, &lcd->vc);
538
539         ED_region_tag_redraw(lcd->ar);
540
541         return 1;
542 }
543
544 static void ringcut_cancel(bContext *C, wmOperator *op)
545 {
546         /* this is just a wrapper around exit() */
547         ringsel_exit(C, op);
548 }
549
550 static void loopcut_update_edge(RingSelOpData *lcd, BMEdge *e, const int previewlines)
551 {
552         if (e != lcd->eed) {
553                 lcd->eed = e;
554                 ringsel_find_edge(lcd, previewlines);
555         }
556 }
557
558 static void loopcut_mouse_move(const struct EvaluationContext *eval_ctx, RingSelOpData *lcd, const int previewlines)
559 {
560         float dist = ED_view3d_select_dist_px();
561         BMEdge *e = EDBM_edge_find_nearest(eval_ctx, &lcd->vc, &dist);
562         loopcut_update_edge(lcd, e, previewlines);
563 }
564
565 /* called by both init() and exec() */
566 static int loopcut_init(bContext *C, wmOperator *op, const wmEvent *event)
567 {
568         EvaluationContext eval_ctx;
569         const bool is_interactive = (event != NULL);
570         Object *obedit = CTX_data_edit_object(C);
571         RingSelOpData *lcd;
572
573         CTX_data_eval_ctx(C, &eval_ctx);
574
575         if (modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
576                 BKE_report(op->reports, RPT_WARNING, "Loop cut does not work well on deformed edit mesh display");
577
578         view3d_operator_needs_opengl(C);
579
580         /* for re-execution, check edge index is in range before we setup ringsel */
581         if (is_interactive == false) {
582                 const int e_index = RNA_int_get(op->ptr, "edge_index");
583                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
584                 if (UNLIKELY((e_index == -1) || (e_index >= em->bm->totedge))) {
585                         return OPERATOR_CANCELLED;
586                 }
587         }
588
589         if (!ringsel_init(C, op, true))
590                 return OPERATOR_CANCELLED;
591
592         /* add a modal handler for this operator - handles loop selection */
593         if (is_interactive) {
594                 op->flag |= OP_IS_MODAL_CURSOR_REGION;
595                 WM_event_add_modal_handler(C, op);
596         }
597
598         lcd = op->customdata;
599
600         if (is_interactive) {
601                 copy_v2_v2_int(lcd->vc.mval, event->mval);
602                 loopcut_mouse_move(&eval_ctx, lcd, is_interactive ? 1 : 0);
603         }
604         else {
605                 const int e_index = RNA_int_get(op->ptr, "edge_index");
606                 BMEdge *e;
607                 BM_mesh_elem_table_ensure(lcd->em->bm, BM_EDGE);
608                 e = BM_edge_at_index(lcd->em->bm, e_index);
609                 loopcut_update_edge(lcd, e, 0);
610         }
611
612 #ifdef USE_LOOPSLIDE_HACK
613         /* for use in macro so we can restore, HACK */
614         {
615                 Scene *scene = CTX_data_scene(C);
616                 ToolSettings *settings = scene->toolsettings;
617                 const int mesh_select_mode[3] = {
618                     (settings->selectmode & SCE_SELECT_VERTEX) != 0,
619                     (settings->selectmode & SCE_SELECT_EDGE)   != 0,
620                     (settings->selectmode & SCE_SELECT_FACE)   != 0,
621                 };
622
623                 RNA_boolean_set_array(op->ptr, "mesh_select_mode_init", mesh_select_mode);
624         }
625 #endif
626
627         if (is_interactive) {
628                 ScrArea *sa = CTX_wm_area(C);
629                 ED_area_headerprint(sa, IFACE_("Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts, "
630                                                "hold Alt for smooth"));
631                 return OPERATOR_RUNNING_MODAL;
632         }
633         else {
634                 ringsel_finish(C, op);
635                 ringsel_exit(C, op);
636                 return OPERATOR_FINISHED;
637         }
638 }
639
640 static int ringcut_invoke(bContext *C, wmOperator *op, const wmEvent *event)
641 {
642         return loopcut_init(C, op, event);
643 }
644
645 static int loopcut_exec(bContext *C, wmOperator *op)
646 {
647         return loopcut_init(C, op, NULL);
648 }
649
650 static int loopcut_finish(RingSelOpData *lcd, bContext *C, wmOperator *op)
651 {
652         /* finish */
653         ED_region_tag_redraw(lcd->ar);
654         ED_area_headerprint(CTX_wm_area(C), NULL);
655
656         if (lcd->eed) {
657                 /* set for redo */
658                 BM_mesh_elem_index_ensure(lcd->em->bm, BM_EDGE);
659                 RNA_int_set(op->ptr, "edge_index", BM_elem_index_get(lcd->eed));
660
661                 /* execute */
662                 ringsel_finish(C, op);
663                 ringsel_exit(C, op);
664         }
665         else {
666                 ringcut_cancel(C, op);
667                 return OPERATOR_CANCELLED;
668         }
669
670         return OPERATOR_FINISHED;
671 }
672
673 static int loopcut_modal(bContext *C, wmOperator *op, const wmEvent *event)
674 {
675         EvaluationContext eval_ctx;
676         RingSelOpData *lcd = op->customdata;
677         float cuts = lcd->cuts;
678         float smoothness = lcd->smoothness;
679         bool show_cuts = false;
680         const bool has_numinput = hasNumInput(&lcd->num);
681
682         CTX_data_eval_ctx(C, &eval_ctx);
683         em_setup_viewcontext(C, &lcd->vc);
684         lcd->ar = lcd->vc.ar;
685
686         view3d_operator_needs_opengl(C);
687
688         /* using the keyboard to input the number of cuts */
689         /* Modal numinput active, try to handle numeric inputs first... */
690         if (event->val == KM_PRESS && has_numinput && handleNumInput(C, &lcd->num, event)) {
691                 float values[2] = {cuts, smoothness};
692                 applyNumInput(&lcd->num, values);
693                 cuts = values[0];
694                 smoothness = values[1];
695         }
696         else {
697                 bool handled = false;
698                 switch (event->type) {
699                         case RETKEY:
700                         case PADENTER:
701                         case LEFTMOUSE: /* confirm */ // XXX hardcoded
702                                 if (event->val == KM_PRESS)
703                                         return loopcut_finish(lcd, C, op);
704                                 
705                                 ED_region_tag_redraw(lcd->ar);
706                                 handled = true;
707                                 break;
708                         case RIGHTMOUSE: /* abort */ // XXX hardcoded
709                                 ED_region_tag_redraw(lcd->ar);
710                                 ringsel_exit(C, op);
711                                 ED_area_headerprint(CTX_wm_area(C), NULL);
712
713                                 return OPERATOR_CANCELLED;
714                         case ESCKEY:
715                                 if (event->val == KM_RELEASE) {
716                                         /* cancel */
717                                         ED_region_tag_redraw(lcd->ar);
718                                         ED_area_headerprint(CTX_wm_area(C), NULL);
719                                         
720                                         ringcut_cancel(C, op);
721                                         return OPERATOR_CANCELLED;
722                                 }
723                                 
724                                 ED_region_tag_redraw(lcd->ar);
725                                 handled = true;
726                                 break;
727                         case MOUSEPAN:
728                                 if (event->alt == 0) {
729                                         cuts += 0.02f * (event->y - event->prevy);
730                                         if (cuts < 1 && lcd->cuts >= 1)
731                                                 cuts = 1;
732                                 }
733                                 else {
734                                         smoothness += 0.002f * (event->y - event->prevy);
735                                 }
736                                 handled = true;
737                                 break;
738                         case PADPLUSKEY:
739                         case PAGEUPKEY:
740                         case WHEELUPMOUSE:  /* change number of cuts */
741                                 if (event->val == KM_RELEASE)
742                                         break;
743                                 if (event->alt == 0) {
744                                         cuts += 1;
745                                 }
746                                 else {
747                                         smoothness += 0.05f;
748                                 }
749                                 handled = true;
750                                 break;
751                         case PADMINUS:
752                         case PAGEDOWNKEY:
753                         case WHEELDOWNMOUSE:  /* change number of cuts */
754                                 if (event->val == KM_RELEASE)
755                                         break;
756                                 if (event->alt == 0) {
757                                         cuts = max_ff(cuts - 1, 1);
758                                 }
759                                 else {
760                                         smoothness -= 0.05f;
761                                 }
762                                 handled = true;
763                                 break;
764                         case MOUSEMOVE:  /* mouse moved somewhere to select another loop */
765                                 if (!has_numinput) {
766                                         lcd->vc.mval[0] = event->mval[0];
767                                         lcd->vc.mval[1] = event->mval[1];
768                                         loopcut_mouse_move(&eval_ctx, lcd, (int)lcd->cuts);
769
770                                         ED_region_tag_redraw(lcd->ar);
771                                         handled = true;
772                                 }
773                                 break;
774                 }
775
776                 /* Modal numinput inactive, try to handle numeric inputs last... */
777                 if (!handled && event->val == KM_PRESS && handleNumInput(C, &lcd->num, event)) {
778                         float values[2] = {cuts, smoothness};
779                         applyNumInput(&lcd->num, values);
780                         cuts = values[0];
781                         smoothness = values[1];
782                 }
783         }
784
785         if (cuts != lcd->cuts) {
786                 /* allow zero so you can backspace and type in a value
787                  * otherwise 1 as minimum would make more sense */
788                 lcd->cuts = CLAMPIS(cuts, 0, SUBD_CUTS_MAX);
789                 RNA_int_set(op->ptr, "number_cuts", (int)lcd->cuts);
790                 ringsel_find_edge(lcd, (int)lcd->cuts);
791                 show_cuts = true;
792                 ED_region_tag_redraw(lcd->ar);
793         }
794
795         if (smoothness != lcd->smoothness) {
796                 lcd->smoothness = CLAMPIS(smoothness, -SUBD_SMOOTH_MAX, SUBD_SMOOTH_MAX);
797                 RNA_float_set(op->ptr, "smoothness", lcd->smoothness);
798                 show_cuts = true;
799                 ED_region_tag_redraw(lcd->ar);
800         }
801
802         if (show_cuts) {
803                 Scene *sce = CTX_data_scene(C);
804                 char buf[UI_MAX_DRAW_STR];
805                 char str_rep[NUM_STR_REP_LEN * 2];
806                 if (hasNumInput(&lcd->num)) {
807                         outputNumInput(&lcd->num, str_rep, &sce->unit);
808                 }
809                 else {
810                         BLI_snprintf(str_rep, NUM_STR_REP_LEN, "%d", (int)lcd->cuts);
811                         BLI_snprintf(str_rep + NUM_STR_REP_LEN, NUM_STR_REP_LEN, "%.2f", smoothness);
812                 }
813                 BLI_snprintf(buf, sizeof(buf), IFACE_("Number of Cuts: %s, Smooth: %s (Alt)"),
814                              str_rep, str_rep + NUM_STR_REP_LEN);
815                 ED_area_headerprint(CTX_wm_area(C), buf);
816         }
817         
818         /* keep going until the user confirms */
819         return OPERATOR_RUNNING_MODAL;
820 }
821
822 /* for bmesh this tool is in bmesh_select.c */
823 #if 0
824
825 void MESH_OT_edgering_select(wmOperatorType *ot)
826 {
827         /* description */
828         ot->name = "Edge Ring Select";
829         ot->idname = "MESH_OT_edgering_select";
830         ot->description = "Select an edge ring";
831         
832         /* callbacks */
833         ot->invoke = ringsel_invoke;
834         ot->poll = ED_operator_editmesh_region_view3d; 
835         
836         /* flags */
837         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
838
839         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
840 }
841
842 #endif
843
844 void MESH_OT_loopcut(wmOperatorType *ot)
845 {
846         PropertyRNA *prop;
847
848         /* description */
849         ot->name = "Loop Cut";
850         ot->idname = "MESH_OT_loopcut";
851         ot->description = "Add a new loop between existing loops";
852         
853         /* callbacks */
854         ot->invoke = ringcut_invoke;
855         ot->exec = loopcut_exec;
856         ot->modal = loopcut_modal;
857         ot->cancel = ringcut_cancel;
858         ot->poll = ED_operator_editmesh_region_view3d;
859         
860         /* flags */
861         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
862
863         /* properties */
864         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, 1000000, "Number of Cuts", "", 1, 100);
865         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
866         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
867
868         prop = RNA_def_float(ot->srna, "smoothness", 0.0f, -1e3f, 1e3f,
869                              "Smoothness", "Smoothness factor", -SUBD_SMOOTH_MAX, SUBD_SMOOTH_MAX);
870         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
871
872         WM_operatortype_props_advanced_begin(ot);
873
874         prop = RNA_def_property(ot->srna, "falloff", PROP_ENUM, PROP_NONE);
875         RNA_def_property_enum_items(prop, rna_enum_proportional_falloff_curve_only_items);
876         RNA_def_property_enum_default(prop, PROP_INVSQUARE);
877         RNA_def_property_ui_text(prop, "Falloff", "Falloff type the feather");
878         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
879
880         prop = RNA_def_int(ot->srna, "edge_index", -1, -1, INT_MAX, "Edge Index", "", 0, INT_MAX);
881         RNA_def_property_flag(prop, PROP_HIDDEN);
882
883 #ifdef USE_LOOPSLIDE_HACK
884         prop = RNA_def_boolean_array(ot->srna, "mesh_select_mode_init", 3, NULL, "", "");
885         RNA_def_property_flag(prop, PROP_HIDDEN);
886 #endif
887 }