Merge branch 'blender2.7'
[blender.git] / source / blender / windowmanager / intern / wm_gesture.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup wm
22  *
23  * Gestures (cursor motions) creating, evaluating and drawing, shared between operators.
24  */
25
26 #include "DNA_screen_types.h"
27 #include "DNA_vec_types.h"
28 #include "DNA_userdef_types.h"
29 #include "DNA_windowmanager_types.h"
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_bitmap_draw_2d.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_lasso_2d.h"
38
39 #include "BKE_context.h"
40
41 #include "WM_api.h"
42 #include "WM_types.h"
43
44 #include "wm.h"
45 #include "wm_draw.h"
46
47 #include "GPU_immediate.h"
48 #include "GPU_immediate_util.h"
49 #include "GPU_state.h"
50
51 #include "BIF_glutil.h"
52
53
54 /* context checked on having screen, window and area */
55 wmGesture *WM_gesture_new(bContext *C, const wmEvent *event, int type)
56 {
57         wmGesture *gesture = MEM_callocN(sizeof(wmGesture), "new gesture");
58         wmWindow *window = CTX_wm_window(C);
59         ARegion *ar = CTX_wm_region(C);
60
61         BLI_addtail(&window->gesture, gesture);
62
63         gesture->type = type;
64         gesture->event_type = event->type;
65         gesture->winrct = ar->winrct;
66         gesture->userdata_free = true;   /* Free if userdata is set. */
67         gesture->modal_state = GESTURE_MODAL_NOP;
68
69         if (ELEM(type, WM_GESTURE_RECT, WM_GESTURE_CROSS_RECT, WM_GESTURE_TWEAK,
70                   WM_GESTURE_CIRCLE, WM_GESTURE_STRAIGHTLINE))
71         {
72                 rcti *rect = MEM_callocN(sizeof(rcti), "gesture rect new");
73
74                 gesture->customdata = rect;
75                 rect->xmin = event->x - gesture->winrct.xmin;
76                 rect->ymin = event->y - gesture->winrct.ymin;
77                 if (type == WM_GESTURE_CIRCLE) {
78                         /* caller is responsible for initializing 'xmax' to radius. */
79                 }
80                 else {
81                         rect->xmax = event->x - gesture->winrct.xmin;
82                         rect->ymax = event->y - gesture->winrct.ymin;
83                 }
84         }
85         else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
86                 short *lasso;
87                 gesture->points_alloc = 1024;
88                 gesture->customdata = lasso = MEM_mallocN(sizeof(short[2]) * gesture->points_alloc, "lasso points");
89                 lasso[0] = event->x - gesture->winrct.xmin;
90                 lasso[1] = event->y - gesture->winrct.ymin;
91                 gesture->points = 1;
92         }
93
94         return gesture;
95 }
96
97 void WM_gesture_end(bContext *C, wmGesture *gesture)
98 {
99         wmWindow *win = CTX_wm_window(C);
100
101         if (win->tweak == gesture)
102                 win->tweak = NULL;
103         BLI_remlink(&win->gesture, gesture);
104         MEM_freeN(gesture->customdata);
105         if (gesture->userdata && gesture->userdata_free) {
106                 MEM_freeN(gesture->userdata);
107         }
108         MEM_freeN(gesture);
109 }
110
111 void WM_gestures_remove(bContext *C)
112 {
113         wmWindow *win = CTX_wm_window(C);
114
115         while (win->gesture.first)
116                 WM_gesture_end(C, win->gesture.first);
117 }
118
119
120 /* tweak and line gestures */
121 int wm_gesture_evaluate(wmGesture *gesture)
122 {
123         if (gesture->type == WM_GESTURE_TWEAK) {
124                 rcti *rect = gesture->customdata;
125                 int dx = BLI_rcti_size_x(rect);
126                 int dy = BLI_rcti_size_y(rect);
127                 float tweak_threshold = U.tweak_threshold * U.dpi_fac;
128                 if (abs(dx) + abs(dy) > tweak_threshold) {
129                         int theta = round_fl_to_int(4.0f * atan2f((float)dy, (float)dx) / (float)M_PI);
130                         int val = EVT_GESTURE_W;
131
132                         if (theta == 0) val = EVT_GESTURE_E;
133                         else if (theta == 1) val = EVT_GESTURE_NE;
134                         else if (theta == 2) val = EVT_GESTURE_N;
135                         else if (theta == 3) val = EVT_GESTURE_NW;
136                         else if (theta == -1) val = EVT_GESTURE_SE;
137                         else if (theta == -2) val = EVT_GESTURE_S;
138                         else if (theta == -3) val = EVT_GESTURE_SW;
139
140 #if 0
141                         /* debug */
142                         if (val == 1) printf("tweak north\n");
143                         if (val == 2) printf("tweak north-east\n");
144                         if (val == 3) printf("tweak east\n");
145                         if (val == 4) printf("tweak south-east\n");
146                         if (val == 5) printf("tweak south\n");
147                         if (val == 6) printf("tweak south-west\n");
148                         if (val == 7) printf("tweak west\n");
149                         if (val == 8) printf("tweak north-west\n");
150 #endif
151                         return val;
152                 }
153         }
154         return 0;
155 }
156
157
158 /* ******************* gesture draw ******************* */
159
160 static void wm_gesture_draw_line(wmGesture *gt)
161 {
162         rcti *rect = (rcti *)gt->customdata;
163
164         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
165
166         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
167
168         float viewport_size[4];
169         glGetFloatv(GL_VIEWPORT, viewport_size);
170         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
171
172         immUniform1i("colors_len", 2);  /* "advanced" mode */
173         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
174         immUniform1f("dash_width", 8.0f);
175
176         float xmin = (float)rect->xmin;
177         float ymin = (float)rect->ymin;
178
179         immBegin(GPU_PRIM_LINES, 2);
180         immVertex2f(shdr_pos, xmin, ymin);
181         immVertex2f(shdr_pos, (float)rect->xmax, (float)rect->ymax);
182         immEnd();
183
184         immUnbindProgram();
185 }
186
187 static void wm_gesture_draw_rect(wmGesture *gt)
188 {
189         rcti *rect = (rcti *)gt->customdata;
190
191         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
192
193         glEnable(GL_BLEND);
194
195         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
196         immUniformColor4f(1.0f, 1.0f, 1.0f, 0.05f);
197
198         immRecti(shdr_pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
199
200         immUnbindProgram();
201
202         glDisable(GL_BLEND);
203
204         shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
205
206         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
207
208         float viewport_size[4];
209         glGetFloatv(GL_VIEWPORT, viewport_size);
210         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
211
212         immUniform1i("colors_len", 2);  /* "advanced" mode */
213         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
214         immUniform1f("dash_width", 8.0f);
215
216         imm_draw_box_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, (float)rect->ymax);
217
218         immUnbindProgram();
219
220         // wm_gesture_draw_line(gt); // draws a diagonal line in the lined box to test wm_gesture_draw_line
221 }
222
223 static void wm_gesture_draw_circle(wmGesture *gt)
224 {
225         rcti *rect = (rcti *)gt->customdata;
226
227         glEnable(GL_BLEND);
228
229         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
230
231         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
232
233         immUniformColor4f(1.0f, 1.0f, 1.0f, 0.05f);
234         imm_draw_circle_fill_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, 40);
235
236         immUnbindProgram();
237
238         glDisable(GL_BLEND);
239
240         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
241
242         float viewport_size[4];
243         glGetFloatv(GL_VIEWPORT, viewport_size);
244         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
245
246         immUniform1i("colors_len", 2);  /* "advanced" mode */
247         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
248         immUniform1f("dash_width", 4.0f);
249
250         imm_draw_circle_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, 40);
251
252         immUnbindProgram();
253 }
254
255 struct LassoFillData {
256         unsigned char *px;
257         int width;
258 };
259
260 static void draw_filled_lasso_px_cb(int x, int x_end, int y, void *user_data)
261 {
262         struct LassoFillData *data = user_data;
263         unsigned char *col = &(data->px[(y * data->width) + x]);
264         memset(col, 0x10, x_end - x);
265 }
266
267 static void draw_filled_lasso(wmGesture *gt)
268 {
269         const short *lasso = (short *)gt->customdata;
270         const int tot = gt->points;
271         int (*moves)[2] = MEM_mallocN(sizeof(*moves) * (tot + 1), __func__);
272         int i;
273         rcti rect;
274         float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
275
276         for (i = 0; i < tot; i++, lasso += 2) {
277                 moves[i][0] = lasso[0];
278                 moves[i][1] = lasso[1];
279         }
280
281         BLI_lasso_boundbox(&rect, (const int (*)[2])moves, tot);
282
283         BLI_rcti_translate(&rect, gt->winrct.xmin, gt->winrct.ymin);
284         BLI_rcti_isect(&gt->winrct, &rect, &rect);
285         BLI_rcti_translate(&rect, -gt->winrct.xmin, -gt->winrct.ymin);
286
287         /* highly unlikely this will fail, but could crash if (tot == 0) */
288         if (BLI_rcti_is_empty(&rect) == false) {
289                 const int w = BLI_rcti_size_x(&rect);
290                 const int h = BLI_rcti_size_y(&rect);
291                 unsigned char *pixel_buf = MEM_callocN(sizeof(*pixel_buf) * w * h, __func__);
292                 struct LassoFillData lasso_fill_data = {pixel_buf, w};
293
294                 BLI_bitmap_draw_2d_poly_v2i_n(
295                        rect.xmin, rect.ymin, rect.xmax, rect.ymax,
296                        (const int (*)[2])moves, tot,
297                        draw_filled_lasso_px_cb, &lasso_fill_data);
298
299                 /* Additive Blending */
300                 glEnable(GL_BLEND);
301                 glBlendFunc(GL_ONE, GL_ONE);
302
303                 GLint unpack_alignment;
304                 glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_alignment);
305
306                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
307
308                 IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
309                 GPU_shader_bind(state.shader);
310                 GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform_ensure(state.shader, "shuffle"), 4, 1, red);
311
312                 immDrawPixelsTex(&state, rect.xmin, rect.ymin, w, h, GL_RED, GL_UNSIGNED_BYTE, GL_NEAREST, pixel_buf, 1.0f, 1.0f, NULL);
313
314                 GPU_shader_unbind();
315
316                 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment);
317
318                 MEM_freeN(pixel_buf);
319
320                 glDisable(GL_BLEND);
321                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
322         }
323
324         MEM_freeN(moves);
325 }
326
327
328 static void wm_gesture_draw_lasso(wmGesture *gt, bool filled)
329 {
330         const short *lasso = (short *)gt->customdata;
331         int i;
332
333         if (filled) {
334                 draw_filled_lasso(gt);
335         }
336
337         const int numverts = gt->points;
338
339         /* Nothing to draw, do early output. */
340         if (numverts < 2) {
341                 return;
342         }
343
344         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
345
346         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
347
348         float viewport_size[4];
349         glGetFloatv(GL_VIEWPORT, viewport_size);
350         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
351
352         immUniform1i("colors_len", 2);  /* "advanced" mode */
353         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
354         immUniform1f("dash_width", 2.0f);
355
356         immBegin((gt->type == WM_GESTURE_LASSO) ? GPU_PRIM_LINE_LOOP : GPU_PRIM_LINE_STRIP, numverts);
357
358         for (i = 0; i < gt->points; i++, lasso += 2) {
359                 immVertex2f(shdr_pos, (float)lasso[0], (float)lasso[1]);
360         }
361
362         immEnd();
363
364         immUnbindProgram();
365 }
366
367 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
368 {
369         rcti *rect = (rcti *)gt->customdata;
370         const int winsize_x = WM_window_pixels_x(win);
371         const int winsize_y = WM_window_pixels_y(win);
372
373         float x1, x2, y1, y2;
374
375         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
376
377         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
378
379         float viewport_size[4];
380         glGetFloatv(GL_VIEWPORT, viewport_size);
381         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
382
383         immUniform1i("colors_len", 2);  /* "advanced" mode */
384         immUniformArray4fv("colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
385         immUniform1f("dash_width", 8.0f);
386
387         immBegin(GPU_PRIM_LINES, 4);
388
389         x1 = (float)(rect->xmin - winsize_x);
390         y1 = (float)rect->ymin;
391         x2 = (float)(rect->xmin + winsize_x);
392         y2 = y1;
393
394         immVertex2f(shdr_pos, x1, y1);
395         immVertex2f(shdr_pos, x2, y2);
396
397         x1 = (float)rect->xmin;
398         y1 = (float)(rect->ymin - winsize_y);
399         x2 = x1;
400         y2 = (float)(rect->ymin + winsize_y);
401
402         immVertex2f(shdr_pos, x1, y1);
403         immVertex2f(shdr_pos, x2, y2);
404
405         immEnd();
406
407         immUnbindProgram();
408 }
409
410 /* called in wm_draw.c */
411 void wm_gesture_draw(wmWindow *win)
412 {
413         wmGesture *gt = (wmGesture *)win->gesture.first;
414
415         GPU_line_width(1.0f);
416         for (; gt; gt = gt->next) {
417                 /* all in subwindow space */
418                 wmViewport(&gt->winrct);
419
420                 if (gt->type == WM_GESTURE_RECT)
421                         wm_gesture_draw_rect(gt);
422 //              else if (gt->type == WM_GESTURE_TWEAK)
423 //                      wm_gesture_draw_line(gt);
424                 else if (gt->type == WM_GESTURE_CIRCLE)
425                         wm_gesture_draw_circle(gt);
426                 else if (gt->type == WM_GESTURE_CROSS_RECT) {
427                         if (gt->is_active) {
428                                 wm_gesture_draw_rect(gt);
429                         }
430                         else {
431                                 wm_gesture_draw_cross(win, gt);
432                         }
433                 }
434                 else if (gt->type == WM_GESTURE_LINES)
435                         wm_gesture_draw_lasso(gt, false);
436                 else if (gt->type == WM_GESTURE_LASSO)
437                         wm_gesture_draw_lasso(gt, true);
438                 else if (gt->type == WM_GESTURE_STRAIGHTLINE)
439                         wm_gesture_draw_line(gt);
440         }
441 }
442
443 void wm_gesture_tag_redraw(bContext *C)
444 {
445         bScreen *screen = CTX_wm_screen(C);
446
447         if (screen)
448                 screen->do_draw_gesture = true;
449 }