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