1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 */
16
17 /** \file
18 * \ingroup edmesh
19 *
20 * Creation gizmos.
21 */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "BLI_math.h"
26
27 #include "DNA_object_types.h"
28 #include "DNA_scene_types.h"
29
30 #include "BKE_context.h"
31 #include "BKE_editmesh.h"
32 #include "BKE_scene.h"
33
34 #include "ED_gizmo_library.h"
35 #include "ED_gizmo_utils.h"
36 #include "ED_mesh.h"
37 #include "ED_object.h"
38 #include "ED_screen.h"
39 #include "ED_undo.h"
40 #include "ED_view3d.h"
41
42 #include "RNA_access.h"
43 #include "RNA_define.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "UI_resources.h"
49
50 #include "BLT_translation.h"
51
52 #include "mesh_intern.h" /* own include */
53
54 /* -------------------------------------------------------------------- */
55 /** \name Helper Functions
56 * \{ */
57
58 /**
59 * When we place a shape, pick a plane.
60 *
61 * We may base this choice on context,
62 * for now pick the "ground" based on the 3D cursor's dominant plane
63 * pointing down relative to the view.
64 */
calc_initial_placement_point_from_view(bContext * C,const float mval[2],float r_location[3],float r_rotation[3][3])65 static void calc_initial_placement_point_from_view(bContext *C,
66 const float mval[2],
67 float r_location[3],
68 float r_rotation[3][3])
69 {
70
71 Scene *scene = CTX_data_scene(C);
72 ARegion *region = CTX_wm_region(C);
73 RegionView3D *rv3d = region->regiondata;
74
75 bool use_mouse_project = true; /* TODO: make optional */
76
77 float cursor_matrix[4][4];
78 float orient_matrix[3][3];
79 BKE_scene_cursor_to_mat4(&scene->cursor, cursor_matrix);
80
81 const float dots[3] = {
82 dot_v3v3(rv3d->viewinv[2], cursor_matrix[0]),
83 dot_v3v3(rv3d->viewinv[2], cursor_matrix[1]),
84 dot_v3v3(rv3d->viewinv[2], cursor_matrix[2]),
85 };
86 const int axis = axis_dominant_v3_single(dots);
87
88 copy_v3_v3(orient_matrix[0], cursor_matrix[(axis + 1) % 3]);
89 copy_v3_v3(orient_matrix[1], cursor_matrix[(axis + 2) % 3]);
90 copy_v3_v3(orient_matrix[2], cursor_matrix[axis]);
91
92 if (dot_v3v3(rv3d->viewinv[2], orient_matrix[2]) < 0.0f) {
93 negate_v3(orient_matrix[2]);
94 }
95 if (is_negative_m3(orient_matrix)) {
96 swap_v3_v3(orient_matrix[0], orient_matrix[1]);
97 }
98
99 if (use_mouse_project) {
100 float plane[4];
101 plane_from_point_normal_v3(plane, cursor_matrix[3], orient_matrix[2]);
102 if (ED_view3d_win_to_3d_on_plane(region, plane, mval, true, r_location)) {
103 copy_m3_m3(r_rotation, orient_matrix);
104 return;
105 }
106 }
107
108 /* fallback */
109 copy_v3_v3(r_location, cursor_matrix[3]);
110 copy_m3_m3(r_rotation, orient_matrix);
111 }
112
113 /** \} */
114
115 /* -------------------------------------------------------------------- */
116 /** \name Placement Gizmo
117 * \{ */
118
119 typedef struct GizmoPlacementGroup {
120 struct wmGizmo *cage;
121 struct {
122 bContext *context;
123 wmOperator *op;
124 PropertyRNA *prop_matrix;
125 } data;
126 } GizmoPlacementGroup;
127
128 /**
129 * \warning Calling redo from property updates is not great.
130 * This is needed because changing the RNA doesn't cause a redo
131 * and we're not using operator UI which does just this.
132 */
gizmo_placement_exec(GizmoPlacementGroup * ggd)133 static void gizmo_placement_exec(GizmoPlacementGroup *ggd)
134 {
135 wmOperator *op = ggd->data.op;
136 if (op == WM_operator_last_redo((bContext *)ggd->data.context)) {
137 ED_undo_operator_repeat((bContext *)ggd->data.context, op);
138 }
139 }
140
gizmo_mesh_placement_update_from_op(GizmoPlacementGroup * ggd)141 static void gizmo_mesh_placement_update_from_op(GizmoPlacementGroup *ggd)
142 {
143 wmOperator *op = ggd->data.op;
144 UNUSED_VARS(op);
145 /* For now don't read back from the operator. */
146 #if 0
147 RNA_property_float_get_array(op->ptr, ggd->data.prop_matrix, &ggd->cage->matrix_offset[0][0]);
148 #endif
149 }
150
151 /* translate callbacks */
gizmo_placement_prop_matrix_get(const wmGizmo * gz,wmGizmoProperty * gz_prop,void * value_p)152 static void gizmo_placement_prop_matrix_get(const wmGizmo *gz,
153 wmGizmoProperty *gz_prop,
154 void *value_p)
155 {
156 GizmoPlacementGroup *ggd = gz->parent_gzgroup->customdata;
157 wmOperator *op = ggd->data.op;
158 float *value = value_p;
159 BLI_assert(gz_prop->type->array_length == 16);
160 UNUSED_VARS_NDEBUG(gz_prop);
161
162 if (value_p != ggd->cage->matrix_offset) {
163 mul_m4_m4m4(value_p, ggd->cage->matrix_basis, ggd->cage->matrix_offset);
164 RNA_property_float_get_array(op->ptr, ggd->data.prop_matrix, value);
165 }
166 }
167
gizmo_placement_prop_matrix_set(const wmGizmo * gz,wmGizmoProperty * gz_prop,const void * value)168 static void gizmo_placement_prop_matrix_set(const wmGizmo *gz,
169 wmGizmoProperty *gz_prop,
170 const void *value)
171 {
172 GizmoPlacementGroup *ggd = gz->parent_gzgroup->customdata;
173 wmOperator *op = ggd->data.op;
174
175 BLI_assert(gz_prop->type->array_length == 16);
176 UNUSED_VARS_NDEBUG(gz_prop);
177
178 float mat[4][4];
179 mul_m4_m4m4(mat, ggd->cage->matrix_basis, value);
180
181 if (is_negative_m4(mat)) {
182 negate_mat3_m4(mat);
183 }
184
185 RNA_property_float_set_array(op->ptr, ggd->data.prop_matrix, &mat[0][0]);
186
187 gizmo_placement_exec(ggd);
188 }
189
gizmo_mesh_placement_poll(const bContext * C,wmGizmoGroupType * gzgt)190 static bool gizmo_mesh_placement_poll(const bContext *C, wmGizmoGroupType *gzgt)
191 {
192 return ED_gizmo_poll_or_unlink_delayed_from_operator(
193 C, gzgt, "MESH_OT_primitive_cube_add_gizmo");
194 }
195
gizmo_mesh_placement_modal_from_setup(const bContext * C,wmGizmoGroup * gzgroup)196 static void gizmo_mesh_placement_modal_from_setup(const bContext *C, wmGizmoGroup *gzgroup)
197 {
198 GizmoPlacementGroup *ggd = gzgroup->customdata;
199
200 /* Initial size. */
201 {
202 wmGizmo *gz = ggd->cage;
203 zero_m4(gz->matrix_offset);
204
205 /* TODO: support zero scaled matrix in 'GIZMO_GT_cage_3d'. */
206 gz->matrix_offset[0][0] = 0.01;
207 gz->matrix_offset[1][1] = 0.01;
208 gz->matrix_offset[2][2] = 0.01;
209 gz->matrix_offset[3][3] = 1.0f;
210 }
211
212 /* Start off dragging. */
213 {
214 wmWindow *win = CTX_wm_window(C);
215 ARegion *region = CTX_wm_region(C);
216 wmGizmo *gz = ggd->cage;
217
218 {
219 float mat3[3][3];
220 float location[3];
221 calc_initial_placement_point_from_view((bContext *)C,
222 (float[2]){
223 win->eventstate->x - region->winrct.xmin,
224 win->eventstate->y - region->winrct.ymin,
225 },
226 location,
227 mat3);
228 copy_m4_m3(gz->matrix_basis, mat3);
229 copy_v3_v3(gz->matrix_basis[3], location);
230 }
231
232 if (1) {
233 wmGizmoMap *gzmap = gzgroup->parent_gzmap;
234 WM_gizmo_modal_set_from_setup(gzmap,
235 (bContext *)C,
236 ggd->cage,
237 ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z,
238 win->eventstate);
239 }
240 }
241 }
242
gizmo_mesh_placement_setup(const bContext * C,wmGizmoGroup * gzgroup)243 static void gizmo_mesh_placement_setup(const bContext *C, wmGizmoGroup *gzgroup)
244 {
245 wmOperator *op = WM_operator_last_redo(C);
246
247 if (op == NULL || !STREQ(op->type->idname, "MESH_OT_primitive_cube_add_gizmo")) {
248 return;
249 }
250
251 struct GizmoPlacementGroup *ggd = MEM_callocN(sizeof(GizmoPlacementGroup), __func__);
252 gzgroup->customdata = ggd;
253
254 const wmGizmoType *gzt_cage = WM_gizmotype_find("GIZMO_GT_cage_3d", true);
255
256 ggd->cage = WM_gizmo_new_ptr(gzt_cage, gzgroup, NULL);
257
258 UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, ggd->cage->color);
259
260 RNA_enum_set(ggd->cage->ptr,
261 "transform",
262 ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE | ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
263 ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_SIGNED);
264
265 WM_gizmo_set_flag(ggd->cage, WM_GIZMO_DRAW_VALUE, true);
266
267 ggd->data.context = (bContext *)C;
268 ggd->data.op = op;
269 ggd->data.prop_matrix = RNA_struct_find_property(op->ptr, "matrix");
270
271 gizmo_mesh_placement_update_from_op(ggd);
272
273 /* Setup property callbacks */
274 {
275 WM_gizmo_target_property_def_func(ggd->cage,
276 "matrix",
277 &(const struct wmGizmoPropertyFnParams){
278 .value_get_fn = gizmo_placement_prop_matrix_get,
279 .value_set_fn = gizmo_placement_prop_matrix_set,
280 .range_get_fn = NULL,
281 .user_data = NULL,
282 });
283 }
284
285 gizmo_mesh_placement_modal_from_setup(C, gzgroup);
286 }
287
gizmo_mesh_placement_draw_prepare(const bContext * UNUSED (C),wmGizmoGroup * gzgroup)288 static void gizmo_mesh_placement_draw_prepare(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
289 {
290 GizmoPlacementGroup *ggd = gzgroup->customdata;
291 if (ggd->data.op->next) {
292 ggd->data.op = WM_operator_last_redo((bContext *)ggd->data.context);
293 }
294 gizmo_mesh_placement_update_from_op(ggd);
295 }
296
MESH_GGT_add_bounds(struct wmGizmoGroupType * gzgt)297 static void MESH_GGT_add_bounds(struct wmGizmoGroupType *gzgt)
298 {
299 gzgt->name = "Mesh Add Bounds";
300 gzgt->idname = "MESH_GGT_add_bounds";
301
302 gzgt->flag = WM_GIZMOGROUPTYPE_3D;
303
304 gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
305 gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
306
307 gzgt->poll = gizmo_mesh_placement_poll;
308 gzgt->setup = gizmo_mesh_placement_setup;
309 gzgt->draw_prepare = gizmo_mesh_placement_draw_prepare;
310 }
311
312 /** \} */
313
314 /* -------------------------------------------------------------------- */
315 /** \name Add Cube Gizmo-Operator
316 *
317 * For now we use a separate operator to add a cube,
318 * we can try to merge then however they are invoked differently
319 * and share the same BMesh creation code.
320 * \{ */
321
add_primitive_cube_gizmo_exec(bContext * C,wmOperator * op)322 static int add_primitive_cube_gizmo_exec(bContext *C, wmOperator *op)
323 {
324 Object *obedit = CTX_data_edit_object(C);
325 BMEditMesh *em = BKE_editmesh_from_object(obedit);
326 float matrix[4][4];
327
328 /* Get the matrix that defines the cube bounds (as set by the gizmo cage). */
329 {
330 PropertyRNA *prop_matrix = RNA_struct_find_property(op->ptr, "matrix");
331 if (RNA_property_is_set(op->ptr, prop_matrix)) {
332 RNA_property_float_get_array(op->ptr, prop_matrix, &matrix[0][0]);
333 invert_m4_m4(obedit->imat, obedit->obmat);
334 mul_m4_m4m4(matrix, obedit->imat, matrix);
335 }
336 else {
337 /* For the first update the widget may not set the matrix. */
338 return OPERATOR_FINISHED;
339 }
340 }
341
342 const bool calc_uvs = RNA_boolean_get(op->ptr, "calc_uvs");
343
344 if (calc_uvs) {
345 ED_mesh_uv_texture_ensure(obedit->data, NULL);
346 }
347
348 if (!EDBM_op_call_and_selectf(em,
349 op,
350 "verts.out",
351 false,
352 "create_cube matrix=%m4 size=%f calc_uvs=%b",
353 matrix,
354 1.0f,
355 calc_uvs)) {
356 return OPERATOR_CANCELLED;
357 }
358
359 EDBM_selectmode_flush_ex(em, SCE_SELECT_VERTEX);
360 EDBM_update_generic(obedit->data, true, true);
361
362 return OPERATOR_FINISHED;
363 }
364
add_primitive_cube_gizmo_invoke(bContext * C,wmOperator * op,const wmEvent * UNUSED (event))365 static int add_primitive_cube_gizmo_invoke(bContext *C,
366 wmOperator *op,
367 const wmEvent *UNUSED(event))
368 {
369 View3D *v3d = CTX_wm_view3d(C);
370
371 int ret = add_primitive_cube_gizmo_exec(C, op);
372 if (ret & OPERATOR_FINISHED) {
373 /* Setup gizmos */
374 if (v3d && ((v3d->gizmo_flag & V3D_GIZMO_HIDE) == 0)) {
375 wmGizmoGroupType *gzgt = WM_gizmogrouptype_find("MESH_GGT_add_bounds", false);
376 if (!WM_gizmo_group_type_ensure_ptr(gzgt)) {
377 struct Main *bmain = CTX_data_main(C);
378 WM_gizmo_group_type_reinit_ptr(bmain, gzgt);
379 }
380 }
381 }
382
383 return ret;
384 }
385
MESH_OT_primitive_cube_add_gizmo(wmOperatorType * ot)386 void MESH_OT_primitive_cube_add_gizmo(wmOperatorType *ot)
387 {
388 /* identifiers */
389 ot->name = "Add Cube";
390 ot->description = "Construct a cube mesh";
391 ot->idname = "MESH_OT_primitive_cube_add_gizmo";
392
393 /* api callbacks */
394 ot->invoke = add_primitive_cube_gizmo_invoke;
395 ot->exec = add_primitive_cube_gizmo_exec;
396 ot->poll = ED_operator_editmesh_view3d;
397
398 /* flags */
399 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
400
401 ED_object_add_mesh_props(ot);
402 ED_object_add_generic_props(ot, true);
403
404 /* hidden props */
405 PropertyRNA *prop = RNA_def_float_matrix(
406 ot->srna, "matrix", 4, 4, NULL, 0.0f, 0.0f, "Matrix", "", 0.0f, 0.0f);
407 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
408
409 WM_gizmogrouptype_append(MESH_GGT_add_bounds);
410 }
411
412 /** \} */
413