4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 #define _USE_MATH_DEFINES
32 #include "DNA_screen_types.h"
33 #include "DNA_vec_types.h"
34 #include "DNA_windowmanager_types.h"
36 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
40 #include "BKE_context.h"
41 #include "BKE_utildefines.h"
47 #include "wm_event_system.h"
48 #include "wm_subwindow.h"
50 #include "ED_screen.h"
53 #include "BIF_glutil.h"
56 /* context checked on having screen, window and area */
57 wmGesture *WM_gesture_new(bContext *C, wmEvent *event, int type)
59 wmGesture *gesture= MEM_callocN(sizeof(wmGesture), "new gesture");
60 wmWindow *window= CTX_wm_window(C);
61 bScreen *screen= CTX_wm_screen(C);
64 BLI_addtail(&window->gesture, gesture);
67 gesture->event_type= event->type;
68 gesture->swinid= screen->subwinactive; /* means only in area-region context! */
70 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
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");
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
81 rect->xmax= event->x - sx;
82 rect->ymax= event->y - sy;
85 else if (type==WM_GESTURE_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;
96 void WM_gesture_end(bContext *C, wmGesture *gesture)
98 BLI_remlink(&CTX_wm_window(C)->gesture, gesture);
99 MEM_freeN(gesture->customdata);
103 /* tweak and line gestures */
104 #define TWEAK_THRESHOLD 10
105 int wm_gesture_evaluate(bContext *C, wmGesture *gesture)
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;
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;
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");
141 /* ******************* gesture draw ******************* */
143 static void wm_gesture_draw_rect(wmWindow *win, wmGesture *gt)
145 rcti *rect= (rcti *)gt->customdata;
147 glEnable(GL_LINE_STIPPLE);
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);
157 static void wm_gesture_draw_line(wmWindow *win, wmGesture *gt)
159 rcti *rect= (rcti *)gt->customdata;
161 glEnable(GL_LINE_STIPPLE);
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);
169 glDisable(GL_LINE_STIPPLE);
173 static void wm_gesture_draw_circle(wmWindow *win, wmGesture *gt)
175 rcti *rect= (rcti *)gt->customdata;
177 glTranslatef((float)rect->xmin, (float)rect->ymin, 0.0f);
179 glEnable(GL_LINE_STIPPLE);
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);
187 glDisable(GL_LINE_STIPPLE);
188 glTranslatef((float)-rect->xmin, (float)-rect->ymin, 0.0f);
192 static void wm_gesture_draw_lasso(wmWindow *win, wmGesture *gt)
194 short *lasso= (short *)gt->customdata;
197 glEnable(GL_LINE_STIPPLE);
199 glLineStipple(1, 0xAAAA);
200 glBegin(GL_LINE_STRIP);
201 for(i=0; i<gt->points; i++, lasso+=2)
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)
213 glDisable(GL_LINE_STIPPLE);
217 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
219 rcti *rect= (rcti *)gt->customdata;
221 glEnable(GL_LINE_STIPPLE);
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);
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);
234 /* called in wm_event_system.c */
235 void wm_gesture_draw(wmWindow *win)
237 wmGesture *gt= (wmGesture *)win->gesture.first;
239 for(; gt; gt= gt->next) {
240 /* all in subwindow space */
241 wmSubWindowSet(win, gt->swinid);
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) {
251 wm_gesture_draw_rect(win, gt);
253 wm_gesture_draw_cross(win, gt);
255 else if(gt->type==WM_GESTURE_LASSO)
256 wm_gesture_draw_lasso(win, gt);
260 void wm_gesture_tag_redraw(bContext *C)
262 bScreen *screen= CTX_wm_screen(C);
263 ARegion *ar= CTX_wm_region(C);
266 screen->do_gesture= 1;
268 ED_region_tag_redraw(ar);