doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / windowmanager / intern / wm_gesture.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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "DNA_screen_types.h"
30 #include "DNA_vec_types.h"
31 #include "DNA_userdef_types.h"
32 #include "DNA_windowmanager_types.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_editVert.h"       /* lasso tessellation */
38 #include "BLI_math.h"
39 #include "BLI_scanfill.h"       /* lasso tessellation */
40 #include "BLI_utildefines.h"
41
42 #include "BKE_context.h"
43
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "wm.h"
49 #include "wm_event_system.h"
50 #include "wm_subwindow.h"
51 #include "wm_draw.h"
52
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56
57
58 /* context checked on having screen, window and area */
59 wmGesture *WM_gesture_new(bContext *C, wmEvent *event, int type)
60 {
61         wmGesture *gesture= MEM_callocN(sizeof(wmGesture), "new gesture");
62         wmWindow *window= CTX_wm_window(C);
63         ARegion *ar= CTX_wm_region(C);
64         int sx, sy;
65         
66         BLI_addtail(&window->gesture, gesture);
67         
68         gesture->type= type;
69         gesture->event_type= event->type;
70         gesture->swinid= ar->swinid;    /* means only in area-region context! */
71         
72         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
73         
74         if( ELEM5(type, WM_GESTURE_RECT, WM_GESTURE_CROSS_RECT, WM_GESTURE_TWEAK, WM_GESTURE_CIRCLE, WM_GESTURE_STRAIGHTLINE)) {
75                 rcti *rect= MEM_callocN(sizeof(rcti), "gesture rect new");
76                 
77                 gesture->customdata= rect;
78                 rect->xmin= event->x - sx;
79                 rect->ymin= event->y - sy;
80                 if(type==WM_GESTURE_CIRCLE) {
81 #ifdef GESTURE_MEMORY
82                         rect->xmax= circle_select_size;
83 #else
84                         rect->xmax= 25; // XXX temp
85 #endif
86                 } else {
87                         rect->xmax= event->x - sx;
88                         rect->ymax= event->y - sy;
89                 }
90         }
91         else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
92                 short *lasso;
93                 gesture->customdata= lasso= MEM_callocN(2*sizeof(short)*WM_LASSO_MIN_POINTS, "lasso points");
94                 lasso[0] = event->x - sx;
95                 lasso[1] = event->y - sy;
96                 gesture->points= 1;
97                 gesture->size = WM_LASSO_MIN_POINTS;
98         }
99         
100         return gesture;
101 }
102
103 void WM_gesture_end(bContext *C, wmGesture *gesture)
104 {
105         wmWindow *win= CTX_wm_window(C);
106         
107         if(win->tweak==gesture)
108                 win->tweak= NULL;
109         BLI_remlink(&win->gesture, gesture);
110         MEM_freeN(gesture->customdata);
111         MEM_freeN(gesture);
112 }
113
114 void WM_gestures_remove(bContext *C)
115 {
116         wmWindow *win= CTX_wm_window(C);
117         
118         while(win->gesture.first)
119                 WM_gesture_end(C, win->gesture.first);
120 }
121
122
123 /* tweak and line gestures */
124 #define TWEAK_THRESHOLD         10
125 int wm_gesture_evaluate(wmGesture *gesture)
126 {
127         if(gesture->type==WM_GESTURE_TWEAK) {
128                 rcti *rect= gesture->customdata;
129                 int dx= rect->xmax - rect->xmin;
130                 int dy= rect->ymax - rect->ymin;
131                 if(ABS(dx)+ABS(dy) > TWEAK_THRESHOLD) {
132                         int theta= (int)floor(4.0f*atan2((float)dy, (float)dx)/M_PI + 0.5);
133                         int val= EVT_GESTURE_W;
134                         
135                         if(theta==0) val= EVT_GESTURE_E;
136                         else if(theta==1) val= EVT_GESTURE_NE;
137                         else if(theta==2) val= EVT_GESTURE_N;
138                         else if(theta==3) val= EVT_GESTURE_NW;
139                         else if(theta==-1) val= EVT_GESTURE_SE;
140                         else if(theta==-2) val= EVT_GESTURE_S;
141                         else if(theta==-3) val= EVT_GESTURE_SW;
142                         
143 #if 0
144                         /* debug */
145                         if(val==1) printf("tweak north\n");
146                         if(val==2) printf("tweak north-east\n");
147                         if(val==3) printf("tweak east\n");
148                         if(val==4) printf("tweak south-east\n");
149                         if(val==5) printf("tweak south\n");
150                         if(val==6) printf("tweak south-west\n");
151                         if(val==7) printf("tweak west\n");
152                         if(val==8) printf("tweak north-west\n");
153 #endif                  
154                         return val;
155                 }
156         }
157         return 0;
158 }
159
160
161 /* ******************* gesture draw ******************* */
162
163 static void wm_gesture_draw_rect(wmGesture *gt)
164 {
165         rcti *rect= (rcti *)gt->customdata;
166         
167         glEnable(GL_BLEND);
168         glColor4f(1.0, 1.0, 1.0, 0.05);
169         glBegin(GL_QUADS);
170         glVertex2s(rect->xmax, rect->ymin);
171         glVertex2s(rect->xmax, rect->ymax);
172         glVertex2s(rect->xmin, rect->ymax);
173         glVertex2s(rect->xmin, rect->ymin);
174         glEnd();
175         glDisable(GL_BLEND);
176         
177         glEnable(GL_LINE_STIPPLE);
178         glColor3ub(96, 96, 96);
179         glLineStipple(1, 0xCCCC);
180         sdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
181         glColor3ub(255, 255, 255);
182         glLineStipple(1, 0x3333);
183         sdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
184         glDisable(GL_LINE_STIPPLE);
185 }
186
187 static void wm_gesture_draw_line(wmGesture *gt)
188 {
189         rcti *rect= (rcti *)gt->customdata;
190         
191         glEnable(GL_LINE_STIPPLE);
192         glColor3ub(96, 96, 96);
193         glLineStipple(1, 0xAAAA);
194         sdrawline(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
195         glColor3ub(255, 255, 255);
196         glLineStipple(1, 0x5555);
197         sdrawline(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
198
199         glDisable(GL_LINE_STIPPLE);
200         
201 }
202
203 static void wm_gesture_draw_circle(wmGesture *gt)
204 {
205         rcti *rect= (rcti *)gt->customdata;
206
207         glTranslatef((float)rect->xmin, (float)rect->ymin, 0.0f);
208
209         glEnable(GL_BLEND);
210         glColor4f(1.0, 1.0, 1.0, 0.05);
211         glutil_draw_filled_arc(0.0, M_PI*2.0, rect->xmax, 40);
212         glDisable(GL_BLEND);
213         
214         glEnable(GL_LINE_STIPPLE);
215         glColor3ub(96, 96, 96);
216         glLineStipple(1, 0xAAAA);
217         glutil_draw_lined_arc(0.0, M_PI*2.0, rect->xmax, 40);
218         glColor3ub(255, 255, 255);
219         glLineStipple(1, 0x5555);
220         glutil_draw_lined_arc(0.0, M_PI*2.0, rect->xmax, 40);
221         
222         glDisable(GL_LINE_STIPPLE);
223         glTranslatef((float)-rect->xmin, (float)-rect->ymin, 0.0f);
224         
225 }
226
227 static void draw_filled_lasso(wmGesture *gt)
228 {
229         EditVert *v=NULL, *lastv=NULL, *firstv=NULL;
230         EditEdge *e;
231         EditFace *efa;
232         short *lasso= (short *)gt->customdata;
233         int i;
234         
235         for (i=0; i<gt->points; i++, lasso+=2) {
236                 float co[3];
237
238                 co[0]= (float)lasso[0];
239                 co[1]= (float)lasso[1];
240                 co[2]= 0.0f;
241
242                 v = BLI_addfillvert(co);
243                 if (lastv)
244                         e = BLI_addfilledge(lastv, v);
245                 lastv = v;
246                 if (firstv==NULL) firstv = v;
247         }
248         
249         /* highly unlikely this will fail, but could crash if (gt->points == 0) */
250         if(firstv) {
251                 BLI_addfilledge(firstv, v);
252                 BLI_edgefill(0);
253         
254                 glEnable(GL_BLEND);
255                 glColor4f(1.0, 1.0, 1.0, 0.05);
256                 glBegin(GL_TRIANGLES);
257                 for (efa = fillfacebase.first; efa; efa=efa->next) {
258                         glVertex2f(efa->v1->co[0], efa->v1->co[1]);
259                         glVertex2f(efa->v2->co[0], efa->v2->co[1]);
260                         glVertex2f(efa->v3->co[0], efa->v3->co[1]);
261                 }
262                 glEnd();
263                 glDisable(GL_BLEND);
264         
265                 BLI_end_edgefill();
266         }
267 }
268
269 static void wm_gesture_draw_lasso(wmGesture *gt)
270 {
271         short *lasso= (short *)gt->customdata;
272         int i;
273
274         draw_filled_lasso(gt);
275         
276         glEnable(GL_LINE_STIPPLE);
277         glColor3ub(96, 96, 96);
278         glLineStipple(1, 0xAAAA);
279         glBegin(GL_LINE_STRIP);
280         for(i=0; i<gt->points; i++, lasso+=2)
281                 glVertex2sv(lasso);
282         if(gt->type==WM_GESTURE_LASSO)
283                 glVertex2sv((short *)gt->customdata);
284         glEnd();
285         
286         glColor3ub(255, 255, 255);
287         glLineStipple(1, 0x5555);
288         glBegin(GL_LINE_STRIP);
289         lasso= (short *)gt->customdata;
290         for(i=0; i<gt->points; i++, lasso+=2)
291                 glVertex2sv(lasso);
292         if(gt->type==WM_GESTURE_LASSO)
293                 glVertex2sv((short *)gt->customdata);
294         glEnd();
295         
296         glDisable(GL_LINE_STIPPLE);
297         
298 }
299
300 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
301 {
302         rcti *rect= (rcti *)gt->customdata;
303         
304         glEnable(GL_LINE_STIPPLE);
305         glColor3ub(96, 96, 96);
306         glLineStipple(1, 0xCCCC);
307         sdrawline(rect->xmin - win->sizex, rect->ymin, rect->xmin + win->sizex, rect->ymin);
308         sdrawline(rect->xmin, rect->ymin - win->sizey, rect->xmin, rect->ymin + win->sizey);
309         
310         glColor3ub(255, 255, 255);
311         glLineStipple(1, 0x3333);
312         sdrawline(rect->xmin - win->sizex, rect->ymin, rect->xmin + win->sizex, rect->ymin);
313         sdrawline(rect->xmin, rect->ymin - win->sizey, rect->xmin, rect->ymin + win->sizey);
314         glDisable(GL_LINE_STIPPLE);
315 }
316
317 /* called in wm_draw.c */
318 void wm_gesture_draw(wmWindow *win)
319 {
320         wmGesture *gt= (wmGesture *)win->gesture.first;
321         
322         for(; gt; gt= gt->next) {
323                 /* all in subwindow space */
324                 wmSubWindowSet(win, gt->swinid);
325                 
326                 if(gt->type==WM_GESTURE_RECT)
327                         wm_gesture_draw_rect(gt);
328 //              else if(gt->type==WM_GESTURE_TWEAK)
329 //                      wm_gesture_draw_line(gt);
330                 else if(gt->type==WM_GESTURE_CIRCLE)
331                         wm_gesture_draw_circle(gt);
332                 else if(gt->type==WM_GESTURE_CROSS_RECT) {
333                         if(gt->mode==1)
334                                 wm_gesture_draw_rect(gt);
335                         else
336                                 wm_gesture_draw_cross(win, gt);
337                 }
338                 else if(gt->type==WM_GESTURE_LINES) 
339                         wm_gesture_draw_lasso(gt);
340                 else if(gt->type==WM_GESTURE_LASSO) 
341                         wm_gesture_draw_lasso(gt);
342                 else if(gt->type==WM_GESTURE_STRAIGHTLINE)
343                         wm_gesture_draw_line(gt);
344         }
345 }
346
347 void wm_gesture_tag_redraw(bContext *C)
348 {
349         wmWindow *win= CTX_wm_window(C);
350         bScreen *screen= CTX_wm_screen(C);
351         ARegion *ar= CTX_wm_region(C);
352         
353         if(screen)
354                 screen->do_draw_gesture= 1;
355
356         wm_tag_redraw_overlay(win, ar);
357 }