Blender V5.0
view2d_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
9#include "BLI_utildefines.h"
10
11#include "BKE_context.hh"
12
13#include "ED_gizmo_library.hh"
14#include "ED_screen.hh"
15
16#include "UI_resources.hh"
17
18#include "MEM_guardedalloc.h"
19
20#include "RNA_access.hh"
21
22#include "WM_api.hh"
23#include "WM_types.hh"
24
25#include "UI_view2d.hh"
26
27/* -------------------------------------------------------------------- */
34
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
42namespace {
43
44enum {
45 GZ_INDEX_MOVE = 0,
46 GZ_INDEX_ZOOM = 1,
47
48 GZ_INDEX_TOTAL = 2,
49};
50
51struct NavigateGizmoInfo {
52 const char *opname;
53 const char *gizmo;
54 uint icon;
55};
56
57struct NavigateWidgetGroup {
58 wmGizmo *gz_array[GZ_INDEX_TOTAL];
59 /* Store the view state to check for changes. */
60 struct {
61 rcti rect_visible;
62 } state;
63};
64
65} // namespace
66
67static NavigateGizmoInfo g_navigate_params_for_space_image[GZ_INDEX_TOTAL] = {
68 {
69 "IMAGE_OT_view_pan",
70 "GIZMO_GT_button_2d",
71 ICON_VIEW_PAN,
72 },
73 {
74 "IMAGE_OT_view_zoom",
75 "GIZMO_GT_button_2d",
76 ICON_VIEW_ZOOM,
77 },
78};
79
80static NavigateGizmoInfo g_navigate_params_for_space_clip[GZ_INDEX_TOTAL] = {
81 {
82 "CLIP_OT_view_pan",
83 "GIZMO_GT_button_2d",
84 ICON_VIEW_PAN,
85 },
86 {
87 "CLIP_OT_view_zoom",
88 "GIZMO_GT_button_2d",
89 ICON_VIEW_ZOOM,
90 },
91};
92
93static NavigateGizmoInfo g_navigate_params_for_view2d[GZ_INDEX_TOTAL] = {
94 {
95 "VIEW2D_OT_pan",
96 "GIZMO_GT_button_2d",
97 ICON_VIEW_PAN,
98 },
99 {
100 "VIEW2D_OT_zoom",
101 "GIZMO_GT_button_2d",
102 ICON_VIEW_ZOOM,
103 },
104};
105
106static NavigateGizmoInfo *navigate_params_from_space_type(short space_type)
107{
108 switch (space_type) {
109 case SPACE_IMAGE:
111 case SPACE_CLIP:
113 default:
114 /* Used for sequencer. */
116 }
117}
118
120{
121 if ((U.uiflag & USER_SHOW_GIZMO_NAVIGATE) == 0) {
122 return false;
123 }
124 ScrArea *area = CTX_wm_area(C);
125 if (area == nullptr) {
126 return false;
127 }
128 switch (area->spacetype) {
129 case SPACE_SEQ: {
130 const SpaceSeq *sseq = static_cast<const SpaceSeq *>(area->spacedata.first);
132 return false;
133 }
134 break;
135 }
136 case SPACE_IMAGE: {
137 const SpaceImage *sima = static_cast<const SpaceImage *>(area->spacedata.first);
139 return false;
140 }
141 break;
142 }
143 case SPACE_CLIP: {
144 const SpaceClip *sc = static_cast<const SpaceClip *>(area->spacedata.first);
146 return false;
147 }
148 break;
149 }
150 }
151 return true;
152}
153
154static void WIDGETGROUP_navigate_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
155{
156 NavigateWidgetGroup *navgroup = MEM_callocN<NavigateWidgetGroup>(__func__);
157
158 const NavigateGizmoInfo *navigate_params = navigate_params_from_space_type(
159 gzgroup->type->gzmap_params.spaceid);
160
161 for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
162 const NavigateGizmoInfo *info = &navigate_params[i];
163 navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, nullptr);
164 wmGizmo *gz = navgroup->gz_array[i];
166
167 {
168 uchar icon_color[3];
169 UI_GetThemeColor3ubv(TH_TEXT, icon_color);
170 int color_tint, color_tint_hi;
171 if (icon_color[0] > 128) {
172 color_tint = -40;
173 color_tint_hi = 60;
174 gz->color[3] = 0.5f;
175 gz->color_hi[3] = 0.5f;
176 }
177 else {
178 color_tint = 60;
179 color_tint_hi = 60;
180 gz->color[3] = 0.5f;
181 gz->color_hi[3] = 0.75f;
182 }
183 UI_GetThemeColorShade3fv(TH_HEADER, color_tint, gz->color);
184 UI_GetThemeColorShade3fv(TH_HEADER, color_tint_hi, gz->color_hi);
185 }
186
187 /* may be overwritten later */
189 if (info->icon != 0) {
190 PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
191 RNA_property_enum_set(gz->ptr, prop, info->icon);
194 }
195
196 wmOperatorType *ot = WM_operatortype_find(info->opname, false);
197#ifdef WITH_PYTHON
198 if (ot != nullptr)
199#endif
200 {
201 WM_gizmo_operator_set(gz, 0, ot, nullptr);
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_ZOOM};
208 for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
209 wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
211 RNA_boolean_set(&gzop->ptr, "use_cursor_init", false);
212 }
213 }
214
215 gzgroup->customdata = navgroup;
216}
217
219{
220 NavigateWidgetGroup *navgroup = static_cast<NavigateWidgetGroup *>(gzgroup->customdata);
221 ARegion *region = CTX_wm_region(C);
222
223 const rcti *rect_visible = ED_region_visible_rect(region);
224
225 if ((navgroup->state.rect_visible.xmax == rect_visible->xmax) &&
226 (navgroup->state.rect_visible.ymax == rect_visible->ymax))
227 {
228 return;
229 }
230
231 navgroup->state.rect_visible = *rect_visible;
232
233 const float icon_size = GIZMO_SIZE;
234 const float icon_offset_mini = icon_size * GIZMO_MINI_OFFSET_FAC * UI_SCALE_FAC;
235 const float co[2] = {
236 roundf(rect_visible->xmax - (icon_offset_mini * 0.75f)),
237 roundf(rect_visible->ymax - (icon_offset_mini * 0.75f)),
238 };
239
240 wmGizmo *gz;
241
242 for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
243 gz = navgroup->gz_array[i];
245 }
246
247 int icon_mini_slot = 0;
248
249 gz = navgroup->gz_array[GZ_INDEX_ZOOM];
250 gz->matrix_basis[3][0] = roundf(co[0]);
251 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
253
254 gz = navgroup->gz_array[GZ_INDEX_MOVE];
255 gz->matrix_basis[3][0] = roundf(co[0]);
256 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
258}
259
260void VIEW2D_GGT_navigate_impl(wmGizmoGroupType *gzgt, const char *idname)
261{
262 gzgt->name = "View2D Navigate";
263 gzgt->idname = idname;
264
267
271}
272
ScrArea * CTX_wm_area(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
unsigned char uchar
unsigned int uint
#define ARRAY_SIZE(arr)
@ SI_GIZMO_HIDE
@ SI_GIZMO_HIDE_NAVIGATE
@ SPACE_CLIP
@ SPACE_SEQ
@ SPACE_IMAGE
@ SCLIP_GIZMO_HIDE
@ SCLIP_GIZMO_HIDE_NAVIGATE
@ SEQ_GIZMO_HIDE
@ SEQ_GIZMO_HIDE_NAVIGATE
#define UI_SCALE_FAC
@ USER_SHOW_GIZMO_NAVIGATE
@ ED_GIZMO_BUTTON_SHOW_BACKDROP
@ ED_GIZMO_BUTTON_SHOW_OUTLINE
const rcti * ED_region_visible_rect(ARegion *region)
Definition area.cc:4301
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
@ TH_HEADER
@ TH_TEXT
void UI_GetThemeColorShade3fv(int colorid, int offset, float col[3])
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
@ WM_GIZMO_HIDDEN
@ WM_GIZMO_MOVE_CURSOR
@ WM_GIZMO_DRAW_MODAL
@ WM_GIZMOGROUPTYPE_SCALE
@ WM_GIZMOGROUPTYPE_DRAW_MODAL_ALL
@ WM_GIZMOGROUPTYPE_PERSISTENT
#define U
#define roundf(x)
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
static ulong state[N]
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
void * first
ListBase spacedata
int ymax
int xmax
wmGizmoGroupFnInit setup
const char * idname
wmGizmoMapType_Params gzmap_params
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
wmGizmoGroupFnDrawPrepare draw_prepare
wmGizmoGroupType * type
float matrix_basis[4][4]
float color_hi[4]
float color[4]
PointerRNA * ptr
float scale_basis
eWM_GizmoFlag flag
i
Definition text_draw.cc:230
static void WIDGETGROUP_navigate_setup(const bContext *, wmGizmoGroup *gzgroup)
#define GIZMO_MINI_FAC
static NavigateGizmoInfo g_navigate_params_for_view2d[GZ_INDEX_TOTAL]
static NavigateGizmoInfo g_navigate_params_for_space_clip[GZ_INDEX_TOTAL]
static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *)
#define GIZMO_SIZE
static NavigateGizmoInfo g_navigate_params_for_space_image[GZ_INDEX_TOTAL]
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 NavigateGizmoInfo * navigate_params_from_space_type(short space_type)
wmOperatorType * ot
Definition wm_files.cc:4237
wmGizmoOpElem * WM_gizmo_operator_get(wmGizmo *gz, int part_index)
Definition wm_gizmo.cc:195
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:307
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition wm_gizmo.cc:203
wmGizmo * WM_gizmo_new(const StringRef idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:98
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)