Fix syntax for ID keyword.
[blender-staging.git] / source / blender / editors / mesh / loopcut.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joseph Eagar, Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <float.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <stdio.h>
33
34 #include "DNA_ID.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_userdef_types.h"
38 #include "DNA_windowmanager_types.h"
39 #include "DNA_object_types.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "PIL_time.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_dynstr.h" /*for WM_operator_pystring */
47 #include "BLI_editVert.h"
48 #include "BLI_math.h"
49
50 #include "BKE_blender.h"
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_scene.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_mesh.h"
56
57 #include "BIF_gl.h"
58 #include "BIF_glutil.h" /* for paint cursor */
59
60 #include "IMB_imbuf_types.h"
61
62 #include "ED_screen.h"
63 #include "ED_util.h"
64 #include "ED_space_api.h"
65 #include "ED_view3d.h"
66 #include "ED_mesh.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70
71 #include "UI_interface.h"
72 #include "UI_resources.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "mesh_intern.h"
78
79 /* ringsel operator */
80
81 /* struct for properties used while drawing */
82 typedef struct tringselOpData {
83         ARegion *ar;            /* region that ringsel was activated in */
84         void *draw_handle;      /* for drawing preview loop */
85         
86         float (*edges)[2][3];
87         int totedge;
88
89         ViewContext vc;
90
91         Object *ob;
92         EditMesh *em;
93         EditEdge *eed;
94
95         int extend;
96         int do_cut;
97 } tringselOpData;
98
99 /* modal loop selection drawing callback */
100 static void ringsel_draw(const bContext *C, ARegion *ar, void *arg)
101 {
102         int i;
103         tringselOpData *lcd = arg;
104         
105         if (lcd->totedge > 0) {
106                 glDisable(GL_DEPTH_TEST);
107
108                 glPushMatrix();
109                 glMultMatrixf(lcd->ob->obmat);
110
111                 glColor3ub(255, 0, 255);
112                 glBegin(GL_LINES);
113                 for (i=0; i<lcd->totedge; i++) {
114                         glVertex3fv(lcd->edges[i][0]);
115                         glVertex3fv(lcd->edges[i][1]);
116                 }
117                 glEnd();
118
119                 glPopMatrix();
120                 glEnable(GL_DEPTH_TEST);
121         }
122 }
123
124 static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
125 {
126         EditMesh *em = lcd->em;
127         EditEdge *startedge = lcd->eed;
128         EditEdge *eed;
129         EditFace *efa;
130         EditVert *v[2][2];
131         float (*edges)[2][3] = NULL;
132         V_DYNDECLARE(edges);
133         float co[2][3];
134         int looking=1, i, tot=0;
135         
136         if (!startedge)
137                 return;
138
139         if (lcd->edges) {
140                 MEM_freeN(lcd->edges);
141                 lcd->edges = NULL;
142                 lcd->totedge = 0;
143         }
144
145         if (!lcd->extend) {
146                 EM_clear_flag_all(lcd->em, SELECT);
147         }
148
149         /* in eed->f1 we put the valence (amount of faces in edge) */
150         /* in eed->f2 we put tagged flag as correct loop */
151         /* in efa->f1 we put tagged flag as correct to select */
152
153         for(eed= em->edges.first; eed; eed= eed->next) {
154                 eed->f1= 0;
155                 eed->f2= 0;
156         }
157
158         for(efa= em->faces.first; efa; efa= efa->next) {
159                 efa->f1= 0;
160                 if(efa->h==0) {
161                         efa->e1->f1++;
162                         efa->e2->f1++;
163                         efa->e3->f1++;
164                         if(efa->e4) efa->e4->f1++;
165                 }
166         }
167         
168         // tag startedge OK
169         startedge->f2= 1;
170         
171         while(looking) {
172                 looking= 0;
173                 
174                 for(efa= em->faces.first; efa; efa= efa->next) {
175                         if(efa->e4 && efa->f1==0 && efa->h == 0) {      // not done quad
176                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
177
178                                         // if edge tagged, select opposing edge and mark face ok
179                                         if(efa->e1->f2) {
180                                                 efa->e3->f2= 1;
181                                                 efa->f1= 1;
182                                                 looking= 1;
183                                         }
184                                         else if(efa->e2->f2) {
185                                                 efa->e4->f2= 1;
186                                                 efa->f1= 1;
187                                                 looking= 1;
188                                         }
189                                         if(efa->e3->f2) {
190                                                 efa->e1->f2= 1;
191                                                 efa->f1= 1;
192                                                 looking= 1;
193                                         }
194                                         if(efa->e4->f2) {
195                                                 efa->e2->f2= 1;
196                                                 efa->f1= 1;
197                                                 looking= 1;
198                                         }
199                                 }
200                         }
201                 }
202         }
203         
204         if(previewlines > 0 && !select){
205                         for(efa= em->faces.first; efa; efa= efa->next) {
206                                 if(efa->v4 == NULL) {  continue; }
207                                 if(efa->h == 0){
208                                         if(efa->e1->f2 == 1){
209                                                 if(efa->e1->h == 1 || efa->e3->h == 1 )
210                                                         continue;
211                                                 
212                                                 v[0][0] = efa->v1;
213                                                 v[0][1] = efa->v2;
214                                                 v[1][0] = efa->v4;
215                                                 v[1][1] = efa->v3;
216                                         } else if(efa->e2->f2 == 1){
217                                                 if(efa->e2->h == 1 || efa->e4->h == 1)
218                                                         continue;
219                                                 v[0][0] = efa->v2;
220                                                 v[0][1] = efa->v3;
221                                                 v[1][0] = efa->v1;
222                                                 v[1][1] = efa->v4;                                      
223                                         } else { continue; }
224                                                                                   
225                                         for(i=1;i<=previewlines;i++){
226                                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
227                                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
228                                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
229
230                                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
231                                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
232                                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
233                                                 
234                                                 V_GROW(edges);
235                                                 VECCOPY(edges[tot][0], co[0]);
236                                                 VECCOPY(edges[tot][1], co[1]);
237                                                 tot++;
238                                         }
239                                 }
240                         }
241         } else {
242                 select = (startedge->f & SELECT) == 0;
243
244                 /* select the edges */
245                 for(eed= em->edges.first; eed; eed= eed->next) {
246                         if(eed->f2) EM_select_edge(eed, select);
247                 }
248         }
249
250         lcd->edges = edges;
251         lcd->totedge = tot;
252 }
253
254 static void ringsel_find_edge(tringselOpData *lcd, const bContext *C, ARegion *ar, int cuts)
255 {
256         if (lcd->eed) {
257                 edgering_sel(lcd, cuts, 0);
258         } else {
259                 MEM_freeN(lcd->edges);
260                 lcd->edges = NULL;
261                 lcd->totedge = 0;
262         }
263 }
264
265 static void ringsel_finish(bContext *C, wmOperator *op)
266 {
267         tringselOpData *lcd= op->customdata;
268         int cuts= (lcd->do_cut)? RNA_int_get(op->ptr,"number_cuts"): 0;
269
270         if (lcd->eed) {
271                 edgering_sel(lcd, cuts, 1);
272                 if (lcd->do_cut) {
273                         EditMesh *em = BKE_mesh_get_editmesh(lcd->ob->data);
274                         esubdivideflag(lcd->ob, em, SELECT, 0.0f, 0.0f, 0, cuts, SUBDIV_SELECT_LOOPCUT);
275                         
276                         DAG_id_flush_update(lcd->ob->data, OB_RECALC_DATA);
277                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, lcd->ob->data);
278                 }
279                 else {
280                         EM_selectmode_flush(lcd->em);
281                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
282                 }
283         }
284 }
285
286 /* called when modal loop selection is done... */
287 static void ringsel_exit (bContext *C, wmOperator *op)
288 {
289         tringselOpData *lcd= op->customdata;
290
291         /* deactivate the extra drawing stuff in 3D-View */
292         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
293         
294         if (lcd->edges)
295                 MEM_freeN(lcd->edges);
296
297         ED_region_tag_redraw(lcd->ar);
298
299         /* free the custom data */
300         MEM_freeN(lcd);
301         op->customdata= NULL;
302 }
303
304 /* called when modal loop selection gets set up... */
305 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
306 {
307         tringselOpData *lcd;
308         
309         /* alloc new customdata */
310         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
311         
312         /* assign the drawing handle for drawing preview line... */
313         lcd->ar= CTX_wm_region(C);
314         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
315         lcd->ob = CTX_data_edit_object(C);
316         lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
317         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
318         lcd->do_cut = do_cut;
319         em_setup_viewcontext(C, &lcd->vc);
320
321         ED_region_tag_redraw(lcd->ar);
322
323         return 1;
324 }
325
326 static int ringcut_cancel (bContext *C, wmOperator *op)
327 {
328         /* this is just a wrapper around exit() */
329         ringsel_exit(C, op);
330         return OPERATOR_CANCELLED;
331 }
332
333 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
334 {
335         tringselOpData *lcd;
336         EditEdge *edge;
337         int dist = 75;
338         
339         view3d_operator_needs_opengl(C);
340
341         if (!ringsel_init(C, op, 0))
342                 return OPERATOR_CANCELLED;
343         
344         lcd = op->customdata;
345         
346         if (lcd->em->selectmode == SCE_SELECT_FACE) {
347                 ringsel_exit(C, op);
348                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, NULL);
349                 return OPERATOR_CANCELLED;
350         }
351
352         lcd->vc.mval[0] = evt->mval[0];
353         lcd->vc.mval[1] = evt->mval[1];
354         
355         edge = findnearestedge(&lcd->vc, &dist);
356         if(!edge) {
357                 ringsel_exit(C, op);
358                 return OPERATOR_CANCELLED;
359         }
360
361         lcd->eed = edge;
362         ringsel_find_edge(lcd, C, lcd->ar, 1);
363
364         ringsel_finish(C, op);
365         ringsel_exit(C, op);
366
367         return OPERATOR_FINISHED;
368 }
369
370 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
371 {
372         tringselOpData *lcd;
373         EditEdge *edge;
374         int dist = 75;
375
376         view3d_operator_needs_opengl(C);
377
378         if (!ringsel_init(C, op, 1))
379                 return OPERATOR_CANCELLED;
380         
381         /* add a modal handler for this operator - handles loop selection */
382         WM_event_add_modal_handler(C, op);
383
384         lcd = op->customdata;
385         lcd->vc.mval[0] = evt->mval[0];
386         lcd->vc.mval[1] = evt->mval[1];
387         
388         edge = findnearestedge(&lcd->vc, &dist);
389         if (edge != lcd->eed) {
390                 lcd->eed = edge;
391                 ringsel_find_edge(lcd, C, lcd->ar, 1);
392         }
393
394         return OPERATOR_RUNNING_MODAL;
395 }
396
397 static int ringcut_modal (bContext *C, wmOperator *op, wmEvent *event)
398 {
399         int cuts= RNA_int_get(op->ptr,"number_cuts");
400         tringselOpData *lcd= op->customdata;
401
402         view3d_operator_needs_opengl(C);
403
404
405         switch (event->type) {
406                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
407                         if (event->val == KM_RELEASE) {
408                                 /* finish */
409                                 ED_region_tag_redraw(lcd->ar);
410                                 
411                                 ringsel_finish(C, op);
412                                 ringsel_exit(C, op);
413                                 
414                                 return OPERATOR_FINISHED;
415                         }
416                         
417                         ED_region_tag_redraw(lcd->ar);
418                         break;
419                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
420                 case ESCKEY:
421                         if (event->val == KM_RELEASE) {
422                                 /* cancel */
423                                 ED_region_tag_redraw(lcd->ar);
424                                 
425                                 return ringcut_cancel(C, op);
426                         }
427                         
428                         ED_region_tag_redraw(lcd->ar);
429                         break;
430                 case WHEELUPMOUSE:  /* change number of cuts */
431                 case PAGEUPKEY:
432                         if (event->val == KM_PRESS) {
433                                 cuts++;
434                                 RNA_int_set(op->ptr, "number_cuts",cuts);
435                                 ringsel_find_edge(lcd, C, lcd->ar, cuts);
436                                 
437                                 ED_region_tag_redraw(lcd->ar);
438                         }
439                         break;
440                 case WHEELDOWNMOUSE:  /* change number of cuts */
441                 case PAGEDOWNKEY:
442                         if (event->val == KM_PRESS) {
443                                 cuts=MAX2(cuts-1,1);
444                                 RNA_int_set(op->ptr,"number_cuts",cuts);
445                                 ringsel_find_edge(lcd, C, lcd->ar,cuts);
446                                 
447                                 ED_region_tag_redraw(lcd->ar);
448                         }
449                         break;
450                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
451                         int dist = 75;
452                         EditEdge *edge;
453
454                         lcd->vc.mval[0] = event->mval[0];
455                         lcd->vc.mval[1] = event->mval[1];
456                         edge = findnearestedge(&lcd->vc, &dist);
457
458                         if (edge != lcd->eed) {
459                                 lcd->eed = edge;
460                                 ringsel_find_edge(lcd, C, lcd->ar, cuts);
461                         }
462
463                         ED_region_tag_redraw(lcd->ar);
464                         break;
465                 }                       
466         }
467         
468         /* keep going until the user confirms */
469         return OPERATOR_RUNNING_MODAL;
470 }
471
472 void MESH_OT_edgering_select (wmOperatorType *ot)
473 {
474         /* description */
475         ot->name= "Edge Ring Select";
476         ot->idname= "MESH_OT_edgering_select";
477         ot->description= "Select an edge ring";
478         
479         /* callbacks */
480         ot->invoke= ringsel_invoke;
481         ot->poll= ED_operator_editmesh_view3d;
482         
483         /* flags */
484         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
485
486         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
487 }
488
489 void MESH_OT_loopcut (wmOperatorType *ot)
490 {
491         /* description */
492         ot->name= "Loop Cut";
493         ot->idname= "MESH_OT_loopcut";
494         ot->description= "Add a new loop between existing loops";
495         
496         /* callbacks */
497         ot->invoke= ringcut_invoke;
498         ot->modal= ringcut_modal;
499         ot->cancel= ringcut_cancel;
500         ot->poll= ED_operator_editmesh_view3d;
501         
502         /* flags */
503         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
504
505         /* properties */
506         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
507 }