2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2016 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): none yet.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/transform/transform_manipulator2d.c
27 * \ingroup edtransform
29 * \name 2D Transform Manipulator
31 * Used for UV/Image Editor
34 #include "BKE_context.h"
35 #include "BKE_editmesh.h"
37 #include "BLI_listbase.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_space_types.h"
44 #include "DNA_view3d_types.h"
47 #include "ED_screen.h"
48 #include "ED_uvedit.h"
49 #include "ED_manipulator_library.h"
51 #include "MEM_guardedalloc.h"
53 #include "RNA_access.h"
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
60 #include "wm.h" /* XXX */
62 #include "transform.h" /* own include */
66 MAN2D_AXIS_TRANS_X = 0,
72 typedef struct ManipulatorGroup2D {
73 wmManipulator *translate_x,
76 /* Current origin in view space, used to update widget origin for possible view changes */
81 /* **************** Utilities **************** */
84 #define MAN2D_ITER_AXES_BEGIN(axis, axis_idx) \
86 wmManipulator *axis; \
88 for (axis_idx = 0; axis_idx < MAN2D_AXIS_LAST; axis_idx++) { \
89 axis = manipulator2d_get_axis_from_index(man, axis_idx);
91 #define MAN2D_ITER_AXES_END \
95 static wmManipulator *manipulator2d_get_axis_from_index(const ManipulatorGroup2D *man, const short axis_idx)
97 BLI_assert(IN_RANGE_INCL(axis_idx, (float)MAN2D_AXIS_TRANS_X, (float)MAN2D_AXIS_TRANS_Y));
100 case MAN2D_AXIS_TRANS_X:
101 return man->translate_x;
102 case MAN2D_AXIS_TRANS_Y:
103 return man->translate_y;
109 static void manipulator2d_get_axis_color(const int axis_idx, float *r_col, float *r_col_hi)
111 const float alpha = 0.6f;
112 const float alpha_hi = 1.0f;
116 case MAN2D_AXIS_TRANS_X:
119 case MAN2D_AXIS_TRANS_Y:
124 UI_GetThemeColor4fv(col_id, r_col);
126 copy_v4_v4(r_col_hi, r_col);
128 r_col_hi[3] *= alpha_hi;
131 static ManipulatorGroup2D *manipulatorgroup2d_init(wmManipulatorGroup *mgroup)
133 const wmManipulatorType *wt_arrow = WM_manipulatortype_find("MANIPULATOR_WT_arrow_2d", true);
135 ManipulatorGroup2D *man = MEM_callocN(sizeof(ManipulatorGroup2D), __func__);
137 man->translate_x = WM_manipulator_new_ptr(wt_arrow, mgroup, "translate_x", NULL);
138 man->translate_y = WM_manipulator_new_ptr(wt_arrow, mgroup, "translate_y", NULL);
144 * Calculates origin in view space, use with #manipulator2d_origin_to_region.
146 static void manipulator2d_calc_origin(const bContext *C, float *r_origin)
148 SpaceImage *sima = CTX_wm_space_image(C);
149 Image *ima = ED_space_image(sima);
151 if (sima->around == V3D_AROUND_CURSOR) {
152 copy_v2_v2(r_origin, sima->cursor);
155 ED_uvedit_center(CTX_data_scene(C), ima, CTX_data_edit_object(C), r_origin, sima->around);
160 * Convert origin (or any other point) from view to region space.
162 BLI_INLINE void manipulator2d_origin_to_region(ARegion *ar, float *r_origin)
164 UI_view2d_view_to_region_fl(&ar->v2d, r_origin[0], r_origin[1], &r_origin[0], &r_origin[1]);
168 * Custom handler for manipulator widgets
170 static void manipulator2d_modal(
171 bContext *C, wmManipulator *widget, const wmEvent *UNUSED(event), const int UNUSED(flag))
173 ARegion *ar = CTX_wm_region(C);
176 manipulator2d_calc_origin(C, origin);
177 manipulator2d_origin_to_region(ar, origin);
178 WM_manipulator_set_matrix_location(widget, origin);
180 ED_region_tag_redraw(ar);
183 void ED_widgetgroup_manipulator2d_setup(const bContext *UNUSED(C), wmManipulatorGroup *mgroup)
185 wmOperatorType *ot_translate = WM_operatortype_find("TRANSFORM_OT_translate", true);
186 ManipulatorGroup2D *man = manipulatorgroup2d_init(mgroup);
187 mgroup->customdata = man;
189 MAN2D_ITER_AXES_BEGIN(axis, axis_idx)
191 const float offset[3] = {0.0f, 0.2f};
193 float col[4], col_hi[4];
194 manipulator2d_get_axis_color(axis_idx, col, col_hi);
196 /* custom handler! */
197 WM_manipulator_set_fn_custom_modal(axis, manipulator2d_modal);
198 /* set up widget data */
199 RNA_float_set(axis->ptr, "angle", -M_PI_2 * axis_idx);
200 RNA_float_set(axis->ptr, "length", 0.8f);
201 WM_manipulator_set_matrix_offset_location(axis, offset);
202 WM_manipulator_set_line_width(axis, MANIPULATOR_AXIS_LINE_WIDTH);
203 WM_manipulator_set_scale(axis, U.manipulator_size);
204 WM_manipulator_set_color(axis, col);
205 WM_manipulator_set_color_highlight(axis, col_hi);
207 /* assign operator */
208 PointerRNA *ptr = WM_manipulator_set_operator(axis, ot_translate, NULL);
209 int constraint[3] = {0.0f};
210 constraint[(axis_idx + 1) % 2] = 1;
211 if (RNA_struct_find_property(ptr, "constraint_axis"))
212 RNA_boolean_set_array(ptr, "constraint_axis", constraint);
213 RNA_boolean_set(ptr, "release_confirm", 1);
218 void ED_widgetgroup_manipulator2d_refresh(const bContext *C, wmManipulatorGroup *mgroup)
220 ManipulatorGroup2D *man = mgroup->customdata;
223 manipulator2d_calc_origin(C, origin);
224 copy_v2_v2(man->origin, origin);
227 void ED_widgetgroup_manipulator2d_draw_prepare(const bContext *C, wmManipulatorGroup *mgroup)
229 ManipulatorGroup2D *man = mgroup->customdata;
230 float origin[3] = {UNPACK2(man->origin), 0.0f};
232 manipulator2d_origin_to_region(CTX_wm_region(C), origin);
234 MAN2D_ITER_AXES_BEGIN(axis, axis_idx)
236 WM_manipulator_set_matrix_location(axis, origin);
242 * - Called on every redraw, better to do a more simple poll and check for selection in _refresh
243 * - UV editing only, could be expanded for other things.
245 bool ED_widgetgroup_manipulator2d_poll(const bContext *C, wmManipulatorGroupType *UNUSED(wgt))
247 SpaceImage *sima = CTX_wm_space_image(C);
248 Object *obedit = CTX_data_edit_object(C);
250 if (ED_space_image_show_uvedit(sima, obedit)) {
251 Image *ima = ED_space_image(sima);
252 Scene *scene = CTX_data_scene(C);
253 BMEditMesh *em = BKE_editmesh_from_object(obedit);
258 const int cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV);
260 /* check if there's a selected poly */
261 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
262 if (!uvedit_face_visible_test(scene, ima, efa))
265 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
266 if (uvedit_uv_select_test(scene, l, cd_loop_uv_offset)) {