Cleanup: rename manipulator references
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_navigate.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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/space_view3d/view3d_gizmo_navigate.c
22  *  \ingroup spview3d
23  */
24
25 #include "BLI_blenlib.h"
26 #include "BLI_math.h"
27 #include "BLI_utildefines.h"
28
29 #include "BKE_context.h"
30 #include "BKE_object.h"
31
32 #include "DNA_object_types.h"
33
34 #include "ED_screen.h"
35 #include "ED_gizmo_library.h"
36
37 #include "UI_interface.h"
38 #include "UI_resources.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "RNA_access.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "view3d_intern.h"  /* own include */
48
49 /* -------------------------------------------------------------------- */
50 /** \name View3D Navigation Gizmo Group
51  * \{ */
52
53 /* Offset from screen edge. */
54 #define GIZMO_OFFSET_FAC 1.2f
55 /* Size of main icon. */
56 #define GIZMO_SIZE 80
57 /* Factor for size of smaller button. */
58 #define GIZMO_MINI_FAC 0.35f
59 /* How much mini buttons offset from the primary. */
60 #define GIZMO_MINI_OFFSET_FAC 0.38f
61
62
63 enum {
64         GZ_INDEX_MOVE = 0,
65         GZ_INDEX_ROTATE = 1,
66         GZ_INDEX_ZOOM = 2,
67
68         /* just buttons */
69         /* overlaps GZ_INDEX_ORTHO (switch between) */
70         GZ_INDEX_PERSP = 3,
71         GZ_INDEX_ORTHO = 4,
72         GZ_INDEX_CAMERA = 5,
73
74         GZ_INDEX_TOTAL = 6,
75 };
76
77 struct NavigateGizmoInfo {
78         const char *opname;
79         const char *gizmo;
80         uint icon;
81 };
82
83 static struct NavigateGizmoInfo g_navigate_params[GZ_INDEX_TOTAL] = {
84         {
85                 .opname = "VIEW3D_OT_move",
86                 .gizmo = "GIZMO_GT_button_2d",
87                 ICON_VIEW_PAN,
88         }, {
89                 .opname = "VIEW3D_OT_rotate",
90                 .gizmo = "VIEW3D_GT_navigate_rotate",
91                 0,
92         }, {
93                 .opname = "VIEW3D_OT_zoom",
94                 .gizmo = "GIZMO_GT_button_2d",
95                 ICON_VIEW_ZOOM,
96         }, {
97                 .opname = "VIEW3D_OT_view_persportho",
98                 .gizmo = "GIZMO_GT_button_2d",
99                 ICON_VIEW_PERSPECTIVE,
100         }, {
101                 .opname = "VIEW3D_OT_view_persportho",
102                 .gizmo = "GIZMO_GT_button_2d",
103                 ICON_VIEW_ORTHO,
104         }, {
105                 .opname = "VIEW3D_OT_view_camera",
106                 .gizmo = "GIZMO_GT_button_2d",
107                 ICON_VIEW_CAMERA,
108         },
109 };
110
111 struct NavigateWidgetGroup {
112         wmGizmo *gz_array[GZ_INDEX_TOTAL];
113         /* Store the view state to check for changes. */
114         struct {
115                 rcti rect_visible;
116                 struct {
117                         char is_persp;
118                         char is_camera;
119                         char viewlock;
120                 } rv3d;
121         } state;
122         int region_size[2];
123 };
124
125 static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
126 {
127         View3D *v3d = CTX_wm_view3d(C);
128         if (((U.uiflag & USER_SHOW_GIZMO_AXIS) == 0) ||
129             (v3d->flag2 & V3D_RENDER_OVERRIDE) ||
130             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_NAVIGATE)))
131         {
132                 return false;
133         }
134         return true;
135
136 }
137
138 static void WIDGETGROUP_navigate_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
139 {
140         struct NavigateWidgetGroup *navgroup = MEM_callocN(sizeof(struct NavigateWidgetGroup), __func__);
141
142         navgroup->region_size[0] = -1;
143         navgroup->region_size[1] = -1;
144
145         wmOperatorType *ot_view_axis = WM_operatortype_find("VIEW3D_OT_view_axis", true);
146         wmOperatorType *ot_view_camera = WM_operatortype_find("VIEW3D_OT_view_camera", true);
147
148         for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
149                 const struct NavigateGizmoInfo *info = &g_navigate_params[i];
150                 navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, NULL);
151                 wmGizmo *gz = navgroup->gz_array[i];
152                 gz->flag |= WM_GIZMO_MOVE_CURSOR | WM_GIZMO_DRAW_MODAL;
153
154                 if (i == GZ_INDEX_ROTATE) {
155                         gz->color[3] = 0.0f;
156                         gz->color_hi[3] = 0.1f;
157                 }
158                 else {
159                         UI_GetThemeColorShade3fv(TH_HEADER, -40, gz->color);
160                         UI_GetThemeColorShade3fv(TH_HEADER, 160, gz->color_hi);
161                         gz->color[3] = 0.4f;
162                         gz->color_hi[3] = 0.2f;
163                 }
164
165
166                 /* may be overwritten later */
167                 gz->scale_basis = (GIZMO_SIZE * GIZMO_MINI_FAC) / 2;
168                 if (info->icon != 0) {
169                         PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
170                         RNA_property_enum_set(gz->ptr, prop, info->icon);
171                         RNA_enum_set(gz->ptr, "draw_options", ED_GIZMO_BUTTON_SHOW_OUTLINE | ED_GIZMO_BUTTON_SHOW_BACKDROP);
172                 }
173
174                 wmOperatorType *ot = WM_operatortype_find(info->opname, true);
175                 WM_gizmo_operator_set(gz, 0, ot, NULL);
176         }
177
178         {
179                 wmGizmo *gz = navgroup->gz_array[GZ_INDEX_CAMERA];
180                 WM_gizmo_operator_set(gz, 0, ot_view_camera, NULL);
181         }
182
183         /* Click only buttons (not modal). */
184         {
185                 int gz_ids[] = {GZ_INDEX_PERSP, GZ_INDEX_ORTHO, GZ_INDEX_CAMERA};
186                 for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
187                         wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
188                         RNA_boolean_set(gz->ptr, "show_drag", false);
189                 }
190         }
191
192         /* Modal operators, don't use initial mouse location since we're clicking on a button. */
193         {
194                 int gz_ids[] = {GZ_INDEX_MOVE, GZ_INDEX_ROTATE, GZ_INDEX_ZOOM};
195                 for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
196                         wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
197                         wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
198                         RNA_boolean_set(&gzop->ptr, "use_mouse_init", false);
199                 }
200         }
201
202         {
203                 wmGizmo *gz = navgroup->gz_array[GZ_INDEX_ROTATE];
204                 gz->scale_basis = GIZMO_SIZE / 2;
205                 char mapping[6] = {
206                         RV3D_VIEW_LEFT,
207                         RV3D_VIEW_RIGHT,
208                         RV3D_VIEW_FRONT,
209                         RV3D_VIEW_BACK,
210                         RV3D_VIEW_BOTTOM,
211                         RV3D_VIEW_TOP,
212                 };
213
214                 for (int part_index = 0; part_index < 6; part_index += 1) {
215                         PointerRNA *ptr = WM_gizmo_operator_set(gz, part_index + 1, ot_view_axis, NULL);
216                         RNA_enum_set(ptr, "type", mapping[part_index]);
217                 }
218
219                 /* When dragging an axis, use this instead. */
220                 gz->drag_part = 0;
221         }
222
223         gzgroup->customdata = navgroup;
224 }
225
226 static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
227 {
228         struct NavigateWidgetGroup *navgroup = gzgroup->customdata;
229         ARegion *ar = CTX_wm_region(C);
230         const RegionView3D *rv3d = ar->regiondata;
231
232         for (int i = 0; i < 3; i++) {
233                 copy_v3_v3(navgroup->gz_array[GZ_INDEX_ROTATE]->matrix_offset[i], rv3d->viewmat[i]);
234         }
235
236         rcti rect_visible;
237         ED_region_visible_rect(ar, &rect_visible);
238
239         if ((navgroup->state.rect_visible.xmax == rect_visible.xmax) &&
240             (navgroup->state.rect_visible.ymax == rect_visible.ymax) &&
241             (navgroup->state.rv3d.is_persp == rv3d->is_persp) &&
242             (navgroup->state.rv3d.is_camera == (rv3d->persp == RV3D_CAMOB)) &&
243             (navgroup->state.rv3d.viewlock == rv3d->viewlock))
244         {
245                 return;
246         }
247
248         navgroup->state.rect_visible = rect_visible;
249         navgroup->state.rv3d.is_persp = rv3d->is_persp;
250         navgroup->state.rv3d.is_camera = (rv3d->persp == RV3D_CAMOB);
251         navgroup->state.rv3d.viewlock = rv3d->viewlock;
252
253         const bool show_rotate = (
254                 ((rv3d->viewlock & RV3D_LOCKED) == 0) &&
255                 (navgroup->state.rv3d.is_camera == false));
256         const bool show_fixed_offset = navgroup->state.rv3d.is_camera;
257         const float icon_size = GIZMO_SIZE;
258         const float icon_offset = (icon_size * 0.52f) * GIZMO_OFFSET_FAC * UI_DPI_FAC;
259         const float icon_offset_mini = icon_size * GIZMO_MINI_OFFSET_FAC * UI_DPI_FAC;
260         const float co_rotate[2] = {
261                 rect_visible.xmax - icon_offset,
262                 rect_visible.ymax - icon_offset,
263         };
264         const float co[2] = {
265                 rect_visible.xmax - ((show_rotate || show_fixed_offset) ? (icon_offset * 2.0f) : (icon_offset_mini * 0.75f)),
266                 rect_visible.ymax - icon_offset_mini * 0.75f,
267         };
268
269         wmGizmo *gz;
270
271         for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
272                 gz = navgroup->gz_array[i];
273                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
274         }
275
276         /* RV3D_LOCKED or Camera: only show supported buttons. */
277         if (show_rotate) {
278                 gz = navgroup->gz_array[GZ_INDEX_ROTATE];
279                 gz->matrix_basis[3][0] = co_rotate[0];
280                 gz->matrix_basis[3][1] = co_rotate[1];
281                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
282         }
283
284         int icon_mini_slot = 0;
285
286         gz = navgroup->gz_array[GZ_INDEX_ZOOM];
287         gz->matrix_basis[3][0] = co[0] - (icon_offset_mini * icon_mini_slot++);
288         gz->matrix_basis[3][1] = co[1];
289         WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
290
291         gz = navgroup->gz_array[GZ_INDEX_MOVE];
292         gz->matrix_basis[3][0] = co[0] - (icon_offset_mini * icon_mini_slot++);
293         gz->matrix_basis[3][1] = co[1];
294         WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
295
296         if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
297                 gz = navgroup->gz_array[GZ_INDEX_CAMERA];
298                 gz->matrix_basis[3][0] = co[0] - (icon_offset_mini * icon_mini_slot++);
299                 gz->matrix_basis[3][1] = co[1];
300                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
301
302                 if (navgroup->state.rv3d.is_camera == false) {
303                         gz = navgroup->gz_array[rv3d->is_persp ? GZ_INDEX_PERSP : GZ_INDEX_ORTHO];
304                         gz->matrix_basis[3][0] = co[0] - (icon_offset_mini * icon_mini_slot++);
305                         gz->matrix_basis[3][1] = co[1];
306                         WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
307                 }
308         }
309 }
310
311 void VIEW3D_GGT_navigate(wmGizmoGroupType *gzgt)
312 {
313         gzgt->name = "View3D Navigate";
314         gzgt->idname = "VIEW3D_GGT_navigate";
315
316         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
317                       WM_GIZMOGROUPTYPE_SCALE |
318                       WM_GIZMOGROUPTYPE_DRAW_MODAL_ALL);
319
320         gzgt->poll = WIDGETGROUP_navigate_poll;
321         gzgt->setup = WIDGETGROUP_navigate_setup;
322         gzgt->draw_prepare = WIDGETGROUP_navigate_draw_prepare;
323 }
324
325 /** \} */