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