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 * The Original Code is Copyright (C) 2008 Blender Foundation.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup edutil
22 */
23
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_armature_types.h"
31 #include "DNA_mesh_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_packedFile_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_path_util.h"
40 #include "BLI_string.h"
41 #include "BLI_utildefines.h"
42
43 #include "BLT_translation.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_layer.h"
48 #include "BKE_main.h"
49 #include "BKE_material.h"
50 #include "BKE_multires.h"
51 #include "BKE_object.h"
52 #include "BKE_packedFile.h"
53 #include "BKE_paint.h"
54 #include "BKE_screen.h"
55 #include "BKE_undo_system.h"
56 #include "BKE_workspace.h"
57
58 #include "DEG_depsgraph.h"
59
60 #include "ED_armature.h"
61 #include "ED_image.h"
62 #include "ED_mesh.h"
63 #include "ED_node.h"
64 #include "ED_object.h"
65 #include "ED_outliner.h"
66 #include "ED_paint.h"
67 #include "ED_space_api.h"
68 #include "ED_util.h"
69
70 #include "GPU_immediate.h"
71 #include "GPU_state.h"
72
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75
76 #include "RNA_access.h"
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 /* ********* general editor util funcs, not BKE stuff please! ********* */
81
ED_editors_init_for_undo(Main * bmain)82 void ED_editors_init_for_undo(Main *bmain)
83 {
84 wmWindowManager *wm = bmain->wm.first;
85 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
86 ViewLayer *view_layer = WM_window_get_active_view_layer(win);
87 Base *base = BASACT(view_layer);
88 if (base != NULL) {
89 Object *ob = base->object;
90 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
91 Scene *scene = WM_window_get_active_scene(win);
92
93 BKE_texpaint_slots_refresh_object(scene, ob);
94 ED_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
95 }
96 }
97 }
98 }
99
ED_editors_init(bContext * C)100 void ED_editors_init(bContext *C)
101 {
102 struct Depsgraph *depsgraph = CTX_data_expect_evaluated_depsgraph(C);
103 Main *bmain = CTX_data_main(C);
104 Scene *scene = CTX_data_scene(C);
105 wmWindowManager *wm = CTX_wm_manager(C);
106
107 /* This is called during initialization, so we don't want to store any reports */
108 ReportList *reports = CTX_wm_reports(C);
109 int reports_flag_prev = reports->flag & ~RPT_STORE;
110
111 SWAP(int, reports->flag, reports_flag_prev);
112
113 /* Don't do undo pushes when calling an operator. */
114 wm->op_undo_depth++;
115
116 /* toggle on modes for objects that were saved with these enabled. for
117 * e.g. linked objects we have to ensure that they are actually the
118 * active object in this scene. */
119 Object *obact = CTX_data_active_object(C);
120 for (Object *ob = bmain->objects.first; ob; ob = ob->id.next) {
121 int mode = ob->mode;
122 if (mode == OB_MODE_OBJECT) {
123 continue;
124 }
125 if (BKE_object_has_mode_data(ob, mode)) {
126 continue;
127 }
128 if (ob->type == OB_GPENCIL) {
129 /* For multi-edit mode we may already have mode data (grease pencil does not need it).
130 * However we may have a non-active object stuck in a grease-pencil edit mode. */
131 if (ob != obact) {
132 ob->mode = OB_MODE_OBJECT;
133 DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
134 }
135 continue;
136 }
137
138 ID *ob_data = ob->data;
139 ob->mode = OB_MODE_OBJECT;
140 DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
141 if (obact && (ob->type == obact->type) && !ID_IS_LINKED(ob) &&
142 !(ob_data && ID_IS_LINKED(ob_data))) {
143 if (mode == OB_MODE_EDIT) {
144 ED_object_editmode_enter_ex(bmain, scene, ob, 0);
145 }
146 else if (mode == OB_MODE_POSE) {
147 ED_object_posemode_enter_ex(bmain, ob);
148 }
149 else if (mode & OB_MODE_ALL_SCULPT) {
150 if (obact == ob) {
151 if (mode == OB_MODE_SCULPT) {
152 ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, true, reports);
153 }
154 else if (mode == OB_MODE_VERTEX_PAINT) {
155 ED_object_vpaintmode_enter_ex(bmain, depsgraph, scene, ob);
156 }
157 else if (mode == OB_MODE_WEIGHT_PAINT) {
158 ED_object_wpaintmode_enter_ex(bmain, depsgraph, scene, ob);
159 }
160 else {
161 BLI_assert(0);
162 }
163 }
164 else {
165 /* Create data for non-active objects which need it for
166 * mode-switching but don't yet support multi-editing. */
167 if (mode & OB_MODE_ALL_SCULPT) {
168 ob->mode = mode;
169 BKE_object_sculpt_data_create(ob);
170 }
171 }
172 }
173 else {
174 /* TODO(campbell): avoid operator calls. */
175 if (obact == ob) {
176 ED_object_mode_set(C, mode);
177 }
178 }
179 }
180 }
181
182 /* image editor paint mode */
183 if (scene) {
184 ED_space_image_paint_update(bmain, wm, scene);
185 }
186
187 SWAP(int, reports->flag, reports_flag_prev);
188 wm->op_undo_depth--;
189 }
190
191 /* frees all editmode stuff */
ED_editors_exit(Main * bmain,bool do_undo_system)192 void ED_editors_exit(Main *bmain, bool do_undo_system)
193 {
194 if (!bmain) {
195 return;
196 }
197
198 /* frees all editmode undos */
199 if (do_undo_system && G_MAIN->wm.first) {
200 wmWindowManager *wm = G_MAIN->wm.first;
201 /* normally we don't check for NULL undo stack,
202 * do here since it may run in different context. */
203 if (wm->undo_stack) {
204 BKE_undosys_stack_destroy(wm->undo_stack);
205 wm->undo_stack = NULL;
206 }
207 }
208
209 for (Object *ob = bmain->objects.first; ob; ob = ob->id.next) {
210 if (ob->type == OB_MESH) {
211 Mesh *me = ob->data;
212 if (me->edit_mesh) {
213 EDBM_mesh_free(me->edit_mesh);
214 MEM_freeN(me->edit_mesh);
215 me->edit_mesh = NULL;
216 }
217 }
218 else if (ob->type == OB_ARMATURE) {
219 bArmature *arm = ob->data;
220 if (arm->edbo) {
221 ED_armature_edit_free(ob->data);
222 }
223 }
224 }
225
226 /* global in meshtools... */
227 ED_mesh_mirror_spatial_table_end(NULL);
228 ED_mesh_mirror_topo_table_end(NULL);
229 }
230
ED_editors_flush_edits_for_object_ex(Main * bmain,Object * ob,bool for_render,bool check_needs_flush)231 bool ED_editors_flush_edits_for_object_ex(Main *bmain,
232 Object *ob,
233 bool for_render,
234 bool check_needs_flush)
235 {
236 bool has_edited = false;
237 if (ob->mode & OB_MODE_SCULPT) {
238 /* Don't allow flushing while in the middle of a stroke (frees data in use).
239 * Auto-save prevents this from happening but scripts
240 * may cause a flush on saving: T53986. */
241 if (ob->sculpt != NULL && ob->sculpt->cache == NULL) {
242 char *needs_flush_ptr = &ob->sculpt->needs_flush_to_id;
243 if (check_needs_flush && (*needs_flush_ptr == 0)) {
244 return false;
245 }
246 *needs_flush_ptr = 0;
247
248 /* flush multires changes (for sculpt) */
249 multires_flush_sculpt_updates(ob);
250 has_edited = true;
251
252 if (for_render) {
253 /* flush changes from dynamic topology sculpt */
254 BKE_sculptsession_bm_to_me_for_render(ob);
255 }
256 else {
257 /* Set reorder=false so that saving the file doesn't reorder
258 * the BMesh's elements */
259 BKE_sculptsession_bm_to_me(ob, false);
260 }
261 }
262 }
263 else if (ob->mode & OB_MODE_EDIT) {
264
265 char *needs_flush_ptr = BKE_object_data_editmode_flush_ptr_get(ob->data);
266 if (needs_flush_ptr != NULL) {
267 if (check_needs_flush && (*needs_flush_ptr == 0)) {
268 return false;
269 }
270 *needs_flush_ptr = 0;
271 }
272
273 /* get editmode results */
274 has_edited = true;
275 ED_object_editmode_load(bmain, ob);
276 }
277 return has_edited;
278 }
279
ED_editors_flush_edits_for_object(Main * bmain,Object * ob)280 bool ED_editors_flush_edits_for_object(Main *bmain, Object *ob)
281 {
282 return ED_editors_flush_edits_for_object_ex(bmain, ob, false, false);
283 }
284
285 /* flush any temp data from object editing to DNA before writing files,
286 * rendering, copying, etc. */
ED_editors_flush_edits_ex(Main * bmain,bool for_render,bool check_needs_flush)287 bool ED_editors_flush_edits_ex(Main *bmain, bool for_render, bool check_needs_flush)
288 {
289 bool has_edited = false;
290 Object *ob;
291
292 /* loop through all data to find edit mode or object mode, because during
293 * exiting we might not have a context for edit object and multiple sculpt
294 * objects can exist at the same time */
295 for (ob = bmain->objects.first; ob; ob = ob->id.next) {
296 has_edited |= ED_editors_flush_edits_for_object_ex(bmain, ob, for_render, check_needs_flush);
297 }
298
299 bmain->is_memfile_undo_flush_needed = false;
300
301 return has_edited;
302 }
303
ED_editors_flush_edits(Main * bmain)304 bool ED_editors_flush_edits(Main *bmain)
305 {
306 return ED_editors_flush_edits_ex(bmain, false, false);
307 }
308
309 /* ***** XXX: functions are using old blender names, cleanup later ***** */
310
311 /* now only used in 2d spaces, like time, ipo, nla, sima... */
312 /* XXX shift/ctrl not configurable */
apply_keyb_grid(int shift,int ctrl,float * val,float fac1,float fac2,float fac3,int invert)313 void apply_keyb_grid(
314 int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
315 {
316 /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
317 if (invert) {
318 ctrl = !ctrl;
319 }
320
321 if (ctrl && shift) {
322 if (fac3 != 0.0f) {
323 *val = fac3 * floorf(*val / fac3 + 0.5f);
324 }
325 }
326 else if (ctrl) {
327 if (fac2 != 0.0f) {
328 *val = fac2 * floorf(*val / fac2 + 0.5f);
329 }
330 }
331 else {
332 if (fac1 != 0.0f) {
333 *val = fac1 * floorf(*val / fac1 + 0.5f);
334 }
335 }
336 }
337
unpack_menu(bContext * C,const char * opname,const char * id_name,const char * abs_name,const char * folder,struct PackedFile * pf)338 void unpack_menu(bContext *C,
339 const char *opname,
340 const char *id_name,
341 const char *abs_name,
342 const char *folder,
343 struct PackedFile *pf)
344 {
345 Main *bmain = CTX_data_main(C);
346 PointerRNA props_ptr;
347 uiPopupMenu *pup;
348 uiLayout *layout;
349 char line[FILE_MAX + 100];
350 wmOperatorType *ot = WM_operatortype_find(opname, 1);
351
352 pup = UI_popup_menu_begin(C, IFACE_("Unpack File"), ICON_NONE);
353 layout = UI_popup_menu_layout(pup);
354
355 uiItemFullO_ptr(
356 layout, ot, IFACE_("Remove Pack"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
357 RNA_enum_set(&props_ptr, "method", PF_REMOVE);
358 RNA_string_set(&props_ptr, "id", id_name);
359
360 if (G.relbase_valid) {
361 char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
362
363 BLI_split_file_part(abs_name, fi, sizeof(fi));
364 BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
365 if (!STREQ(abs_name, local_name)) {
366 switch (BKE_packedfile_compare_to_file(BKE_main_blendfile_path(bmain), local_name, pf)) {
367 case PF_CMP_NOFILE:
368 BLI_snprintf(line, sizeof(line), TIP_("Create %s"), local_name);
369 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
370 RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
371 RNA_string_set(&props_ptr, "id", id_name);
372
373 break;
374 case PF_CMP_EQUAL:
375 BLI_snprintf(line, sizeof(line), TIP_("Use %s (identical)"), local_name);
376 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
377 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
378 RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
379 RNA_string_set(&props_ptr, "id", id_name);
380
381 break;
382 case PF_CMP_DIFFERS:
383 BLI_snprintf(line, sizeof(line), TIP_("Use %s (differs)"), local_name);
384 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
385 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
386 RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
387 RNA_string_set(&props_ptr, "id", id_name);
388
389 BLI_snprintf(line, sizeof(line), TIP_("Overwrite %s"), local_name);
390 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_LOCAL);
391 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
392 RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
393 RNA_string_set(&props_ptr, "id", id_name);
394 break;
395 }
396 }
397 }
398
399 switch (BKE_packedfile_compare_to_file(BKE_main_blendfile_path(bmain), abs_name, pf)) {
400 case PF_CMP_NOFILE:
401 BLI_snprintf(line, sizeof(line), TIP_("Create %s"), abs_name);
402 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
403 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
404 RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
405 RNA_string_set(&props_ptr, "id", id_name);
406 break;
407 case PF_CMP_EQUAL:
408 BLI_snprintf(line, sizeof(line), TIP_("Use %s (identical)"), abs_name);
409 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
410 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
411 RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
412 RNA_string_set(&props_ptr, "id", id_name);
413 break;
414 case PF_CMP_DIFFERS:
415 BLI_snprintf(line, sizeof(line), TIP_("Use %s (differs)"), abs_name);
416 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
417 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
418 RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
419 RNA_string_set(&props_ptr, "id", id_name);
420
421 BLI_snprintf(line, sizeof(line), TIP_("Overwrite %s"), abs_name);
422 // uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
423 uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
424 RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
425 RNA_string_set(&props_ptr, "id", id_name);
426 break;
427 }
428
429 UI_popup_menu_end(C, pup);
430 }
431
432 /* ********************* generic callbacks for drawcall api *********************** */
433
434 /**
435 * Callback that draws a line between the mouse and a position given as the initial argument.
436 */
ED_region_draw_mouse_line_cb(const bContext * C,ARegion * region,void * arg_info)437 void ED_region_draw_mouse_line_cb(const bContext *C, ARegion *region, void *arg_info)
438 {
439 wmWindow *win = CTX_wm_window(C);
440 const float *mval_src = (float *)arg_info;
441 const float mval_dst[2] = {
442 win->eventstate->x - region->winrct.xmin,
443 win->eventstate->y - region->winrct.ymin,
444 };
445
446 const uint shdr_pos = GPU_vertformat_attr_add(
447 immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
448
449 GPU_line_width(1.0f);
450
451 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
452
453 float viewport_size[4];
454 GPU_viewport_size_get_f(viewport_size);
455 immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
456
457 immUniform1i("colors_len", 0); /* "simple" mode */
458 immUniformThemeColor3(TH_VIEW_OVERLAY);
459 immUniform1f("dash_width", 6.0f);
460 immUniform1f("dash_factor", 0.5f);
461
462 immBegin(GPU_PRIM_LINES, 2);
463 immVertex2fv(shdr_pos, mval_src);
464 immVertex2fv(shdr_pos, mval_dst);
465 immEnd();
466
467 immUnbindProgram();
468 }
469
470 /**
471 * Use to free ID references within runtime data (stored outside of DNA)
472 *
473 * \param new_id: may be NULL to unlink \a old_id.
474 */
ED_spacedata_id_remap(struct ScrArea * area,struct SpaceLink * sl,ID * old_id,ID * new_id)475 void ED_spacedata_id_remap(struct ScrArea *area, struct SpaceLink *sl, ID *old_id, ID *new_id)
476 {
477 SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
478
479 if (st && st->id_remap) {
480 st->id_remap(area, sl, old_id, new_id);
481 }
482 }
483
ed_flush_edits_exec(bContext * C,wmOperator * UNUSED (op))484 static int ed_flush_edits_exec(bContext *C, wmOperator *UNUSED(op))
485 {
486 Main *bmain = CTX_data_main(C);
487 ED_editors_flush_edits(bmain);
488 return OPERATOR_FINISHED;
489 }
490
ED_OT_flush_edits(wmOperatorType * ot)491 void ED_OT_flush_edits(wmOperatorType *ot)
492 {
493 /* identifiers */
494 ot->name = "Flush Edits";
495 ot->description = "Flush edit data from active editing modes";
496 ot->idname = "ED_OT_flush_edits";
497
498 /* api callbacks */
499 ot->exec = ed_flush_edits_exec;
500
501 /* flags */
502 ot->flag = OPTYPE_INTERNAL;
503 }
504