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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* ***** 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.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/transform/transform_gizmo_2d.c
* \ingroup edtransform
*
* \name 2D Transform Gizmo
*
* Used for UV/Image Editor
*/
#include "MEM_guardedalloc.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_view3d_types.h"
#include "BKE_context.h"
#include "BKE_editmesh.h"
#include "BKE_layer.h"
#include "RNA_access.h"
#include "UI_resources.h"
#include "UI_view2d.h"
#include "WM_api.h"
#include "WM_types.h"
#include "wm.h" /* XXX */
#include "ED_image.h"
#include "ED_screen.h"
#include "ED_uvedit.h"
#include "ED_gizmo_library.h"
#include "transform.h" /* own include */
/* axes as index */
enum {
MAN2D_AXIS_TRANS_X = 0,
MAN2D_AXIS_TRANS_Y,
MAN2D_AXIS_LAST,
};
typedef struct GizmoGroup2D {
wmGizmo *translate_x,
*translate_y;
wmGizmo *cage;
/* Current origin in view space, used to update widget origin for possible view changes */
float origin[2];
float min[2];
float max[2];
} GizmoGroup2D;
/* **************** Utilities **************** */
/* loop over axes */
#define MAN2D_ITER_AXES_BEGIN(axis, axis_idx) \
{ \
wmGizmo *axis; \
int axis_idx; \
for (axis_idx = 0; axis_idx < MAN2D_AXIS_LAST; axis_idx++) { \
axis = gizmo2d_get_axis_from_index(ggd, axis_idx);
#define MAN2D_ITER_AXES_END \
} \
} ((void)0)
static wmGizmo *gizmo2d_get_axis_from_index(const GizmoGroup2D *ggd, const short axis_idx)
{
BLI_assert(IN_RANGE_INCL(axis_idx, (float)MAN2D_AXIS_TRANS_X, (float)MAN2D_AXIS_TRANS_Y));
switch (axis_idx) {
case MAN2D_AXIS_TRANS_X:
return ggd->translate_x;
case MAN2D_AXIS_TRANS_Y:
return ggd->translate_y;
}
return NULL;
}
static void gizmo2d_get_axis_color(const int axis_idx, float *r_col, float *r_col_hi)
{
const float alpha = 0.6f;
const float alpha_hi = 1.0f;
int col_id;
switch (axis_idx) {
case MAN2D_AXIS_TRANS_X:
col_id = TH_AXIS_X;
break;
case MAN2D_AXIS_TRANS_Y:
col_id = TH_AXIS_Y;
break;
}
UI_GetThemeColor4fv(col_id, r_col);
copy_v4_v4(r_col_hi, r_col);
r_col[3] *= alpha;
r_col_hi[3] *= alpha_hi;
}
static GizmoGroup2D *gizmogroup2d_init(wmGizmoGroup *gzgroup)
{
const wmGizmoType *gzt_arrow = WM_gizmotype_find("GIZMO_GT_arrow_2d", true);
const wmGizmoType *gzt_cage = WM_gizmotype_find("GIZMO_GT_cage_2d", true);
GizmoGroup2D *ggd = MEM_callocN(sizeof(GizmoGroup2D), __func__);
ggd->translate_x = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
ggd->translate_y = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
ggd->cage = WM_gizmo_new_ptr(gzt_cage, gzgroup, NULL);
RNA_enum_set(ggd->cage->ptr, "transform",
ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE);
return ggd;
}
/**
* Calculates origin in view space, use with #gizmo2d_origin_to_region.
*/
static void gizmo2d_calc_bounds(const bContext *C, float *r_center, float *r_min, float *r_max)
{
float min_buf[2], max_buf[2];
if (r_min == NULL) {
r_min = min_buf;
}
if (r_max == NULL) {
r_max = max_buf;
}
ScrArea *sa = CTX_wm_area(C);
if (sa->spacetype == SPACE_IMAGE) {
SpaceImage *sima = sa->spacedata.first;
ViewLayer *view_layer = CTX_data_view_layer(C);
View3D *v3d = CTX_wm_view3d(C);
Image *ima = ED_space_image(sima);
uint objects_len = 0;
Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(
view_layer, v3d, &objects_len);
if (!ED_uvedit_minmax_multi(CTX_data_scene(C), ima, objects, objects_len, r_min, r_max)) {
zero_v2(r_min);
zero_v2(r_max);
}
MEM_freeN(objects);
}
else {
zero_v2(r_min);
zero_v2(r_max);
}
mid_v2_v2v2(r_center, r_min, r_max);
}
/**
* Convert origin (or any other point) from view to region space.
*/
BLI_INLINE void gizmo2d_origin_to_region(ARegion *ar, float *r_origin)
{
UI_view2d_view_to_region_fl(&ar->v2d, r_origin[0], r_origin[1], &r_origin[0], &r_origin[1]);
}
/**
* Custom handler for gizmo widgets
*/
static int gizmo2d_modal(
bContext *C, wmGizmo *widget, const wmEvent *UNUSED(event),
eWM_GizmoFlagTweak UNUSED(tweak_flag))
{
ARegion *ar = CTX_wm_region(C);
float origin[3];
gizmo2d_calc_bounds(C, origin, NULL, NULL);
gizmo2d_origin_to_region(ar, origin);
WM_gizmo_set_matrix_location(widget, origin);
ED_region_tag_redraw(ar);
return OPERATOR_RUNNING_MODAL;
}
void ED_widgetgroup_gizmo2d_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
{
wmOperatorType *ot_translate = WM_operatortype_find("TRANSFORM_OT_translate", true);
GizmoGroup2D *ggd = gizmogroup2d_init(gzgroup);
gzgroup->customdata = ggd;
MAN2D_ITER_AXES_BEGIN(axis, axis_idx)
{
const float offset[3] = {0.0f, 0.2f};
float color[4], color_hi[4];
gizmo2d_get_axis_color(axis_idx, color, color_hi);
/* custom handler! */
WM_gizmo_set_fn_custom_modal(axis, gizmo2d_modal);
/* set up widget data */
RNA_float_set(axis->ptr, "angle", -M_PI_2 * axis_idx);
RNA_float_set(axis->ptr, "length", 0.8f);
WM_gizmo_set_matrix_offset_location(axis, offset);
WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH);
WM_gizmo_set_scale(axis, U.gizmo_size);
WM_gizmo_set_color(axis, color);
WM_gizmo_set_color_highlight(axis, color_hi);
/* assign operator */
PointerRNA *ptr = WM_gizmo_operator_set(axis, 0, ot_translate, NULL);
bool constraint[3] = {0};
constraint[(axis_idx + 1) % 2] = 1;
if (RNA_struct_find_property(ptr, "constraint_axis"))
RNA_boolean_set_array(ptr, "constraint_axis", constraint);
RNA_boolean_set(ptr, "release_confirm", 1);
}
MAN2D_ITER_AXES_END;
{
wmOperatorType *ot_resize = WM_operatortype_find("TRANSFORM_OT_resize", true);
wmOperatorType *ot_rotate = WM_operatortype_find("TRANSFORM_OT_rotate", true);
PointerRNA *ptr;
/* assign operator */
ptr = WM_gizmo_operator_set(ggd->cage, 0, ot_translate, NULL);
RNA_boolean_set(ptr, "release_confirm", 1);
bool constraint_x[3] = {1, 0, 0};
bool constraint_y[3] = {0, 1, 0};
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_X, ot_resize, NULL);
PropertyRNA *prop_release_confirm = RNA_struct_find_property(ptr, "release_confirm");
PropertyRNA *prop_constraint_axis = RNA_struct_find_property(ptr, "constraint_axis");
RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint_x);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_X, ot_resize, NULL);
RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint_x);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y, ot_resize, NULL);
RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint_y);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y, ot_resize, NULL);
RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint_y);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y, ot_resize, NULL);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y, ot_resize, NULL);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y, ot_resize, NULL);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y, ot_resize, NULL);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
ptr = WM_gizmo_operator_set(ggd->cage, ED_GIZMO_CAGE2D_PART_ROTATE, ot_rotate, NULL);
RNA_property_boolean_set(ptr, prop_release_confirm, true);
}
}
void ED_widgetgroup_gizmo2d_refresh(const bContext *C, wmGizmoGroup *gzgroup)
{
GizmoGroup2D *ggd = gzgroup->customdata;
float origin[3];
gizmo2d_calc_bounds(C, origin, ggd->min, ggd->max);
copy_v2_v2(ggd->origin, origin);
bool show_cage = !equals_v2v2(ggd->min, ggd->max);
if (show_cage) {
ggd->cage->flag &= ~WM_GIZMO_HIDDEN;
ggd->translate_x->flag |= WM_GIZMO_HIDDEN;
ggd->translate_y->flag |= WM_GIZMO_HIDDEN;
}
else {
ggd->cage->flag |= WM_GIZMO_HIDDEN;
ggd->translate_x->flag &= ~WM_GIZMO_HIDDEN;
ggd->translate_y->flag &= ~WM_GIZMO_HIDDEN;
}
if (show_cage) {
wmGizmoOpElem *gzop;
float mid[2];
const float *min = ggd->min;
const float *max = ggd->max;
mid_v2_v2v2(mid, min, max);
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_X);
PropertyRNA *prop_center_override = RNA_struct_find_property(&gzop->ptr, "center_override");
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){max[0], mid[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_X);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){min[0], mid[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){mid[0], max[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){mid[0], min[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){max[0], max[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){max[0], min[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){min[0], max[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){min[0], min[1], 0.0f});
gzop = WM_gizmo_operator_get(ggd->cage, ED_GIZMO_CAGE2D_PART_ROTATE);
RNA_property_float_set_array(&gzop->ptr, prop_center_override, (float[3]){mid[0], mid[1], 0.0f});
}
}
void ED_widgetgroup_gizmo2d_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
{
ARegion *ar = CTX_wm_region(C);
GizmoGroup2D *ggd = gzgroup->customdata;
float origin[3] = {UNPACK2(ggd->origin), 0.0f};
float origin_aa[3] = {UNPACK2(ggd->origin), 0.0f};
gizmo2d_origin_to_region(ar, origin);
MAN2D_ITER_AXES_BEGIN(axis, axis_idx)
{
WM_gizmo_set_matrix_location(axis, origin);
}
MAN2D_ITER_AXES_END;
UI_view2d_view_to_region_m4(&ar->v2d, ggd->cage->matrix_space);
WM_gizmo_set_matrix_offset_location(ggd->cage, origin_aa);
ggd->cage->matrix_offset[0][0] = (ggd->max[0] - ggd->min[0]);
ggd->cage->matrix_offset[1][1] = (ggd->max[1] - ggd->min[1]);
}
/* TODO (Julian)
* - Called on every redraw, better to do a more simple poll and check for selection in _refresh
* - UV editing only, could be expanded for other things.
*/
bool ED_widgetgroup_gizmo2d_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
{
if ((U.gizmo_flag & USER_GIZMO_DRAW) == 0) {
return false;
}
SpaceImage *sima = CTX_wm_space_image(C);
Object *obedit = CTX_data_edit_object(C);
if (ED_space_image_show_uvedit(sima, obedit)) {
Image *ima = ED_space_image(sima);
Scene *scene = CTX_data_scene(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMFace *efa;
BMLoop *l;
BMIter iter, liter;
const int cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV);
/* check if there's a selected poly */
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (!uvedit_face_visible_test(scene, obedit, ima, efa))
continue;
BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
if (uvedit_uv_select_test(scene, l, cd_loop_uv_offset)) {
return true;
}
}
}
}
return false;
}
|