Blender V5.3
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.hh"
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
27namespace blender::ui {
28
29/* -------------------------------------------------------------------- */
36
37/* Size of main icon. */
38#define GIZMO_SIZE 80
39/* Factor for size of smaller button. */
40#define GIZMO_MINI_FAC 0.35f
41/* How much mini buttons offset from the primary. */
42#define GIZMO_MINI_OFFSET_FAC 0.38f
43
44namespace {
45
46enum {
47 GZ_INDEX_MOVE = 0,
48 GZ_INDEX_ZOOM = 1,
49
50 GZ_INDEX_TOTAL = 2,
51};
52
53struct NavigateGizmoInfo {
54 const char *opname;
55 const char *gizmo;
56 uint icon;
57};
58
59struct NavigateWidgetGroup {
60 wmGizmo *gz_array[GZ_INDEX_TOTAL];
61 /* Store the view state to check for changes. */
62 struct {
63 rcti rect_visible;
64 } state;
65};
66
67} // namespace
68
69static NavigateGizmoInfo g_navigate_params_for_space_image[GZ_INDEX_TOTAL] = {
70 {
71 "IMAGE_OT_view_pan",
72 "GIZMO_GT_button_2d",
73 ICON_VIEW_PAN,
74 },
75 {
76 "IMAGE_OT_view_zoom",
77 "GIZMO_GT_button_2d",
78 ICON_VIEW_ZOOM,
79 },
80};
81
82static NavigateGizmoInfo g_navigate_params_for_space_clip[GZ_INDEX_TOTAL] = {
83 {
84 "CLIP_OT_view_pan",
85 "GIZMO_GT_button_2d",
86 ICON_VIEW_PAN,
87 },
88 {
89 "CLIP_OT_view_zoom",
90 "GIZMO_GT_button_2d",
91 ICON_VIEW_ZOOM,
92 },
93};
94
95static NavigateGizmoInfo g_navigate_params_for_view2d[GZ_INDEX_TOTAL] = {
96 {
97 "VIEW2D_OT_pan",
98 "GIZMO_GT_button_2d",
99 ICON_VIEW_PAN,
100 },
101 {
102 "VIEW2D_OT_zoom",
103 "GIZMO_GT_button_2d",
104 ICON_VIEW_ZOOM,
105 },
106};
107
108static 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
122{
123 if ((U.uiflag & USER_SHOW_GIZMO_NAVIGATE) == 0) {
124 return false;
125 }
126 ScrArea *area = CTX_wm_area(C);
127 if (area == nullptr) {
128 return false;
129 }
130 switch (area->spacetype) {
131 case SPACE_SEQ: {
132 const SpaceSeq *sseq = area->spacedata.first_as<SpaceSeq>();
134 return false;
135 }
136 break;
137 }
138 case SPACE_IMAGE: {
139 const SpaceImage *sima = area->spacedata.first_as<SpaceImage>();
141 return false;
142 }
143 break;
144 }
145 case SPACE_CLIP: {
146 const SpaceClip *sc = area->spacedata.first_as<SpaceClip>();
148 return false;
149 }
150 break;
151 }
152 }
153 return true;
154}
155
156static void WIDGETGROUP_navigate_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
157{
158 NavigateWidgetGroup *navgroup = MEM_new_zeroed<NavigateWidgetGroup>(__func__);
159
160 const NavigateGizmoInfo *navigate_params = navigate_params_from_space_type(
161 gzgroup->type->gzmap_params.spaceid);
162
163 for (int i = 0; i < GZ_INDEX_TOTAL; i++) {
164 const NavigateGizmoInfo *info = &navigate_params[i];
165 navgroup->gz_array[i] = WM_gizmo_new(info->gizmo, gzgroup, nullptr);
166 wmGizmo *gz = navgroup->gz_array[i];
168
169 gz->color[3] = 0.0f;
170 gz->color_hi[3] = 0.0f;
171
172 /* may be overwritten later */
174 if (info->icon != 0) {
175 PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon");
176 RNA_property_enum_set(gz->ptr, prop, info->icon);
179 }
180
181 wmOperatorType *ot = WM_operatortype_find(info->opname, false);
182#ifdef WITH_PYTHON
183 if (ot != nullptr)
184#endif
185 {
186 WM_gizmo_operator_set(gz, 0, ot, nullptr);
187 }
188 }
189
190 /* Modal operators, don't use initial mouse location since we're clicking on a button. */
191 {
192 int gz_ids[] = {GZ_INDEX_ZOOM};
193 for (int i = 0; i < ARRAY_SIZE(gz_ids); i++) {
194 wmGizmo *gz = navgroup->gz_array[gz_ids[i]];
196 RNA_boolean_set(&gzop->ptr, "use_cursor_init", false);
197 }
198 }
199
200 gzgroup->customdata = navgroup;
201}
202
204{
205 NavigateWidgetGroup *navgroup = static_cast<NavigateWidgetGroup *>(gzgroup->customdata);
206 ARegion *region = CTX_wm_region(C);
207
208 const rcti *rect_visible = ED_region_visible_rect(region);
209
210 if ((navgroup->state.rect_visible.xmax == rect_visible->xmax) &&
211 (navgroup->state.rect_visible.ymax == rect_visible->ymax))
212 {
213 return;
214 }
215
216 navgroup->state.rect_visible = *rect_visible;
217
218 const float icon_size = GIZMO_SIZE;
219 const float icon_offset_mini = icon_size * GIZMO_MINI_OFFSET_FAC * UI_SCALE_FAC;
220 const float co[2] = {
221 roundf(rect_visible->xmax - (icon_offset_mini * 0.75f)),
222 roundf(rect_visible->ymax - (icon_offset_mini * 0.75f)),
223 };
224
225 wmGizmo *gz;
226
227 for (uint i = 0; i < ARRAY_SIZE(navgroup->gz_array); i++) {
228 gz = navgroup->gz_array[i];
230 }
231
232 int icon_mini_slot = 0;
233
234 gz = navgroup->gz_array[GZ_INDEX_ZOOM];
235 gz->matrix_basis[3][0] = roundf(co[0]);
236 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
238
239 gz = navgroup->gz_array[GZ_INDEX_MOVE];
240 gz->matrix_basis[3][0] = roundf(co[0]);
241 gz->matrix_basis[3][1] = roundf(co[1] - (icon_offset_mini * icon_mini_slot++));
243}
244
258
260
261} // namespace blender::ui
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define roundf(x)
const int state
static NavigateGizmoInfo * navigate_params_from_space_type(short space_type)
static void WIDGETGROUP_navigate_setup(const bContext *, wmGizmoGroup *gzgroup)
static NavigateGizmoInfo g_navigate_params_for_view2d[GZ_INDEX_TOTAL]
static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *)
static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
static NavigateGizmoInfo g_navigate_params_for_space_clip[GZ_INDEX_TOTAL]
void VIEW2D_GGT_navigate_impl(wmGizmoGroupType *gzgt, const char *idname)
static NavigateGizmoInfo g_navigate_params_for_space_image[GZ_INDEX_TOTAL]
UserDef U
Definition blender.cc:58
@ SEQ_GIZMO_HIDE_NAVIGATE
ScrArea * CTX_wm_area(const bContext *C)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
unsigned int uint
Definition BKE_text.h:55
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
const rcti * ED_region_visible_rect(ARegion *region)
Definition area.cc:4791
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ SI_GIZMO_HIDE_NAVIGATE
void ED_gizmo_button2d_group_background(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
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
@ WM_GIZMOGROUPTYPE_DRAW_MODAL_ALL
@ WM_GIZMOGROUPTYPE_PERSISTENT
@ WM_GIZMOGROUPTYPE_SCALE
@ WM_GIZMO_MOVE_CURSOR
@ WM_GIZMO_DRAW_MODAL
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
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)
@ SCLIP_GIZMO_HIDE_NAVIGATE
ListBaseT< SpaceLink > spacedata
eSpaceClip_Gizmo_Flag gizmo_flag
eSpaceImage_Gizmo_Flag gizmo_flag
eSpaceSeq_Gizmo_Flag gizmo_flag
wmGizmoGroupFnDrawBackground draw_background
wmGizmoMapType_Params gzmap_params
wmGizmoGroupFnDrawPrepare draw_prepare
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupType * type
float matrix_basis[4][4]
eWM_GizmoFlag flag
#define GIZMO_MINI_FAC
#define GIZMO_SIZE
#define GIZMO_MINI_OFFSET_FAC