1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2015 Blender Foundation.
* All rights reserved.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file gizmo_library_utils.c
* \ingroup wm
*
* \name Gizmo Library Utilities
*
* \brief This file contains functions for common behaviors of gizmos.
*/
#include "BLI_math.h"
#include "BLI_listbase.h"
#include "DNA_view3d_types.h"
#include "DNA_screen_types.h"
#include "BKE_context.h"
#include "RNA_access.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_view3d.h"
/* own includes */
#include "gizmo_library_intern.h"
/* factor for precision tweaking */
#define GIZMO_PRECISION_FAC 0.05f
BLI_INLINE float gizmo_offset_from_value_constr(
const float range_fac, const float min, const float range, const float value,
const bool inverted)
{
return inverted ? (range_fac * (min + range - value) / range) : (range_fac * (value / range));
}
BLI_INLINE float gizmo_value_from_offset_constr(
const float range_fac, const float min, const float range, const float value,
const bool inverted)
{
return inverted ? (min + range - (value * range / range_fac)) : (value * range / range_fac);
}
float gizmo_offset_from_value(
GizmoCommonData *data, const float value, const bool constrained, const bool inverted)
{
if (constrained)
return gizmo_offset_from_value_constr(data->range_fac, data->min, data->range, value, inverted);
return value;
}
float gizmo_value_from_offset(
GizmoCommonData *data, GizmoInteraction *inter, const float offset,
const bool constrained, const bool inverted, const bool use_precision)
{
const float max = data->min + data->range;
if (use_precision) {
/* add delta offset of this step to total precision_offset */
inter->precision_offset += offset - inter->prev_offset;
}
inter->prev_offset = offset;
float ofs_new = inter->init_offset + offset - inter->precision_offset * (1.0f - GIZMO_PRECISION_FAC);
float value;
if (constrained) {
value = gizmo_value_from_offset_constr(data->range_fac, data->min, data->range, ofs_new, inverted);
}
else {
value = ofs_new;
}
/* clamp to custom range */
if (data->flag & GIZMO_CUSTOM_RANGE_SET) {
CLAMP(value, data->min, max);
}
return value;
}
void gizmo_property_data_update(
wmGizmo *gz, GizmoCommonData *data, wmGizmoProperty *gz_prop,
const bool constrained, const bool inverted)
{
if (gz_prop->custom_func.value_get_fn != NULL) {
/* pass */
}
else if (gz_prop->prop != NULL) {
/* pass */
}
else {
data->offset = 0.0f;
return;
}
float value = WM_gizmo_target_property_float_get(gz, gz_prop);
if (constrained) {
if ((data->flag & GIZMO_CUSTOM_RANGE_SET) == 0) {
float range[2];
if (WM_gizmo_target_property_float_range_get(gz, gz_prop, range)) {
data->range = range[1] - range[0];
data->min = range[0];
}
else {
BLI_assert(0);
}
}
data->offset = gizmo_offset_from_value_constr(data->range_fac, data->min, data->range, value, inverted);
}
else {
data->offset = value;
}
}
void gizmo_property_value_reset(
bContext *C, const wmGizmo *gz, GizmoInteraction *inter,
wmGizmoProperty *gz_prop)
{
WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init_value);
}
/* -------------------------------------------------------------------- */
void gizmo_color_get(
const wmGizmo *gz, const bool highlight,
float r_col[4])
{
if (highlight && !(gz->flag & WM_GIZMO_DRAW_HOVER)) {
copy_v4_v4(r_col, gz->color_hi);
}
else {
copy_v4_v4(r_col, gz->color);
}
}
/* -------------------------------------------------------------------- */
/**
* Takes mouse coordinates and returns them in relation to the gizmo.
* Both 2D & 3D supported, use so we can use 2D gizmos in the 3D view.
*/
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 mat[4][4];
{
float mat_identity[4][4];
struct WM_GizmoMatrixParams params = {NULL};
if (use_offset == false) {
unit_m4(mat_identity);
params.matrix_offset = mat_identity;
}
WM_gizmo_calc_matrix_final_params(gz, ¶ms, mat);
}
/* rotate mouse in relation to the center and relocate it */
if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
/* For 3d views, transform 2D mouse pos onto plane. */
View3D *v3d = CTX_wm_view3d(C);
ARegion *ar = CTX_wm_region(C);
float plane[4];
plane_from_point_normal_v3(plane, mat[3], mat[2]);
float ray_origin[3], ray_direction[3];
if (ED_view3d_win_to_ray(CTX_data_depsgraph(C), ar, v3d, mval, ray_origin, ray_direction, false)) {
float lambda;
if (isect_ray_plane_v3(ray_origin, ray_direction, plane, &lambda, true)) {
float co[3];
madd_v3_v3v3fl(co, ray_origin, ray_direction, lambda);
float imat[4][4];
invert_m4_m4(imat, mat);
mul_m4_v3(imat, co);
r_co[0] = co[(axis + 1) % 3];
r_co[1] = co[(axis + 2) % 3];
return true;
}
}
return false;
}
else {
float co[3] = {mval[0], mval[1], 0.0f};
float imat[4][4];
invert_m4_m4(imat, mat);
mul_m4_v3(imat, co);
copy_v2_v2(r_co, co);
return true;
}
}
bool gizmo_window_project_3d(
bContext *C, const struct wmGizmo *gz, const float mval[2], bool use_offset,
float r_co[3])
{
float mat[4][4];
{
float mat_identity[4][4];
struct WM_GizmoMatrixParams params = {NULL};
if (use_offset == false) {
unit_m4(mat_identity);
params.matrix_offset = mat_identity;
}
WM_gizmo_calc_matrix_final_params(gz, ¶ms, mat);
}
if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
View3D *v3d = CTX_wm_view3d(C);
ARegion *ar = CTX_wm_region(C);
/* Note: we might want a custom reference point passed in,
* instead of the gizmo center. */
ED_view3d_win_to_3d(v3d, ar, mat[3], mval, r_co);
invert_m4(mat);
mul_m4_v3(mat, r_co);
return true;
}
else {
float co[3] = {mval[0], mval[1], 0.0f};
float imat[4][4];
invert_m4_m4(imat, mat);
mul_m4_v3(imat, co);
copy_v2_v2(r_co, co);
return true;
}
}
|