Merge branch 'blender2.7'
[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  * Gestures (cursor motions) creating, evaluating and drawing, shared between operators.
31  */
32
33 #include "DNA_screen_types.h"
34 #include "DNA_vec_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_bitmap_draw_2d.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_lasso_2d.h"
45
46 #include "BKE_context.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "wm.h"
52 #include "wm_draw.h"
53
54 #include "GPU_immediate.h"
55 #include "GPU_immediate_util.h"
56 #include "GPU_state.h"
57
58 #include "BIF_glutil.h"
59
60
61 /* context checked on having screen, window and area */
62 wmGesture *WM_gesture_new(bContext *C, const 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
68         BLI_addtail(&window->gesture, gesture);
69
70         gesture->type = type;
71         gesture->event_type = event->type;
72         gesture->winrct = ar->winrct;
73         gesture->userdata_free = true;   /* Free if userdata is set. */
74         gesture->modal_state = GESTURE_MODAL_NOP;
75
76         if (ELEM(type, WM_GESTURE_RECT, WM_GESTURE_CROSS_RECT, WM_GESTURE_TWEAK,
77                   WM_GESTURE_CIRCLE, WM_GESTURE_STRAIGHTLINE))
78         {
79                 rcti *rect = MEM_callocN(sizeof(rcti), "gesture rect new");
80
81                 gesture->customdata = rect;
82                 rect->xmin = event->x - gesture->winrct.xmin;
83                 rect->ymin = event->y - gesture->winrct.ymin;
84                 if (type == WM_GESTURE_CIRCLE) {
85                         /* caller is responsible for initializing 'xmax' to radius. */
86                 }
87                 else {
88                         rect->xmax = event->x - gesture->winrct.xmin;
89                         rect->ymax = event->y - gesture->winrct.ymin;
90                 }
91         }
92         else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
93                 short *lasso;
94                 gesture->points_alloc = 1024;
95                 gesture->customdata = lasso = MEM_mallocN(sizeof(short[2]) * gesture->points_alloc, "lasso points");
96                 lasso[0] = event->x - gesture->winrct.xmin;
97                 lasso[1] = event->y - gesture->winrct.ymin;
98                 gesture->points = 1;
99         }
100
101         return gesture;
102 }
103
104 void WM_gesture_end(bContext *C, wmGesture *gesture)
105 {
106         wmWindow *win = CTX_wm_window(C);
107
108         if (win->tweak == gesture)
109                 win->tweak = NULL;
110         BLI_remlink(&win->gesture, gesture);
111         MEM_freeN(gesture->customdata);
112         if (gesture->userdata && gesture->userdata_free) {
113                 MEM_freeN(gesture->userdata);
114         }
115         MEM_freeN(gesture);
116 }
117
118 void WM_gestures_remove(bContext *C)
119 {
120         wmWindow *win = CTX_wm_window(C);
121
122         while (win->gesture.first)
123                 WM_gesture_end(C, win->gesture.first);
124 }
125
126
127 /* tweak and line gestures */
128 int wm_gesture_evaluate(wmGesture *gesture)
129 {
130         if (gesture->type == WM_GESTURE_TWEAK) {
131                 rcti *rect = gesture->customdata;
132                 int dx = BLI_rcti_size_x(rect);
133                 int dy = BLI_rcti_size_y(rect);
134                 float tweak_threshold = U.tweak_threshold * U.dpi_fac;
135                 if (abs(dx) + abs(dy) > tweak_threshold) {
136                         int theta = round_fl_to_int(4.0f * atan2f((float)dy, (float)dx) / (float)M_PI);
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_line(wmGesture *gt)
168 {
169         rcti *rect = (rcti *)gt->customdata;
170
171         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
172
173         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
174
175         float viewport_size[4];
176         glGetFloatv(GL_VIEWPORT, viewport_size);
177         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
178
179         immUniform1i("colors_len", 2);  /* "advanced" mode */
180         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
181         immUniform1f("dash_width", 8.0f);
182
183         float xmin = (float)rect->xmin;
184         float ymin = (float)rect->ymin;
185
186         immBegin(GPU_PRIM_LINES, 2);
187         immVertex2f(shdr_pos, xmin, ymin);
188         immVertex2f(shdr_pos, (float)rect->xmax, (float)rect->ymax);
189         immEnd();
190
191         immUnbindProgram();
192 }
193
194 static void wm_gesture_draw_rect(wmGesture *gt)
195 {
196         rcti *rect = (rcti *)gt->customdata;
197
198         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
199
200         glEnable(GL_BLEND);
201
202         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
203         immUniformColor4f(1.0f, 1.0f, 1.0f, 0.05f);
204
205         immRecti(shdr_pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
206
207         immUnbindProgram();
208
209         glDisable(GL_BLEND);
210
211         shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
212
213         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
214
215         float viewport_size[4];
216         glGetFloatv(GL_VIEWPORT, viewport_size);
217         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
218
219         immUniform1i("colors_len", 2);  /* "advanced" mode */
220         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
221         immUniform1f("dash_width", 8.0f);
222
223         imm_draw_box_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, (float)rect->ymax);
224
225         immUnbindProgram();
226
227         // wm_gesture_draw_line(gt); // draws a diagonal line in the lined box to test wm_gesture_draw_line
228 }
229
230 static void wm_gesture_draw_circle(wmGesture *gt)
231 {
232         rcti *rect = (rcti *)gt->customdata;
233
234         glEnable(GL_BLEND);
235
236         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
237
238         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
239
240         immUniformColor4f(1.0f, 1.0f, 1.0f, 0.05f);
241         imm_draw_circle_fill_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, 40);
242
243         immUnbindProgram();
244
245         glDisable(GL_BLEND);
246
247         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
248
249         float viewport_size[4];
250         glGetFloatv(GL_VIEWPORT, viewport_size);
251         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
252
253         immUniform1i("colors_len", 2);  /* "advanced" mode */
254         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
255         immUniform1f("dash_width", 4.0f);
256
257         imm_draw_circle_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, 40);
258
259         immUnbindProgram();
260 }
261
262 struct LassoFillData {
263         unsigned char *px;
264         int width;
265 };
266
267 static void draw_filled_lasso_px_cb(int x, int x_end, int y, void *user_data)
268 {
269         struct LassoFillData *data = user_data;
270         unsigned char *col = &(data->px[(y * data->width) + x]);
271         memset(col, 0x10, x_end - x);
272 }
273
274 static void draw_filled_lasso(wmGesture *gt)
275 {
276         const short *lasso = (short *)gt->customdata;
277         const int tot = gt->points;
278         int (*moves)[2] = MEM_mallocN(sizeof(*moves) * (tot + 1), __func__);
279         int i;
280         rcti rect;
281         float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
282
283         for (i = 0; i < tot; i++, lasso += 2) {
284                 moves[i][0] = lasso[0];
285                 moves[i][1] = lasso[1];
286         }
287
288         BLI_lasso_boundbox(&rect, (const int (*)[2])moves, tot);
289
290         BLI_rcti_translate(&rect, gt->winrct.xmin, gt->winrct.ymin);
291         BLI_rcti_isect(&gt->winrct, &rect, &rect);
292         BLI_rcti_translate(&rect, -gt->winrct.xmin, -gt->winrct.ymin);
293
294         /* highly unlikely this will fail, but could crash if (tot == 0) */
295         if (BLI_rcti_is_empty(&rect) == false) {
296                 const int w = BLI_rcti_size_x(&rect);
297                 const int h = BLI_rcti_size_y(&rect);
298                 unsigned char *pixel_buf = MEM_callocN(sizeof(*pixel_buf) * w * h, __func__);
299                 struct LassoFillData lasso_fill_data = {pixel_buf, w};
300
301                 BLI_bitmap_draw_2d_poly_v2i_n(
302                        rect.xmin, rect.ymin, rect.xmax, rect.ymax,
303                        (const int (*)[2])moves, tot,
304                        draw_filled_lasso_px_cb, &lasso_fill_data);
305
306                 /* Additive Blending */
307                 glEnable(GL_BLEND);
308                 glBlendFunc(GL_ONE, GL_ONE);
309
310                 GLint unpack_alignment;
311                 glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_alignment);
312
313                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
314
315                 IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
316                 GPU_shader_bind(state.shader);
317                 GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, red);
318
319                 immDrawPixelsTex(&state, rect.xmin, rect.ymin, w, h, GL_RED, GL_UNSIGNED_BYTE, GL_NEAREST, pixel_buf, 1.0f, 1.0f, NULL);
320
321                 GPU_shader_unbind();
322
323                 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment);
324
325                 MEM_freeN(pixel_buf);
326
327                 glDisable(GL_BLEND);
328                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
329         }
330
331         MEM_freeN(moves);
332 }
333
334
335 static void wm_gesture_draw_lasso(wmGesture *gt, bool filled)
336 {
337         const short *lasso = (short *)gt->customdata;
338         int i;
339
340         if (filled) {
341                 draw_filled_lasso(gt);
342         }
343
344         const int numverts = gt->points;
345
346         /* Nothing to draw, do early output. */
347         if (numverts < 2) {
348                 return;
349         }
350
351         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
352
353         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
354
355         float viewport_size[4];
356         glGetFloatv(GL_VIEWPORT, viewport_size);
357         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
358
359         immUniform1i("colors_len", 2);  /* "advanced" mode */
360         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
361         immUniform1f("dash_width", 2.0f);
362
363         immBegin((gt->type == WM_GESTURE_LASSO) ? GPU_PRIM_LINE_LOOP : GPU_PRIM_LINE_STRIP, numverts);
364
365         for (i = 0; i < gt->points; i++, lasso += 2) {
366                 immVertex2f(shdr_pos, (float)lasso[0], (float)lasso[1]);
367         }
368
369         immEnd();
370
371         immUnbindProgram();
372 }
373
374 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
375 {
376         rcti *rect = (rcti *)gt->customdata;
377         const int winsize_x = WM_window_pixels_x(win);
378         const int winsize_y = WM_window_pixels_y(win);
379
380         float x1, x2, y1, y2;
381
382         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
383
384         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
385
386         float viewport_size[4];
387         glGetFloatv(GL_VIEWPORT, viewport_size);
388         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
389
390         immUniform1i("colors_len", 2);  /* "advanced" mode */
391         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
392         immUniform1f("dash_width", 8.0f);
393
394         immBegin(GPU_PRIM_LINES, 4);
395
396         x1 = (float)(rect->xmin - winsize_x);
397         y1 = (float)rect->ymin;
398         x2 = (float)(rect->xmin + winsize_x);
399         y2 = y1;
400
401         immVertex2f(shdr_pos, x1, y1);
402         immVertex2f(shdr_pos, x2, y2);
403
404         x1 = (float)rect->xmin;
405         y1 = (float)(rect->ymin - winsize_y);
406         x2 = x1;
407         y2 = (float)(rect->ymin + winsize_y);
408
409         immVertex2f(shdr_pos, x1, y1);
410         immVertex2f(shdr_pos, x2, y2);
411
412         immEnd();
413
414         immUnbindProgram();
415 }
416
417 /* called in wm_draw.c */
418 void wm_gesture_draw(wmWindow *win)
419 {
420         wmGesture *gt = (wmGesture *)win->gesture.first;
421
422         GPU_line_width(1.0f);
423         for (; gt; gt = gt->next) {
424                 /* all in subwindow space */
425                 wmViewport(&gt->winrct);
426
427                 if (gt->type == WM_GESTURE_RECT)
428                         wm_gesture_draw_rect(gt);
429 //              else if (gt->type == WM_GESTURE_TWEAK)
430 //                      wm_gesture_draw_line(gt);
431                 else if (gt->type == WM_GESTURE_CIRCLE)
432                         wm_gesture_draw_circle(gt);
433                 else if (gt->type == WM_GESTURE_CROSS_RECT) {
434                         if (gt->is_active) {
435                                 wm_gesture_draw_rect(gt);
436                         }
437                         else {
438                                 wm_gesture_draw_cross(win, gt);
439                         }
440                 }
441                 else if (gt->type == WM_GESTURE_LINES)
442                         wm_gesture_draw_lasso(gt, false);
443                 else if (gt->type == WM_GESTURE_LASSO)
444                         wm_gesture_draw_lasso(gt, true);
445                 else if (gt->type == WM_GESTURE_STRAIGHTLINE)
446                         wm_gesture_draw_line(gt);
447         }
448 }
449
450 void wm_gesture_tag_redraw(bContext *C)
451 {
452         bScreen *screen = CTX_wm_screen(C);
453
454         if (screen)
455                 screen->do_draw_gesture = true;
456 }