Blender  V2.93
view3d_gizmo_navigate.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_math.h"
22 #include "BLI_utildefines.h"
23 
24 #include "BKE_context.h"
25 
26 #include "DNA_object_types.h"
27 
28 #include "ED_gizmo_library.h"
29 #include "ED_screen.h"
30 
31 #include "UI_interface.h"
32 #include "UI_resources.h"
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "RNA_access.h"
37 
38 #include "WM_api.h"
39 #include "WM_types.h"
40 
41 #include "view3d_intern.h" /* own include */
42 
43 /* -------------------------------------------------------------------- */
47 /* Size of main icon. */
48 #define GIZMO_SIZE U.gizmo_size_navigate_v3d
49 
50 /* Main gizmo offset from screen edges in unscaled pixels. */
51 #define GIZMO_OFFSET 10.0f
52 
53 /* Width of smaller buttons in unscaled pixels. */
54 #define GIZMO_MINI_SIZE 28.0f
55 
56 /* Margin around the smaller buttons. */
57 #define GIZMO_MINI_OFFSET 2.0f
58 
59 enum {
63 
64  /* just buttons */
65  /* overlaps GZ_INDEX_ORTHO (switch between) */
69 
71 };
72 
73 struct NavigateGizmoInfo {
74  const char *opname;
75  const char *gizmo;
76  uint icon;
77 };
78 
80  {
81  .opname = "VIEW3D_OT_move",
82  .gizmo = "GIZMO_GT_button_2d",
83  .icon = ICON_VIEW_PAN,
84  },
85  {
86  .opname = "VIEW3D_OT_rotate",
87  .gizmo = "VIEW3D_GT_navigate_rotate",
88  .icon = ICON_NONE,
89  },
90  {
91  .opname = "VIEW3D_OT_zoom",
92  .gizmo = "GIZMO_GT_button_2d",
93  .icon = ICON_VIEW_ZOOM,
94  },
95  {
96  .opname = "VIEW3D_OT_view_persportho",
97  .gizmo = "GIZMO_GT_button_2d",
98  .icon = ICON_VIEW_PERSPECTIVE,
99  },
100  {
101  .opname = "VIEW3D_OT_view_persportho",
102  .gizmo = "GIZMO_GT_button_2d",
103  .icon = ICON_VIEW_ORTHO,
104  },
105  {
106  .opname = "VIEW3D_OT_view_camera",
107  .gizmo = "GIZMO_GT_button_2d",
108  .icon = ICON_VIEW_CAMERA,
109  },
110 };
111 
112 struct NavigateWidgetGroup {
114  /* Store the view state to check for changes. */
115  struct {
117  struct {
118  char is_persp;
119  bool is_camera;
120  char viewlock;
121  } rv3d;
122  } state;
123  int region_size[2];
124 };
125 
127 {
128  View3D *v3d = CTX_wm_view3d(C);
129  if ((((U.uiflag & USER_SHOW_GIZMO_NAVIGATE) == 0) &&
130  (U.mini_axis_type != USER_MINI_AXIS_TYPE_GIZMO)) ||
132  return false;
133  }
134  return true;
135 }
136 
137 static void WIDGETGROUP_navigate_setup(const bContext *C, wmGizmoGroup *gzgroup)
138 {
139  struct NavigateWidgetGroup *navgroup = MEM_callocN(sizeof(struct NavigateWidgetGroup), __func__);
140 
141  navgroup->region_size[0] = -1;
142  navgroup->region_size[1] = -1;
143 
144  wmOperatorType *ot_view_axis = WM_operatortype_find("VIEW3D_OT_view_axis", true);
145  wmOperatorType *ot_view_camera = WM_operatortype_find("VIEW3D_OT_view_camera", true);
146 
147  for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
148  const struct NavigateGizmoInfo *info = &g_navigate_params[i];
149  navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, NULL);
150  wmGizmo *gz = navgroup->gz_array[i];
152 
153  if (i == GZ_INDEX_ROTATE) {
154  gz->color[3] = 0.0f;
155  copy_v3_fl(gz->color_hi, 0.5f);
156  gz->color_hi[3] = 0.5f;
157  }
158  else {
159  uchar icon_color[3];
160  UI_GetThemeColor3ubv(TH_TEXT, icon_color);
161  int color_tint, color_tint_hi;
162  if (icon_color[0] > 128) {
163  color_tint = -40;
164  color_tint_hi = 60;
165  gz->color[3] = 0.5f;
166  gz->color_hi[3] = 0.5f;
167  }
168  else {
169  color_tint = 60;
170  color_tint_hi = 60;
171  gz->color[3] = 0.5f;
172  gz->color_hi[3] = 0.75f;
173  }
174  UI_GetThemeColorShade3fv(TH_HEADER, color_tint, gz->color);
175  UI_GetThemeColorShade3fv(TH_HEADER, color_tint_hi, gz->color_hi);
176  }
177 
178  /* may be overwritten later */
179  gz->scale_basis = GIZMO_MINI_SIZE / 2.0f;
180  if (info->icon != ICON_NONE) {
181  PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
182  RNA_property_enum_set(gz->ptr, prop, info->icon);
183  RNA_enum_set(
185  }
186 
188  WM_gizmo_operator_set(gz, 0, ot, NULL);
189  }
190 
191  {
192  wmGizmo *gz = navgroup->gz_array[GZ_INDEX_CAMERA];
193  WM_gizmo_operator_set(gz, 0, ot_view_camera, NULL);
194  }
195 
196  /* Click only buttons (not modal). */
197  {
198  int gz_ids[] = {GZ_INDEX_PERSP, GZ_INDEX_ORTHO, GZ_INDEX_CAMERA};
199  for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
200  wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
201  RNA_boolean_set(gz->ptr, "show_drag", false);
202  }
203  }
204 
205  /* Modal operators, don't use initial mouse location since we're clicking on a button. */
206  {
207  int gz_ids[] = {GZ_INDEX_MOVE, GZ_INDEX_ROTATE, GZ_INDEX_ZOOM};
208  for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
209  wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
210  wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
211  RNA_boolean_set(&gzop->ptr, "use_cursor_init", false);
212  }
213  }
214 
215  {
216  wmGizmo *gz = navgroup->gz_array[GZ_INDEX_ROTATE];
217  gz->scale_basis = GIZMO_SIZE / 2.0f;
218  const char mapping[6] = {
225  };
226 
227  for (int part_index = 0; part_index < 6; part_index += 1) {
228  PointerRNA *ptr = WM_gizmo_operator_set(gz, part_index + 1, ot_view_axis, NULL);
229  RNA_enum_set(ptr, "type", mapping[part_index]);
230  }
231 
232  /* When dragging an axis, use this instead. */
235  gz->drag_part = 0;
236  }
237 
238  gzgroup->customdata = navgroup;
239 }
240 
242 {
243  struct NavigateWidgetGroup *navgroup = gzgroup->customdata;
244  ARegion *region = CTX_wm_region(C);
245  const RegionView3D *rv3d = region->regiondata;
246 
247  for (int i = 0; i < 3; i++) {
248  copy_v3_v3(navgroup->gz_array[GZ_INDEX_ROTATE]->matrix_offset[i], rv3d->viewmat[i]);
249  }
250 
251  const rcti *rect_visible = ED_region_visible_rect(region);
252 
253  /* Ensure types match so bits are never lost on assignment. */
254  CHECK_TYPE_PAIR(navgroup->state.rv3d.viewlock, rv3d->viewlock);
255 
256  if ((navgroup->state.rect_visible.xmax == rect_visible->xmax) &&
257  (navgroup->state.rect_visible.ymax == rect_visible->ymax) &&
258  (navgroup->state.rv3d.is_persp == rv3d->is_persp) &&
259  (navgroup->state.rv3d.is_camera == (rv3d->persp == RV3D_CAMOB)) &&
260  (navgroup->state.rv3d.viewlock == RV3D_LOCK_FLAGS(rv3d))) {
261  return;
262  }
263 
264  navgroup->state.rect_visible = *rect_visible;
265  navgroup->state.rv3d.is_persp = rv3d->is_persp;
266  navgroup->state.rv3d.is_camera = (rv3d->persp == RV3D_CAMOB);
267  navgroup->state.rv3d.viewlock = RV3D_LOCK_FLAGS(rv3d);
268 
269  const bool show_navigate = (U.uiflag & USER_SHOW_GIZMO_NAVIGATE) != 0;
270  const bool show_rotate_gizmo = (U.mini_axis_type == USER_MINI_AXIS_TYPE_GIZMO);
271  const float icon_offset = ((GIZMO_SIZE / 2.0f) + GIZMO_OFFSET) * UI_DPI_FAC;
272  const float icon_offset_mini = (GIZMO_MINI_SIZE + GIZMO_MINI_OFFSET) * UI_DPI_FAC;
273  const float co_rotate[2] = {
274  rect_visible->xmax - icon_offset,
275  rect_visible->ymax - icon_offset,
276  };
277 
278  float icon_offset_from_axis = 0.0f;
279  switch ((eUserpref_MiniAxisType)U.mini_axis_type) {
281  icon_offset_from_axis = icon_offset * 2.1f;
282  break;
284  icon_offset_from_axis = (UI_UNIT_X * 2.5) + ((U.rvisize * U.pixelsize * 2.0f));
285  break;
287  icon_offset_from_axis = icon_offset_mini * 0.75f;
288  break;
289  }
290 
291  const float co[2] = {
292  roundf(rect_visible->xmax - icon_offset_mini * 0.75f),
293  roundf(rect_visible->ymax - icon_offset_from_axis),
294  };
295 
296  wmGizmo *gz;
297 
298  for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
299  gz = navgroup->gz_array[i];
301  }
302 
303  if (show_rotate_gizmo) {
304  gz = navgroup->gz_array[GZ_INDEX_ROTATE];
305  gz->matrix_basis[3][0] = roundf(co_rotate[0]);
306  gz->matrix_basis[3][1] = roundf(co_rotate[1]);
308  }
309 
310  if (show_navigate) {
311  int icon_mini_slot = 0;
313  gz = navgroup->gz_array[GZ_INDEX_ZOOM];
314  gz->matrix_basis[3][0] = roundf(co[0]);
315  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
317  }
318 
319  if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_LOCATION) == 0) {
320  gz = navgroup->gz_array[GZ_INDEX_MOVE];
321  gz->matrix_basis[3][0] = roundf(co[0]);
322  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
324  }
325 
326  if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) {
327  gz = navgroup->gz_array[GZ_INDEX_CAMERA];
328  gz->matrix_basis[3][0] = roundf(co[0]);
329  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
331 
332  if (navgroup->state.rv3d.is_camera == false) {
333  gz = navgroup->gz_array[rv3d->is_persp ? GZ_INDEX_PERSP : GZ_INDEX_ORTHO];
334  gz->matrix_basis[3][0] = roundf(co[0]);
335  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
337  }
338  }
339  }
340 }
341 
343 {
344  gzgt->name = "View3D Navigate";
345  gzgt->idname = "VIEW3D_GGT_navigate";
346 
349 
353 }
354 
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
#define CHECK_TYPE_PAIR(var_a, var_b)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl(float r[3], float f)
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
#define ARRAY_SIZE(arr)
#define UNUSED(x)
Object is a sort of wrapper for general info.
@ USER_SHOW_GIZMO_NAVIGATE
eUserpref_MiniAxisType
@ USER_MINI_AXIS_TYPE_GIZMO
@ USER_MINI_AXIS_TYPE_MINIMAL
@ USER_MINI_AXIS_TYPE_NONE
#define RV3D_LOCK_FLAGS(rv3d)
#define RV3D_CAMOB
#define RV3D_VIEW_BACK
#define RV3D_VIEW_BOTTOM
@ RV3D_LOCK_ROTATION
@ RV3D_LOCK_LOCATION
@ RV3D_LOCK_ZOOM_AND_DOLLY
#define RV3D_VIEW_LEFT
#define RV3D_VIEW_RIGHT
#define RV3D_VIEW_TOP
#define RV3D_VIEW_FRONT
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_NAVIGATE
@ ED_GIZMO_BUTTON_SHOW_BACKDROP
@ ED_GIZMO_BUTTON_SHOW_OUTLINE
const rcti * ED_region_visible_rect(ARegion *region)
Definition: area.c:3682
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define UI_DPI_FAC
Definition: UI_interface.h:309
#define UI_UNIT_X
@ TH_HEADER
Definition: UI_resources.h:66
@ TH_TEXT
Definition: UI_resources.h:58
void UI_GetThemeColorShade3fv(int colorid, int offset, float col[3])
Definition: resources.c:1218
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
Definition: resources.c:1350
@ WM_GIZMO_HIDDEN
@ WM_GIZMO_MOVE_CURSOR
@ WM_GIZMO_DRAW_MODAL
@ WM_GIZMOGROUPTYPE_SCALE
@ WM_GIZMOGROUPTYPE_DRAW_MODAL_ALL
@ WM_GIZMOGROUPTYPE_PERSISTENT
unsigned int U
Definition: btGjkEpa3.h:78
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
Definition: rna_access.c:6272
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
Definition: rna_access.c:3562
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:6413
void * regiondata
wmGizmo * gz_array[GZ_INDEX_TOTAL]
struct NavigateWidgetGroup::@549::@550 rv3d
struct NavigateWidgetGroup::@425 state
char gizmo_flag
int ymax
Definition: DNA_vec_types.h:80
int xmax
Definition: DNA_vec_types.h:79
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
const char * name
wmGizmoGroupFnDrawPrepare draw_prepare
PointerRNA ptr
float matrix_basis[4][4]
float matrix_offset[4][4]
float color_hi[4]
struct wmKeyMap * keymap
float color[4]
struct PointerRNA * ptr
float scale_basis
eWM_GizmoFlag flag
static void WIDGETGROUP_navigate_setup(const bContext *C, wmGizmoGroup *gzgroup)
#define GIZMO_MINI_OFFSET
#define GIZMO_MINI_SIZE
static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
void VIEW3D_GGT_navigate(wmGizmoGroupType *gzgt)
@ GZ_INDEX_ZOOM
@ GZ_INDEX_MOVE
@ GZ_INDEX_TOTAL
@ GZ_INDEX_ORTHO
@ GZ_INDEX_ROTATE
@ GZ_INDEX_PERSP
@ GZ_INDEX_CAMERA
#define GIZMO_SIZE
#define GIZMO_OFFSET
static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
static struct NavigateGizmoInfo g_navigate_params[GZ_INDEX_TOTAL]
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition: wm_gizmo.c:232
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:339
struct wmGizmoOpElem * WM_gizmo_operator_get(wmGizmo *gz, int part_index)
Definition: wm_gizmo.c:224
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:114
struct wmKeyMap * WM_gizmo_keymap_generic_click_drag(wmWindowManager *wm)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)