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