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