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 #include "DNA_screen_types.h"
30 #include "DNA_vec_types.h"
31 #include "DNA_userdef_types.h"
32 #include "DNA_windowmanager_types.h"
34 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_editVert.h" /* lasso tessellation */
39 #include "BLI_scanfill.h" /* lasso tessellation */
41 #include "BKE_context.h"
42 #include "BKE_utildefines.h"
48 #include "wm_event_system.h"
49 #include "wm_subwindow.h"
51 #include "ED_screen.h"
54 #include "BIF_glutil.h"
57 /* context checked on having screen, window and area */
58 wmGesture *WM_gesture_new(bContext *C, wmEvent *event, int type)
60 wmGesture *gesture= MEM_callocN(sizeof(wmGesture), "new gesture");
61 wmWindow *window= CTX_wm_window(C);
62 ARegion *ar= CTX_wm_region(C);
65 BLI_addtail(&window->gesture, gesture);
68 gesture->event_type= event->type;
69 gesture->swinid= ar->swinid; /* means only in area-region context! */
71 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
73 if( ELEM4(type, WM_GESTURE_RECT, WM_GESTURE_CROSS_RECT, WM_GESTURE_TWEAK, WM_GESTURE_CIRCLE)) {
74 rcti *rect= MEM_callocN(sizeof(rcti), "gesture rect new");
76 gesture->customdata= rect;
77 rect->xmin= event->x - sx;
78 rect->ymin= event->y - sy;
79 if(type==WM_GESTURE_CIRCLE) {
81 rect->xmax= circle_select_size;
83 rect->xmax= 25; // XXX temp
86 rect->xmax= event->x - sx;
87 rect->ymax= event->y - sy;
90 else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
92 gesture->customdata= lasso= MEM_callocN(2*sizeof(short)*WM_LASSO_MIN_POINTS, "lasso points");
93 lasso[0] = event->x - sx;
94 lasso[1] = event->y - sy;
96 gesture->size = WM_LASSO_MIN_POINTS;
102 void WM_gesture_end(bContext *C, wmGesture *gesture)
104 wmWindow *win= CTX_wm_window(C);
106 if(win->tweak==gesture)
108 BLI_remlink(&win->gesture, gesture);
109 MEM_freeN(gesture->customdata);
113 void WM_gestures_remove(bContext *C)
115 wmWindow *win= CTX_wm_window(C);
117 while(win->gesture.first)
118 WM_gesture_end(C, win->gesture.first);
122 /* tweak and line gestures */
123 #define TWEAK_THRESHOLD 10
124 int wm_gesture_evaluate(bContext *C, wmGesture *gesture)
126 if(gesture->type==WM_GESTURE_TWEAK) {
127 rcti *rect= gesture->customdata;
128 int dx= rect->xmax - rect->xmin;
129 int dy= rect->ymax - rect->ymin;
130 if(ABS(dx)+ABS(dy) > TWEAK_THRESHOLD) {
131 int theta= (int)floor(4.0f*atan2((float)dy, (float)dx)/M_PI + 0.5);
132 int val= EVT_GESTURE_W;
134 if(theta==0) val= EVT_GESTURE_E;
135 else if(theta==1) val= EVT_GESTURE_NE;
136 else if(theta==2) val= EVT_GESTURE_N;
137 else if(theta==3) val= EVT_GESTURE_NW;
138 else if(theta==-1) val= EVT_GESTURE_SE;
139 else if(theta==-2) val= EVT_GESTURE_S;
140 else if(theta==-3) val= EVT_GESTURE_SW;
144 if(val==1) printf("tweak north\n");
145 if(val==2) printf("tweak north-east\n");
146 if(val==3) printf("tweak east\n");
147 if(val==4) printf("tweak south-east\n");
148 if(val==5) printf("tweak south\n");
149 if(val==6) printf("tweak south-west\n");
150 if(val==7) printf("tweak west\n");
151 if(val==8) printf("tweak north-west\n");
160 /* ******************* gesture draw ******************* */
162 static void wm_gesture_draw_rect(wmWindow *win, wmGesture *gt)
164 rcti *rect= (rcti *)gt->customdata;
167 glColor4f(1.0, 1.0, 1.0, 0.05);
169 glVertex2s(rect->xmax, rect->ymin);
170 glVertex2s(rect->xmax, rect->ymax);
171 glVertex2s(rect->xmin, rect->ymax);
172 glVertex2s(rect->xmin, rect->ymin);
176 glEnable(GL_LINE_STIPPLE);
177 glColor3ub(96, 96, 96);
178 glLineStipple(1, 0xCCCC);
179 sdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
180 glColor3ub(255, 255, 255);
181 glLineStipple(1, 0x3333);
182 sdrawbox(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
183 glDisable(GL_LINE_STIPPLE);
186 static void wm_gesture_draw_line(wmWindow *win, wmGesture *gt)
188 rcti *rect= (rcti *)gt->customdata;
190 glEnable(GL_LINE_STIPPLE);
191 glColor3ub(96, 96, 96);
192 glLineStipple(1, 0xAAAA);
193 sdrawline(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
194 glColor3ub(255, 255, 255);
195 glLineStipple(1, 0x5555);
196 sdrawline(rect->xmin, rect->ymin, rect->xmax, rect->ymax);
198 glDisable(GL_LINE_STIPPLE);
202 static void wm_gesture_draw_circle(wmWindow *win, wmGesture *gt)
204 rcti *rect= (rcti *)gt->customdata;
206 glTranslatef((float)rect->xmin, (float)rect->ymin, 0.0f);
209 glColor4f(1.0, 1.0, 1.0, 0.05);
210 glutil_draw_filled_arc(0.0, M_PI*2.0, rect->xmax, 40);
213 glEnable(GL_LINE_STIPPLE);
214 glColor3ub(96, 96, 96);
215 glLineStipple(1, 0xAAAA);
216 glutil_draw_lined_arc(0.0, M_PI*2.0, rect->xmax, 40);
217 glColor3ub(255, 255, 255);
218 glLineStipple(1, 0x5555);
219 glutil_draw_lined_arc(0.0, M_PI*2.0, rect->xmax, 40);
221 glDisable(GL_LINE_STIPPLE);
222 glTranslatef((float)-rect->xmin, (float)-rect->ymin, 0.0f);
226 static void draw_filled_lasso(wmGesture *gt)
228 EditVert *v=NULL, *lastv=NULL, *firstv=NULL;
231 short *lasso= (short *)gt->customdata;
234 for (i=0; i<gt->points; i++, lasso+=2) {
235 float co[3] = {(float)lasso[0], (float)lasso[1], 0.f};
237 v = BLI_addfillvert(co);
239 e = BLI_addfilledge(lastv, v);
241 if (firstv==NULL) firstv = v;
244 /* highly unlikely this will fail, but could crash if (gt->points == 0) */
246 BLI_addfilledge(firstv, v);
250 glColor4f(1.0, 1.0, 1.0, 0.05);
251 glBegin(GL_TRIANGLES);
252 for (efa = fillfacebase.first; efa; efa=efa->next) {
253 glVertex2f(efa->v1->co[0], efa->v1->co[1]);
254 glVertex2f(efa->v2->co[0], efa->v2->co[1]);
255 glVertex2f(efa->v3->co[0], efa->v3->co[1]);
264 static void wm_gesture_draw_lasso(wmWindow *win, wmGesture *gt)
266 short *lasso= (short *)gt->customdata;
269 draw_filled_lasso(gt);
271 glEnable(GL_LINE_STIPPLE);
272 glColor3ub(96, 96, 96);
273 glLineStipple(1, 0xAAAA);
274 glBegin(GL_LINE_STRIP);
275 lasso= (short *)gt->customdata;
276 for(i=0; i<gt->points; i++, lasso+=2)
278 if(gt->type==WM_GESTURE_LASSO)
279 glVertex2sv((short *)gt->customdata);
282 glColor3ub(255, 255, 255);
283 glLineStipple(1, 0x5555);
284 glBegin(GL_LINE_STRIP);
285 lasso= (short *)gt->customdata;
286 for(i=0; i<gt->points; i++, lasso+=2)
288 if(gt->type==WM_GESTURE_LASSO)
289 glVertex2sv((short *)gt->customdata);
292 glDisable(GL_LINE_STIPPLE);
296 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
298 rcti *rect= (rcti *)gt->customdata;
300 glEnable(GL_LINE_STIPPLE);
301 glColor3ub(96, 96, 96);
302 glLineStipple(1, 0xCCCC);
303 sdrawline(rect->xmin - win->sizex, rect->ymin, rect->xmin + win->sizex, rect->ymin);
304 sdrawline(rect->xmin, rect->ymin - win->sizey, rect->xmin, rect->ymin + win->sizey);
306 glColor3ub(255, 255, 255);
307 glLineStipple(1, 0x3333);
308 sdrawline(rect->xmin - win->sizex, rect->ymin, rect->xmin + win->sizex, rect->ymin);
309 sdrawline(rect->xmin, rect->ymin - win->sizey, rect->xmin, rect->ymin + win->sizey);
310 glDisable(GL_LINE_STIPPLE);
313 /* called in wm_draw.c */
314 void wm_gesture_draw(wmWindow *win)
316 wmGesture *gt= (wmGesture *)win->gesture.first;
318 for(; gt; gt= gt->next) {
319 /* all in subwindow space */
320 wmSubWindowSet(win, gt->swinid);
323 if(gt->type==WM_GESTURE_RECT)
324 wm_gesture_draw_rect(win, gt);
325 else if(gt->type==WM_GESTURE_TWEAK)
326 wm_gesture_draw_line(win, gt);
327 else if(gt->type==WM_GESTURE_CIRCLE)
328 wm_gesture_draw_circle(win, gt);
329 else if(gt->type==WM_GESTURE_CROSS_RECT) {
331 wm_gesture_draw_rect(win, gt);
333 wm_gesture_draw_cross(win, gt);
335 else if(gt->type==WM_GESTURE_LINES)
336 wm_gesture_draw_lasso(win, gt);
337 else if(gt->type==WM_GESTURE_LASSO)
338 wm_gesture_draw_lasso(win, gt);
342 void wm_gesture_tag_redraw(bContext *C)
344 wmWindow *win= CTX_wm_window(C);
345 bScreen *screen= CTX_wm_screen(C);
346 ARegion *ar= CTX_wm_region(C);
349 screen->do_draw_gesture= 1;
350 if(ar && win->drawmethod != USER_DRAW_TRIPLE)
351 ED_region_tag_redraw(ar);