Blender  V3.3
view3d_gizmo_tool_generic.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_math.h"
8 #include "BLI_utildefines.h"
9 
10 #include "BKE_context.h"
11 #include "BKE_global.h"
12 
13 #include "ED_gizmo_library.h"
14 #include "ED_gizmo_utils.h"
15 #include "ED_screen.h"
16 #include "ED_transform.h"
17 
18 #include "UI_resources.h"
19 
20 #include "MEM_guardedalloc.h"
21 
22 #include "RNA_access.h"
23 #include "RNA_define.h"
24 
25 #include "WM_api.h"
26 #include "WM_message.h"
27 #include "WM_toolsystem.h"
28 #include "WM_types.h"
29 
30 #include "view3d_intern.h" /* own include */
31 
32 static const char *handle_normal_id;
33 static const char *handle_free_id;
34 
35 static const float handle_normal_radius_default = 100.0f;
36 static const float handle_free_radius_default = 36.0f;
37 
38 /* -------------------------------------------------------------------- */
43 {
45  return false;
46  }
47 
48  View3D *v3d = CTX_wm_view3d(C);
50  return false;
51  }
52 
53  /* Without this, refreshing the gizmo jitters in some cases with edit-mesh smooth. See T72948. */
54  if (G.moving & G_TRANSFORM_EDIT) {
55  return false;
56  }
57 
58  return true;
59 }
60 
62 {
63 
64  wmGizmo *gz = WM_gizmo_new("GIZMO_GT_button_2d", gzgroup, NULL);
66 
69 
71 
72  RNA_enum_set(gz->ptr, "icon", ICON_NONE);
73 
75  PointerRNA gzgt_ptr;
76  const bool gzgt_ptr_is_valid = WM_toolsystem_ref_properties_get_from_gizmo_group(
77  tref, gzgroup->type, &gzgt_ptr);
78 
79  if (gzgroup->type->idname == handle_normal_id) {
80  const float radius = (gzgt_ptr_is_valid ? RNA_float_get(&gzgt_ptr, "radius") :
82  12.0f;
83 
84  gz->scale_basis = radius / U.gizmo_size;
85  gz->matrix_offset[3][2] -= 12.0;
86  RNA_enum_set(gz->ptr,
87  "draw_options",
90  }
91  else {
92  const float radius = gzgt_ptr_is_valid ? RNA_float_get(&gzgt_ptr, "radius") :
94 
95  gz->scale_basis = radius / U.gizmo_size;
96 
97  RNA_enum_set(gz->ptr, "draw_options", ED_GIZMO_BUTTON_SHOW_BACKDROP);
98 
99  /* Make the center low alpha. */
100  WM_gizmo_set_line_width(gz, 2.0f);
101  RNA_float_set(gz->ptr,
102  "backdrop_fill_alpha",
103  gzgt_ptr_is_valid ? RNA_float_get(&gzgt_ptr, "backdrop_fill_alpha") : 0.125f);
104  }
105 
107  struct wmKeyConfig *kc = wm->defaultconf;
108 
110  return gz;
111 }
112 
114 {
115  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
116  wwrapper->gizmo = tool_generic_create_gizmo(C, gzgroup);
117  gzgroup->customdata = wwrapper;
118 }
119 
121 {
122  wmGizmoWrapper *wwrapper = gzgroup->customdata;
123  wmGizmo *gz = wwrapper->gizmo;
124 
128  return;
129  }
130 
131  /* skip, we don't draw anything anyway */
132  {
133  int orientation;
134  if (gzgroup->type->idname == handle_normal_id) {
135  orientation = V3D_ORIENT_NORMAL;
136  }
137  else {
138  orientation = V3D_ORIENT_GLOBAL; /* dummy, use view. */
139  }
140 
141  struct TransformBounds tbounds;
142  const bool hide = ED_transform_calc_gizmo_stats(C,
143  &(struct TransformCalcParams){
144  .use_only_center = true,
145  .orientation_index = orientation + 1,
146  },
147  &tbounds) == 0;
148 
150  if (hide) {
151  return;
152  }
153  copy_m4_m3(gz->matrix_basis, tbounds.axis);
154  copy_v3_v3(gz->matrix_basis[3], tbounds.center);
155  negate_v3(gz->matrix_basis[2]);
156  }
157 
159 }
160 
162  wmGizmoGroup *gzgroup,
163  struct wmMsgBus *mbus)
164 {
165  ARegion *region = CTX_wm_region(C);
166 
167  wmMsgSubscribeValue msg_sub_value_gz_tag_refresh = {
168  .owner = region,
169  .user_data = gzgroup->parent_gzmap,
171  };
172 
173  {
174  const PropertyRNA *props[] = {
175  &rna_ToolSettings_workspace_tool_type,
176  };
177 
179  PointerRNA toolsettings_ptr;
180  RNA_pointer_create(&scene->id, &RNA_ToolSettings, scene->toolsettings, &toolsettings_ptr);
181 
182  for (int i = 0; i < ARRAY_SIZE(props); i++) {
184  mbus, &toolsettings_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
185  }
186  }
187 }
188 
189 static const char *handle_normal_id = "VIEW3D_GGT_tool_generic_handle_normal";
190 static const char *handle_free_id = "VIEW3D_GGT_tool_generic_handle_free";
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 
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:713
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
struct ToolSettings * CTX_data_tool_settings(const bContext *C)
Definition: context.c:1282
@ G_TRANSFORM_EDIT
Definition: BKE_global.h:248
void unit_m4(float m[4][4])
Definition: rct.c:1090
void copy_m4_m3(float m1[4][4], const float m2[3][3])
Definition: math_matrix.c:102
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_ORIENT_NORMAL
@ V3D_ORIENT_GLOBAL
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_CONTEXT
@ 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 struct bContext *C, struct wmGizmoGroupType *gzgt)
int ED_transform_calc_gizmo_stats(const struct bContext *C, const struct TransformCalcParams *params, struct TransformBounds *tbounds)
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void UI_GetThemeColor3fv(int colorid, float col[3])
Definition: resources.c:1165
@ TH_GIZMO_HI
Definition: UI_resources.h:304
@ TH_GIZMO_PRIMARY
Definition: UI_resources.h:305
@ 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
Scene scene
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
#define G(x, y, z)
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
Definition: rna_access.c:4968
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:5015
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3836
struct ToolSettings * toolsettings
float axis[3][3]
Definition: ED_transform.h:179
char gizmo_flag
bToolRef_Runtime * runtime
wmGizmoGroupFnMsgBusSubscribe message_subscribe
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
struct StructRNA * srna
struct wmGizmoMapType_Params gzmap_params
const char * name
struct wmGizmoGroupType * type
struct wmGizmoMap * parent_gzmap
struct wmGizmo * gizmo
float matrix_basis[4][4]
float matrix_offset[4][4]
float color_hi[4]
struct wmKeyMap * keymap
float color[4]
struct PointerRNA * ptr
float scale_basis
eWM_GizmoFlag flag
struct wmKeyConfig * defaultconf
static const char * handle_normal_id
static const float handle_free_radius_default
static const float handle_normal_radius_default
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 const char * handle_free_id
static void WIDGETGROUP_tool_generic_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static bool WIDGETGROUP_tool_generic_poll(const bContext *C, wmGizmoGroupType *gzgt)
static void WIDGETGROUP_gizmo_message_subscribe(const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
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.c:319
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:304
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:94
void WM_gizmo_do_msg_notify_tag_refresh(bContext *UNUSED(C), wmMsgSubscribeKey *UNUSED(msg_key), wmMsgSubscribeValue *msg_val)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition: wm_keymap.c:852
void WM_msg_subscribe_rna(struct wmMsgBus *mbus, PointerRNA *ptr, const PropertyRNA *prop, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
struct bToolRef * WM_toolsystem_ref_from_context(struct bContext *C)
Definition: wm_toolsystem.c:57