Blender V5.3
move3d_gizmo.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
18
19#include "MEM_guardedalloc.h"
20
21#include "BLI_math_matrix_c.hh"
22#include "BLI_math_vector_c.hh"
24
25#include "BKE_context.hh"
26
27#include "GPU_immediate.hh"
28#include "GPU_immediate_util.hh"
29#include "GPU_matrix.hh"
30#include "GPU_select.hh"
31#include "GPU_state.hh"
32
33#include "RNA_access.hh"
34#include "RNA_define.hh"
35
36#include "WM_api.hh"
37#include "WM_types.hh"
38
39#include "ED_gizmo_library.hh"
40#include "ED_screen.hh"
42#include "ED_view3d.hh"
43
44/* own includes */
46
47namespace blender {
48
49#define MVAL_MAX_PX_DIST 12.0f
50#define RING_2D_RESOLUTION 32
51
54 /* Added to 'matrix_basis' when calculating the matrix. */
55 float prop_co[3];
56};
57
58static void gizmo_move_matrix_basis_get(const wmGizmo *gz, float r_matrix[4][4])
59{
60 MoveGizmo3D *move = reinterpret_cast<MoveGizmo3D *>(const_cast<wmGizmo *>(gz));
61
62 copy_m4_m4(r_matrix, move->gizmo.matrix_basis);
63 add_v3_v3(r_matrix[3], move->prop_co);
64}
65
66static wmOperatorStatus gizmo_move_modal(bContext *C,
67 wmGizmo *gz,
68 const wmEvent *event,
69 eWM_GizmoFlagTweak tweak_flag);
70
72 struct {
73 float mval[2];
74 /* Only for when using properties. */
75 float prop_co[3];
76 float matrix_final[4][4];
81
82 /* We could have other snap contexts, for now only support 3D view. */
84};
85
86/* -------------------------------------------------------------------- */
87
88static void move_geom_draw(const wmGizmo *gz,
89 const float color[4],
90 const bool select,
91 const int draw_options)
92{
93#ifdef USE_GIZMO_CUSTOM_DIAL
94 UNUSED_VARS(move3d, col, axis_modal_mat);
95 wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_move3d, select);
96#else
97 const int draw_style = RNA_enum_get(gz->ptr, "draw_style");
98 const bool filled = (draw_style != ED_GIZMO_MOVE_STYLE_CROSS_2D) &&
99 (draw_options & (select ? (ED_GIZMO_MOVE_DRAW_FLAG_FILL |
102
103 GPUVertFormat *format = immVertexFormat();
104 /* NOTE(Metal): Prefer using 3D coordinates with 3D shader, even if rendering 2D gizmo's. */
105 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
106
109
110 float viewport[4];
111 GPU_viewport_size_get_f(viewport);
112 immUniform2fv("viewportSize", &viewport[2]);
113 immUniform1f("lineWidth", (gz->line_width * U.pixelsize) + WM_gizmo_select_bias(select));
114
116
117 /* Use the final scale as a radius if it's not already applied to the final matrix. */
118 const float radius = (gz->flag & WM_GIZMO_DRAW_NO_SCALE) ? gz->scale_final : 1.0f;
119
120 if (draw_style == ED_GIZMO_MOVE_STYLE_RING_2D) {
121 if (filled) {
122 imm_draw_circle_fill_3d(pos, 0.0f, 0.0f, radius, RING_2D_RESOLUTION);
123 }
124 else {
125 imm_draw_circle_wire_3d(pos, 0.0f, 0.0f, radius, RING_2D_RESOLUTION);
126 }
127 }
128 else if (draw_style == ED_GIZMO_MOVE_STYLE_CROSS_2D) {
129 const float radius_diag = M_SQRT1_2 * radius;
131 immVertex3f(pos, radius_diag, radius_diag, 0.0f);
132 immVertex3f(pos, -radius_diag, -radius_diag, 0.0f);
133
134 immVertex3f(pos, -radius_diag, radius_diag, 0.0f);
135 immVertex3f(pos, radius_diag, -radius_diag, 0.0f);
136 immEnd();
137 }
138 else {
139 BLI_assert(0);
140 }
141
143
145#endif
146}
147
148static void move3d_get_translate(const wmGizmo *gz,
149 const wmEvent *event,
150 const ARegion *region,
151 float co_delta[3])
152{
153 MoveInteraction *inter = static_cast<MoveInteraction *>(gz->interaction_data);
154 const float xy_delta[2] = {
155 event->mval[0] - inter->init.mval[0],
156 event->mval[1] - inter->init.mval[1],
157 };
158
159 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
160 float co_ref[3];
161 mul_v3_mat3_m4v3(co_ref, gz->matrix_space, inter->init.prop_co);
162 const float zfac = ED_view3d_calc_zfac(rv3d, co_ref);
163
164 ED_view3d_win_to_delta(region, xy_delta, zfac, co_delta);
165
166 float matrix_space_inv[3][3];
167 copy_m3_m4(matrix_space_inv, gz->matrix_space);
168 invert_m3(matrix_space_inv);
169 mul_m3_v3(matrix_space_inv, co_delta);
170}
171
172static void move3d_draw_intern(const bContext *C,
173 wmGizmo *gz,
174 const bool select,
175 const bool highlight)
176{
177 MoveInteraction *inter = static_cast<MoveInteraction *>(gz->interaction_data);
178 const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
179 const bool align_view = (draw_options & ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW) != 0;
180 float color[4];
181 float matrix_final[4][4];
182 float matrix_align[4][4];
183
184 gizmo_color_get(gz, highlight, color);
185 WM_gizmo_calc_matrix_final(gz, matrix_final);
186
188 GPU_matrix_mul(matrix_final);
189
190 if (align_view) {
191 float matrix_final_unit[4][4];
193 normalize_m4_m4(matrix_final_unit, matrix_final);
194 mul_m4_m4m4(matrix_align, rv3d->viewmat, matrix_final_unit);
195 zero_v3(matrix_align[3]);
196 transpose_m4(matrix_align);
197 GPU_matrix_mul(matrix_align);
198 }
199
201 move_geom_draw(gz, color, select, draw_options);
204
205 if (gz->interaction_data) {
207 GPU_matrix_mul(inter->init.matrix_final);
208
209 if (align_view) {
210 GPU_matrix_mul(matrix_align);
211 }
212
214 move_geom_draw(gz, float4(0.5f, 0.5f, 0.5f, 0.5f), select, draw_options);
217 }
218}
219
220static void gizmo_move_draw_select(const bContext *C, wmGizmo *gz, int select_id)
221{
222 GPU_select_load_id(select_id);
223 move3d_draw_intern(C, gz, true, false);
224}
225
226static void gizmo_move_draw(const bContext *C, wmGizmo *gz)
227{
228 const bool is_modal = gz->state & WM_GIZMO_STATE_MODAL;
229 const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0;
230
231 (void)is_modal;
232
234 move3d_draw_intern(C, gz, false, is_highlight);
236}
237
239 wmGizmo *gz,
240 const wmEvent *event,
241 eWM_GizmoFlagTweak tweak_flag)
242{
243 using namespace blender::ed;
244 MoveInteraction *inter = static_cast<MoveInteraction *>(gz->interaction_data);
245 if ((event->type != MOUSEMOVE) && (inter->prev.tweak_flag == tweak_flag)) {
247 }
248 MoveGizmo3D *move = reinterpret_cast<MoveGizmo3D *>(gz);
249 ARegion *region = CTX_wm_region(C);
250
251 float prop_delta[3];
252 if (CTX_wm_area(C)->spacetype == SPACE_VIEW3D) {
253 move3d_get_translate(gz, event, region, prop_delta);
254 }
255 else {
256 float mval_proj_init[2], mval_proj_curr[2];
257 if ((gizmo_window_project_2d(C, gz, inter->init.mval, 2, false, mval_proj_init) == false) ||
258 (gizmo_window_project_2d(C, gz, float2(int2(event->mval)), 2, false, mval_proj_curr) ==
259 false))
260 {
262 }
263 sub_v2_v2v2(prop_delta, mval_proj_curr, mval_proj_init);
264 if ((gz->flag & WM_GIZMO_DRAW_NO_SCALE) == 0) {
265 mul_v2_fl(prop_delta, gz->scale_final);
266 }
267 prop_delta[2] = 0.0f;
268 }
269
270 if (tweak_flag & WM_GIZMO_TWEAK_PRECISE) {
271 mul_v3_fl(prop_delta, 0.1f);
272 }
273
274 add_v3_v3v3(move->prop_co, inter->init.prop_co, prop_delta);
275
276 if (tweak_flag & WM_GIZMO_TWEAK_SNAP) {
277 if (inter->snap_context_v3d) {
278 float dist_px = MVAL_MAX_PX_DIST * U.pixelsize;
279 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
280 float co[3];
286 inter->snap_context_v3d,
288 region,
291 &params,
292 nullptr,
293 mval_fl,
294 nullptr,
295 &dist_px,
296 co,
297 nullptr))
298 {
299 float matrix_space_inv[4][4];
300 invert_m4_m4(matrix_space_inv, gz->matrix_space);
301 mul_v3_m4v3(move->prop_co, matrix_space_inv, co);
302 }
303 }
304 }
305
306 /* set the property for the operator and call its modal function */
307 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
310 }
311 else {
312 zero_v3(move->prop_co);
313 }
314
316
317 inter->prev.tweak_flag = tweak_flag;
318
320}
321
322static void gizmo_move_exit(bContext *C, wmGizmo *gz, const bool cancel)
323{
324 MoveInteraction *inter = static_cast<MoveInteraction *>(gz->interaction_data);
325 bool use_reset_value = false;
326 const float *reset_value = nullptr;
327 if (cancel) {
328 /* Set the property for the operator and call its modal function. */
329 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
331 use_reset_value = true;
332 reset_value = inter->init.prop_co;
333 }
334 }
335
336 if (use_reset_value) {
337 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
339 WM_gizmo_target_property_float_set_array(C, gz, gz_prop, reset_value);
340 }
341 }
342
343 if (inter->snap_context_v3d) {
345 inter->snap_context_v3d = nullptr;
346 }
347
348 if (!cancel) {
349 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
352 }
353 }
354}
355
357{
358 const bool use_snap = RNA_boolean_get(gz->ptr, "use_snap");
359
361 inter->init.mval[0] = event->mval[0];
362 inter->init.mval[1] = event->mval[1];
363
364#if 0
365 copy_v3_v3(inter->init.prop_co, move->prop_co);
366#else
367 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset");
369 WM_gizmo_target_property_float_get_array(gz, gz_prop, inter->init.prop_co);
370 }
371#endif
372
373 WM_gizmo_calc_matrix_final(gz, inter->init.matrix_final);
374
375 if (use_snap) {
376 ScrArea *area = CTX_wm_area(C);
377 if (area) {
378 switch (area->spacetype) {
379 case SPACE_VIEW3D: {
381 break;
382 }
383 default:
384 /* Not yet supported. */
385 BLI_assert(0);
386 }
387 }
388 }
389
390 gz->interaction_data = inter;
391
393}
394
395static int gizmo_move_test_select(bContext *C, wmGizmo *gz, const int mval[2])
396{
397 float point_local[2];
398
399 if (gizmo_window_project_2d(C, gz, float2(int2(mval)), 2, true, point_local) == false) {
400 return -1;
401 }
402
403 /* The 'gz->scale_final' is already applied to the projection
404 * when #WM_GIZMO_DRAW_NO_SCALE isn't set. */
405 const float radius = (gz->flag & WM_GIZMO_DRAW_NO_SCALE) ? gz->scale_final : 1.0f;
406 if (len_squared_v2(point_local) < radius) {
407 return 0;
408 }
409
410 return -1;
411}
412
414{
415 MoveGizmo3D *move = reinterpret_cast<MoveGizmo3D *>(gz);
418 }
419 else {
420 zero_v3(move->prop_co);
421 }
422}
423
424static int gizmo_move_cursor_get(wmGizmo * /*gz*/)
425{
427}
428
429/* -------------------------------------------------------------------- */
432
434{
435 /* identifiers */
436 gzt->idname = "GIZMO_GT_move_3d";
437
438 /* API callbacks. */
439 gzt->draw = gizmo_move_draw;
445 gzt->modal = gizmo_move_modal;
446 gzt->exit = gizmo_move_exit;
448
449 gzt->struct_size = sizeof(MoveGizmo3D);
450
451 /* rna */
452 static const EnumPropertyItem rna_enum_draw_style[] = {
453 {ED_GIZMO_MOVE_STYLE_RING_2D, "RING_2D", 0, "Ring", ""},
454 {ED_GIZMO_MOVE_STYLE_CROSS_2D, "CROSS_2D", 0, "Ring", ""},
455 {0, nullptr, 0, nullptr, nullptr},
456 };
457 static const EnumPropertyItem rna_enum_draw_options[] = {
458 {ED_GIZMO_MOVE_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""},
459 {ED_GIZMO_MOVE_DRAW_FLAG_FILL_SELECT, "FILL_SELECT", 0, "Use fill for selection test", ""},
460 {ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW, "ALIGN_VIEW", 0, "Align View", ""},
461 {0, nullptr, 0, nullptr, nullptr},
462 };
463
465 gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_MOVE_STYLE_RING_2D, "Draw Style", "");
466 RNA_def_enum_flag(gzt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", "");
467 RNA_def_boolean(gzt->srna, "use_snap", false, "Use Snap", "");
468
470}
471
476 /* Move Gizmo API */
478
479} // namespace blender
#define M_SQRT1_2
#define SCE_SNAP_TO_VERTEX
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS(...)
#define MVAL_MAX_PX_DIST
#define RING_2D_RESOLUTION
void snap_object_context_destroy(SnapObjectContext *sctx)
SnapObjectContext * snap_object_context_create()
eSnapMode snap_object_project_view3d(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const eSnapMode snap_to, const SnapObjectParams *params, const float init_co[3], const float mval[2], const float prev_co[3], float *dist_px, float r_loc[3], float r_no[3])
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_m3_v3(const float M[3][3], float r[3])
static int gizmo_move_cursor_get(wmGizmo *)
void wm_gizmo_geometryinfo_draw(const GizmoGeomInfo *info, const bool, const float color[4])
static void gizmo_move_matrix_basis_get(const wmGizmo *gz, float r_matrix[4][4])
ScrArea * CTX_wm_area(const bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void immEnd()
void ED_gizmotypes_move_3d()
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
bool GPU_select_load_id(unsigned int id)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void WM_gizmotype_append(void(*gtfunc)(wmGizmoType *))
void WM_gizmo_target_property_float_get_array(const wmGizmo *gz, wmGizmoProperty *gz_prop, float *value)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v2_fl(float r[2], float f)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
GPUVertFormat * immVertexFormat()
void WM_gizmo_target_property_anim_autokey(bContext *C, const wmGizmo *, wmGizmoProperty *gz_prop)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void immBegin(GPUPrimType, uint vertex_len)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void WM_gizmo_calc_matrix_final(const wmGizmo *gz, float r_mat[4][4])
Definition wm_gizmo.cc:569
void immUniformColor4fv(const float rgba[4])
void WM_gizmotype_target_property_def(wmGizmoType *gzt, const char *idname, int data_type, int array_length)
void ED_region_tag_redraw_editor_overlays(ARegion *region)
Definition area.cc:745
void immUnbindProgram()
void GPU_matrix_mul(const float m[4][4])
MINLINE void mul_v3_fl(float r[3], float f)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
static wmOperatorStatus gizmo_move_modal(bContext *C, wmGizmo *gz, const wmEvent *event, eWM_GizmoFlagTweak tweak_flag)
static void move_geom_draw(const wmGizmo *gz, const float color[4], const bool select, const int draw_options)
VecBase< float, 4 > float4
static float WM_gizmo_select_bias(bool select)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void WM_gizmo_target_property_float_set_array(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float *value)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
static void gizmo_move_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
eWM_GizmoFlagTweak
Gizmo tweak flag. Bit-flag passed to gizmo while tweaking.
@ WM_GIZMO_TWEAK_PRECISE
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
static int gizmo_move_test_select(bContext *C, wmGizmo *gz, const int mval[2])
static void GIZMO_GT_move_3d(wmGizmoType *gzt)
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
static void move3d_draw_intern(const bContext *C, wmGizmo *gz, const bool select, const bool highlight)
@ ED_GIZMO_MOVE_DRAW_FLAG_FILL
@ ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW
@ ED_GIZMO_MOVE_DRAW_FLAG_FILL_SELECT
@ ED_GIZMO_MOVE_STYLE_CROSS_2D
@ ED_GIZMO_MOVE_STYLE_RING_2D
bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
float ED_view3d_calc_zfac(const RegionView3D *rv3d, const float co[3])
RegionView3D * CTX_wm_region_view3d(const bContext *C)
void gizmo_color_get(const wmGizmo *gz, bool highlight, float r_color[4])
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void imm_draw_circle_fill_3d(uint pos, float x, float y, float radius, int nsegments)
MINLINE void zero_v3(float r[3])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
bool invert_m3(float mat[3][3])
@ WM_GIZMO_DRAW_NO_SCALE
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
@ WM_CURSOR_NSEW_SCROLL
Definition wm_cursors.hh:54
void transpose_m4(float R[4][4])
@ WM_GIZMO_STATE_MODAL
@ WM_GIZMO_STATE_HIGHLIGHT
bool gizmo_window_project_2d(bContext *C, const wmGizmo *gz, const float mval[2], int axis, bool use_offset, float r_co[2])
static void move3d_get_translate(const wmGizmo *gz, const wmEvent *event, const ARegion *region, float co_delta[3])
static void gizmo_move_draw(const bContext *C, wmGizmo *gz)
PropertyRNA * RNA_def_enum_flag(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void immUniform2fv(const char *name, const float data[2])
void immVertex3f(uint attr_id, float x, float y, float z)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void copy_m4_m4(float m1[4][4], const float m2[4][4])
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void normalize_m4_m4(float rmat[4][4], const float mat[4][4]) ATTR_NONNULL()
void GPU_matrix_pop()
void imm_draw_circle_wire_3d(uint pos, float x, float y, float radius, int nsegments)
static wmOperatorStatus gizmo_move_invoke(bContext *C, wmGizmo *gz, const wmEvent *event)
ARegion * CTX_wm_region(const bContext *C)
static void gizmo_move_exit(bContext *C, wmGizmo *gz, const bool cancel)
void ED_view3d_win_to_delta(const ARegion *region, const float xy_delta[2], float zfac, float r_out[3], bool precise=false)
View3D * CTX_wm_view3d(const bContext *C)
static void gizmo_move_draw_select(const bContext *C, wmGizmo *gz, int select_id)
void GPU_matrix_push()
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ed::transform::SnapObjectContext * snap_context_v3d
struct blender::MoveInteraction::@134353076163105073340152261275001032372262143206 prev
struct blender::MoveInteraction::@111261155212355140066241025203027312125143212057 init
wmEventType type
Definition WM_types.hh:817
wmGizmoFnInvoke invoke
wmGizmoFnDrawSelect draw_select
wmGizmoFnCursorGet cursor_get
wmGizmoFnMatrixBasisGet matrix_basis_get
wmGizmoFnPropertyUpdate property_update
wmGizmoFnTestSelect test_select
float matrix_basis[4][4]
float matrix_space[4][4]
eWM_GizmoFlag flag
eWM_GizmoFlagState state