Blender  V3.3
gizmo_library_utils.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2015 Blender Foundation. All rights reserved. */
3 
12 #include "BLI_math.h"
13 
14 #include "DNA_screen_types.h"
15 #include "DNA_view3d_types.h"
16 
17 #include "BKE_context.h"
18 
19 #include "RNA_access.h"
20 
21 #include "WM_api.h"
22 #include "WM_types.h"
23 
24 #include "ED_view3d.h"
25 
26 #include "CLG_log.h"
27 
28 /* own includes */
29 #include "gizmo_library_intern.h"
30 
31 static CLG_LogRef LOG = {"ed.gizmo.library_utils"};
32 
33 /* factor for precision tweaking */
34 #define GIZMO_PRECISION_FAC 0.05f
35 
36 BLI_INLINE float gizmo_offset_from_value_constr(const float range_fac,
37  const float min,
38  const float range,
39  const float value,
40  const bool inverted)
41 {
42  return inverted ? (range_fac * (min + range - value) / range) : (range_fac * (value / range));
43 }
44 
45 BLI_INLINE float gizmo_value_from_offset_constr(const float range_fac,
46  const float min,
47  const float range,
48  const float value,
49  const bool inverted)
50 {
51  return inverted ? (min + range - (value * range / range_fac)) : (value * range / range_fac);
52 }
53 
55  const float value,
56  const bool constrained,
57  const bool inverted)
58 {
59  if (constrained) {
61  data->range_fac, data->min, data->range, value, inverted);
62  }
63 
64  return value;
65 }
66 
68  GizmoInteraction *inter,
69  const float offset,
70  const bool constrained,
71  const bool inverted,
72  const bool use_precision)
73 {
74  const float max = data->min + data->range;
75 
76  if (use_precision) {
77  /* add delta offset of this step to total precision_offset */
78  inter->precision_offset += offset - inter->prev_offset;
79  }
80  inter->prev_offset = offset;
81 
82  float ofs_new = inter->init_offset + offset -
83  inter->precision_offset * (1.0f - GIZMO_PRECISION_FAC);
84  float value;
85 
86  if (constrained) {
88  data->range_fac, data->min, data->range, ofs_new, inverted);
89  }
90  else {
91  value = ofs_new;
92  }
93 
94  /* clamp to custom range */
95  if (data->is_custom_range_set) {
96  CLAMP(value, data->min, max);
97  }
98 
99  return value;
100 }
101 
104  wmGizmoProperty *gz_prop,
105  const bool constrained,
106  const bool inverted)
107 {
108  if (gz_prop->custom_func.value_get_fn != NULL) {
109  /* Pass. */
110  }
111  else if (gz_prop->prop != NULL) {
112  /* Pass. */
113  }
114  else {
115  data->offset = 0.0f;
116  return;
117  }
118 
119  float value = WM_gizmo_target_property_float_get(gz, gz_prop);
120 
121  if (constrained) {
122  if (data->is_custom_range_set == false) {
123  float range[2];
124  if (WM_gizmo_target_property_float_range_get(gz, gz_prop, range)) {
125  data->range = range[1] - range[0];
126  data->min = range[0];
127  }
128  else {
129  BLI_assert(0);
130  }
131  }
133  data->range_fac, data->min, data->range, value, inverted);
134  }
135  else {
136  data->offset = value;
137  }
138 }
139 
141  const wmGizmo *gz,
142  GizmoInteraction *inter,
143  wmGizmoProperty *gz_prop)
144 {
145  WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init_value);
146 }
147 
148 /* -------------------------------------------------------------------- */
149 
150 void gizmo_color_get(const wmGizmo *gz, const bool highlight, float r_col[4])
151 {
152  if (highlight && !(gz->flag & WM_GIZMO_DRAW_HOVER)) {
153  copy_v4_v4(r_col, gz->color_hi);
154  }
155  else {
156  copy_v4_v4(r_col, gz->color);
157  }
158 }
159 
160 /* -------------------------------------------------------------------- */
161 
163  const struct wmGizmo *gz,
164  const float mval[2],
165  int axis,
166  bool use_offset,
167  float r_co[2])
168 {
169  float mat[4][4], imat[4][4];
170  {
171  float mat_identity[4][4];
172  struct WM_GizmoMatrixParams params = {NULL};
173  if (use_offset == false) {
174  unit_m4(mat_identity);
175  params.matrix_offset = mat_identity;
176  }
178  }
179 
180  if (!invert_m4_m4(imat, mat)) {
181  CLOG_WARN(&LOG,
182  "Gizmo \"%s\" of group \"%s\" has matrix that could not be inverted "
183  "(projection will fail)",
184  gz->type->idname,
185  gz->parent_gzgroup->type->idname);
186  }
187 
188  /* rotate mouse in relation to the center and relocate it */
190  /* For 3d views, transform 2D mouse pos onto plane. */
191  ARegion *region = CTX_wm_region(C);
192 
193  float plane[4], co[3];
194  plane_from_point_normal_v3(plane, mat[3], mat[2]);
195  bool clip_ray = ((RegionView3D *)region->regiondata)->is_persp;
196  if (ED_view3d_win_to_3d_on_plane(region, plane, mval, clip_ray, co)) {
197  mul_m4_v3(imat, co);
198  r_co[0] = co[(axis + 1) % 3];
199  r_co[1] = co[(axis + 2) % 3];
200  return true;
201  }
202  return false;
203  }
204 
205  float co[3] = {mval[0], mval[1], 0.0f};
206  mul_m4_v3(imat, co);
207  copy_v2_v2(r_co, co);
208  return true;
209 }
210 
212  bContext *C, const struct wmGizmo *gz, const float mval[2], bool use_offset, float r_co[3])
213 {
214  float mat[4][4], imat[4][4];
215  {
216  float mat_identity[4][4];
217  struct WM_GizmoMatrixParams params = {NULL};
218  if (use_offset == false) {
219  unit_m4(mat_identity);
220  params.matrix_offset = mat_identity;
221  }
223  }
224 
225  if (!invert_m4_m4(imat, mat)) {
226  CLOG_WARN(&LOG,
227  "Gizmo \"%s\" of group \"%s\" has matrix that could not be inverted "
228  "(projection will fail)",
229  gz->type->idname,
230  gz->parent_gzgroup->type->idname);
231  }
232 
234  View3D *v3d = CTX_wm_view3d(C);
235  ARegion *region = CTX_wm_region(C);
236  /* NOTE: we might want a custom reference point passed in,
237  * instead of the gizmo center. */
238  ED_view3d_win_to_3d(v3d, region, mat[3], mval, r_co);
239  mul_m4_v3(imat, r_co);
240  return true;
241  }
242 
243  float co[3] = {mval[0], mval[1], 0.0f};
244  mul_m4_v3(imat, co);
245  copy_v2_v2(r_co, co);
246  return true;
247 }
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_INLINE
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition: math_geom.c:209
void unit_m4(float m[4][4])
Definition: rct.c:1090
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void copy_v2_v2(float r[2], const float a[2])
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:189
bool ED_view3d_win_to_3d_on_plane(const struct ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
void ED_view3d_win_to_3d(const struct View3D *v3d, const struct ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position CLAMP
#define C
Definition: RandGen.cpp:25
@ WM_GIZMO_DRAW_HOVER
@ WM_GIZMOGROUPTYPE_3D
bool gizmo_window_project_3d(bContext *C, const struct wmGizmo *gz, const float mval[2], bool use_offset, float r_co[3])
#define GIZMO_PRECISION_FAC
BLI_INLINE float gizmo_value_from_offset_constr(const float range_fac, const float min, const float range, const float value, const 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)
float gizmo_value_from_offset(GizmoCommonData *data, GizmoInteraction *inter, const float offset, const bool constrained, const bool inverted, const bool use_precision)
void gizmo_property_data_update(wmGizmo *gz, GizmoCommonData *data, wmGizmoProperty *gz_prop, const bool constrained, const bool inverted)
void gizmo_color_get(const wmGizmo *gz, const bool highlight, float r_col[4])
bool gizmo_window_project_2d(bContext *C, const struct wmGizmo *gz, const float mval[2], int axis, bool use_offset, float r_co[2])
float gizmo_offset_from_value(GizmoCommonData *data, const float value, const bool constrained, const bool inverted)
static CLG_LogRef LOG
void gizmo_property_value_reset(bContext *C, const wmGizmo *gz, GizmoInteraction *inter, wmGizmoProperty *gz_prop)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
#define min(a, b)
Definition: sort.c:35
void * regiondata
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
struct wmGizmoGroupType * type
PropertyRNA * prop
wmGizmoPropertyFnGet value_get_fn
struct wmGizmoProperty::@1185 custom_func
const char * idname
struct wmGizmoGroup * parent_gzgroup
float color_hi[4]
float color[4]
eWM_GizmoFlag flag
const struct wmGizmoType * type
float max
void WM_gizmo_calc_matrix_final_params(const wmGizmo *gz, const struct WM_GizmoMatrixParams *params, float r_mat[4][4])
Definition: wm_gizmo.c:502
void WM_gizmo_target_property_float_set(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float value)
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])