Blender V4.3
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
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/* -------------------------------------------------------------------- */
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->defaultconf;
111
113 return gz;
114}
115
117{
118 wmGizmoWrapper *wwrapper = static_cast<wmGizmoWrapper *>(
119 MEM_mallocN(sizeof(wmGizmoWrapper), __func__));
120 wwrapper->gizmo = tool_generic_create_gizmo(C, gzgroup);
121 gzgroup->customdata = wwrapper;
122
123 /* The tool handles undo, no need to set #WM_GIZMO_NEEDS_UNDO. */
124}
125
127{
128 wmGizmoWrapper *wwrapper = static_cast<wmGizmoWrapper *>(gzgroup->customdata);
129 wmGizmo *gz = wwrapper->gizmo;
130
134 return;
135 }
136
137 /* skip, we don't draw anything anyway */
138 {
139 int orientation;
140 if (gzgroup->type->idname == handle_normal_id) {
141 orientation = V3D_ORIENT_NORMAL;
142 }
143 else {
144 orientation = V3D_ORIENT_GLOBAL; /* dummy, use view. */
145 }
146
147 RegionView3D *rv3d = static_cast<RegionView3D *>(CTX_wm_region_data(C));
148 TransformBounds tbounds;
150 params.use_only_center = true;
151 params.orientation_index = orientation + 1;
152 const bool hide = ED_transform_calc_gizmo_stats(C, &params, &tbounds, rv3d) == 0;
153
155 if (hide) {
156 return;
157 }
158 copy_m4_m3(gz->matrix_basis, tbounds.axis);
159 copy_v3_v3(gz->matrix_basis[3], tbounds.center);
160 negate_v3(gz->matrix_basis[2]);
161 }
162
164}
165
167 wmGizmoGroup *gzgroup,
168 wmMsgBus *mbus)
169{
170 ARegion *region = CTX_wm_region(C);
171
172 wmMsgSubscribeValue msg_sub_value_gz_tag_refresh{};
173 msg_sub_value_gz_tag_refresh.owner = region;
174 msg_sub_value_gz_tag_refresh.user_data = gzgroup->parent_gzmap;
175 msg_sub_value_gz_tag_refresh.notify = WM_gizmo_do_msg_notify_tag_refresh;
176
177 {
178 const PropertyRNA *props[] = {
179 &rna_ToolSettings_workspace_tool_type,
180 };
181
182 Scene *scene = CTX_data_scene(C);
183 PointerRNA toolsettings_ptr = RNA_pointer_create(
184 &scene->id, &RNA_ToolSettings, scene->toolsettings);
185
186 for (int i = 0; i < ARRAY_SIZE(props); i++) {
188 mbus, &toolsettings_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
189 }
190 }
191}
192
194{
195 gzgt->name = "Generic Tool Widget Normal";
196 gzgt->idname = handle_normal_id;
197
200
203
208
209 RNA_def_float(gzgt->srna,
210 "radius",
212 0.0f,
213 1000.0,
214 "Radius",
215 "Radius in pixels",
216 0.0f,
217 1000.0f);
218}
219
221{
222 gzgt->name = "Generic Tool Widget Free";
223 gzgt->idname = handle_free_id;
224
225 /* Don't use 'WM_GIZMOGROUPTYPE_DELAY_REFRESH_FOR_TWEAK' here since this style of gizmo
226 * is better suited to being activated immediately. */
228
231
236
237 RNA_def_float(gzgt->srna,
238 "radius",
240 0.0f,
241 1000.0,
242 "Radius",
243 "Radius in pixels",
244 0.0f,
245 1000.0f);
247 gzgt->srna, "backdrop_fill_alpha", 0.125, 0.0f, 1.0f, "Backdrop Alpha", "", 0.0f, 1.0f);
248}
249
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 unit_m4(float m[4][4])
Definition rct.c:1127
void copy_m4_m3(float m1[4][4], const float m2[3][3])
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)
int ED_transform_calc_gizmo_stats(const bContext *C, const TransformCalcParams *params, TransformBounds *tbounds, RegionView3D *rv3d)
Read Guarded memory(de)allocation.
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)
unsigned int U
Definition btGjkEpa3.h:78
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
#define G(x, y, z)
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(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)
float axis[3][3]
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
struct wmKeyConfig * defaultconf
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:318
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:94
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
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:897
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)