Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / editors / physics / ed_pointcache.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) 2007 by Janne Karhu.
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 "MEM_guardedalloc.h"
31
32 #include "DNA_scene_types.h"
33 #include "DNA_object_force.h"
34 #include "DNA_modifier_types.h"
35
36 #include "BKE_context.h"
37 #include "BKE_particle.h"
38 #include "BKE_report.h"
39 #include "BKE_scene.h"
40 #include "BKE_utildefines.h" 
41 #include "BKE_pointcache.h"
42 #include "BKE_global.h"
43 #include "BKE_modifier.h"
44
45 #include "BLI_blenlib.h"
46
47 #include "ED_screen.h"
48 #include "ED_physics.h"
49
50 #include "UI_interface.h"
51 #include "UI_resources.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58
59 #include "physics_intern.h"
60
61 static int cache_break_test(void *cbd) {
62         return G.afbreek==1;
63 }
64 static int ptcache_bake_all_poll(bContext *C)
65 {
66         Scene *scene= CTX_data_scene(C);
67
68         if(!scene)
69                 return 0;
70         
71         return 1;
72 }
73
74 static int ptcache_bake_all_exec(bContext *C, wmOperator *op)
75 {
76         Scene *scene= CTX_data_scene(C);
77         PTCacheBaker baker;
78
79
80         baker.scene = scene;
81         baker.pid = NULL;
82         baker.bake = RNA_boolean_get(op->ptr, "bake");
83         baker.render = 0;
84         baker.anim_init = 0;
85         baker.quick_step = 1;
86         baker.break_test = cache_break_test;
87         baker.break_data = NULL;
88         baker.progressbar = (void (*)(void *, int))WM_timecursor;
89         baker.progresscontext = CTX_wm_window(C);
90
91         BKE_ptcache_make_cache(&baker);
92
93         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
94
95         return OPERATOR_FINISHED;
96 }
97 static int ptcache_free_bake_all_exec(bContext *C, wmOperator *op)
98 {
99         Scene *scene= CTX_data_scene(C);
100         Base *base;
101         PTCacheID *pid;
102         ListBase pidlist;
103
104         for(base=scene->base.first; base; base= base->next) {
105                 BKE_ptcache_ids_from_object(&pidlist, base->object);
106
107                 for(pid=pidlist.first; pid; pid=pid->next) {
108                         pid->cache->flag &= ~PTCACHE_BAKED;
109                 }
110                 
111                 BLI_freelistN(&pidlist);
112         }
113
114         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
115
116         return OPERATOR_FINISHED;
117 }
118
119 void PTCACHE_OT_bake_all(wmOperatorType *ot)
120 {
121         /* identifiers */
122         ot->name= "Bake All Physics";
123         ot->idname= "PTCACHE_OT_bake_all";
124         
125         /* api callbacks */
126         ot->exec= ptcache_bake_all_exec;
127         ot->poll= ptcache_bake_all_poll;
128
129         /* flags */
130         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
131
132         RNA_def_boolean(ot->srna, "bake", 1, "Bake", "");
133 }
134 void PTCACHE_OT_free_bake_all(wmOperatorType *ot)
135 {
136         /* identifiers */
137         ot->name= "Free All Physics Bakes";
138         ot->idname= "PTCACHE_OT_free_bake_all";
139         
140         /* api callbacks */
141         ot->exec= ptcache_free_bake_all_exec;
142         ot->poll= ptcache_bake_all_poll;
143
144         /* flags */
145         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
146 }
147 static int ptcache_bake_exec(bContext *C, wmOperator *op)
148 {
149         Scene *scene = CTX_data_scene(C);
150         PointerRNA ptr= CTX_data_pointer_get_type(C, "PointCache", &RNA_PointCache);
151         Object *ob= ptr.id.data;
152         PointCache *cache= ptr.data;
153         PTCacheBaker baker;
154         PTCacheID *pid;
155         ListBase pidlist;
156
157         BKE_ptcache_ids_from_object(&pidlist, ob);
158         
159         for(pid=pidlist.first; pid; pid=pid->next) {
160                 if(pid->cache == cache)
161                         break;
162         }
163
164         baker.scene = scene;
165         baker.pid = pid;
166         baker.bake = RNA_boolean_get(op->ptr, "bake");
167         baker.render = 0;
168         baker.anim_init = 0;
169         baker.quick_step = 1;
170         baker.break_test = cache_break_test;
171         baker.break_data = NULL;
172         baker.progressbar = (void (*)(void *, int))WM_timecursor;
173         baker.progresscontext = CTX_wm_window(C);
174
175         BKE_ptcache_make_cache(&baker);
176
177         BLI_freelistN(&pidlist);
178
179         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
180
181         return OPERATOR_FINISHED;
182 }
183 static int ptcache_free_bake_exec(bContext *C, wmOperator *op)
184 {
185         PointerRNA ptr= CTX_data_pointer_get_type(C, "PointCache", &RNA_PointCache);
186         PointCache *cache= ptr.data;
187         
188         cache->flag &= ~PTCACHE_BAKED;
189
190         return OPERATOR_FINISHED;
191 }
192 static int ptcache_bake_from_cache_exec(bContext *C, wmOperator *op)
193 {
194         PointerRNA ptr= CTX_data_pointer_get_type(C, "PointCache", &RNA_PointCache);
195         PointCache *cache= ptr.data;
196         
197         cache->flag |= PTCACHE_BAKED;
198
199         return OPERATOR_FINISHED;
200 }
201 void PTCACHE_OT_bake(wmOperatorType *ot)
202 {
203         /* identifiers */
204         ot->name= "Bake Physics";
205         ot->idname= "PTCACHE_OT_bake";
206         
207         /* api callbacks */
208         ot->exec= ptcache_bake_exec;
209         ot->poll= ptcache_bake_all_poll;
210
211         /* flags */
212         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
213
214         RNA_def_boolean(ot->srna, "bake", 1, "Bake", "");
215 }
216 void PTCACHE_OT_free_bake(wmOperatorType *ot)
217 {
218         /* identifiers */
219         ot->name= "Free Physics Bake";
220         ot->idname= "PTCACHE_OT_free_bake";
221         
222         /* api callbacks */
223         ot->exec= ptcache_free_bake_exec;
224         ot->poll= ptcache_bake_all_poll;
225
226         /* flags */
227         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
228 }
229 void PTCACHE_OT_bake_from_cache(wmOperatorType *ot)
230 {
231         /* identifiers */
232         ot->name= "Bake From Cache";
233         ot->idname= "PTCACHE_OT_bake_from_cache";
234         
235         /* api callbacks */
236         ot->exec= ptcache_bake_from_cache_exec;
237         ot->poll= ptcache_bake_all_poll;
238
239         /* flags */
240         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
241 }
242
243 static int ptcache_add_new_exec(bContext *C, wmOperator *op)
244 {
245         Scene *scene = CTX_data_scene(C);
246         PointerRNA ptr= CTX_data_pointer_get_type(C, "PointCache", &RNA_PointCache);
247         Object *ob= ptr.id.data;
248         PointCache *cache= ptr.data;
249         PTCacheID *pid;
250         ListBase pidlist;
251
252         BKE_ptcache_ids_from_object(&pidlist, ob);
253         
254         for(pid=pidlist.first; pid; pid=pid->next) {
255                 if(pid->cache == cache) {
256                         *(pid->cache_ptr) = BKE_ptcache_add(pid->ptcaches);
257                         break;
258                 }
259         }
260
261         BLI_freelistN(&pidlist);
262
263         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
264
265         return OPERATOR_FINISHED;
266 }
267 static int ptcache_remove_exec(bContext *C, wmOperator *op)
268 {
269         PointerRNA ptr= CTX_data_pointer_get_type(C, "PointCache", &RNA_PointCache);
270         Object *ob= ptr.id.data;
271         PointCache *cache= ptr.data;
272         PTCacheID *pid;
273         ListBase pidlist;
274
275         BKE_ptcache_ids_from_object(&pidlist, ob);
276         
277         for(pid=pidlist.first; pid; pid=pid->next) {
278                 if(pid->cache == cache) {
279                         if(pid->ptcaches->first == pid->ptcaches->last)
280                                 continue; /* don't delete last cache */
281
282                         BLI_remlink(pid->ptcaches, pid->cache);
283                         BKE_ptcache_free(pid->cache);
284                         *(pid->cache_ptr) = pid->ptcaches->first;
285
286                         break;
287                 }
288         }
289
290         BLI_freelistN(&pidlist);
291
292         return OPERATOR_FINISHED;
293 }
294 void PTCACHE_OT_add_new(wmOperatorType *ot)
295 {
296         /* identifiers */
297         ot->name= "Add new cache";
298         ot->idname= "PTCACHE_OT_add_new";
299         
300         /* api callbacks */
301         ot->exec= ptcache_add_new_exec;
302         ot->poll= ptcache_bake_all_poll;
303
304         /* flags */
305         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
306 }
307 void PTCACHE_OT_remove(wmOperatorType *ot)
308 {
309         /* identifiers */
310         ot->name= "Delete current cache";
311         ot->idname= "PTCACHE_OT_remove";
312         
313         /* api callbacks */
314         ot->exec= ptcache_remove_exec;
315         ot->poll= ptcache_bake_all_poll;
316
317         /* flags */
318         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
319 }
320 /**************************** registration **********************************/
321
322 void ED_operatortypes_pointcache(void)
323 {
324         WM_operatortype_append(PTCACHE_OT_bake_all);
325         WM_operatortype_append(PTCACHE_OT_free_bake_all);
326         WM_operatortype_append(PTCACHE_OT_bake);
327         WM_operatortype_append(PTCACHE_OT_free_bake);
328         WM_operatortype_append(PTCACHE_OT_bake_from_cache);
329         WM_operatortype_append(PTCACHE_OT_add_new);
330         WM_operatortype_append(PTCACHE_OT_remove);
331 }
332
333 //void ED_keymap_pointcache(wmWindowManager *wm)
334 //{
335 //      ListBase *keymap= WM_keymap_listbase(wm, "Pointcache", 0, 0);
336 //      
337 //      WM_keymap_add_item(keymap, "PHYSICS_OT_bake_all", AKEY, KM_PRESS, 0, 0);
338 //      WM_keymap_add_item(keymap, "PHYSICS_OT_free_all", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
339 //      WM_keymap_add_item(keymap, "PHYSICS_OT_bake_particle_system", PADMINUS, KM_PRESS, KM_CTRL, 0);
340 //      WM_keymap_add_item(keymap, "PHYSICS_OT_free_particle_system", LKEY, KM_PRESS, 0, 0);
341 //}
342