merged 30707:31141 from trunk
[blender-staging.git] / source / blender / editors / util / ed_util.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_editVert.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43
44 #include "ED_armature.h"
45 #include "ED_mesh.h"
46 #include "ED_object.h"
47 #include "ED_sculpt.h"
48 #include "ED_util.h"
49
50 #include "UI_interface.h"
51
52 /* ********* general editor util funcs, not BKE stuff please! ********* */
53
54 void ED_editors_init(bContext *C)
55 {
56         Main *bmain= CTX_data_main(C);
57         Scene *sce= CTX_data_scene(C);
58         Object *ob, *obact= (sce && sce->basact)? sce->basact->object: NULL;
59         ID *data;
60
61         /* toggle on modes for objects that were saved with these enabled. for
62            e.g. linked objects we have to ensure that they are actually the
63            active object in this scene. */
64         for(ob=bmain->object.first; ob; ob=ob->id.next) {
65                 int mode= ob->mode;
66
67                 if(mode && (mode != OB_MODE_POSE)) {
68                         ob->mode= 0;
69                         data= ob->data;
70
71                         if(ob == obact && !ob->id.lib && !(data && data->lib))
72                                 ED_object_toggle_modes(C, mode);
73                 }
74         }
75 }
76
77 /* frees all editmode stuff */
78 void ED_editors_exit(bContext *C)
79 {
80         Main *bmain= CTX_data_main(C);
81         Scene *sce;
82
83         if(!bmain)
84                 return;
85         
86         /* frees all editmode undos */
87         undo_editmode_clear();
88         ED_undo_paint_free();
89         
90         for(sce=bmain->scene.first; sce; sce= sce->id.next) {
91                 if(sce->obedit) {
92                         Object *ob= sce->obedit;
93                 
94                         /* global in meshtools... */
95                         mesh_octree_table(NULL, NULL, NULL, 'e');
96                         mesh_mirrtopo_table(NULL, 'e');
97                         
98                         if(ob) {
99                                 if(ob->type==OB_MESH) {
100                                         Mesh *me= ob->data;
101                                         if(me->edit_mesh) {
102                                                 free_editMesh(me->edit_mesh);
103                                                 MEM_freeN(me->edit_mesh);
104                                                 me->edit_mesh= NULL;
105                                         }
106                                 }
107                                 else if(ob->type==OB_ARMATURE) {
108                                         ED_armature_edit_free(ob);
109                                 }
110                                 else if(ob->type==OB_FONT) {
111                                         //                      free_editText();
112                                 }
113                                 //              else if(ob->type==OB_MBALL) 
114                                 //                      BLI_freelistN(&editelems);
115                                 //      free_editLatt();
116                                 //      free_posebuf();         // XXX this is still a global...
117                         }
118                 }
119                 else if(sce->basact && sce->basact->object) {
120                         Object *ob= sce->basact->object;
121                         
122                         /* if weight-painting is on, free mesh octree data */
123                         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
124                                 mesh_octree_table(NULL, NULL, NULL, 'e');
125                                 mesh_mirrtopo_table(NULL, 'e');
126                         }
127                 }
128         }
129         
130 }
131
132
133 /* ***** XXX: functions are using old blender names, cleanup later ***** */
134
135
136 /* now only used in 2d spaces, like time, ipo, nla, sima... */
137 /* XXX shift/ctrl not configurable */
138 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
139 {
140         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
141         if(invert)
142                 ctrl= !ctrl;
143         
144         if(ctrl && shift) {
145                 if(fac3!= 0.0) *val= fac3*floor(*val/fac3 +.5);
146         }
147         else if(ctrl) {
148                 if(fac2!= 0.0) *val= fac2*floor(*val/fac2 +.5);
149         }
150         else {
151                 if(fac1!= 0.0) *val= fac1*floor(*val/fac1 +.5);
152         }
153 }
154
155
156 int GetButStringLength(char *str) 
157 {
158         int rt;
159         
160         rt= UI_GetStringWidth(str);
161         
162         return rt + 15;
163 }
164