Blender  V2.93
view3d_gizmo_tool_generic.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_math.h"
22 #include "BLI_utildefines.h"
23 
24 #include "BKE_context.h"
25 #include "BKE_global.h"
26 
27 #include "ED_gizmo_library.h"
28 #include "ED_gizmo_utils.h"
29 #include "ED_screen.h"
30 #include "ED_transform.h"
31 
32 #include "UI_resources.h"
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "WM_toolsystem.h"
37 
38 #include "RNA_access.h"
39 
40 #include "WM_api.h"
41 #include "WM_message.h"
42 #include "WM_toolsystem.h"
43 #include "WM_types.h"
44 
45 #include "view3d_intern.h" /* own include */
46 
47 static const char *handle_normal_id;
48 static const char *handle_free_id;
49 
50 /* -------------------------------------------------------------------- */
55 {
57  return false;
58  }
59 
60  View3D *v3d = CTX_wm_view3d(C);
62  return false;
63  }
64 
65  /* Without this, refreshing the gizmo jitters in some cases with edit-mesh smooth. See T72948. */
66  if (G.moving & G_TRANSFORM_EDIT) {
67  return false;
68  }
69 
70  return true;
71 }
72 
74 {
75  wmGizmo *gz = WM_gizmo_new("GIZMO_GT_button_2d", gzgroup, NULL);
77 
80 
82 
83  RNA_enum_set(gz->ptr, "icon", ICON_NONE);
84 
85  if (gzgroup->type->idname == handle_normal_id) {
86  gz->scale_basis = 0.12f;
87  gz->matrix_offset[3][2] -= 12.0;
88  RNA_enum_set(gz->ptr,
89  "draw_options",
92  }
93  else {
94  gz->scale_basis = 0.16f * 3;
95 
96  RNA_enum_set(gz->ptr, "draw_options", ED_GIZMO_BUTTON_SHOW_BACKDROP);
97 
98  /* Make the center low alpha. */
99  WM_gizmo_set_line_width(gz, 2.0f);
100  RNA_float_set(gz->ptr, "backdrop_fill_alpha", 0.125f);
101  }
102 
105  struct wmKeyConfig *kc = wm->defaultconf;
106 
108  return gz;
109 }
110 
112 {
113  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
114  wwrapper->gizmo = tool_generic_create_gizmo(C, gzgroup);
115  gzgroup->customdata = wwrapper;
116 }
117 
119 {
120  wmGizmoWrapper *wwrapper = gzgroup->customdata;
121  wmGizmo *gz = wwrapper->gizmo;
122 
125  gzgroup->use_fallback_keymap = false;
127  return;
128  }
129 
130  gzgroup->use_fallback_keymap = true;
131 
132  /* skip, we don't draw anything anyway */
133  {
134  int orientation;
135  if (gzgroup->type->idname == handle_normal_id) {
136  orientation = V3D_ORIENT_NORMAL;
137  }
138  else {
139  orientation = V3D_ORIENT_GLOBAL; /* dummy, use view. */
140  }
141 
142  struct TransformBounds tbounds;
143  const bool hide = ED_transform_calc_gizmo_stats(C,
144  &(struct TransformCalcParams){
145  .use_only_center = true,
146  .orientation_index = orientation + 1,
147  },
148  &tbounds) == 0;
149 
151  if (hide) {
152  return;
153  }
154  copy_m4_m3(gz->matrix_basis, tbounds.axis);
155  copy_v3_v3(gz->matrix_basis[3], tbounds.center);
156  negate_v3(gz->matrix_basis[2]);
157  }
158 
160 }
161 
163  wmGizmoGroup *gzgroup,
164  struct wmMsgBus *mbus)
165 {
166  ARegion *region = CTX_wm_region(C);
167 
168  wmMsgSubscribeValue msg_sub_value_gz_tag_refresh = {
169  .owner = region,
170  .user_data = gzgroup->parent_gzmap,
172  };
173 
174  {
175  extern PropertyRNA rna_ToolSettings_workspace_tool_type;
176  const PropertyRNA *props[] = {
177  &rna_ToolSettings_workspace_tool_type,
178  };
179 
181  PointerRNA toolsettings_ptr;
182  RNA_pointer_create(&scene->id, &RNA_ToolSettings, scene->toolsettings, &toolsettings_ptr);
183 
184  for (int i = 0; i < ARRAY_SIZE(props); i++) {
186  mbus, &toolsettings_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
187  }
188  }
189 }
190 
191 static const char *handle_normal_id = "VIEW3D_GGT_tool_generic_handle_normal";
192 static const char *handle_free_id = "VIEW3D_GGT_tool_generic_handle_free";
193 
195 {
196  gzgt->name = "Generic Tool Widget Normal";
197  gzgt->idname = handle_normal_id;
198 
201 
204 
209 }
210 
212 {
213  gzgt->name = "Generic Tool Widget Free";
214  gzgt->idname = handle_free_id;
215 
216  /* Don't use 'WM_GIZMOGROUPTYPE_DELAY_REFRESH_FOR_TWEAK' here since this style of gizmo
217  * is better suited to being activated immediately. */
219 
222 
227 }
228 
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
struct ToolSettings * CTX_data_tool_settings(const bContext *C)
Definition: context.c:1208
@ G_TRANSFORM_EDIT
Definition: BKE_global.h:219
void unit_m4(float m[4][4])
Definition: rct.c:1140
void copy_m4_m3(float m1[4][4], const float m2[3][3])
Definition: math_matrix.c:120
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.
StructRNA RNA_ToolSettings
#define C
Definition: RandGen.cpp:39
void UI_GetThemeColor3fv(int colorid, float col[3])
Definition: resources.c:1191
@ TH_GIZMO_HI
Definition: UI_resources.h:320
@ TH_GIZMO_PRIMARY
Definition: UI_resources.h:321
@ 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
Scene scene
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
Definition: rna_access.c:6366
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:6413
struct ToolSettings * toolsettings
float axis[3][3]
Definition: ED_transform.h:180
char gizmo_flag
bToolRef_Runtime * runtime
wmGizmoGroupFnMsgBusSubscribe message_subscribe
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
struct wmGizmoMapType_Params gzmap_params
const char * name
struct wmGizmoGroupType * type
struct wmGizmoMap * parent_gzmap
bool use_fallback_keymap
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
#define G(x, y, z)
static const char * handle_normal_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 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:354
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:339
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:114
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:71