Fix #30091: Shift-Ctrl-Alt-RMB problem in face mode
[blender.git] / source / blender / editors / mesh / 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/loopcut.c
28  *  \ingroup edmesh
29  */
30
31
32 #include <float.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdio.h>
36
37 #include "DNA_ID.h"
38 #include "DNA_object_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_userdef_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "PIL_time.h"
47
48 #include "BLI_array.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_dynstr.h" /*for WM_operator_pystring */
51 #include "BLI_editVert.h"
52 #include "BLI_math.h"
53 #include "BLI_utildefines.h"
54
55 #include "BKE_blender.h"
56 #include "BKE_context.h"
57 #include "BKE_depsgraph.h"
58 #include "BKE_mesh.h"
59 #include "BKE_modifier.h"
60 #include "BKE_report.h"
61 #include "BKE_scene.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h" /* for paint cursor */
65
66 #include "IMB_imbuf_types.h"
67
68 #include "ED_screen.h"
69 #include "ED_space_api.h"
70 #include "ED_view3d.h"
71 #include "ED_mesh.h"
72 #include "ED_numinput.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "UI_interface.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 #include "mesh_intern.h"
83
84 /* ringsel operator */
85
86 /* struct for properties used while drawing */
87 typedef struct tringselOpData {
88         ARegion *ar;            /* region that ringsel was activated in */
89         void *draw_handle;      /* for drawing preview loop */
90         
91         float (*edges)[2][3];
92         int totedge;
93
94         ViewContext vc;
95
96         Object *ob;
97         EditMesh *em;
98         EditEdge *eed;
99         NumInput num;
100
101         int extend;
102         int do_cut;
103 } tringselOpData;
104
105 /* modal loop selection drawing callback */
106 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
107 {
108         View3D *v3d = CTX_wm_view3d(C);
109         tringselOpData *lcd = arg;
110         int i;
111         
112         if (lcd->totedge > 0) {
113                 if(v3d && v3d->zbuf)
114                         glDisable(GL_DEPTH_TEST);
115
116                 glPushMatrix();
117                 glMultMatrixf(lcd->ob->obmat);
118
119                 glColor3ub(255, 0, 255);
120                 glBegin(GL_LINES);
121                 for (i=0; i<lcd->totedge; i++) {
122                         glVertex3fv(lcd->edges[i][0]);
123                         glVertex3fv(lcd->edges[i][1]);
124                 }
125                 glEnd();
126
127                 glPopMatrix();
128                 if(v3d && v3d->zbuf)
129                         glEnable(GL_DEPTH_TEST);
130         }
131 }
132
133 static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
134 {
135         EditMesh *em = lcd->em;
136         EditEdge *startedge = lcd->eed;
137         EditEdge *eed;
138         EditFace *efa;
139         EditVert *v[2][2];
140         float (*edges)[2][3] = NULL;
141         BLI_array_declare(edges);
142         float co[2][3];
143         int looking=1, i, tot=0;
144         
145         if (!startedge)
146                 return;
147
148         if (lcd->edges) {
149                 MEM_freeN(lcd->edges);
150                 lcd->edges = NULL;
151                 lcd->totedge = 0;
152         }
153
154         if (!lcd->extend) {
155                 EM_clear_flag_all(lcd->em, SELECT);
156         }
157
158         /* in eed->f1 we put the valence (amount of faces in edge) */
159         /* in eed->f2 we put tagged flag as correct loop */
160         /* in efa->f1 we put tagged flag as correct to select */
161
162         for(eed= em->edges.first; eed; eed= eed->next) {
163                 eed->f1= 0;
164                 eed->f2= 0;
165         }
166
167         for(efa= em->faces.first; efa; efa= efa->next) {
168                 efa->f1= 0;
169                 if(efa->h==0) {
170                         efa->e1->f1++;
171                         efa->e2->f1++;
172                         efa->e3->f1++;
173                         if(efa->e4) efa->e4->f1++;
174                 }
175         }
176         
177         // tag startedge OK
178         startedge->f2= 1;
179         
180         while(looking) {
181                 looking= 0;
182                 
183                 for(efa= em->faces.first; efa; efa= efa->next) {
184                         if(efa->e4 && efa->f1==0 && efa->h == 0) {      // not done quad
185                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
186
187                                         // if edge tagged, select opposing edge and mark face ok
188                                         if(efa->e1->f2) {
189                                                 efa->e3->f2= 1;
190                                                 efa->f1= 1;
191                                                 looking= 1;
192                                         }
193                                         else if(efa->e2->f2) {
194                                                 efa->e4->f2= 1;
195                                                 efa->f1= 1;
196                                                 looking= 1;
197                                         }
198                                         if(efa->e3->f2) {
199                                                 efa->e1->f2= 1;
200                                                 efa->f1= 1;
201                                                 looking= 1;
202                                         }
203                                         if(efa->e4->f2) {
204                                                 efa->e2->f2= 1;
205                                                 efa->f1= 1;
206                                                 looking= 1;
207                                         }
208                                 }
209                         }
210                 }
211         }
212         
213         if(previewlines > 0 && !select){
214                         for(efa= em->faces.first; efa; efa= efa->next) {
215                                 if(efa->v4 == NULL) {  continue; }
216                                 if(efa->h == 0){
217                                         if(efa->e1->f2 == 1){
218                                                 if(efa->e1->h == 1 || efa->e3->h == 1 )
219                                                         continue;
220                                                 
221                                                 v[0][0] = efa->v1;
222                                                 v[0][1] = efa->v2;
223                                                 v[1][0] = efa->v4;
224                                                 v[1][1] = efa->v3;
225                                         } else if(efa->e2->f2 == 1){
226                                                 if(efa->e2->h == 1 || efa->e4->h == 1)
227                                                         continue;
228                                                 v[0][0] = efa->v2;
229                                                 v[0][1] = efa->v3;
230                                                 v[1][0] = efa->v1;
231                                                 v[1][1] = efa->v4;                                      
232                                         } else { continue; }
233                                                                                   
234                                         for(i=1;i<=previewlines;i++){
235                                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
236                                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
237                                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
238
239                                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
240                                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
241                                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
242                                                 
243                                                 BLI_array_growone(edges);
244                                                 copy_v3_v3(edges[tot][0], co[0]);
245                                                 copy_v3_v3(edges[tot][1], co[1]);
246                                                 tot++;
247                                         }
248                                 }
249                         }
250         } else {
251                 select = (startedge->f & SELECT) == 0;
252
253                 /* select the edges */
254                 for(eed= em->edges.first; eed; eed= eed->next) {
255                         if(eed->f2) EM_select_edge(eed, select);
256                 }
257         }
258
259         lcd->edges = edges;
260         lcd->totedge = tot;
261 }
262
263 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
264 {
265         if (lcd->eed) {
266                 edgering_sel(lcd, cuts, 0);
267         } else if(lcd->edges) {
268                 MEM_freeN(lcd->edges);
269                 lcd->edges = NULL;
270                 lcd->totedge = 0;
271         }
272 }
273
274 static void ringsel_finish(bContext *C, wmOperator *op)
275 {
276         tringselOpData *lcd= op->customdata;
277         int cuts= (lcd->do_cut)? RNA_int_get(op->ptr,"number_cuts"): 0;
278
279         if (lcd->eed) {
280                 EditMesh *em = BKE_mesh_get_editmesh(lcd->ob->data);
281                 
282                 edgering_sel(lcd, cuts, 1);
283                 
284                 if (lcd->do_cut) {
285
286                         esubdivideflag(lcd->ob, em, SELECT, 0.0f, 0.0f, 0, cuts, 0, SUBDIV_SELECT_LOOPCUT);
287
288                         /* force edge slide to edge select mode in in face select mode */
289                         if (em->selectmode & SCE_SELECT_FACE) {
290                                 if (em->selectmode == SCE_SELECT_FACE)
291                                         em->selectmode = SCE_SELECT_EDGE;
292                                 else
293                                         em->selectmode &= ~SCE_SELECT_FACE;
294                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
295                                 EM_selectmode_set(em);
296
297                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
298                         }
299                         
300                         DAG_id_tag_update(lcd->ob->data, 0);
301                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, lcd->ob->data);
302                 }
303                 else {
304                         
305                         /* sets as active, useful for other tools */
306                         if(em->selectmode & SCE_SELECT_VERTEX)
307                                 EM_store_selection(em, lcd->eed->v1, EDITVERT);
308                         if(em->selectmode & SCE_SELECT_EDGE)
309                                 EM_store_selection(em, lcd->eed, EDITEDGE);
310                         
311                         EM_selectmode_flush(lcd->em);
312                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
313                 }
314         }
315 }
316
317 /* called when modal loop selection is done... */
318 static void ringsel_exit(wmOperator *op)
319 {
320         tringselOpData *lcd= op->customdata;
321
322         /* deactivate the extra drawing stuff in 3D-View */
323         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
324         
325         if (lcd->edges)
326                 MEM_freeN(lcd->edges);
327
328         ED_region_tag_redraw(lcd->ar);
329
330         /* free the custom data */
331         MEM_freeN(lcd);
332         op->customdata= NULL;
333 }
334
335 /* called when modal loop selection gets set up... */
336 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
337 {
338         tringselOpData *lcd;
339         
340         /* alloc new customdata */
341         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
342         
343         /* assign the drawing handle for drawing preview line... */
344         lcd->ar= CTX_wm_region(C);
345         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
346         lcd->ob = CTX_data_edit_object(C);
347         lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
348         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
349         lcd->do_cut = do_cut;
350         
351         initNumInput(&lcd->num);
352         lcd->num.idx_max = 0;
353         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
354         
355         em_setup_viewcontext(C, &lcd->vc);
356
357         ED_region_tag_redraw(lcd->ar);
358
359         return 1;
360 }
361
362 static int ringcut_cancel (bContext *UNUSED(C), wmOperator *op)
363 {
364         /* this is just a wrapper around exit() */
365         ringsel_exit(op);
366         return OPERATOR_CANCELLED;
367 }
368
369 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
370 {
371         tringselOpData *lcd;
372         EditEdge *edge;
373         int dist = 75;
374         
375         view3d_operator_needs_opengl(C);
376
377         if (!ringsel_init(C, op, 0))
378                 return OPERATOR_CANCELLED;
379         
380         lcd = op->customdata;
381         
382         if (lcd->em->selectmode == SCE_SELECT_FACE) {
383                 PointerRNA props_ptr;
384                 int extend = RNA_boolean_get(op->ptr, "extend");
385
386                 ringsel_exit(op);
387
388                 WM_operator_properties_create(&props_ptr, "MESH_OT_loop_select");
389                 RNA_boolean_set(&props_ptr, "extend", extend);
390                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, &props_ptr);
391                 WM_operator_properties_free(&props_ptr);
392
393                 return OPERATOR_CANCELLED;
394         }
395
396         lcd->vc.mval[0] = evt->mval[0];
397         lcd->vc.mval[1] = evt->mval[1];
398         
399         edge = findnearestedge(&lcd->vc, &dist);
400         if(!edge) {
401                 ringsel_exit(op);
402                 return OPERATOR_CANCELLED;
403         }
404
405         lcd->eed = edge;
406         ringsel_find_edge(lcd, 1);
407
408         ringsel_finish(C, op);
409         ringsel_exit(op);
410
411         return OPERATOR_FINISHED;
412 }
413
414 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
415 {
416         Object *obedit= CTX_data_edit_object(C);
417         tringselOpData *lcd;
418         EditEdge *edge;
419         int dist = 75;
420
421         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
422                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
423         
424         view3d_operator_needs_opengl(C);
425
426         if (!ringsel_init(C, op, 1))
427                 return OPERATOR_CANCELLED;
428         
429         /* add a modal handler for this operator - handles loop selection */
430         WM_event_add_modal_handler(C, op);
431
432         lcd = op->customdata;
433         lcd->vc.mval[0] = evt->mval[0];
434         lcd->vc.mval[1] = evt->mval[1];
435         
436         edge = findnearestedge(&lcd->vc, &dist);
437         if (edge != lcd->eed) {
438                 lcd->eed = edge;
439                 ringsel_find_edge(lcd, 1);
440         }
441         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
442         
443         return OPERATOR_RUNNING_MODAL;
444 }
445
446 static int ringcut_modal (bContext *C, wmOperator *op, wmEvent *event)
447 {
448         int cuts= RNA_int_get(op->ptr,"number_cuts");
449         tringselOpData *lcd= op->customdata;
450
451         view3d_operator_needs_opengl(C);
452
453
454         switch (event->type) {
455                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
456                         if (event->val == KM_PRESS) {
457                                 /* finish */
458                                 ED_region_tag_redraw(lcd->ar);
459                                 
460                                 ringsel_finish(C, op);
461                                 ringsel_exit(op);
462                                 ED_area_headerprint(CTX_wm_area(C), NULL);
463                                 
464                                 return OPERATOR_FINISHED;
465                         }
466                         
467                         ED_region_tag_redraw(lcd->ar);
468                         break;
469                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
470                 case ESCKEY:
471                         if (event->val == KM_RELEASE) {
472                                 /* cancel */
473                                 ED_region_tag_redraw(lcd->ar);
474                                 ED_area_headerprint(CTX_wm_area(C), NULL);
475                                 
476                                 return ringcut_cancel(C, op);
477                         }
478                         
479                         ED_region_tag_redraw(lcd->ar);
480                         break;
481                 case WHEELUPMOUSE:  /* change number of cuts */
482                 case PADPLUSKEY:
483                 case PAGEUPKEY:
484                         if (event->val == KM_PRESS) {
485                                 cuts++;
486                                 RNA_int_set(op->ptr, "number_cuts",cuts);
487                                 ringsel_find_edge(lcd, cuts);
488                                 
489                                 ED_region_tag_redraw(lcd->ar);
490                         }
491                         break;
492                 case WHEELDOWNMOUSE:  /* change number of cuts */
493                 case PADMINUS:
494                 case PAGEDOWNKEY:
495                         if (event->val == KM_PRESS) {
496                                 cuts=MAX2(cuts-1,1);
497                                 RNA_int_set(op->ptr,"number_cuts",cuts);
498                                 ringsel_find_edge(lcd, cuts);
499                                 
500                                 ED_region_tag_redraw(lcd->ar);
501                         }
502                         break;
503                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
504                         int dist = 75;
505                         EditEdge *edge;
506
507                         lcd->vc.mval[0] = event->mval[0];
508                         lcd->vc.mval[1] = event->mval[1];
509                         edge = findnearestedge(&lcd->vc, &dist);
510
511                         if (edge != lcd->eed) {
512                                 lcd->eed = edge;
513                                 ringsel_find_edge(lcd, cuts);
514                         }
515
516                         ED_region_tag_redraw(lcd->ar);
517                         break;
518                 }                       
519         }
520         
521         /* using the keyboard to input the number of cuts */
522         if (event->val==KM_PRESS) {
523                 float value;
524                 
525                 if (handleNumInput(&lcd->num, event))
526                 {
527                         applyNumInput(&lcd->num, &value);
528                         
529                         cuts= CLAMPIS(value, 1, 32);
530                         
531                         RNA_int_set(op->ptr,"number_cuts",cuts);
532                         ringsel_find_edge(lcd, cuts);
533                         
534                         ED_region_tag_redraw(lcd->ar);
535                 }
536         }
537         
538         /* keep going until the user confirms */
539         return OPERATOR_RUNNING_MODAL;
540 }
541
542 void MESH_OT_edgering_select (wmOperatorType *ot)
543 {
544         /* description */
545         ot->name= "Edge Ring Select";
546         ot->idname= "MESH_OT_edgering_select";
547         ot->description= "Select an edge ring";
548         
549         /* callbacks */
550         ot->invoke= ringsel_invoke;
551         ot->poll= ED_operator_editmesh_region_view3d; 
552         
553         /* flags */
554         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
555
556         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
557 }
558
559 void MESH_OT_loopcut (wmOperatorType *ot)
560 {
561         /* description */
562         ot->name= "Loop Cut";
563         ot->idname= "MESH_OT_loopcut";
564         ot->description= "Add a new loop between existing loops";
565         
566         /* callbacks */
567         ot->invoke= ringcut_invoke;
568         ot->modal= ringcut_modal;
569         ot->cancel= ringcut_cancel;
570         ot->poll= ED_operator_editmesh_region_view3d;
571         
572         /* flags */
573         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
574
575         /* properties */
576         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
577 }