Gizmo: dial3d option to draw a partial arc
[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],
110         const float arc_partial_angle, const float arc_inner_factor)
111 {
112 #ifdef USE_GIZMO_CUSTOM_DIAL
113         UNUSED_VARS(gz, axis_modal_mat, clip_plane);
114         wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_dial, select, color);
115 #else
116         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
117         const bool filled = (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_FILL) != 0;
118
119         GPU_line_width(gz->line_width);
120
121         GPUVertFormat *format = immVertexFormat();
122         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
123
124         if (clip_plane) {
125                 immBindBuiltinProgram(GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR);
126                 float clip_plane_f[4] = {clip_plane[0], clip_plane[1], clip_plane[2], clip_plane[3]};
127                 immUniform4fv("ClipPlane", clip_plane_f);
128                 immUniformMatrix4fv("ModelMatrix", axis_modal_mat);
129         }
130         else {
131                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
132         }
133
134         immUniformColor4fv(color);
135
136         if (filled) {
137                 imm_draw_circle_fill_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION);
138         }
139         else {
140                 if (arc_partial_angle == 0.0f) {
141                         imm_draw_circle_wire_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION);
142                         if (arc_inner_factor != 0.0f) {
143                                 imm_draw_circle_wire_2d(pos, 0, 0, arc_inner_factor, DIAL_RESOLUTION);
144                         }
145                 }
146                 else {
147                         float arc_partial_deg = RAD2DEGF((M_PI * 2) - arc_partial_angle);
148                         imm_draw_circle_partial_wire_2d(
149                                 pos, 0, 0, 1.0, DIAL_RESOLUTION,
150                                 -arc_partial_deg / 2, arc_partial_deg);
151 #if 0
152                         if (arc_inner_factor != 0.0f) {
153                                 BLI_assert(0);
154                         }
155 #endif
156                 }
157         }
158
159         immUnbindProgram();
160
161         UNUSED_VARS(select);
162 #endif
163 }
164
165 /**
166  * Draws a line from (0, 0, 0) to \a co_outer, at \a angle.
167  */
168 static void dial_ghostarc_draw_helpline(
169         const float angle, const float co_outer[3], const float color[4])
170 {
171         GPU_matrix_push();
172         GPU_matrix_rotate_3f(RAD2DEGF(angle), 0.0f, 0.0f, -1.0f);
173
174         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
175
176         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
177
178         immUniformColor4fv(color);
179
180         immBegin(GPU_PRIM_LINE_STRIP, 2);
181         immVertex3f(pos, 0.0f, 0, 0.0f);
182         immVertex3fv(pos, co_outer);
183         immEnd();
184
185         immUnbindProgram();
186
187         GPU_matrix_pop();
188 }
189
190 static void dial_ghostarc_draw(
191         const float angle_ofs, const float angle_delta,
192         const float arc_inner_factor, const float color[4])
193 {
194         const float width_inner = DIAL_WIDTH;
195         GPUVertFormat *format = immVertexFormat();
196         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
197         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
198
199         if (arc_inner_factor != 0.0) {
200                 float color_dark[4] = {0};
201                 color_dark[3] = color[3] / 2;
202                 immUniformColor4fv(color_dark);
203                 imm_draw_disk_partial_fill_2d(
204                         pos, 0, 0, arc_inner_factor, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(M_PI * 2));
205         }
206
207         immUniformColor4fv(color);
208         imm_draw_disk_partial_fill_2d(
209                 pos, 0, 0, arc_inner_factor, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(angle_delta));
210         immUnbindProgram();
211 }
212
213 static void dial_ghostarc_get_angles(
214         const wmGizmo *gz,
215         const wmEvent *event,
216         const ARegion *ar,
217         float mat[4][4], const float co_outer[3],
218         float *r_start, float *r_delta)
219 {
220         DialInteraction *inter = gz->interaction_data;
221         const RegionView3D *rv3d = ar->regiondata;
222         const float mval[2] = {event->x - ar->winrct.xmin, event->y - ar->winrct.ymin};
223
224         /* We might need to invert the direction of the angles. */
225         float view_vec[3], axis_vec[3];
226         ED_view3d_global_to_vector(rv3d, gz->matrix_basis[3], view_vec);
227         normalize_v3_v3(axis_vec, gz->matrix_basis[2]);
228
229         float proj_outer_rel[3];
230         mul_v3_project_m4_v3(proj_outer_rel, mat, co_outer);
231         sub_v3_v3(proj_outer_rel, gz->matrix_basis[3]);
232
233         float proj_mval_new_rel[3];
234         float proj_mval_init_rel[3];
235         float dial_plane[4];
236
237         plane_from_point_normal_v3(dial_plane, gz->matrix_basis[3], axis_vec);
238
239         if (!ED_view3d_win_to_3d_on_plane(ar, dial_plane, inter->init.mval, false, proj_mval_init_rel)) {
240                 goto fail;
241         }
242         sub_v3_v3(proj_mval_init_rel, gz->matrix_basis[3]);
243
244         if (!ED_view3d_win_to_3d_on_plane(ar, dial_plane, mval, false, proj_mval_new_rel)) {
245                 goto fail;
246         }
247         sub_v3_v3(proj_mval_new_rel, gz->matrix_basis[3]);
248
249         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
250
251         /* Start direction from mouse or set by user. */
252         const float *proj_init_rel =
253                 (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_START_Y) ?
254                 gz->matrix_basis[1] : proj_mval_init_rel;
255
256         /* Return angles. */
257         const float start = angle_wrap_rad(angle_signed_on_axis_v3v3_v3(proj_outer_rel, proj_init_rel, axis_vec));
258         const float delta = angle_wrap_rad(angle_signed_on_axis_v3v3_v3(proj_mval_init_rel, proj_mval_new_rel, axis_vec));
259
260         /* Change of sign, we passed the 180 degree threshold. This means we need to add a turn
261          * to distinguish between transition from 0 to -1 and -PI to +PI, use comparison with PI/2.
262          * Logic taken from #BLI_dial_angle */
263         if ((delta * inter->prev.angle < 0.0f) &&
264             (fabsf(inter->prev.angle) > (float)M_PI_2))
265         {
266                 if (inter->prev.angle < 0.0f) {
267                         inter->rotations--;
268                 }
269                 else {
270                         inter->rotations++;
271                 }
272         }
273         inter->prev.angle = delta;
274
275         const bool wrap_angle = RNA_boolean_get(gz->ptr, "wrap_angle");
276         const double delta_final = (double)delta + ((2 * M_PI) * (double)inter->rotations);
277         *r_start = start;
278         *r_delta = (float)(wrap_angle ? fmod(delta_final, 2 * M_PI) : delta_final);
279         return;
280
281         /* If we can't project (unlikely). */
282 fail:
283         *r_start = 0.0;
284         *r_delta = 0.0;
285 }
286
287 static void dial_ghostarc_draw_with_helplines(
288         const float angle_ofs, const float angle_delta,
289         const float arc_inner_factor, const float color_helpline[4], const int draw_options)
290 {
291         /* Coordinate at which the arc drawing will be started. */
292         const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f};
293         dial_ghostarc_draw(angle_ofs, angle_delta, arc_inner_factor, (const float[4]){0.8f, 0.8f, 0.8f, 0.4f});
294         GPU_line_width(1.0f);
295         dial_ghostarc_draw_helpline(angle_ofs, co_outer, color_helpline);
296         if (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE) {
297                 GPU_line_width(3.0f);
298         }
299         dial_ghostarc_draw_helpline(angle_ofs + angle_delta, co_outer, color_helpline);
300 }
301
302 static void dial_draw_intern(
303         const bContext *C, wmGizmo *gz,
304         const bool select, const bool highlight, float clip_plane[4])
305 {
306         float matrix_final[4][4];
307         float color[4];
308
309         BLI_assert(CTX_wm_area(C)->spacetype == SPACE_VIEW3D);
310
311         gizmo_color_get(gz, highlight, color);
312
313         WM_gizmo_calc_matrix_final(gz, matrix_final);
314
315         GPU_matrix_push();
316         GPU_matrix_mul(matrix_final);
317
318         /* FIXME(campbell): look into removing this. */
319         if ((gz->flag & WM_GIZMO_DRAW_VALUE) &&
320             (gz->state & WM_GIZMO_STATE_MODAL))
321         {
322                 /* XXX, View3D rotation gizmo doesn't call modal. */
323                 if (!WM_gizmo_target_property_is_valid_any(gz)) {
324                         wmWindow *win = CTX_wm_window(C);
325                         gizmo_dial_modal((bContext *)C, gz, win->eventstate, 0);
326                 }
327         }
328
329         GPU_polygon_smooth(false);
330
331
332         const float arc_partial_angle = RNA_float_get(gz->ptr, "arc_partial_angle");
333         const float arc_inner_factor = RNA_float_get(gz->ptr, "arc_inner_factor");
334         if (select == false) {
335                 float angle_ofs = 0.0f;
336                 float angle_delta = 0.0f;
337                 bool show_ghostarc = false;
338
339                 /* Draw rotation indicator arc first. */
340                 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
341                 const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
342
343                 if (WM_gizmo_target_property_is_valid(gz_prop) &&
344                     (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE))
345                 {
346                         angle_ofs = 0.0f;
347                         angle_delta = WM_gizmo_target_property_float_get(gz, gz_prop);
348                         show_ghostarc = true;
349                 }
350                 else if ((gz->flag & WM_GIZMO_DRAW_VALUE) &&
351                          (gz->state & WM_GIZMO_STATE_MODAL))
352                 {
353                         DialInteraction *inter = gz->interaction_data;
354                         angle_ofs = inter->output.angle_ofs;
355                         angle_delta = inter->output.angle_delta;
356                         show_ghostarc = true;
357                 }
358
359                 if (show_ghostarc) {
360                         dial_ghostarc_draw_with_helplines(angle_ofs, angle_delta, arc_inner_factor, color, draw_options);
361                         if ((draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_MIRROR) != 0) {
362                                 angle_ofs += M_PI;
363                                 dial_ghostarc_draw_with_helplines(angle_ofs, angle_delta, arc_inner_factor, color, draw_options);
364                         }
365                 }
366         }
367
368         /* Draw actual dial gizmo. */
369         dial_geom_draw(gz, color, select, gz->matrix_basis, clip_plane, arc_partial_angle, arc_inner_factor);
370
371         GPU_matrix_pop();
372 }
373
374 static void gizmo_dial_draw_select(const bContext *C, wmGizmo *gz, int select_id)
375 {
376         float clip_plane_buf[4];
377         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
378         float *clip_plane = (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_CLIP) ? clip_plane_buf : NULL;
379
380         if (clip_plane) {
381                 ARegion *ar = CTX_wm_region(C);
382                 RegionView3D *rv3d = ar->regiondata;
383
384                 copy_v3_v3(clip_plane, rv3d->viewinv[2]);
385                 clip_plane[3] = -dot_v3v3(rv3d->viewinv[2], gz->matrix_basis[3]);
386                 clip_plane[3] += DIAL_CLIP_BIAS;
387                 glEnable(GL_CLIP_DISTANCE0);
388         }
389
390         GPU_select_load_id(select_id);
391         dial_draw_intern(C, gz, true, false, clip_plane);
392
393         if (clip_plane) {
394                 glDisable(GL_CLIP_DISTANCE0);
395         }
396 }
397
398 static void gizmo_dial_draw(const bContext *C, wmGizmo *gz)
399 {
400         const bool is_modal = gz->state & WM_GIZMO_STATE_MODAL;
401         const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0;
402         float clip_plane_buf[4];
403         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
404         float *clip_plane = (!is_modal && (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_CLIP)) ? clip_plane_buf : NULL;
405
406         if (clip_plane) {
407                 ARegion *ar = CTX_wm_region(C);
408                 RegionView3D *rv3d = ar->regiondata;
409
410                 copy_v3_v3(clip_plane, rv3d->viewinv[2]);
411                 clip_plane[3] = -dot_v3v3(rv3d->viewinv[2], gz->matrix_basis[3]);
412                 clip_plane[3] += DIAL_CLIP_BIAS;
413
414                 glEnable(GL_CLIP_DISTANCE0);
415         }
416
417         GPU_blend(true);
418         dial_draw_intern(C, gz, false, is_highlight, clip_plane);
419         GPU_blend(false);
420
421         if (clip_plane) {
422                 glDisable(GL_CLIP_DISTANCE0);
423         }
424 }
425
426 static int gizmo_dial_modal(
427         bContext *C, wmGizmo *gz, const wmEvent *event,
428         eWM_GizmoFlagTweak tweak_flag)
429 {
430         DialInteraction *inter = gz->interaction_data;
431         if ((event->type != MOUSEMOVE) && (inter->prev.tweak_flag == tweak_flag)) {
432                 return OPERATOR_RUNNING_MODAL;
433         }
434         /* Coordinate at which the arc drawing will be started. */
435         const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f};
436         float angle_ofs, angle_delta;
437
438         dial_ghostarc_get_angles(
439                 gz, event, CTX_wm_region(C), gz->matrix_basis, co_outer, &angle_ofs, &angle_delta);
440
441         if (tweak_flag & WM_GIZMO_TWEAK_SNAP) {
442                 const double snap = DEG2RAD(5);
443                 angle_delta = (float)roundf((double)angle_delta / snap) * snap;
444         }
445         if (tweak_flag & WM_GIZMO_TWEAK_PRECISE) {
446                 angle_delta *= 0.1f;
447         }
448         inter->output.angle_delta = angle_delta;
449         inter->output.angle_ofs = angle_ofs;
450
451         /* Set the property for the operator and call its modal function. */
452         wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
453         if (WM_gizmo_target_property_is_valid(gz_prop)) {
454                 WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init.prop_angle + angle_delta);
455         }
456
457         inter->prev.tweak_flag = tweak_flag;
458
459         return OPERATOR_RUNNING_MODAL;
460 }
461
462
463 static void gizmo_dial_setup(wmGizmo *gz)
464 {
465         const float dir_default[3] = {0.0f, 0.0f, 1.0f};
466
467         /* defaults */
468         copy_v3_v3(gz->matrix_basis[2], dir_default);
469 }
470
471 static int gizmo_dial_invoke(
472         bContext *UNUSED(C), wmGizmo *gz, const wmEvent *event)
473 {
474         DialInteraction *inter = MEM_callocN(sizeof(DialInteraction), __func__);
475
476         inter->init.mval[0] = event->mval[0];
477         inter->init.mval[1] = event->mval[1];
478
479         wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
480         if (WM_gizmo_target_property_is_valid(gz_prop)) {
481                 inter->init.prop_angle = WM_gizmo_target_property_float_get(gz, gz_prop);
482         }
483
484         gz->interaction_data = inter;
485
486         return OPERATOR_RUNNING_MODAL;
487 }
488
489 /* -------------------------------------------------------------------- */
490 /** \name Dial Gizmo API
491  *
492  * \{ */
493
494 static void GIZMO_GT_dial_3d(wmGizmoType *gzt)
495 {
496         /* identifiers */
497         gzt->idname = "GIZMO_GT_dial_3d";
498
499         /* api callbacks */
500         gzt->draw = gizmo_dial_draw;
501         gzt->draw_select = gizmo_dial_draw_select;
502         gzt->setup = gizmo_dial_setup;
503         gzt->invoke = gizmo_dial_invoke;
504         gzt->modal = gizmo_dial_modal;
505
506         gzt->struct_size = sizeof(wmGizmo);
507
508         /* rna */
509         static EnumPropertyItem rna_enum_draw_options[] = {
510                 {ED_GIZMO_DIAL_DRAW_FLAG_CLIP, "CLIP", 0, "Clipped", ""},
511                 {ED_GIZMO_DIAL_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""},
512                 {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_MIRROR, "ANGLE_MIRROR", 0, "Angle Mirror", ""},
513                 {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_START_Y, "ANGLE_START_Y", 0, "Angle Start Y", ""},
514                 {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE, "ANGLE_VALUE", 0, "Show Angle Value", ""},
515                 {0, NULL, 0, NULL, NULL}
516         };
517         RNA_def_enum_flag(gzt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", "");
518         RNA_def_boolean(gzt->srna, "wrap_angle", true, "Wrap Angle", "");
519         RNA_def_float_factor(gzt->srna, "arc_inner_factor", 0.0f, 0.0f, 1.0f, "Arc Inner Factor", "", 0.0f, 1.0f);
520         RNA_def_float_factor(gzt->srna, "arc_partial_angle", 0.0f, 0.0f, M_PI * 2, "Show Partial Dial", "", 0.0f, M_PI * 2);
521
522         WM_gizmotype_target_property_def(gzt, "offset", PROP_FLOAT, 1);
523 }
524
525 void ED_gizmotypes_dial_3d(void)
526 {
527         WM_gizmotype_append(GIZMO_GT_dial_3d);
528 }
529
530 /** \} */