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