2.5
[blender.git] / source / blender / editors / object / editgroup.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36
37 #include "DNA_group_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_view3d_types.h"
41
42 #include "BKE_depsgraph.h"
43 #include "BKE_group.h"
44 #include "BKE_global.h"
45 #include "BKE_main.h"
46
47 #include "object_intern.h"
48
49 /* XXX */
50 static void BIF_undo_push() {}
51 static void error() {}
52 static int pupmenu() {return 0;}
53 static int pupmenu_col() {return 0;}
54
55 void add_selected_to_group(Scene *scene, View3D *v3d, Group *group)
56 {
57         Base *base;
58         
59         for(base=FIRSTBASE; base; base= base->next) {
60                 if (TESTBASE(v3d, base)) {
61                         add_to_group(group, base->object);
62                         base->object->flag |= OB_FROMGROUP;
63                         base->flag |= OB_FROMGROUP;
64                 }
65         }
66         
67         DAG_scene_sort(scene);
68         BIF_undo_push("Add to Group");
69 }
70
71 void add_selected_to_act_ob_groups(Scene *scene, View3D *v3d)
72 {
73         Object *ob= OBACT, *obt;
74         Base *base;
75         Group *group;
76         
77         if (!ob) return;
78         
79         /* linking to same group requires its own loop so we can avoid
80            looking up the active objects groups each time */
81
82         group= G.main->group.first;
83         while(group) {
84                 if(object_in_group(ob, group)) {
85                         /* Assign groups to selected objects */
86                         base= FIRSTBASE;
87                         while(base) {
88                                 if(TESTBASE(v3d, base)) {
89                                         obt= base->object;
90                                         add_to_group(group, obt);
91                                         obt->flag |= OB_FROMGROUP;
92                                         base->flag |= OB_FROMGROUP;
93                                 }
94                                 base= base->next;
95                         }
96                 }
97                 group= group->id.next;
98         }
99         DAG_scene_sort(scene);
100         BIF_undo_push("Add to Active Objects Group");
101 }
102
103
104 void rem_selected_from_all_groups(Scene *scene, View3D *v3d)
105 {
106         Base *base;
107         Group *group;
108         
109         for(base=FIRSTBASE; base; base= base->next) {
110                 if (TESTBASE(v3d, base)) {
111                         group = NULL;
112                         while( (group = find_group(base->object, group)) ) {
113                                 rem_from_group(group, base->object);
114                         }
115                         base->object->flag &= ~OB_FROMGROUP;
116                         base->flag &= ~OB_FROMGROUP;
117                 }
118         }
119         
120         DAG_scene_sort(scene);
121         BIF_undo_push("Remove from Group");
122 }
123
124
125 void rem_selected_from_group(Scene *scene, View3D *v3d)
126 {
127         char menutext[30+(22*22)], *menupt;
128         int i=0;
129         short ret;
130         Group *group= NULL;
131         Object *ob;
132         Base *base;
133         Group *group_array[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
134         
135         /* UnSet Tags for Objects and Groups */
136         for(group= G.main->group.first; group; group= group->id.next) {
137                 if(group->id.lib==NULL) {
138                         group->id.flag &= ~LIB_DOIT;
139                 }
140         }
141         for(ob=G.main->object.first; ob; ob= ob->id.next) {
142                 ob->id.flag &= ~LIB_DOIT;
143         }
144         
145         /* Not tag selected objects */
146         for(base=FIRSTBASE; base; base= base->next) {
147                 if (TESTBASELIB(v3d, base)) {
148                         base->object->id.flag |= LIB_DOIT;
149                 }
150         }
151         
152         menupt = menutext;
153         /* Build a list of groups that contain selected objects */
154         for(group= G.main->group.first; group && i<24; group= group->id.next) {
155                 if(group->id.lib==NULL) {
156                         GroupObject *go;
157                         for(go= group->gobject.first; go; go= go->next) {
158                                 if(go->ob->id.flag & LIB_DOIT) {
159                                         group_array[i] = group;
160                                         menupt += sprintf(menupt, "|%s", group->id.name+2);
161                                         i++;
162                                         break; /* Only want to know if this group should go in the list*/
163                                 }
164                         }
165                 }
166         }
167         
168         /* do we have any groups? */
169         if (group_array[0] == NULL) {
170                 error("Object selection contains no groups");
171         } else {
172                 ret = pupmenu(menutext);
173                 if (ret==-1) {
174                         return;
175                 } else { 
176                         group = group_array[ret-1];
177                         for(base=FIRSTBASE; base; base= base->next) {
178                                 if (TESTBASELIB(v3d, base)) {
179                                         /* if we are removed and are not in any group, set our flag */
180                                         if(rem_from_group(group, base->object) && find_group(base->object, NULL)==NULL) {
181                                                 base->object->flag &= ~OB_FROMGROUP;
182                                                 base->flag &= ~OB_FROMGROUP;
183                                         }
184                                 }
185                         }
186                 }
187         }
188         
189         DAG_scene_sort(scene);
190         BIF_undo_push("Remove from Group");
191 }
192
193 void group_operation(Scene *scene, View3D *v3d, int mode)
194 {
195         Group *group= NULL;
196         
197         /* are there existing groups? */
198         for(group= G.main->group.first; group; group= group->id.next)
199                 if(group->id.lib==NULL)
200                         break;
201         
202         if(mode>0) {
203                 if(group==NULL || mode==1) group= add_group( "Group" );
204                 if(mode==3) {
205                         int tot= BLI_countlist(&G.main->group);
206                         char *strp= MEM_callocN(tot*32 + 32, "group menu"), *strp1;
207                         
208                         strp1= strp;
209                         for(tot=1, group= G.main->group.first; group; group= group->id.next, tot++) {
210                                 if(group->id.lib==NULL) {
211                                         strp1 += sprintf(strp1, "%s %%x%d|", group->id.name+2, tot);
212                                 }
213                         }
214                         tot= pupmenu_col(strp, 20);
215                         MEM_freeN(strp);
216                         if(tot>0) group= BLI_findlink(&G.main->group, tot-1);
217                         else return;
218                 }
219                 
220                 if(mode==4) add_selected_to_act_ob_groups(scene, v3d);
221                 else if(mode==1 || mode==3) add_selected_to_group(scene, v3d, group);
222                 else if(mode==2) rem_selected_from_all_groups(scene, v3d);
223                 else if(mode==5) rem_selected_from_group(scene, v3d);
224         }
225 }
226
227 void group_operation_with_menu(Scene *scene, View3D *v3d)
228 {
229         Group *group= NULL;
230         int mode;
231         
232         /* are there existing groups? */
233         for(group= G.main->group.first; group; group= group->id.next)
234                 if(group->id.lib==NULL)
235                         break;
236         
237         if(group)
238                 mode= pupmenu("Groups %t|Add to Existing Group %x3|Add to Active Objects Groups %x4|Add to New Group %x1|Remove from Group %x5|Remove from All Groups %x2");
239         else
240                 mode= pupmenu("Groups %t|Add to New Group %x1|Remove from All Groups %x2");
241         
242         group_operation(scene, v3d, mode);
243 }
244