Blender V5.3
view3d_gizmo_navigate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_utildefines.hh"
11
12#include "BKE_context.hh"
13#include "BKE_screen.hh"
14
15#include "ED_gizmo_library.hh"
16#include "ED_screen.hh"
17
18#include "UI_interface.hh"
19#include "UI_resources.hh"
20
21#include "MEM_guardedalloc.h"
22
23#include "RNA_access.hh"
24
25#include "WM_api.hh"
26#include "WM_types.hh"
27
28#include "view3d_intern.hh" /* own include */
29
30namespace blender {
31
32/* -------------------------------------------------------------------- */
35
36/* Size of main icon. */
37#define GIZMO_SIZE U.gizmo_size_navigate_v3d
38
39/* Main gizmo offset from screen edges in unscaled pixels. */
40#define GIZMO_OFFSET 10.0f
41
42/* Width of smaller buttons in unscaled pixels. */
43#define GIZMO_MINI_SIZE 28.0f
44
45/* Margin around the smaller buttons. */
46#define GIZMO_MINI_OFFSET 2.0f
47
48namespace {
49
50enum {
51 GZ_INDEX_MOVE = 0,
52 GZ_INDEX_ROTATE = 1,
53 GZ_INDEX_ZOOM = 2,
54
55 /* just buttons */
56 /* overlaps GZ_INDEX_ORTHO (switch between) */
57 GZ_INDEX_PERSP = 3,
58 GZ_INDEX_ORTHO = 4,
59
60 GZ_INDEX_CAMERA_OFF = 5,
61 GZ_INDEX_CAMERA_ON = 6,
62
63 GZ_INDEX_CAMERA_LOCK = 7,
64 GZ_INDEX_CAMERA_UNLOCK = 8,
65
66 GZ_INDEX_TOTAL = 9,
67};
68
69struct NavigateGizmoInfo {
70 const char *opname;
71 const char *gizmo;
72 uint icon;
73 void (*op_prop_fn)(PointerRNA *ptr);
74};
75
76struct NavigateWidgetGroup {
77 wmGizmo *gz_array[GZ_INDEX_TOTAL];
78 /* Store the view state to check for changes. */
79 struct {
80 rcti rect_visible;
81 struct {
82 char is_persp;
83 bool is_camera;
85 char cameralock;
86 } rv3d;
87 } state;
88};
89
90} // namespace
91
93{
94 RNA_string_set(ptr, "data_path", "space_data.lock_camera");
95}
96
97static NavigateGizmoInfo g_navigate_params[GZ_INDEX_TOTAL] = {
98 {
99 "VIEW3D_OT_move",
100 "GIZMO_GT_button_2d",
101 ICON_VIEW_PAN,
102 nullptr,
103 },
104 {
105 "VIEW3D_OT_rotate",
106 "VIEW3D_GT_navigate_rotate",
107 ICON_NONE,
108 nullptr,
109 },
110 {
111 "VIEW3D_OT_zoom",
112 "GIZMO_GT_button_2d",
113 ICON_VIEW_ZOOM,
114 nullptr,
115 },
116 {
117 "VIEW3D_OT_view_persportho",
118 "GIZMO_GT_button_2d",
119 ICON_VIEW_PERSPECTIVE,
120 nullptr,
121 },
122 {
123 "VIEW3D_OT_view_persportho",
124 "GIZMO_GT_button_2d",
125 ICON_VIEW_ORTHO,
126 nullptr,
127 },
128 {
129 "VIEW3D_OT_view_camera",
130 "GIZMO_GT_button_2d",
131 ICON_VIEW_CAMERA_UNSELECTED,
132 nullptr,
133 },
134 {
135 "VIEW3D_OT_view_camera",
136 "GIZMO_GT_button_2d",
137 ICON_VIEW_CAMERA,
138 nullptr,
139 },
140 {
141 "WM_OT_context_toggle",
142 "GIZMO_GT_button_2d",
143 ICON_VIEW_LOCKED,
145 },
146 {
147 "WM_OT_context_toggle",
148 "GIZMO_GT_button_2d",
149 ICON_VIEW_UNLOCKED,
151 },
152};
153
155{
156 View3D *v3d = CTX_wm_view3d(C);
157 if ((((U.uiflag & USER_SHOW_GIZMO_NAVIGATE) == 0) &&
158 (U.mini_axis_type != USER_MINI_AXIS_TYPE_GIZMO)) ||
160 {
161 return false;
162 }
163 return true;
164}
165
167{
168 NavigateWidgetGroup *navgroup = MEM_new<NavigateWidgetGroup>(__func__);
169
170 wmOperatorType *ot_view_axis = WM_operatortype_find("VIEW3D_OT_view_axis", true);
171 wmOperatorType *ot_view_camera = WM_operatortype_find("VIEW3D_OT_view_camera", true);
172
173 for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
174 const NavigateGizmoInfo *info = &g_navigate_params[i];
175 navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, nullptr);
176 wmGizmo *gz = navgroup->gz_array[i];
178
179 if (i == GZ_INDEX_ROTATE) {
180 gz->color[3] = 0.0f;
181 copy_v3_fl(gz->color_hi, 0.5f);
182 gz->color_hi[3] = 0.5f;
183 }
184 else {
185 gz->color[3] = 0.0f;
186 gz->color_hi[3] = 0.0f;
187 }
188
189 /* may be overwritten later */
190 gz->scale_basis = GIZMO_MINI_SIZE / 2.0f;
191 if (info->icon != ICON_NONE) {
192 PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
193 RNA_property_enum_set(gz->ptr, prop, info->icon);
196 }
197
198 wmOperatorType *ot = WM_operatortype_find(info->opname, true);
199#ifndef WITH_PYTHON
200 if (ot != nullptr)
201#endif
202 {
203 PointerRNA *ptr = WM_gizmo_operator_set(gz, 0, ot, nullptr);
204 if (info->op_prop_fn != nullptr) {
205 info->op_prop_fn(ptr);
206 }
207 }
208 }
209
210 {
211 wmGizmo *gz = navgroup->gz_array[GZ_INDEX_CAMERA_OFF];
212 WM_gizmo_operator_set(gz, 0, ot_view_camera, nullptr);
213 }
214 {
215 wmGizmo *gz = navgroup->gz_array[GZ_INDEX_CAMERA_ON];
216 WM_gizmo_operator_set(gz, 0, ot_view_camera, nullptr);
217 }
218
219 /* Click only buttons (not modal). */
220 {
221 int gz_ids[] = {GZ_INDEX_PERSP,
222 GZ_INDEX_ORTHO,
223 GZ_INDEX_CAMERA_OFF,
224 GZ_INDEX_CAMERA_ON,
225 GZ_INDEX_CAMERA_LOCK,
226 GZ_INDEX_CAMERA_UNLOCK};
227 for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
228 wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
229 RNA_boolean_set(gz->ptr, "show_drag", false);
230 }
231 }
232
233 /* Modal operators, don't use initial mouse location since we're clicking on a button. */
234 {
235 int gz_ids[] = {GZ_INDEX_MOVE, GZ_INDEX_ROTATE, GZ_INDEX_ZOOM};
236 for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
237 wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
239 RNA_boolean_set(&gzop->ptr, "use_cursor_init", false);
240 }
241 }
242
243 {
244 wmGizmo *gz = navgroup->gz_array[GZ_INDEX_ROTATE];
245 gz->scale_basis = GIZMO_SIZE / 2.0f;
246 const char mapping[6] = {
253 };
254
255 for (int part_index = 0; part_index < 6; part_index += 1) {
256 PointerRNA *ptr = WM_gizmo_operator_set(gz, part_index + 1, ot_view_axis, nullptr);
257 RNA_enum_set(ptr, "type", mapping[part_index]);
258 }
259
260 /* When dragging an axis, use this instead. */
263 gz->drag_part = 0;
264 }
265
266 gzgroup->customdata = navgroup;
267}
268
270{
271 NavigateWidgetGroup *navgroup = static_cast<NavigateWidgetGroup *>(gzgroup->customdata);
272 ARegion *region = CTX_wm_region(C);
273 const RegionView3D *rv3d = static_cast<const RegionView3D *>(region->regiondata);
274 const View3D *v3d = CTX_wm_view3d(C);
275
276 for (int i = 0; i < 3; i++) {
277 copy_v3_v3(navgroup->gz_array[GZ_INDEX_ROTATE]->matrix_offset[i], rv3d->viewmat[i]);
278 }
279
280 const rcti *rect_visible = ED_region_visible_rect(region);
281
282 /* Ensure types match so bits are never lost on assignment. */
283 CHECK_TYPE_PAIR(navgroup->state.rv3d.viewlock, rv3d->viewlock);
284
285 if ((navgroup->state.rect_visible.xmax == rect_visible->xmax) &&
286 (navgroup->state.rect_visible.ymax == rect_visible->ymax) &&
287 (navgroup->state.rv3d.is_persp == rv3d->is_persp) &&
288 (navgroup->state.rv3d.is_camera == (rv3d->persp == RV3D_CAMOB)) &&
289 (navgroup->state.rv3d.cameralock == (v3d->flag2 & V3D_LOCK_CAMERA)) &&
290 (navgroup->state.rv3d.viewlock == RV3D_LOCK_FLAGS(rv3d)))
291 {
292 return;
293 }
294
295 navgroup->state.rect_visible = *rect_visible;
296 navgroup->state.rv3d.is_persp = rv3d->is_persp;
297 navgroup->state.rv3d.is_camera = (rv3d->persp == RV3D_CAMOB);
298 navgroup->state.rv3d.viewlock = RV3D_LOCK_FLAGS(rv3d);
299 navgroup->state.rv3d.cameralock = v3d->flag2 & V3D_LOCK_CAMERA;
300
301 const bool show_navigate = (U.uiflag & USER_SHOW_GIZMO_NAVIGATE) != 0;
302 const bool show_rotate_gizmo = (U.mini_axis_type == USER_MINI_AXIS_TYPE_GIZMO &&
303 (region->alignment != RGN_ALIGN_QSPLIT ||
304 region->runtime->quadview_index ==
306 const float icon_offset = UI_SCALE_FAC *
307 (GIZMO_OFFSET + (show_rotate_gizmo ? GIZMO_SIZE / 2.0f : 0.0f));
308 const float icon_offset_mini = (GIZMO_MINI_SIZE + GIZMO_MINI_OFFSET) * UI_SCALE_FAC;
309 const float co_rotate[2] = {
310 rect_visible->xmax - icon_offset,
311 rect_visible->ymax - icon_offset,
312 };
313
314 float icon_offset_from_axis = icon_offset_mini * 0.75f;
315 switch (eUserpref_MiniAxisType(U.mini_axis_type)) {
317 icon_offset_from_axis = icon_offset * 2.2f;
318 break;
320 if (region->alignment != RGN_ALIGN_QSPLIT ||
322 {
323 icon_offset_from_axis = (UI_UNIT_X * 2.0) + (U.rvisize * UI_SCALE_FAC * 1.6f);
324 }
325 break;
327 icon_offset_from_axis = icon_offset_mini * 0.75f;
328 break;
329 }
330
331 const float co[2] = {
332 roundf(rect_visible->xmax - icon_offset_mini * 0.75f),
333 roundf(rect_visible->ymax - icon_offset_from_axis),
334 };
335
336 wmGizmo *gz;
337
338 for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
339 gz = navgroup->gz_array[i];
341 }
342
343 if (show_rotate_gizmo) {
344 gz = navgroup->gz_array[GZ_INDEX_ROTATE];
345 gz->matrix_basis[3][0] = roundf(co_rotate[0]);
346 gz->matrix_basis[3][1] = roundf(co_rotate[1]);
348 }
349
350 if (show_navigate) {
351 int icon_mini_slot = 0;
352 if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ZOOM_AND_DOLLY) == 0) {
353 gz = navgroup->gz_array[GZ_INDEX_ZOOM];
354 gz->matrix_basis[3][0] = roundf(co[0]);
355 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
357 }
358
359 if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_LOCATION) == 0) {
360 gz = navgroup->gz_array[GZ_INDEX_MOVE];
361 gz->matrix_basis[3][0] = roundf(co[0]);
362 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
364 }
365
366 if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) {
367 gz = navgroup
368 ->gz_array[(rv3d->persp == RV3D_CAMOB) ? GZ_INDEX_CAMERA_ON : GZ_INDEX_CAMERA_OFF];
369 gz->matrix_basis[3][0] = roundf(co[0]);
370 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
372
373 if (navgroup->state.rv3d.is_camera == false) {
374 gz = navgroup->gz_array[rv3d->is_persp ? GZ_INDEX_PERSP : GZ_INDEX_ORTHO];
375 gz->matrix_basis[3][0] = roundf(co[0]);
376 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
378 }
379 }
380
381 if (navgroup->state.rv3d.is_camera == true) {
382 gz = navgroup->gz_array[(v3d->flag2 & V3D_LOCK_CAMERA) ? GZ_INDEX_CAMERA_LOCK :
383 GZ_INDEX_CAMERA_UNLOCK];
384 gz->matrix_basis[3][0] = roundf(co[0]);
385 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
387 }
388 }
389}
390
404
406
407} // namespace blender
#define UI_SCALE_FAC
#define RV3D_LOCK_FLAGS(rv3d)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_X
#define CHECK_TYPE_PAIR(var_a, var_b)
#define roundf(x)
const int state
BMesh Iterators.
UserDef U
Definition blender.cc:58
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void navigate_context_toggle_camera_lock_init(PointerRNA *ptr)
static NavigateGizmoInfo g_navigate_params[GZ_INDEX_TOTAL]
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
const rcti * ED_region_visible_rect(ARegion *region)
Definition area.cc:4791
@ RV3D_LOCK_ZOOM_AND_DOLLY
static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
@ USER_MINI_AXIS_TYPE_GIZMO
@ USER_MINI_AXIS_TYPE_MINIMAL
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_gizmo_button2d_group_background(const bContext *C, wmGizmoGroup *gzgroup)
static void WIDGETGROUP_navigate_setup(const bContext *C, wmGizmoGroup *gzgroup)
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition wm_gizmo.cc:199
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
wmGizmo * WM_gizmo_new(const StringRef idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:97
wmKeyMap * WM_gizmo_keymap_generic_press_drag(wmWindowManager *wm)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
@ WM_GIZMOGROUPTYPE_DRAW_MODAL_ALL
@ WM_GIZMOGROUPTYPE_PERSISTENT
@ WM_GIZMOGROUPTYPE_SCALE
static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *)
@ WM_GIZMO_MOVE_CURSOR
@ WM_GIZMO_DRAW_MODAL
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
MINLINE void copy_v3_fl(float r[3], float f)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void VIEW3D_GGT_navigate(wmGizmoGroupType *gzgt)
wmGizmoOpElem * WM_gizmo_operator_get(wmGizmo *gz, int part_index)
Definition wm_gizmo.cc:191
@ ED_GIZMO_BUTTON_SHOW_BACKDROP
@ ED_GIZMO_BUTTON_SHOW_OUTLINE
ARegion * CTX_wm_region(const bContext *C)
@ V3D_GIZMO_HIDE_NAVIGATE
View3D * CTX_wm_view3d(const bContext *C)
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
eRegionView3D_Persp persp
eRegionView3D_ViewLock viewlock
eView3D_Flag2 flag2
eView3D_GizmoFlag gizmo_flag
ARegionQuadviewIndex quadview_index
wmGizmoGroupFnDrawBackground draw_background
wmGizmoGroupFnDrawPrepare draw_prepare
eWM_GizmoFlagGroupTypeFlag flag
float matrix_basis[4][4]
eWM_GizmoFlag flag
#define GIZMO_SIZE
#define GIZMO_MINI_OFFSET
#define GIZMO_MINI_SIZE
#define GIZMO_OFFSET