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