Blender V5.3
gizmo_library_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include "DNA_screen_types.h"
14#include "DNA_view3d_types.h"
15
16#include "BKE_context.hh"
17#include "BKE_global.hh"
18#include "BKE_main.hh"
19#include "BKE_screen.hh"
20
21#include "BLI_listbase.hh"
22#include "BLI_math_geom_c.hh"
23#include "BLI_math_matrix_c.hh"
24#include "BLI_math_vector_c.hh"
25
26#include "RNA_access.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "ED_screen.hh"
32#include "ED_view3d.hh"
33
34#include "CLG_log.h"
35
36/* own includes */
38
39namespace blender {
40
41static CLG_LogRef LOG = {"tool.gizmo"};
42
43/* factor for precision tweaking */
44#define GIZMO_PRECISION_FAC 0.05f
45
46BLI_INLINE float gizmo_offset_from_value_constr(const float range_fac,
47 const float min,
48 const float range,
49 const float value,
50 const bool inverted)
51{
52 return inverted ? (range_fac * (min + range - value) / range) : (range_fac * (value / range));
53}
54
55BLI_INLINE float gizmo_value_from_offset_constr(const float range_fac,
56 const float min,
57 const float range,
58 const float value,
59 const bool inverted)
60{
61 return inverted ? (min + range - (value * range / range_fac)) : (value * range / range_fac);
62}
63
65 const float value,
66 const bool constrained,
67 const bool inverted)
68{
69 if (constrained) {
71 data->range_fac, data->min, data->range, value, inverted);
72 }
73
74 return value;
75}
76
78 GizmoInteraction *inter,
79 const float offset,
80 const bool constrained,
81 const bool inverted,
82 const bool use_precision)
83{
84 const float max = data->min + data->range;
85
86 if (use_precision) {
87 /* add delta offset of this step to total precision_offset */
88 inter->precision_offset += offset - inter->prev_offset;
89 }
90 inter->prev_offset = offset;
91
92 float ofs_new = inter->init_offset + offset -
93 inter->precision_offset * (1.0f - GIZMO_PRECISION_FAC);
94 float value;
95
96 if (constrained) {
98 data->range_fac, data->min, data->range, ofs_new, inverted);
99 }
100 else {
101 value = ofs_new;
102 }
103
104 /* clamp to custom range */
105 if (data->is_custom_range_set) {
106 CLAMP(value, data->min, max);
107 }
108
109 return value;
110}
111
114 wmGizmoProperty *gz_prop,
115 const bool constrained,
116 const bool inverted)
117{
118 if (gz_prop->custom_func.value_get_fn != nullptr) {
119 /* Pass. */
120 }
121 else if (gz_prop->prop != nullptr) {
122 /* Pass. */
123 }
124 else {
125 data->offset = 0.0f;
126 return;
127 }
128
129 float value = WM_gizmo_target_property_float_get(gz, gz_prop);
130
131 if (constrained) {
132 if (data->is_custom_range_set == false) {
133 float range[2];
134 if (WM_gizmo_target_property_float_range_get(gz, gz_prop, range)) {
135 data->range = range[1] - range[0];
136 data->min = range[0];
137 }
138 else {
139 BLI_assert(0);
140 }
141 }
143 data->range_fac, data->min, data->range, value, inverted);
144 }
145 else {
146 data->offset = value;
147 }
148}
149
151 const wmGizmo *gz,
152 GizmoInteraction *inter,
153 wmGizmoProperty *gz_prop)
154{
155 WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init_value);
156}
157
158/* -------------------------------------------------------------------- */
159
160void gizmo_color_get(const wmGizmo *gz, const bool highlight, float r_color[4])
161{
162 if (highlight && !(gz->flag & WM_GIZMO_DRAW_HOVER)) {
163 copy_v4_v4(r_color, gz->color_hi);
164 }
165 else {
166 copy_v4_v4(r_color, gz->color);
167 }
168}
169
170/* -------------------------------------------------------------------- */
171
173 bContext *C, const wmGizmo *gz, const float mval[2], int axis, bool use_offset, float r_co[2])
174{
175 float mat[4][4], imat[4][4];
176 {
177 float mat_identity[4][4];
178 wmGizmoMatrixParams params = {nullptr};
179 if (use_offset == false) {
180 unit_m4(mat_identity);
181 params.matrix_offset = mat_identity;
182 }
183 WM_gizmo_calc_matrix_final_params(gz, &params, mat);
184 }
185
186 if (!invert_m4_m4(imat, mat)) {
187 CLOG_WARN(&LOG,
188 "Gizmo \"%s\" of group \"%s\" has matrix that could not be inverted "
189 "(projection will fail)",
190 gz->type->idname,
192 }
193
194 /* rotate mouse in relation to the center and relocate it */
196 /* For 3d views, transform 2D mouse pos onto plane. */
197 ARegion *region = CTX_wm_region(C);
198
199 float plane[4], co[3];
200 plane_from_point_normal_v3(plane, mat[3], mat[2]);
201 bool clip_ray = (static_cast<RegionView3D *>(region->regiondata))->is_persp;
202 if (ED_view3d_win_to_3d_on_plane(region, plane, mval, clip_ray, co)) {
203 mul_m4_v3(imat, co);
204 r_co[0] = co[(axis + 1) % 3];
205 r_co[1] = co[(axis + 2) % 3];
206 return true;
207 }
208 return false;
209 }
210
211 float co[3] = {mval[0], mval[1], 0.0f};
212 mul_m4_v3(imat, co);
213 copy_v2_v2(r_co, co);
214 return true;
215}
216
218 bContext *C, const wmGizmo *gz, const float mval[2], bool use_offset, float r_co[3])
219{
220 float mat[4][4], imat[4][4];
221 {
222 float mat_identity[4][4];
223 wmGizmoMatrixParams params = {nullptr};
224 if (use_offset == false) {
225 unit_m4(mat_identity);
226 params.matrix_offset = mat_identity;
227 }
228 WM_gizmo_calc_matrix_final_params(gz, &params, mat);
229 }
230
231 if (!invert_m4_m4(imat, mat)) {
232 CLOG_WARN(&LOG,
233 "Gizmo \"%s\" of group \"%s\" has matrix that could not be inverted "
234 "(projection will fail)",
235 gz->type->idname,
237 }
238
240 View3D *v3d = CTX_wm_view3d(C);
241 ARegion *region = CTX_wm_region(C);
242 /* NOTE: we might want a custom reference point passed in,
243 * instead of the gizmo center. */
244 ED_view3d_win_to_3d(v3d, region, mat[3], mval, r_co);
245 mul_m4_v3(imat, r_co);
246 return true;
247 }
248
249 float co[3] = {mval[0], mval[1], 0.0f};
250 mul_m4_v3(imat, co);
251 copy_v2_v2(r_co, co);
252 return true;
253}
254
255/* -------------------------------------------------------------------- */
258
260 const int spacetype,
261 const int regionid)
262{
263 /* Based on #rna_GizmoProperties_find_operator. */
264
265 for (bScreen *screen = G_MAIN->screens.first(); screen;
266 screen = static_cast<bScreen *>(screen->id.next))
267 {
268 for (ScrArea &area : screen->areabase) {
269 if (!ELEM(spacetype, SPACE_TYPE_ANY, area.spacetype)) {
270 continue;
271 }
272 for (ARegion &region : area.regionbase) {
273 if (region.runtime->gizmo_map == nullptr) {
274 continue;
275 }
276 if (!ELEM(regionid, RGN_TYPE_ANY, region.regiontype)) {
277 continue;
278 }
279
280 for (wmGizmoGroup &gzgroup : *WM_gizmomap_group_list(region.runtime->gizmo_map)) {
281 for (wmGizmo &gz : gzgroup.gizmos) {
282 if (gz.properties == properties) {
283 return &gz;
284 }
285 }
286 }
287 }
288 }
289 }
290 return nullptr;
291}
292
294
295} // namespace blender
#define G_MAIN
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define RGN_TYPE_ANY
#define SPACE_TYPE_ANY
#define C
Definition RandGen.cpp:29
#define CLAMP(a, b, c)
#define BLI_INLINE
#define ELEM(...)
btScalar max
bool clip_ray(float3 &start, float3 &end, const float3 direction, const float max_distance, const float4 frustum_planes[6])
#define GIZMO_PRECISION_FAC
ccl_device_inline float3x3 inverted(const float3x3 m)
BMesh Iterators.
float gizmo_offset_from_value(GizmoCommonData *data, float value, bool constrained, bool inverted)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const ListBaseT< wmGizmoGroup > * WM_gizmomap_group_list(wmGizmoMap *gzmap)
bool gizmo_window_project_3d(bContext *C, const wmGizmo *gz, const float mval[2], bool use_offset, float r_co[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void WM_gizmo_calc_matrix_final_params(const wmGizmo *gz, const wmGizmoMatrixParams *params, float r_mat[4][4])
Definition wm_gizmo.cc:519
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void WM_gizmo_target_property_float_set(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float value)
void mul_m4_v3(const float M[4][4], float r[3])
float gizmo_value_from_offset(GizmoCommonData *data, GizmoInteraction *inter, float offset, bool constrained, bool inverted, bool use_precision)
BMesh const char void * data
bool ED_view3d_win_to_3d_on_plane(const ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
BLI_INLINE float gizmo_value_from_offset_constr(const float range_fac, const float min, const float range, const float value, const bool inverted)
wmGizmo * gizmo_find_from_properties(const IDProperty *properties, const int spacetype, const int regionid)
void unit_m4(float m[4][4])
void gizmo_color_get(const wmGizmo *gz, bool highlight, float r_color[4])
void gizmo_property_data_update(wmGizmo *gz, GizmoCommonData *data, wmGizmoProperty *gz_prop, bool constrained, bool inverted)
BLI_INLINE float gizmo_offset_from_value_constr(const float range_fac, const float min, const float range, const float value, const bool inverted)
@ WM_GIZMO_DRAW_HOVER
bool gizmo_window_project_2d(bContext *C, const wmGizmo *gz, const float mval[2], int axis, bool use_offset, float r_co[2])
static CLG_LogRef LOG
Definition logging.cpp:12
MINLINE void copy_v4_v4(float r[4], const float a[4])
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
void gizmo_property_value_reset(bContext *C, const wmGizmo *gz, GizmoInteraction *inter, wmGizmoProperty *gz_prop)
ARegion * CTX_wm_region(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
float WM_gizmo_target_property_float_get(const wmGizmo *gz, wmGizmoProperty *gz_prop)
bool WM_gizmo_target_property_float_range_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, float range[2])
#define min(a, b)
Definition sort.cc:38
bke::ARegionRuntime * runtime
eRegion_Type regiontype
ListBaseT< ARegion > regionbase
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupType * type
const float(* matrix_offset)[4]
struct blender::wmGizmoProperty::@216011055122204070127377170176223170047224321165 custom_func
IDProperty * properties
const wmGizmoType * type
wmGizmoGroup * parent_gzgroup
eWM_GizmoFlag flag