Blender V5.0
view3d_gizmo_tool_generic.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_math_matrix.h"
10#include "BLI_math_vector.h"
11#include "BLI_utildefines.h"
12
13#include "BKE_context.hh"
14#include "BKE_global.hh"
15
16#include "ED_gizmo_library.hh"
17#include "ED_gizmo_utils.hh"
18#include "ED_screen.hh"
19#include "ED_transform.hh"
20
21#include "UI_resources.hh"
22
23#include "MEM_guardedalloc.h"
24
25#include "RNA_access.hh"
26#include "RNA_define.hh"
27
28#include "WM_api.hh"
29#include "WM_message.hh"
30#include "WM_toolsystem.hh"
31#include "WM_types.hh"
32
33#include "view3d_intern.hh" /* own include */
34
35static const char *handle_normal_id = "VIEW3D_GGT_tool_generic_handle_normal";
36static const char *handle_free_id = "VIEW3D_GGT_tool_generic_handle_free";
37
38static const float handle_normal_radius_default = 100.0f;
39static const float handle_free_radius_default = 36.0f;
40
41/* -------------------------------------------------------------------- */
44
46{
48 return false;
49 }
50
51 View3D *v3d = CTX_wm_view3d(C);
53 return false;
54 }
55
56 /* Without this, refreshing the gizmo jitters in some cases with edit-mesh smooth. See #72948. */
57 if (G.moving & G_TRANSFORM_EDIT) {
58 return false;
59 }
60
61 return true;
62}
63
65{
66
67 wmGizmo *gz = WM_gizmo_new("GIZMO_GT_button_2d", gzgroup, nullptr);
69
72
74
75 RNA_enum_set(gz->ptr, "icon", ICON_NONE);
76
78 PointerRNA gzgt_ptr;
79 const bool gzgt_ptr_is_valid = WM_toolsystem_ref_properties_get_from_gizmo_group(
80 tref, gzgroup->type, &gzgt_ptr);
81
82 if (gzgroup->type->idname == handle_normal_id) {
83 const float radius = (gzgt_ptr_is_valid ? RNA_float_get(&gzgt_ptr, "radius") :
85 12.0f;
86
87 gz->scale_basis = radius / U.gizmo_size;
88 gz->matrix_offset[3][2] -= 12.0;
89 RNA_enum_set(gz->ptr,
90 "draw_options",
93 }
94 else {
95 const float radius = gzgt_ptr_is_valid ? RNA_float_get(&gzgt_ptr, "radius") :
97
98 gz->scale_basis = radius / U.gizmo_size;
99
100 RNA_enum_set(gz->ptr, "draw_options", ED_GIZMO_BUTTON_SHOW_BACKDROP);
101
102 /* Make the center low alpha. */
103 WM_gizmo_set_line_width(gz, 2.0f);
104 RNA_float_set(gz->ptr,
105 "backdrop_fill_alpha",
106 gzgt_ptr_is_valid ? RNA_float_get(&gzgt_ptr, "backdrop_fill_alpha") : 0.125f);
107 }
108
110 wmKeyConfig *kc = wm->runtime->defaultconf;
111
113 return gz;
114}
115
117{
118 wmGizmoWrapper *wwrapper = MEM_mallocN<wmGizmoWrapper>(__func__);
119 wwrapper->gizmo = tool_generic_create_gizmo(C, gzgroup);
120 gzgroup->customdata = wwrapper;
121
122 /* The tool handles undo, no need to set #WM_GIZMO_NEEDS_UNDO. */
123}
124
126{
127 wmGizmoWrapper *wwrapper = static_cast<wmGizmoWrapper *>(gzgroup->customdata);
128 wmGizmo *gz = wwrapper->gizmo;
129
133 return;
134 }
135
136 /* skip, we don't draw anything anyway */
137 {
138 int orientation;
139 if (gzgroup->type->idname == handle_normal_id) {
140 orientation = V3D_ORIENT_NORMAL;
141 }
142 else {
143 orientation = V3D_ORIENT_GLOBAL; /* dummy, use view. */
144 }
145
146 RegionView3D *rv3d = static_cast<RegionView3D *>(CTX_wm_region_data(C));
149 params.use_only_center = true;
150 params.orientation_index = orientation + 1;
151 const bool hide = blender::ed::transform::calc_gizmo_stats(C, &params, &tbounds, rv3d) == 0;
152
154 if (hide) {
155 return;
156 }
157 copy_m4_m3(gz->matrix_basis, tbounds.axis);
158 copy_v3_v3(gz->matrix_basis[3], tbounds.center);
159 negate_v3(gz->matrix_basis[2]);
160 }
161
163}
164
166 wmGizmoGroup *gzgroup,
167 wmMsgBus *mbus)
168{
169 ARegion *region = CTX_wm_region(C);
170
171 wmMsgSubscribeValue msg_sub_value_gz_tag_refresh{};
172 msg_sub_value_gz_tag_refresh.owner = region;
173 msg_sub_value_gz_tag_refresh.user_data = gzgroup->parent_gzmap;
174 msg_sub_value_gz_tag_refresh.notify = WM_gizmo_do_msg_notify_tag_refresh;
175
176 {
177 const PropertyRNA *props[] = {
178 &rna_ToolSettings_workspace_tool_type,
179 };
180
181 Scene *scene = CTX_data_scene(C);
182 PointerRNA toolsettings_ptr = RNA_pointer_create_discrete(
183 &scene->id, &RNA_ToolSettings, scene->toolsettings);
184
185 for (int i = 0; i < ARRAY_SIZE(props); i++) {
187 mbus, &toolsettings_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
188 }
189 }
190}
191
193{
194 gzgt->name = "Generic Tool Widget Normal";
195 gzgt->idname = handle_normal_id;
196
199
202
207
208 RNA_def_float(gzgt->srna,
209 "radius",
211 0.0f,
212 1000.0,
213 "Radius",
214 "Radius in pixels",
215 0.0f,
216 1000.0f);
217}
218
220{
221 gzgt->name = "Generic Tool Widget Free";
222 gzgt->idname = handle_free_id;
223
224 /* Don't use 'WM_GIZMOGROUPTYPE_DELAY_REFRESH_FOR_TWEAK' here since this style of gizmo
225 * is better suited to being activated immediately. */
227
230
235
236 RNA_def_float(gzgt->srna,
237 "radius",
239 0.0f,
240 1000.0,
241 "Radius",
242 "Radius in pixels",
243 0.0f,
244 1000.0f);
246 gzgt->srna, "backdrop_fill_alpha", 0.125, 0.0f, 1.0f, "Backdrop Alpha", "", 0.0f, 1.0f);
247}
248
void * CTX_wm_region_data(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
ToolSettings * CTX_data_tool_settings(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
@ G_TRANSFORM_EDIT
void copy_m4_m3(float m1[4][4], const float m2[3][3])
void unit_m4(float m[4][4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3(float r[3])
#define ARRAY_SIZE(arr)
@ SCE_WORKSPACE_TOOL_FALLBACK
@ RGN_TYPE_WINDOW
@ SPACE_VIEW3D
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_CONTEXT
@ V3D_ORIENT_NORMAL
@ V3D_ORIENT_GLOBAL
@ ED_GIZMO_BUTTON_SHOW_BACKDROP
@ ED_GIZMO_BUTTON_SHOW_OUTLINE
@ ED_GIZMO_BUTTON_SHOW_HELPLINE
bool ED_gizmo_poll_or_unlink_delayed_from_tool(const bContext *C, wmGizmoGroupType *gzgt)
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
void UI_GetThemeColor3fv(int colorid, float col[3])
@ TH_GIZMO_HI
@ TH_GIZMO_PRIMARY
@ WM_GIZMO_HIDDEN
@ WM_GIZMO_OPERATOR_TOOL_INIT
@ WM_GIZMO_DRAW_OFFSET_SCALE
@ WM_GIZMOGROUPTYPE_TOOL_FALLBACK_KEYMAP
@ WM_GIZMOGROUPTYPE_DELAY_REFRESH_FOR_TWEAK
@ WM_GIZMOGROUPTYPE_3D
#define WM_toolsystem_ref_properties_get_from_gizmo_group(tref, gzgroup, r_ptr)
#define U
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
#define G(x, y, z)
int calc_gizmo_stats(const bContext *C, const TransformCalcParams *params, TransformBounds *tbounds, RegionView3D *rv3d)
float RNA_float_get(PointerRNA *ptr, const char *name)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
struct ToolSettings * toolsettings
bToolRef_Runtime * runtime
wmGizmoGroupFnMsgBusSubscribe message_subscribe
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnInit setup
const char * idname
wmGizmoMapType_Params gzmap_params
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
wmGizmoMap * parent_gzmap
wmGizmoGroupType * type
float matrix_basis[4][4]
float matrix_offset[4][4]
float color_hi[4]
float color[4]
PointerRNA * ptr
float scale_basis
eWM_GizmoFlag flag
wmKeyMap * keymap
WindowManagerRuntimeHandle * runtime
i
Definition text_draw.cc:230
static const float handle_free_radius_default
static const char * handle_normal_id
static const float handle_normal_radius_default
static const char * handle_free_id
void VIEW3D_GGT_tool_generic_handle_free(wmGizmoGroupType *gzgt)
static void WIDGETGROUP_tool_generic_setup(const bContext *C, wmGizmoGroup *gzgroup)
void VIEW3D_GGT_tool_generic_handle_normal(wmGizmoGroupType *gzgt)
static void WIDGETGROUP_gizmo_message_subscribe(const bContext *C, wmGizmoGroup *gzgroup, wmMsgBus *mbus)
static void WIDGETGROUP_tool_generic_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static bool WIDGETGROUP_tool_generic_poll(const bContext *C, wmGizmoGroupType *gzgt)
static wmGizmo * tool_generic_create_gizmo(const bContext *C, wmGizmoGroup *gzgroup)
void WM_gizmo_set_line_width(wmGizmo *gz, const float line_width)
Definition wm_gizmo.cc:322
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:307
wmGizmo * WM_gizmo_new(const StringRef idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:98
void WM_gizmo_do_msg_notify_tag_refresh(bContext *, wmMsgSubscribeKey *, wmMsgSubscribeValue *msg_val)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:895
void WM_msg_subscribe_rna(wmMsgBus *mbus, PointerRNA *ptr, const PropertyRNA *prop, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
bToolRef * WM_toolsystem_ref_from_context(const bContext *C)