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         SceneLayer *sl = CTX_data_scene_layer(C);
92         Object *ob, *obact = (sl && sl->basact) ? sl->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                         /* flush multires changes (for sculpt) */
179                         multires_force_update(ob);
180                         has_edited = true;
181
182                         if (for_render) {
183                                 /* flush changes from dynamic topology sculpt */
184                                 BKE_sculptsession_bm_to_me_for_render(ob);
185                         }
186                         else {
187                                 /* Set reorder=false so that saving the file doesn't reorder
188                                  * the BMesh's elements */
189                                 BKE_sculptsession_bm_to_me(ob, false);
190                         }
191                 }
192                 else if (ob->mode & OB_MODE_EDIT) {
193                         /* get editmode results */
194                         has_edited = true;
195                         ED_object_editmode_load(ob);
196                 }
197         }
198
199         return has_edited;
200 }
201
202 /* ***** XXX: functions are using old blender names, cleanup later ***** */
203
204
205 /* now only used in 2d spaces, like time, ipo, nla, sima... */
206 /* XXX shift/ctrl not configurable */
207 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
208 {
209         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
210         if (invert)
211                 ctrl = !ctrl;
212         
213         if (ctrl && shift) {
214                 if (fac3 != 0.0f) *val = fac3 * floorf(*val / fac3 + 0.5f);
215         }
216         else if (ctrl) {
217                 if (fac2 != 0.0f) *val = fac2 * floorf(*val / fac2 + 0.5f);
218         }
219         else {
220                 if (fac1 != 0.0f) *val = fac1 * floorf(*val / fac1 + 0.5f);
221         }
222 }
223
224 void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf)
225 {
226         PointerRNA props_ptr;
227         uiPopupMenu *pup;
228         uiLayout *layout;
229         char line[FILE_MAX + 100];
230         wmOperatorType *ot = WM_operatortype_find(opname, 1);
231
232         pup = UI_popup_menu_begin(C, IFACE_("Unpack File"), ICON_NONE);
233         layout = UI_popup_menu_layout(pup);
234
235         uiItemFullO_ptr(
236                 layout, ot, IFACE_("Remove Pack"), ICON_NONE,
237                 NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
238         RNA_enum_set(&props_ptr, "method", PF_REMOVE);
239         RNA_string_set(&props_ptr, "id", id_name);
240
241         if (G.relbase_valid) {
242                 char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
243
244                 BLI_split_file_part(abs_name, fi, sizeof(fi));
245                 BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
246                 if (!STREQ(abs_name, local_name)) {
247                         switch (checkPackedFile(local_name, pf)) {
248                                 case PF_NOFILE:
249                                         BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), local_name);
250                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
251                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
252                                         RNA_string_set(&props_ptr, "id", id_name);
253
254                                         break;
255                                 case PF_EQUAL:
256                                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (identical)"), local_name);
257                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
258                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
259                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
260                                         RNA_string_set(&props_ptr, "id", id_name);
261
262                                         break;
263                                 case PF_DIFFERS:
264                                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (differs)"), local_name);
265                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
266                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
267                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
268                                         RNA_string_set(&props_ptr, "id", id_name);
269
270                                         BLI_snprintf(line, sizeof(line), IFACE_("Overwrite %s"), local_name);
271                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_LOCAL);
272                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
273                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
274                                         RNA_string_set(&props_ptr, "id", id_name);
275                                         break;
276                         }
277                 }
278         }
279
280         switch (checkPackedFile(abs_name, pf)) {
281                 case PF_NOFILE:
282                         BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), abs_name);
283                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
284                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
285                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
286                         RNA_string_set(&props_ptr, "id", id_name);
287                         break;
288                 case PF_EQUAL:
289                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (identical)"), abs_name);
290                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
291                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
292                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
293                         RNA_string_set(&props_ptr, "id", id_name);
294                         break;
295                 case PF_DIFFERS:
296                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (differs)"), abs_name);
297                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
298                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
299                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
300                         RNA_string_set(&props_ptr, "id", id_name);
301
302                         BLI_snprintf(line, sizeof(line), IFACE_("Overwrite %s"), abs_name);
303                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
304                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
305                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
306                         RNA_string_set(&props_ptr, "id", id_name);
307                         break;
308         }
309
310         UI_popup_menu_end(C, pup);
311 }
312
313 /* ********************* generic callbacks for drawcall api *********************** */
314
315 /**
316  * Callback that draws a line between the mouse and a position given as the initial argument.
317  */
318 void ED_region_draw_mouse_line_cb(const bContext *C, ARegion *ar, void *arg_info)
319 {
320         wmWindow *win = CTX_wm_window(C);
321         const float *mval_src = (float *)arg_info;
322         const float mval_dst[2] = {win->eventstate->x - ar->winrct.xmin,
323                                    win->eventstate->y - ar->winrct.ymin};
324
325         const uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
326
327         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
328
329         float viewport_size[4];
330         glGetFloatv(GL_VIEWPORT, viewport_size);
331         immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
332
333         immUniform1i("num_colors", 0);  /* "simple" mode */
334         immUniformThemeColor(TH_VIEW_OVERLAY);
335         immUniform1f("dash_width", 6.0f);
336         immUniform1f("dash_factor", 0.5f);
337
338         immBegin(GWN_PRIM_LINES, 2);
339         immVertex2fv(shdr_pos, mval_src);
340         immVertex2fv(shdr_pos, mval_dst);
341         immEnd();
342
343         immUnbindProgram();
344 }
345
346 /**
347  * Use to free ID references within runtime data (stored outside of DNA)
348  *
349  * \param new_id may be NULL to unlink \a old_id.
350  */
351 void ED_spacedata_id_remap(struct ScrArea *sa, struct SpaceLink *sl, ID *old_id, ID *new_id)
352 {
353         SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
354
355         if (st && st->id_remap) {
356                 st->id_remap(sa, sl, old_id, new_id);
357         }
358 }
359
360 static int ed_flush_edits_exec(bContext *C, wmOperator *UNUSED(op))
361 {
362         ED_editors_flush_edits(C, false);
363         return OPERATOR_FINISHED;
364 }
365
366 void ED_OT_flush_edits(wmOperatorType *ot)
367 {
368         /* identifiers */
369         ot->name = "Flush Edits";
370         ot->description = "Flush edit data from active editing modes";
371         ot->idname = "ED_OT_flush_edits";
372
373         /* api callbacks */
374         ot->exec = ed_flush_edits_exec;
375
376         /* flags */
377         ot->flag = OPTYPE_INTERNAL;
378 }