Blender  V3.3
view2d_gizmo_navigate.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_math.h"
8 #include "BLI_utildefines.h"
9 
10 #include "BKE_context.h"
11 
12 #include "ED_gizmo_library.h"
13 #include "ED_screen.h"
14 
15 #include "UI_interface.h"
16 #include "UI_resources.h"
17 
18 #include "MEM_guardedalloc.h"
19 
20 #include "RNA_access.h"
21 
22 #include "WM_api.h"
23 #include "WM_types.h"
24 
25 #include "UI_view2d.h"
26 
27 /* -------------------------------------------------------------------- */
35 /* Size of main icon. */
36 #define GIZMO_SIZE 80
37 /* Factor for size of smaller button. */
38 #define GIZMO_MINI_FAC 0.35f
39 /* How much mini buttons offset from the primary. */
40 #define GIZMO_MINI_OFFSET_FAC 0.38f
41 
42 enum {
45 
47 };
48 
50  const char *opname;
51  const char *gizmo;
53 };
54 
56  {
57  "IMAGE_OT_view_pan",
58  "GIZMO_GT_button_2d",
59  ICON_VIEW_PAN,
60  },
61  {
62  "IMAGE_OT_view_zoom",
63  "GIZMO_GT_button_2d",
64  ICON_VIEW_ZOOM,
65  },
66 };
67 
69  {
70  "CLIP_OT_view_pan",
71  "GIZMO_GT_button_2d",
72  ICON_VIEW_PAN,
73  },
74  {
75  "CLIP_OT_view_zoom",
76  "GIZMO_GT_button_2d",
77  ICON_VIEW_ZOOM,
78  },
79 };
80 
82  {
83  "VIEW2D_OT_pan",
84  "GIZMO_GT_button_2d",
85  ICON_VIEW_PAN,
86  },
87  {
88  "VIEW2D_OT_zoom",
89  "GIZMO_GT_button_2d",
90  ICON_VIEW_ZOOM,
91  },
92 };
93 
94 static struct NavigateGizmoInfo *navigate_params_from_space_type(short space_type)
95 {
96  switch (space_type) {
97  case SPACE_IMAGE:
99  case SPACE_CLIP:
101  default:
102  /* Used for sequencer. */
104  }
105 }
106 
109  /* Store the view state to check for changes. */
110  struct {
112  } state;
113  int region_size[2];
114 };
115 
117 {
118  if ((U.uiflag & USER_SHOW_GIZMO_NAVIGATE) == 0) {
119  return false;
120  }
122  if (area == nullptr) {
123  return false;
124  }
125  switch (area->spacetype) {
126  case SPACE_SEQ: {
127  const SpaceSeq *sseq = static_cast<const SpaceSeq *>(area->spacedata.first);
129  return false;
130  }
131  break;
132  }
133  case SPACE_IMAGE: {
134  const SpaceImage *sima = static_cast<const SpaceImage *>(area->spacedata.first);
136  return false;
137  }
138  break;
139  }
140  }
141  return true;
142 }
143 
145 {
146  NavigateWidgetGroup *navgroup = MEM_cnew<NavigateWidgetGroup>(__func__);
147 
148  navgroup->region_size[0] = -1;
149  navgroup->region_size[1] = -1;
150 
151  const struct NavigateGizmoInfo *navigate_params = navigate_params_from_space_type(
152  gzgroup->type->gzmap_params.spaceid);
153 
154  for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
155  const struct NavigateGizmoInfo *info = &navigate_params[i];
156  navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, nullptr);
157  wmGizmo *gz = navgroup->gz_array[i];
159 
160  {
161  uchar icon_color[3];
162  UI_GetThemeColor3ubv(TH_TEXT, icon_color);
163  int color_tint, color_tint_hi;
164  if (icon_color[0] > 128) {
165  color_tint = -40;
166  color_tint_hi = 60;
167  gz->color[3] = 0.5f;
168  gz->color_hi[3] = 0.5f;
169  }
170  else {
171  color_tint = 60;
172  color_tint_hi = 60;
173  gz->color[3] = 0.5f;
174  gz->color_hi[3] = 0.75f;
175  }
176  UI_GetThemeColorShade3fv(TH_HEADER, color_tint, gz->color);
177  UI_GetThemeColorShade3fv(TH_HEADER, color_tint_hi, gz->color_hi);
178  }
179 
180  /* may be overwritten later */
181  gz->scale_basis = (GIZMO_SIZE * GIZMO_MINI_FAC) / 2;
182  if (info->icon != 0) {
183  PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
184  RNA_property_enum_set(gz->ptr, prop, info->icon);
185  RNA_enum_set(
187  }
188 
190  WM_gizmo_operator_set(gz, 0, ot, nullptr);
191  }
192 
193  /* Modal operators, don't use initial mouse location since we're clicking on a button. */
194  {
195  int gz_ids[] = {GZ_INDEX_ZOOM};
196  for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
197  wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
198  wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
199  RNA_boolean_set(&gzop->ptr, "use_cursor_init", false);
200  }
201  }
202 
203  gzgroup->customdata = navgroup;
204 }
205 
207 {
208  NavigateWidgetGroup *navgroup = static_cast<NavigateWidgetGroup *>(gzgroup->customdata);
209  ARegion *region = CTX_wm_region(C);
210 
211  const rcti *rect_visible = ED_region_visible_rect(region);
212 
213  if ((navgroup->state.rect_visible.xmax == rect_visible->xmax) &&
214  (navgroup->state.rect_visible.ymax == rect_visible->ymax)) {
215  return;
216  }
217 
218  navgroup->state.rect_visible = *rect_visible;
219 
220  const float icon_size = GIZMO_SIZE;
221  const float icon_offset_mini = icon_size * GIZMO_MINI_OFFSET_FAC * UI_DPI_FAC;
222  const float co[2] = {
223  roundf(rect_visible->xmax - (icon_offset_mini * 0.75f)),
224  roundf(rect_visible->ymax - (icon_offset_mini * 0.75f)),
225  };
226 
227  wmGizmo *gz;
228 
229  for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
230  gz = navgroup->gz_array[i];
232  }
233 
234  int icon_mini_slot = 0;
235 
236  gz = navgroup->gz_array[GZ_INDEX_ZOOM];
237  gz->matrix_basis[3][0] = roundf(co[0]);
238  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
240 
241  gz = navgroup->gz_array[GZ_INDEX_MOVE];
242  gz->matrix_basis[3][0] = roundf(co[0]);
243  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
245 }
246 
247 void VIEW2D_GGT_navigate_impl(wmGizmoGroupType *gzgt, const char *idname)
248 {
249  gzgt->name = "View2D Navigate";
250  gzgt->idname = idname;
251 
254 
258 }
259 
struct ScrArea * CTX_wm_area(const bContext *C)
Definition: context.c:738
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
#define ARRAY_SIZE(arr)
#define UNUSED(x)
@ SPACE_CLIP
@ SPACE_SEQ
@ SPACE_IMAGE
@ SI_GIZMO_HIDE
@ SI_GIZMO_HIDE_NAVIGATE
@ SEQ_GIZMO_HIDE
@ SEQ_GIZMO_HIDE_NAVIGATE
@ USER_SHOW_GIZMO_NAVIGATE
@ ED_GIZMO_BUTTON_SHOW_BACKDROP
@ ED_GIZMO_BUTTON_SHOW_OUTLINE
const rcti * ED_region_visible_rect(ARegion *region)
Definition: area.c:3763
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
#define UI_DPI_FAC
Definition: UI_interface.h:305
@ TH_HEADER
Definition: UI_resources.h:50
@ TH_TEXT
Definition: UI_resources.h:42
void UI_GetThemeColorShade3fv(int colorid, int offset, float col[3])
Definition: resources.c:1191
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
Definition: resources.c:1323
@ 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
static void area(int d1, int d2, int e1, int e2, float weights[2])
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
Definition: rna_access.c:4874
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
Definition: rna_access.c:3421
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:717
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:5015
wmGizmo * gz_array[GZ_INDEX_TOTAL]
struct NavigateWidgetGroup::@445 state
int ymax
Definition: DNA_vec_types.h:64
int xmax
Definition: DNA_vec_types.h:63
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
struct wmGizmoMapType_Params gzmap_params
const char * name
wmGizmoGroupFnDrawPrepare draw_prepare
struct wmGizmoGroupType * type
PointerRNA ptr
float matrix_basis[4][4]
float color_hi[4]
float color[4]
struct PointerRNA * ptr
float scale_basis
eWM_GizmoFlag flag
static struct NavigateGizmoInfo g_navigate_params_for_space_clip[GZ_INDEX_TOTAL]
static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
#define GIZMO_MINI_FAC
static struct NavigateGizmoInfo * navigate_params_from_space_type(short space_type)
static struct NavigateGizmoInfo g_navigate_params_for_space_image[GZ_INDEX_TOTAL]
#define GIZMO_SIZE
void VIEW2D_GGT_navigate_impl(wmGizmoGroupType *gzgt, const char *idname)
#define GIZMO_MINI_OFFSET_FAC
@ GZ_INDEX_ZOOM
@ GZ_INDEX_MOVE
@ GZ_INDEX_TOTAL
static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
static struct NavigateGizmoInfo g_navigate_params_for_view2d[GZ_INDEX_TOTAL]
static void WIDGETGROUP_navigate_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
wmOperatorType * ot
Definition: wm_files.c:3479
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition: wm_gizmo.c:203
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:304
struct wmGizmoOpElem * WM_gizmo_operator_get(wmGizmo *gz, int part_index)
Definition: wm_gizmo.c:195
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:94
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)