2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/windowmanager/intern/wm_gesture.c
30 * Gestures (cursor motions) creating, evaluating and drawing, shared between operators.
33 #include "DNA_screen_types.h"
34 #include "DNA_vec_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_bitmap_draw_2d.h"
41 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_lasso_2d.h"
46 #include "BKE_context.h"
54 #include "GPU_immediate.h"
55 #include "GPU_immediate_util.h"
56 #include "GPU_state.h"
58 #include "BIF_glutil.h"
61 /* context checked on having screen, window and area */
62 wmGesture *WM_gesture_new(bContext *C, const wmEvent *event, int type)
64 wmGesture *gesture = MEM_callocN(sizeof(wmGesture), "new gesture");
65 wmWindow *window = CTX_wm_window(C);
66 ARegion *ar = CTX_wm_region(C);
68 BLI_addtail(&window->gesture, gesture);
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;
76 if (ELEM(type, WM_GESTURE_RECT, WM_GESTURE_CROSS_RECT, WM_GESTURE_TWEAK,
77 WM_GESTURE_CIRCLE, WM_GESTURE_STRAIGHTLINE))
79 rcti *rect = MEM_callocN(sizeof(rcti), "gesture rect new");
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. */
88 rect->xmax = event->x - gesture->winrct.xmin;
89 rect->ymax = event->y - gesture->winrct.ymin;
92 else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_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;
104 void WM_gesture_end(bContext *C, wmGesture *gesture)
106 wmWindow *win = CTX_wm_window(C);
108 if (win->tweak == gesture)
110 BLI_remlink(&win->gesture, gesture);
111 MEM_freeN(gesture->customdata);
112 if (gesture->userdata && gesture->userdata_free) {
113 MEM_freeN(gesture->userdata);
118 void WM_gestures_remove(bContext *C)
120 wmWindow *win = CTX_wm_window(C);
122 while (win->gesture.first)
123 WM_gesture_end(C, win->gesture.first);
127 /* tweak and line gestures */
128 int wm_gesture_evaluate(wmGesture *gesture)
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;
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;
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");
165 /* ******************* gesture draw ******************* */
167 static void wm_gesture_draw_line(wmGesture *gt)
169 rcti *rect = (rcti *)gt->customdata;
171 uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
173 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
175 float viewport_size[4];
176 glGetFloatv(GL_VIEWPORT, viewport_size);
177 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
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);
183 float xmin = (float)rect->xmin;
184 float ymin = (float)rect->ymin;
186 immBegin(GPU_PRIM_LINES, 2);
187 immVertex2f(shdr_pos, xmin, ymin);
188 immVertex2f(shdr_pos, (float)rect->xmax, (float)rect->ymax);
194 static void wm_gesture_draw_rect(wmGesture *gt)
196 rcti *rect = (rcti *)gt->customdata;
198 uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
202 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
203 immUniformColor4f(1.0f, 1.0f, 1.0f, 0.05f);
205 immRecti(shdr_pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
211 shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
213 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
215 float viewport_size[4];
216 glGetFloatv(GL_VIEWPORT, viewport_size);
217 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
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);
223 imm_draw_box_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, (float)rect->ymax);
227 // wm_gesture_draw_line(gt); // draws a diagonal line in the lined box to test wm_gesture_draw_line
230 static void wm_gesture_draw_circle(wmGesture *gt)
232 rcti *rect = (rcti *)gt->customdata;
236 const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
238 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
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);
247 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
249 float viewport_size[4];
250 glGetFloatv(GL_VIEWPORT, viewport_size);
251 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
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);
257 imm_draw_circle_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, 40);
262 struct LassoFillData {
267 static void draw_filled_lasso_px_cb(int x, int x_end, int y, void *user_data)
269 struct LassoFillData *data = user_data;
270 unsigned char *col = &(data->px[(y * data->width) + x]);
271 memset(col, 0x10, x_end - x);
274 static void draw_filled_lasso(wmGesture *gt)
276 const short *lasso = (short *)gt->customdata;
277 const int tot = gt->points;
278 int (*moves)[2] = MEM_mallocN(sizeof(*moves) * (tot + 1), __func__);
281 float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
283 for (i = 0; i < tot; i++, lasso += 2) {
284 moves[i][0] = lasso[0];
285 moves[i][1] = lasso[1];
288 BLI_lasso_boundbox(&rect, (const int (*)[2])moves, tot);
290 BLI_rcti_translate(&rect, gt->winrct.xmin, gt->winrct.ymin);
291 BLI_rcti_isect(>->winrct, &rect, &rect);
292 BLI_rcti_translate(&rect, -gt->winrct.xmin, -gt->winrct.ymin);
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};
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);
306 /* Additive Blending */
308 glBlendFunc(GL_ONE, GL_ONE);
310 GLint unpack_alignment;
311 glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_alignment);
313 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_ensure(state.shader, "shuffle"), 4, 1, red);
319 immDrawPixelsTex(&state, rect.xmin, rect.ymin, w, h, GL_RED, GL_UNSIGNED_BYTE, GL_NEAREST, pixel_buf, 1.0f, 1.0f, NULL);
323 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment);
325 MEM_freeN(pixel_buf);
328 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
335 static void wm_gesture_draw_lasso(wmGesture *gt, bool filled)
337 const short *lasso = (short *)gt->customdata;
341 draw_filled_lasso(gt);
344 const int numverts = gt->points;
346 /* Nothing to draw, do early output. */
351 const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
353 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
355 float viewport_size[4];
356 glGetFloatv(GL_VIEWPORT, viewport_size);
357 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
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);
363 immBegin((gt->type == WM_GESTURE_LASSO) ? GPU_PRIM_LINE_LOOP : GPU_PRIM_LINE_STRIP, numverts);
365 for (i = 0; i < gt->points; i++, lasso += 2) {
366 immVertex2f(shdr_pos, (float)lasso[0], (float)lasso[1]);
374 static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
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);
380 float x1, x2, y1, y2;
382 const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
384 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
386 float viewport_size[4];
387 glGetFloatv(GL_VIEWPORT, viewport_size);
388 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
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);
394 immBegin(GPU_PRIM_LINES, 4);
396 x1 = (float)(rect->xmin - winsize_x);
397 y1 = (float)rect->ymin;
398 x2 = (float)(rect->xmin + winsize_x);
401 immVertex2f(shdr_pos, x1, y1);
402 immVertex2f(shdr_pos, x2, y2);
404 x1 = (float)rect->xmin;
405 y1 = (float)(rect->ymin - winsize_y);
407 y2 = (float)(rect->ymin + winsize_y);
409 immVertex2f(shdr_pos, x1, y1);
410 immVertex2f(shdr_pos, x2, y2);
417 /* called in wm_draw.c */
418 void wm_gesture_draw(wmWindow *win)
420 wmGesture *gt = (wmGesture *)win->gesture.first;
422 GPU_line_width(1.0f);
423 for (; gt; gt = gt->next) {
424 /* all in subwindow space */
425 wmViewport(>->winrct);
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) {
435 wm_gesture_draw_rect(gt);
438 wm_gesture_draw_cross(win, gt);
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);
450 void wm_gesture_tag_redraw(bContext *C)
452 bScreen *screen = CTX_wm_screen(C);
455 screen->do_draw_gesture = true;