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 #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_main.h"
57 #include "BKE_multires.h"
58 #include "BKE_object.h"
59 #include "BKE_packedFile.h"
60 #include "BKE_paint.h"
61 #include "BKE_screen.h"
62 #include "BKE_workspace.h"
63 #include "BKE_layer.h"
64 #include "BKE_undo_system.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         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                                  * (grease pencil does not need it)
120                                  */
121                                 if (ob->type != OB_GPENCIL) {
122                                         ID *data = ob->data;
123                                         ob->mode = OB_MODE_OBJECT;
124                                         if ((ob == obact) && !ID_IS_LINKED(ob) && !(data && ID_IS_LINKED(data))) {
125                                                 ED_object_mode_toggle(C, mode);
126                                         }
127                                 }
128                         }
129                 }
130         }
131
132         /* image editor paint mode */
133         {
134                 Scene *sce = CTX_data_scene(C);
135                 if (sce) {
136                         ED_space_image_paint_update(bmain, wm, sce);
137                 }
138         }
139
140         SWAP(int, reports->flag, reports_flag_prev);
141 }
142
143 /* frees all editmode stuff */
144 void ED_editors_exit(bContext *C)
145 {
146         Main *bmain = CTX_data_main(C);
147
148         if (!bmain)
149                 return;
150
151         /* frees all editmode undos */
152         if (G_MAIN->wm.first) {
153                 wmWindowManager *wm = G_MAIN->wm.first;
154                 /* normally we don't check for NULL undo stack, do here since it may run in different context. */
155                 if (wm->undo_stack) {
156                         BKE_undosys_stack_destroy(wm->undo_stack);
157                         wm->undo_stack = NULL;
158                 }
159         }
160
161         for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
162                 if (ob->type == OB_MESH) {
163                         Mesh *me = ob->data;
164                         if (me->edit_btmesh) {
165                                 EDBM_mesh_free(me->edit_btmesh);
166                                 MEM_freeN(me->edit_btmesh);
167                                 me->edit_btmesh = NULL;
168                         }
169                 }
170                 else if (ob->type == OB_ARMATURE) {
171                         bArmature *arm = ob->data;
172                         if (arm->edbo) {
173                                 ED_armature_edit_free(ob->data);
174                         }
175                 }
176         }
177
178         /* global in meshtools... */
179         ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e');
180         ED_mesh_mirror_topo_table(NULL, NULL, 'e');
181 }
182
183 /* flush any temp data from object editing to DNA before writing files,
184  * rendering, copying, etc. */
185 bool ED_editors_flush_edits(const bContext *C, bool for_render)
186 {
187         bool has_edited = false;
188         Object *ob;
189         Main *bmain = CTX_data_main(C);
190
191         /* loop through all data to find edit mode or object mode, because during
192          * exiting we might not have a context for edit object and multiple sculpt
193          * objects can exist at the same time */
194         for (ob = bmain->object.first; ob; ob = ob->id.next) {
195                 if (ob->mode & OB_MODE_SCULPT) {
196                         /* Don't allow flushing while in the middle of a stroke (frees data in use).
197                          * Auto-save prevents this from happening but scripts may cause a flush on saving: T53986. */
198                         if ((ob->sculpt && ob->sculpt->cache) == 0) {
199                                 /* flush multires changes (for sculpt) */
200                                 multires_force_update(ob);
201                                 has_edited = true;
202
203                                 if (for_render) {
204                                         /* flush changes from dynamic topology sculpt */
205                                         BKE_sculptsession_bm_to_me_for_render(ob);
206                                 }
207                                 else {
208                                         /* Set reorder=false so that saving the file doesn't reorder
209                                          * the BMesh's elements */
210                                         BKE_sculptsession_bm_to_me(ob, false);
211                                 }
212                         }
213                 }
214                 else if (ob->mode & OB_MODE_EDIT) {
215                         /* get editmode results */
216                         has_edited = true;
217                         ED_object_editmode_load(bmain, ob);
218                 }
219         }
220
221         return has_edited;
222 }
223
224 /* ***** XXX: functions are using old blender names, cleanup later ***** */
225
226
227 /* now only used in 2d spaces, like time, ipo, nla, sima... */
228 /* XXX shift/ctrl not configurable */
229 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
230 {
231         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
232         if (invert)
233                 ctrl = !ctrl;
234
235         if (ctrl && shift) {
236                 if (fac3 != 0.0f) *val = fac3 * floorf(*val / fac3 + 0.5f);
237         }
238         else if (ctrl) {
239                 if (fac2 != 0.0f) *val = fac2 * floorf(*val / fac2 + 0.5f);
240         }
241         else {
242                 if (fac1 != 0.0f) *val = fac1 * floorf(*val / fac1 + 0.5f);
243         }
244 }
245
246 void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf)
247 {
248         Main *bmain = CTX_data_main(C);
249         PointerRNA props_ptr;
250         uiPopupMenu *pup;
251         uiLayout *layout;
252         char line[FILE_MAX + 100];
253         wmOperatorType *ot = WM_operatortype_find(opname, 1);
254
255         pup = UI_popup_menu_begin(C, IFACE_("Unpack File"), ICON_NONE);
256         layout = UI_popup_menu_layout(pup);
257
258         uiItemFullO_ptr(
259                 layout, ot, IFACE_("Remove Pack"), ICON_NONE,
260                 NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
261         RNA_enum_set(&props_ptr, "method", PF_REMOVE);
262         RNA_string_set(&props_ptr, "id", id_name);
263
264         if (G.relbase_valid) {
265                 char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
266
267                 BLI_split_file_part(abs_name, fi, sizeof(fi));
268                 BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
269                 if (!STREQ(abs_name, local_name)) {
270                         switch (checkPackedFile(BKE_main_blendfile_path(bmain), local_name, pf)) {
271                                 case PF_NOFILE:
272                                         BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), local_name);
273                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
274                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
275                                         RNA_string_set(&props_ptr, "id", id_name);
276
277                                         break;
278                                 case PF_EQUAL:
279                                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (identical)"), local_name);
280                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
281                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
282                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
283                                         RNA_string_set(&props_ptr, "id", id_name);
284
285                                         break;
286                                 case PF_DIFFERS:
287                                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (differs)"), local_name);
288                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
289                                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
290                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
291                                         RNA_string_set(&props_ptr, "id", id_name);
292
293                                         BLI_snprintf(line, sizeof(line), IFACE_("Overwrite %s"), local_name);
294                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_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_WRITE_LOCAL);
297                                         RNA_string_set(&props_ptr, "id", id_name);
298                                         break;
299                         }
300                 }
301         }
302
303         switch (checkPackedFile(BKE_main_blendfile_path(bmain), abs_name, pf)) {
304                 case PF_NOFILE:
305                         BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), abs_name);
306                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
307                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
308                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
309                         RNA_string_set(&props_ptr, "id", id_name);
310                         break;
311                 case PF_EQUAL:
312                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (identical)"), abs_name);
313                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
314                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
315                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
316                         RNA_string_set(&props_ptr, "id", id_name);
317                         break;
318                 case PF_DIFFERS:
319                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (differs)"), abs_name);
320                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
321                         uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
322                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
323                         RNA_string_set(&props_ptr, "id", id_name);
324
325                         BLI_snprintf(line, sizeof(line), IFACE_("Overwrite %s"), abs_name);
326                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_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_WRITE_ORIGINAL);
329                         RNA_string_set(&props_ptr, "id", id_name);
330                         break;
331         }
332
333         UI_popup_menu_end(C, pup);
334 }
335
336 /* ********************* generic callbacks for drawcall api *********************** */
337
338 /**
339  * Callback that draws a line between the mouse and a position given as the initial argument.
340  */
341 void ED_region_draw_mouse_line_cb(const bContext *C, ARegion *ar, void *arg_info)
342 {
343         wmWindow *win = CTX_wm_window(C);
344         const float *mval_src = (float *)arg_info;
345         const float mval_dst[2] = {win->eventstate->x - ar->winrct.xmin,
346                                    win->eventstate->y - ar->winrct.ymin};
347
348         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
349
350         GPU_line_width(1.0f * U.pixelsize);
351
352         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
353
354         float viewport_size[4];
355         GPU_viewport_size_get_f(viewport_size);
356         immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
357
358         immUniform1i("colors_len", 0);  /* "simple" mode */
359         immUniformThemeColor(TH_VIEW_OVERLAY);
360         immUniform1f("dash_width", 6.0f);
361         immUniform1f("dash_factor", 0.5f);
362
363         immBegin(GPU_PRIM_LINES, 2);
364         immVertex2fv(shdr_pos, mval_src);
365         immVertex2fv(shdr_pos, mval_dst);
366         immEnd();
367
368         immUnbindProgram();
369 }
370
371 /**
372  * Use to free ID references within runtime data (stored outside of DNA)
373  *
374  * \param new_id may be NULL to unlink \a old_id.
375  */
376 void ED_spacedata_id_remap(struct ScrArea *sa, struct SpaceLink *sl, ID *old_id, ID *new_id)
377 {
378         SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
379
380         if (st && st->id_remap) {
381                 st->id_remap(sa, sl, old_id, new_id);
382         }
383 }
384
385 static int ed_flush_edits_exec(bContext *C, wmOperator *UNUSED(op))
386 {
387         ED_editors_flush_edits(C, false);
388         return OPERATOR_FINISHED;
389 }
390
391 void ED_OT_flush_edits(wmOperatorType *ot)
392 {
393         /* identifiers */
394         ot->name = "Flush Edits";
395         ot->description = "Flush edit data from active editing modes";
396         ot->idname = "ED_OT_flush_edits";
397
398         /* api callbacks */
399         ot->exec = ed_flush_edits_exec;
400
401         /* flags */
402         ot->flag = OPTYPE_INTERNAL;
403 }