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