2.5
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #define _USE_MATH_DEFINES
30 #include <math.h>
31
32 #include "DNA_screen_types.h"
33 #include "DNA_vec_types.h"
34 #include "DNA_windowmanager_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "BKE_context.h"
41 #include "BKE_utildefines.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "wm.h"
47 #include "wm_event_system.h"
48 #include "wm_subwindow.h"
49
50 #include "ED_screen.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55
56 /* context checked on having screen, window and area */
57 wmGesture *WM_gesture_new(bContext *C, wmEvent *event, int type)
58 {
59         wmGesture *gesture= MEM_callocN(sizeof(wmGesture), "new gesture");
60         wmWindow *window= CTX_wm_window(C);
61         bScreen *screen= CTX_wm_screen(C);
62         int sx, sy;
63         
64         BLI_addtail(&window->gesture, gesture);
65         
66         gesture->type= type;
67         gesture->event_type= event->type;
68         gesture->swinid= screen->subwinactive;  /* means only in area-region context! */
69         
70         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
71         
72         if( ELEM4(type, WM_GESTURE_RECT, WM_GESTURE_CROSS_RECT, WM_GESTURE_TWEAK, WM_GESTURE_CIRCLE)) {
73                 rcti *rect= MEM_callocN(sizeof(rcti), "gesture rect new");
74                 
75                 gesture->customdata= rect;
76                 rect->xmin= event->x - sx;
77                 rect->ymin= event->y - sy;
78                 if(type==WM_GESTURE_CIRCLE)
79                         rect->xmax= 25; // XXX temp
80                 else {
81                         rect->xmax= event->x - sx;
82                         rect->ymax= event->y - sy;
83                 }
84         }
85         else if (type==WM_GESTURE_LASSO) {
86                 short *lasso;
87                 gesture->customdata= lasso= MEM_callocN(2*sizeof(short)*WM_LASSO_MAX_POINTS, "lasso points");
88                 lasso[0] = event->x - sx;
89                 lasso[1] = event->y - sy;
90                 gesture->points= 1;
91         }
92         
93         return gesture;
94 }
95
96 void WM_gesture_end(bContext *C, wmGesture *gesture)
97 {
98         BLI_remlink(&CTX_wm_window(C)->gesture, gesture);
99         MEM_freeN(gesture->customdata);
100         MEM_freeN(gesture);
101 }
102
103 /* tweak and line gestures */
104 #define TWEAK_THRESHOLD         10
105 int wm_gesture_evaluate(bContext *C, wmGesture *gesture)
106 {
107         if(gesture->type==WM_GESTURE_TWEAK) {
108                 rcti *rect= gesture->customdata;
109                 int dx= rect->xmax - rect->xmin;
110                 int dy= rect->ymax - rect->ymin;
111                 if(ABS(dx)+ABS(dy) > TWEAK_THRESHOLD) {
112                         int theta= (int)floor(4.0f*atan2((float)dy, (float)dx)/M_PI + 0.5);
113                         int val= EVT_GESTURE_W;
114                         
115                         if(theta==0) val= EVT_GESTURE_E;
116                         else if(theta==1) val= EVT_GESTURE_NE;
117                         else if(theta==2) val= EVT_GESTURE_N;
118                         else if(theta==3) val= EVT_GESTURE_NW;
119                         else if(theta==-1) val= EVT_GESTURE_SE;
120                         else if(theta==-2) val= EVT_GESTURE_S;
121                         else if(theta==-3) val= EVT_GESTURE_SW;
122                         
123 #if 0
124                         /* debug */
125                         if(val==1) printf("tweak north\n");
126                         if(val==2) printf("tweak north-east\n");
127                         if(val==3) printf("tweak east\n");
128                         if(val==4) printf("tweak south-east\n");
129                         if(val==5) printf("tweak south\n");
130                         if(val==6) printf("tweak south-west\n");
131                         if(val==7) printf("tweak west\n");
132                         if(val==8) printf("tweak north-west\n");
133 #endif                  
134                         return val;
135                 }
136         }
137         return 0;
138 }
139
140
141 /* ******************* gesture draw ******************* */
142
143 static void wm_gesture_draw_rect(wmWindow *win, wmGesture *gt)
144 {
145         rcti *rect= (rcti *)gt->customdata;
146         
147         glEnable(GL_LINE_STIPPLE);
148         glColor3ub(0, 0, 0);
149         glLineStipple(1, 0xCCCC);
150         sdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
151         glColor3ub(255, 255, 255);
152         glLineStipple(1, 0x3333);
153         sdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
154         glDisable(GL_LINE_STIPPLE);
155 }
156
157 static void wm_gesture_draw_line(wmWindow *win, wmGesture *gt)
158 {
159         rcti *rect= (rcti *)gt->customdata;
160         
161         glEnable(GL_LINE_STIPPLE);
162         glColor3ub(0, 0, 0);
163         glLineStipple(1, 0xAAAA);
164         sdrawline(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
165         glColor3ub(255, 255, 255);
166         glLineStipple(1, 0x5555);
167         sdrawline(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
168
169         glDisable(GL_LINE_STIPPLE);
170         
171 }
172
173 static void wm_gesture_draw_circle(wmWindow *win, wmGesture *gt)
174 {
175         rcti *rect= (rcti *)gt->customdata;
176
177         glTranslatef((float)rect->xmin, (float)rect->ymin, 0.0f);
178
179         glEnable(GL_LINE_STIPPLE);
180         glColor3ub(0, 0, 0);
181         glLineStipple(1, 0xAAAA);
182         glutil_draw_lined_arc(0.0, M_PI*2.0, rect->xmax, 40);
183         glColor3ub(255, 255, 255);
184         glLineStipple(1, 0x5555);
185         glutil_draw_lined_arc(0.0, M_PI*2.0, rect->xmax, 40);
186         
187         glDisable(GL_LINE_STIPPLE);
188         glTranslatef((float)-rect->xmin, (float)-rect->ymin, 0.0f);
189         
190 }
191
192 static void wm_gesture_draw_lasso(wmWindow *win, wmGesture *gt)
193 {
194         short *lasso= (short *)gt->customdata;
195         int i;
196         
197         glEnable(GL_LINE_STIPPLE);
198         glColor3ub(0, 0, 0);
199         glLineStipple(1, 0xAAAA);
200         glBegin(GL_LINE_STRIP);
201         for(i=0; i<gt->points; i++, lasso+=2)
202                 glVertex2sv(lasso);
203         glEnd();
204         
205         glColor3ub(255, 255, 255);
206         glLineStipple(1, 0x5555);
207         glBegin(GL_LINE_STRIP);
208         lasso= (short *)gt->customdata;
209         for(i=0; i<gt->points; i++, lasso+=2)
210                 glVertex2sv(lasso);
211         glEnd();
212         
213         glDisable(GL_LINE_STIPPLE);
214         
215 }
216
217 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
218 {
219         rcti *rect= (rcti *)gt->customdata;
220         
221         glEnable(GL_LINE_STIPPLE);
222         glColor3ub(0, 0, 0);
223         glLineStipple(1, 0xCCCC);
224         sdrawline(rect->xmin - win->sizex, rect->ymin, rect->xmin + win->sizex, rect->ymin);
225         sdrawline(rect->xmin, rect->ymin - win->sizey, rect->xmin, rect->ymin + win->sizey);
226         
227         glColor3ub(255, 255, 255);
228         glLineStipple(1, 0x3333);
229         sdrawline(rect->xmin - win->sizex, rect->ymin, rect->xmin + win->sizex, rect->ymin);
230         sdrawline(rect->xmin, rect->ymin - win->sizey, rect->xmin, rect->ymin + win->sizey);
231         glDisable(GL_LINE_STIPPLE);
232 }
233
234 /* called in wm_event_system.c */
235 void wm_gesture_draw(wmWindow *win)
236 {
237         wmGesture *gt= (wmGesture *)win->gesture.first;
238         
239         for(; gt; gt= gt->next) {
240                 /* all in subwindow space */
241                 wmSubWindowSet(win, gt->swinid);
242                 
243                 if(gt->type==WM_GESTURE_RECT)
244                         wm_gesture_draw_rect(win, gt);
245                 else if(gt->type==WM_GESTURE_TWEAK)
246                         wm_gesture_draw_line(win, gt);
247                 else if(gt->type==WM_GESTURE_CIRCLE)
248                         wm_gesture_draw_circle(win, gt);
249                 else if(gt->type==WM_GESTURE_CROSS_RECT) {
250                         if(gt->mode==1)
251                                 wm_gesture_draw_rect(win, gt);
252                         else
253                                 wm_gesture_draw_cross(win, gt);
254                 }
255                 else if(gt->type==WM_GESTURE_LASSO) 
256                         wm_gesture_draw_lasso(win, gt);
257         }
258 }
259
260 void wm_gesture_tag_redraw(bContext *C)
261 {
262         bScreen *screen= CTX_wm_screen(C);
263         ARegion *ar= CTX_wm_region(C);
264         
265         if(screen)
266                 screen->do_gesture= 1;
267         if(ar)
268                 ED_region_tag_redraw(ar);
269 }