Gizmo: use ED_view3d_win_to_3d_on_plane
[blender.git] / source / blender / editors / gizmo_library / gizmo_types / dial3d_gizmo.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) 2014 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file dial3d_gizmo.c
27  *  \ingroup edgizmolib
28  *
29  * \name Dial Gizmo
30  *
31  * 3D Gizmo
32  *
33  * \brief Circle shaped gizmo for circular interaction.
34  * Currently no own handling, use with operator only.
35  *
36  * - `matrix[0]` is derived from Y and Z.
37  * - `matrix[1]` is 'up' when DialGizmo.use_start_y_axis is set.
38  * - `matrix[2]` is the axis the dial rotates around (all dials).
39  */
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_math.h"
44
45 #include "BKE_context.h"
46
47 #include "BIF_gl.h"
48 #include "BIF_glutil.h"
49
50 #include "GPU_immediate.h"
51 #include "GPU_immediate_util.h"
52 #include "GPU_matrix.h"
53 #include "GPU_select.h"
54 #include "GPU_state.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "ED_screen.h"
63 #include "ED_view3d.h"
64 #include "ED_gizmo_library.h"
65
66 /* own includes */
67 #include "../gizmo_geometry.h"
68 #include "../gizmo_library_intern.h"
69
70 /* To use custom dials exported to geom_dial_gizmo.c */
71 // #define USE_GIZMO_CUSTOM_DIAL
72
73 static int gizmo_dial_modal(
74         bContext *C, wmGizmo *gz, const wmEvent *event,
75         eWM_GizmoFlagTweak tweak_flag);
76
77 typedef struct DialInteraction {
78         struct {
79                 float mval[2];
80                 /* Only for when using properties. */
81                 float prop_angle;
82         } init;
83         struct {
84                 /* Cache the last angle to detect rotations bigger than -/+ PI. */
85                 eWM_GizmoFlagTweak tweak_flag;
86                 float angle;
87         } prev;
88
89         /* Number of full rotations. */
90         int rotations;
91
92         /* Final output values, used for drawing. */
93         struct {
94                 float angle_ofs;
95                 float angle_delta;
96         } output;
97 } DialInteraction;
98
99 #define DIAL_WIDTH       1.0f
100 #define DIAL_RESOLUTION 48
101
102 /* Could make option, negative to clip more (don't show when view aligned). */
103 #define DIAL_CLIP_BIAS 0.02
104
105 /* -------------------------------------------------------------------- */
106
107 static void dial_geom_draw(
108         const wmGizmo *gz, const float color[4], const bool select,
109         float axis_modal_mat[4][4], float clip_plane[4], const float arc_inner_factor)
110 {
111 #ifdef USE_GIZMO_CUSTOM_DIAL
112         UNUSED_VARS(gz, axis_modal_mat, clip_plane);
113         wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_dial, select, color);
114 #else
115         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
116         const bool filled = (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_FILL) != 0;
117
118         GPU_line_width(gz->line_width);
119
120         GPUVertFormat *format = immVertexFormat();
121         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
122
123         if (clip_plane) {
124                 immBindBuiltinProgram(GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR);
125                 float clip_plane_f[4] = {clip_plane[0], clip_plane[1], clip_plane[2], clip_plane[3]};
126                 immUniform4fv("ClipPlane", clip_plane_f);
127                 immUniformMatrix4fv("ModelMatrix", axis_modal_mat);
128         }
129         else {
130                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
131         }
132
133         immUniformColor4fv(color);
134
135         if (filled) {
136                 imm_draw_circle_fill_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION);
137         }
138         else {
139                 imm_draw_circle_wire_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION);
140                 if (arc_inner_factor != 0.0f) {
141                         imm_draw_circle_wire_2d(pos, 0, 0, arc_inner_factor, DIAL_RESOLUTION);
142                 }
143         }
144
145         immUnbindProgram();
146
147         UNUSED_VARS(select);
148 #endif
149 }
150
151 /**
152  * Draws a line from (0, 0, 0) to \a co_outer, at \a angle.
153  */
154 static void dial_ghostarc_draw_helpline(
155         const float angle, const float co_outer[3], const float color[4])
156 {
157         GPU_matrix_push();
158         GPU_matrix_rotate_3f(RAD2DEGF(angle), 0.0f, 0.0f, -1.0f);
159
160         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
161
162         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
163
164         immUniformColor4fv(color);
165
166         immBegin(GPU_PRIM_LINE_STRIP, 2);
167         immVertex3f(pos, 0.0f, 0, 0.0f);
168         immVertex3fv(pos, co_outer);
169         immEnd();
170
171         immUnbindProgram();
172
173         GPU_matrix_pop();
174 }
175
176 static void dial_ghostarc_draw(
177         const float angle_ofs, const float angle_delta,
178         const float arc_inner_factor, const float color[4])
179 {
180         const float width_inner = DIAL_WIDTH;
181         GPUVertFormat *format = immVertexFormat();
182         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
183         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
184
185         if (arc_inner_factor != 0.0) {
186                 float color_dark[4] = {0};
187                 color_dark[3] = color[3] / 2;
188                 immUniformColor4fv(color_dark);
189                 imm_draw_disk_partial_fill_2d(
190                         pos, 0, 0, arc_inner_factor, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(M_PI * 2));
191         }
192
193         immUniformColor4fv(color);
194         imm_draw_disk_partial_fill_2d(
195                 pos, 0, 0, arc_inner_factor, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(angle_delta));
196         immUnbindProgram();
197 }
198
199 static void dial_ghostarc_get_angles(
200         const wmGizmo *gz,
201         const wmEvent *event,
202         const ARegion *ar,
203         float mat[4][4], const float co_outer[3],
204         float *r_start, float *r_delta)
205 {
206         DialInteraction *inter = gz->interaction_data;
207         const RegionView3D *rv3d = ar->regiondata;
208         const float mval[2] = {event->x - ar->winrct.xmin, event->y - ar->winrct.ymin};
209
210         /* We might need to invert the direction of the angles. */
211         float view_vec[3], axis_vec[3];
212         ED_view3d_global_to_vector(rv3d, gz->matrix_basis[3], view_vec);
213         normalize_v3_v3(axis_vec, gz->matrix_basis[2]);
214
215         float proj_outer_rel[3];
216         mul_v3_project_m4_v3(proj_outer_rel, mat, co_outer);
217         sub_v3_v3(proj_outer_rel, gz->matrix_basis[3]);
218
219         float proj_mval_new_rel[3];
220         float proj_mval_init_rel[3];
221         float dial_plane[4];
222
223         plane_from_point_normal_v3(dial_plane, gz->matrix_basis[3], axis_vec);
224
225         if (!ED_view3d_win_to_3d_on_plane(ar, dial_plane, inter->init.mval, proj_mval_init_rel)) {
226                 goto fail;
227         }
228         sub_v3_v3(proj_mval_init_rel, gz->matrix_basis[3]);
229
230         if (!ED_view3d_win_to_3d_on_plane(ar, dial_plane, mval, proj_mval_new_rel)) {
231                 goto fail;
232         }
233         sub_v3_v3(proj_mval_new_rel, gz->matrix_basis[3]);
234
235         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
236
237         /* Start direction from mouse or set by user. */
238         const float *proj_init_rel =
239                 (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_START_Y) ?
240                 gz->matrix_basis[1] : proj_mval_init_rel;
241
242         /* Return angles. */
243         const float start = angle_wrap_rad(angle_signed_on_axis_v3v3_v3(proj_outer_rel, proj_init_rel, axis_vec));
244         const float delta = angle_wrap_rad(angle_signed_on_axis_v3v3_v3(proj_mval_init_rel, proj_mval_new_rel, axis_vec));
245
246         /* Change of sign, we passed the 180 degree threshold. This means we need to add a turn
247          * to distinguish between transition from 0 to -1 and -PI to +PI, use comparison with PI/2.
248          * Logic taken from #BLI_dial_angle */
249         if ((delta * inter->prev.angle < 0.0f) &&
250             (fabsf(inter->prev.angle) > (float)M_PI_2))
251         {
252                 if (inter->prev.angle < 0.0f) {
253                         inter->rotations--;
254                 }
255                 else {
256                         inter->rotations++;
257                 }
258         }
259         inter->prev.angle = delta;
260
261         const bool wrap_angle = RNA_boolean_get(gz->ptr, "wrap_angle");
262         const double delta_final = (double)delta + ((2 * M_PI) * (double)inter->rotations);
263         *r_start = start;
264         *r_delta = (float)(wrap_angle ? fmod(delta_final, 2 * M_PI) : delta_final);
265         return;
266
267         /* If we can't project (unlikely). */
268 fail:
269         *r_start = 0.0;
270         *r_delta = 0.0;
271 }
272
273 static void dial_ghostarc_draw_with_helplines(
274         const float angle_ofs, const float angle_delta,
275         const float arc_inner_factor, const float color_helpline[4], const int draw_options)
276 {
277         /* Coordinate at which the arc drawing will be started. */
278         const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f};
279         dial_ghostarc_draw(angle_ofs, angle_delta, arc_inner_factor, (const float[4]){0.8f, 0.8f, 0.8f, 0.4f});
280         GPU_line_width(1.0f);
281         dial_ghostarc_draw_helpline(angle_ofs, co_outer, color_helpline);
282         if (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE) {
283                 GPU_line_width(3.0f);
284         }
285         dial_ghostarc_draw_helpline(angle_ofs + angle_delta, co_outer, color_helpline);
286 }
287
288 static void dial_draw_intern(
289         const bContext *C, wmGizmo *gz,
290         const bool select, const bool highlight, float clip_plane[4])
291 {
292         float matrix_final[4][4];
293         float color[4];
294
295         BLI_assert(CTX_wm_area(C)->spacetype == SPACE_VIEW3D);
296
297         gizmo_color_get(gz, highlight, color);
298
299         WM_gizmo_calc_matrix_final(gz, matrix_final);
300
301         GPU_matrix_push();
302         GPU_matrix_mul(matrix_final);
303
304         /* FIXME(campbell): look into removing this. */
305         if ((gz->flag & WM_GIZMO_DRAW_VALUE) &&
306             (gz->state & WM_GIZMO_STATE_MODAL))
307         {
308                 /* XXX, View3D rotation gizmo doesn't call modal. */
309                 if (!WM_gizmo_target_property_is_valid_any(gz)) {
310                         wmWindow *win = CTX_wm_window(C);
311                         gizmo_dial_modal((bContext *)C, gz, win->eventstate, 0);
312                 }
313         }
314
315         GPU_polygon_smooth(false);
316
317         const float arc_inner_factor = RNA_float_get(gz->ptr, "arc_inner_factor");
318         if (select == false) {
319                 float angle_ofs = 0.0f;
320                 float angle_delta = 0.0f;
321                 bool show_ghostarc = false;
322
323                 /* Draw rotation indicator arc first. */
324                 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
325                 const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
326
327                 if (WM_gizmo_target_property_is_valid(gz_prop) &&
328                     (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE))
329                 {
330                         angle_ofs = 0.0f;
331                         angle_delta = WM_gizmo_target_property_float_get(gz, gz_prop);
332                         show_ghostarc = true;
333                 }
334                 else if ((gz->flag & WM_GIZMO_DRAW_VALUE) &&
335                          (gz->state & WM_GIZMO_STATE_MODAL))
336                 {
337                         DialInteraction *inter = gz->interaction_data;
338                         angle_ofs = inter->output.angle_ofs;
339                         angle_delta = inter->output.angle_delta;
340                         show_ghostarc = true;
341                 }
342
343                 if (show_ghostarc) {
344                         dial_ghostarc_draw_with_helplines(angle_ofs, angle_delta, arc_inner_factor, color, draw_options);
345                         if ((draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_MIRROR) != 0) {
346                                 angle_ofs += M_PI;
347                                 dial_ghostarc_draw_with_helplines(angle_ofs, angle_delta, arc_inner_factor, color, draw_options);
348                         }
349                 }
350         }
351
352         /* Draw actual dial gizmo. */
353         dial_geom_draw(gz, color, select, gz->matrix_basis, clip_plane, arc_inner_factor);
354
355         GPU_matrix_pop();
356 }
357
358 static void gizmo_dial_draw_select(const bContext *C, wmGizmo *gz, int select_id)
359 {
360         float clip_plane_buf[4];
361         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
362         float *clip_plane = (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_CLIP) ? clip_plane_buf : NULL;
363
364         if (clip_plane) {
365                 ARegion *ar = CTX_wm_region(C);
366                 RegionView3D *rv3d = ar->regiondata;
367
368                 copy_v3_v3(clip_plane, rv3d->viewinv[2]);
369                 clip_plane[3] = -dot_v3v3(rv3d->viewinv[2], gz->matrix_basis[3]);
370                 clip_plane[3] += DIAL_CLIP_BIAS * gz->scale_final;
371                 glEnable(GL_CLIP_DISTANCE0);
372         }
373
374         GPU_select_load_id(select_id);
375         dial_draw_intern(C, gz, true, false, clip_plane);
376
377         if (clip_plane) {
378                 glDisable(GL_CLIP_DISTANCE0);
379         }
380 }
381
382 static void gizmo_dial_draw(const bContext *C, wmGizmo *gz)
383 {
384         const bool is_modal = gz->state & WM_GIZMO_STATE_MODAL;
385         const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0;
386         float clip_plane_buf[4];
387         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
388         float *clip_plane = (!is_modal && (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_CLIP)) ? clip_plane_buf : NULL;
389
390         if (clip_plane) {
391                 ARegion *ar = CTX_wm_region(C);
392                 RegionView3D *rv3d = ar->regiondata;
393
394                 copy_v3_v3(clip_plane, rv3d->viewinv[2]);
395                 clip_plane[3] = -dot_v3v3(rv3d->viewinv[2], gz->matrix_basis[3]);
396                 clip_plane[3] += DIAL_CLIP_BIAS * gz->scale_final;
397
398                 glEnable(GL_CLIP_DISTANCE0);
399         }
400
401         GPU_blend(true);
402         dial_draw_intern(C, gz, false, is_highlight, clip_plane);
403         GPU_blend(false);
404
405         if (clip_plane) {
406                 glDisable(GL_CLIP_DISTANCE0);
407         }
408 }
409
410 static int gizmo_dial_modal(
411         bContext *C, wmGizmo *gz, const wmEvent *event,
412         eWM_GizmoFlagTweak tweak_flag)
413 {
414         DialInteraction *inter = gz->interaction_data;
415         if ((event->type != MOUSEMOVE) && (inter->prev.tweak_flag == tweak_flag)) {
416                 return OPERATOR_RUNNING_MODAL;
417         }
418         /* Coordinate at which the arc drawing will be started. */
419         const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f};
420         float angle_ofs, angle_delta;
421
422         dial_ghostarc_get_angles(
423                 gz, event, CTX_wm_region(C), gz->matrix_basis, co_outer, &angle_ofs, &angle_delta);
424
425         if (tweak_flag & WM_GIZMO_TWEAK_SNAP) {
426                 const double snap = DEG2RAD(5);
427                 angle_delta = (float)roundf((double)angle_delta / snap) * snap;
428         }
429         if (tweak_flag & WM_GIZMO_TWEAK_PRECISE) {
430                 angle_delta *= 0.1f;
431         }
432         inter->output.angle_delta = angle_delta;
433         inter->output.angle_ofs = angle_ofs;
434
435         /* Set the property for the operator and call its modal function. */
436         wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
437         if (WM_gizmo_target_property_is_valid(gz_prop)) {
438                 WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init.prop_angle + angle_delta);
439         }
440
441         inter->prev.tweak_flag = tweak_flag;
442
443         return OPERATOR_RUNNING_MODAL;
444 }
445
446
447 static void gizmo_dial_setup(wmGizmo *gz)
448 {
449         const float dir_default[3] = {0.0f, 0.0f, 1.0f};
450
451         /* defaults */
452         copy_v3_v3(gz->matrix_basis[2], dir_default);
453 }
454
455 static int gizmo_dial_invoke(
456         bContext *UNUSED(C), wmGizmo *gz, const wmEvent *event)
457 {
458         DialInteraction *inter = MEM_callocN(sizeof(DialInteraction), __func__);
459
460         inter->init.mval[0] = event->mval[0];
461         inter->init.mval[1] = event->mval[1];
462
463         wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
464         if (WM_gizmo_target_property_is_valid(gz_prop)) {
465                 inter->init.prop_angle = WM_gizmo_target_property_float_get(gz, gz_prop);
466         }
467
468         gz->interaction_data = inter;
469
470         return OPERATOR_RUNNING_MODAL;
471 }
472
473 /* -------------------------------------------------------------------- */
474 /** \name Dial Gizmo API
475  *
476  * \{ */
477
478 static void GIZMO_GT_dial_3d(wmGizmoType *gzt)
479 {
480         /* identifiers */
481         gzt->idname = "GIZMO_GT_dial_3d";
482
483         /* api callbacks */
484         gzt->draw = gizmo_dial_draw;
485         gzt->draw_select = gizmo_dial_draw_select;
486         gzt->setup = gizmo_dial_setup;
487         gzt->invoke = gizmo_dial_invoke;
488         gzt->modal = gizmo_dial_modal;
489
490         gzt->struct_size = sizeof(wmGizmo);
491
492         /* rna */
493         static EnumPropertyItem rna_enum_draw_options[] = {
494                 {ED_GIZMO_DIAL_DRAW_FLAG_CLIP, "CLIP", 0, "Clipped", ""},
495                 {ED_GIZMO_DIAL_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""},
496                 {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_MIRROR, "ANGLE_MIRROR", 0, "Angle Mirror", ""},
497                 {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_START_Y, "ANGLE_START_Y", 0, "Angle Start Y", ""},
498                 {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE, "ANGLE_VALUE", 0, "Show Angle Value", ""},
499                 {0, NULL, 0, NULL, NULL}
500         };
501         RNA_def_enum_flag(gzt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", "");
502         RNA_def_boolean(gzt->srna, "wrap_angle", true, "Wrap Angle", "");
503         RNA_def_float_factor(gzt->srna, "arc_inner_factor", 0.0f, 0.0f, 1.0f, "Arc Inner Factor", "", 0.0f, 1.0f);
504
505         WM_gizmotype_target_property_def(gzt, "offset", PROP_FLOAT, 1);
506 }
507
508 void ED_gizmotypes_dial_3d(void)
509 {
510         WM_gizmotype_append(GIZMO_GT_dial_3d);
511 }
512
513 /** \} */