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