Blender  V2.93
view2d_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 "ED_gizmo_library.h"
27 #include "ED_screen.h"
28 
29 #include "UI_interface.h"
30 #include "UI_resources.h"
31 
32 #include "MEM_guardedalloc.h"
33 
34 #include "RNA_access.h"
35 
36 #include "WM_api.h"
37 #include "WM_types.h"
38 
39 #include "UI_view2d.h"
40 
41 /* -------------------------------------------------------------------- */
49 /* Size of main icon. */
50 #define GIZMO_SIZE 80
51 /* Factor for size of smaller button. */
52 #define GIZMO_MINI_FAC 0.35f
53 /* How much mini buttons offset from the primary. */
54 #define GIZMO_MINI_OFFSET_FAC 0.38f
55 
56 enum {
59 
61 };
62 
64  const char *opname;
65  const char *gizmo;
67 };
68 
70  {
71  .opname = "IMAGE_OT_view_pan",
72  .gizmo = "GIZMO_GT_button_2d",
73  ICON_VIEW_PAN,
74  },
75  {
76  .opname = "IMAGE_OT_view_zoom",
77  .gizmo = "GIZMO_GT_button_2d",
78  ICON_VIEW_ZOOM,
79  },
80 };
81 
83  {
84  .opname = "CLIP_OT_view_pan",
85  .gizmo = "GIZMO_GT_button_2d",
86  ICON_VIEW_PAN,
87  },
88  {
89  .opname = "CLIP_OT_view_zoom",
90  .gizmo = "GIZMO_GT_button_2d",
91  ICON_VIEW_ZOOM,
92  },
93 };
94 
96  {
97  .opname = "VIEW2D_OT_pan",
98  .gizmo = "GIZMO_GT_button_2d",
99  ICON_VIEW_PAN,
100  },
101  {
102  .opname = "VIEW2D_OT_zoom",
103  .gizmo = "GIZMO_GT_button_2d",
104  ICON_VIEW_ZOOM,
105  },
106 };
107 
108 static struct NavigateGizmoInfo *navigate_params_from_space_type(short space_type)
109 {
110  switch (space_type) {
111  case SPACE_IMAGE:
113  case SPACE_CLIP:
115  default:
116  /* Used for sequencer. */
118  }
119 }
120 
123  /* Store the view state to check for changes. */
124  struct {
126  } state;
127  int region_size[2];
128 };
129 
131 {
132  if ((U.uiflag & USER_SHOW_GIZMO_NAVIGATE) == 0) {
133  return false;
134  }
135  return true;
136 }
137 
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  const struct NavigateGizmoInfo *navigate_params = navigate_params_from_space_type(
146  gzgroup->type->gzmap_params.spaceid);
147 
148  for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
149  const struct NavigateGizmoInfo *info = &navigate_params[i];
150  navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, NULL);
151  wmGizmo *gz = navgroup->gz_array[i];
153 
154  {
155  uchar icon_color[3];
156  UI_GetThemeColor3ubv(TH_TEXT, icon_color);
157  int color_tint, color_tint_hi;
158  if (icon_color[0] > 128) {
159  color_tint = -40;
160  color_tint_hi = 60;
161  gz->color[3] = 0.5f;
162  gz->color_hi[3] = 0.5f;
163  }
164  else {
165  color_tint = 60;
166  color_tint_hi = 60;
167  gz->color[3] = 0.5f;
168  gz->color_hi[3] = 0.75f;
169  }
170  UI_GetThemeColorShade3fv(TH_HEADER, color_tint, gz->color);
171  UI_GetThemeColorShade3fv(TH_HEADER, color_tint_hi, gz->color_hi);
172  }
173 
174  /* may be overwritten later */
175  gz->scale_basis = (GIZMO_SIZE * GIZMO_MINI_FAC) / 2;
176  if (info->icon != 0) {
177  PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
178  RNA_property_enum_set(gz->ptr, prop, info->icon);
179  RNA_enum_set(
181  }
182 
184  WM_gizmo_operator_set(gz, 0, ot, NULL);
185  }
186 
187  /* Modal operators, don't use initial mouse location since we're clicking on a button. */
188  {
189  int gz_ids[] = {GZ_INDEX_ZOOM};
190  for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
191  wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
192  wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
193  RNA_boolean_set(&gzop->ptr, "use_cursor_init", false);
194  }
195  }
196 
197  gzgroup->customdata = navgroup;
198 }
199 
201 {
202  struct NavigateWidgetGroup *navgroup = gzgroup->customdata;
203  ARegion *region = CTX_wm_region(C);
204 
205  const rcti *rect_visible = ED_region_visible_rect(region);
206 
207  if ((navgroup->state.rect_visible.xmax == rect_visible->xmax) &&
208  (navgroup->state.rect_visible.ymax == rect_visible->ymax)) {
209  return;
210  }
211 
212  navgroup->state.rect_visible = *rect_visible;
213 
214  const float icon_size = GIZMO_SIZE;
215  const float icon_offset_mini = icon_size * GIZMO_MINI_OFFSET_FAC * UI_DPI_FAC;
216  const float co[2] = {
217  roundf(rect_visible->xmax - (icon_offset_mini * 0.75f)),
218  roundf(rect_visible->ymax - (icon_offset_mini * 0.75f)),
219  };
220 
221  wmGizmo *gz;
222 
223  for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
224  gz = navgroup->gz_array[i];
226  }
227 
228  int icon_mini_slot = 0;
229 
230  gz = navgroup->gz_array[GZ_INDEX_ZOOM];
231  gz->matrix_basis[3][0] = roundf(co[0]);
232  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
234 
235  gz = navgroup->gz_array[GZ_INDEX_MOVE];
236  gz->matrix_basis[3][0] = roundf(co[0]);
237  gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
239 }
240 
241 /* Caller defines the name for gizmo group. */
242 void VIEW2D_GGT_navigate_impl(wmGizmoGroupType *gzgt, const char *idname)
243 {
244  gzgt->name = "View2D Navigate";
245  gzgt->idname = idname;
246 
249 
253 }
254 
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
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)
@ SPACE_CLIP
@ SPACE_IMAGE
@ 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:3682
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define UI_DPI_FAC
Definition: UI_interface.h:309
@ 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
wmGizmo * gz_array[GZ_INDEX_TOTAL]
struct NavigateWidgetGroup::@425 state
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
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]
#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
static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
static struct NavigateGizmoInfo g_navigate_params_for_view2d[GZ_INDEX_TOTAL]
static bool WIDGETGROUP_navigate_poll(const bContext *UNUSED(C), wmGizmoGroupType *UNUSED(gzgt))
static void WIDGETGROUP_navigate_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
@ GZ_INDEX_ZOOM
@ GZ_INDEX_MOVE
@ GZ_INDEX_TOTAL
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
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)