2.5 - Animation Playback Tweaks
[blender.git] / source / blender / blenkernel / intern / sca.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) 2001-2002 by NaN Holding BV.
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  * these all are linked to objects (listbase)
29  * all data is 'direct data', not Blender lib data.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_text_types.h"
41 #include "DNA_controller_types.h"
42 #include "DNA_sensor_types.h"
43 #include "DNA_actuator_types.h"
44 #include "DNA_object_types.h"
45
46 #include "BLI_blenlib.h"
47 #include "BKE_utildefines.h"
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_blender.h"
51 #include "BKE_sca.h"
52
53 //#include "wm_event_types.h"
54
55 void free_text_controllers(Text *txt)
56 {
57         Object *ob;
58         bController *cont;
59         
60         ob= G.main->object.first;
61         while(ob) {
62                 cont= ob->controllers.first;
63                 while(cont) {
64                         if(cont->type==CONT_PYTHON) {
65                                 bPythonCont *pc;
66                                 
67                                 pc= cont->data;
68                                 if(pc->text==txt) pc->text= NULL;
69                         }
70                         cont= cont->next;
71                 }
72                 ob= ob->id.next;
73         }
74 }
75
76
77 /* ******************* SENSORS ************************ */
78
79 void free_sensor(bSensor *sens)
80 {
81         if(sens->links) MEM_freeN(sens->links);
82         if(sens->data) MEM_freeN(sens->data);
83         MEM_freeN(sens);
84         
85 }
86
87 void free_sensors(ListBase *lb)
88 {
89         bSensor *sens;
90         
91         while((sens= lb->first)) {
92                 BLI_remlink(lb, sens);
93                 free_sensor(sens);
94         }
95 }
96
97 bSensor *copy_sensor(bSensor *sens)
98 {
99         bSensor *sensn;
100         
101         sensn= MEM_dupallocN(sens);
102         sensn->flag |= SENS_NEW;
103         if(sens->data) {
104                 sensn->data= MEM_dupallocN(sens->data);
105         }
106
107         if(sens->links) sensn->links= MEM_dupallocN(sens->links);
108         
109         return sensn;
110 }
111
112 void copy_sensors(ListBase *lbn, ListBase *lbo)
113 {
114         bSensor *sens, *sensn;
115         
116         lbn->first= lbn->last= 0;
117         sens= lbo->first;
118         while(sens) {
119                 sensn= copy_sensor(sens);
120                 BLI_addtail(lbn, sensn);
121                 sens= sens->next;
122         }
123 }
124
125 void init_sensor(bSensor *sens)
126 {
127         /* also use when sensor changes type */
128         bNearSensor *ns;
129         bMouseSensor *ms;
130         
131         if(sens->data) MEM_freeN(sens->data);
132         sens->data= NULL;
133         sens->pulse = 0;
134         
135         switch(sens->type) {
136         case SENS_ALWAYS:
137                 sens->pulse = 0;
138                 break;
139         case SENS_TOUCH:
140                 sens->data= MEM_callocN(sizeof(bTouchSensor), "touchsens");
141                 break;
142         case SENS_NEAR:
143                 ns=sens->data= MEM_callocN(sizeof(bNearSensor), "nearsens");
144                 ns->dist= 1.0;
145                 ns->resetdist= 2.0;
146                 break;
147         case SENS_KEYBOARD:
148                 sens->data= MEM_callocN(sizeof(bKeyboardSensor), "keysens");
149                 break;
150         case SENS_PROPERTY:
151                 sens->data= MEM_callocN(sizeof(bPropertySensor), "propsens");
152                 break;
153         case SENS_ACTUATOR:
154                 sens->data= MEM_callocN(sizeof(bActuatorSensor), "actsens");
155                 break;
156         case SENS_DELAY:
157                 sens->data= MEM_callocN(sizeof(bDelaySensor), "delaysens");
158                 break;
159         case SENS_MOUSE:
160                 ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens");
161                 //XXX ms->type= LEFTMOUSE;
162                 break;
163         case SENS_COLLISION:
164                 sens->data= MEM_callocN(sizeof(bCollisionSensor), "colsens");
165                 break;
166         case SENS_RADAR:
167                 sens->data= MEM_callocN(sizeof(bRadarSensor), "radarsens");
168                 break;
169         case SENS_RANDOM:
170                 sens->data= MEM_callocN(sizeof(bRandomSensor), "randomsens");
171                 break;
172         case SENS_RAY:
173                 sens->data= MEM_callocN(sizeof(bRaySensor), "raysens");
174                 break;
175         case SENS_MESSAGE:
176                 sens->data= MEM_callocN(sizeof(bMessageSensor), "messagesens");
177                 break;
178         case SENS_JOYSTICK:
179                 sens->data= MEM_callocN(sizeof(bJoystickSensor), "joysticksens");
180                 break;
181         default:
182                 ; /* this is very severe... I cannot make any memory for this        */
183                 /* logic brick...                                                    */
184         }
185 }
186
187 bSensor *new_sensor(int type)
188 {
189         bSensor *sens;
190
191         sens= MEM_callocN(sizeof(bSensor), "Sensor");
192         sens->type= type;
193         sens->flag= SENS_SHOW;
194         
195         init_sensor(sens);
196         
197         strcpy(sens->name, "sensor");
198 // XXX  make_unique_prop_names(sens->name);
199         
200         return sens;
201 }
202
203 /* ******************* CONTROLLERS ************************ */
204
205 void unlink_controller(bController *cont)
206 {
207         bSensor *sens;
208         Object *ob;
209         int a, removed;
210         
211         /* check for controller pointers in sensors */
212         ob= G.main->object.first;
213         while(ob) {
214                 sens= ob->sensors.first;
215                 while(sens) {
216                         removed= 0;
217                         for(a=0; a<sens->totlinks; a++) {
218                                 if(removed) (sens->links)[a-1] = (sens->links)[a];
219                                 else if((sens->links)[a] == cont) removed= 1;
220                         }
221                         if(removed) {
222                                 sens->totlinks--;
223                                 
224                                 if(sens->totlinks==0) {
225                                         MEM_freeN(sens->links);
226                                         sens->links= NULL;
227                                 }
228                         }
229                         sens= sens->next;
230                 }
231                 ob= ob->id.next;
232         }
233 }
234
235 void unlink_controllers(ListBase *lb)
236 {
237         bController *cont;
238         
239         for (cont= lb->first; cont; cont= cont->next)
240                 unlink_controller(cont);        
241 }
242
243 void free_controller(bController *cont)
244 {
245         if(cont->links) MEM_freeN(cont->links);
246
247         /* the controller itself */
248         if(cont->data) MEM_freeN(cont->data);
249         MEM_freeN(cont);
250         
251 }
252
253 void free_controllers(ListBase *lb)
254 {
255         bController *cont;
256         
257         while((cont= lb->first)) {
258                 BLI_remlink(lb, cont);
259                 if(cont->slinks) MEM_freeN(cont->slinks);
260                 free_controller(cont);
261         }
262 }
263
264 bController *copy_controller(bController *cont)
265 {
266         bController *contn;
267         
268         cont->mynew=contn= MEM_dupallocN(cont);
269         contn->flag |= CONT_NEW;
270         if(cont->data) {
271                 contn->data= MEM_dupallocN(cont->data);
272         }
273
274         if(cont->links) contn->links= MEM_dupallocN(cont->links);
275         contn->slinks= NULL;
276         contn->totslinks= 0;
277         
278         return contn;
279 }
280
281 void copy_controllers(ListBase *lbn, ListBase *lbo)
282 {
283         bController *cont, *contn;
284         
285         lbn->first= lbn->last= 0;
286         cont= lbo->first;
287         while(cont) {
288                 contn= copy_controller(cont);
289                 BLI_addtail(lbn, contn);
290                 cont= cont->next;
291         }
292 }
293
294 void init_controller(bController *cont)
295 {
296         /* also use when controller changes type, leave actuators... */
297         
298         if(cont->data) MEM_freeN(cont->data);
299         cont->data= 0;
300         
301         switch(cont->type) {
302         case CONT_EXPRESSION:
303                 cont->data= MEM_callocN(sizeof(bExpressionCont), "expcont");
304                 break;
305         case CONT_PYTHON:
306                 cont->data= MEM_callocN(sizeof(bPythonCont), "pycont");
307                 break;
308         }
309 }
310
311 bController *new_controller(int type)
312 {
313         bController *cont;
314
315         cont= MEM_callocN(sizeof(bController), "Controller");
316         cont->type= type;
317         cont->flag= CONT_SHOW;
318
319         init_controller(cont);
320         
321         strcpy(cont->name, "cont");
322 // XXX  make_unique_prop_names(cont->name);
323         
324         return cont;
325 }
326
327 /* ******************* ACTUATORS ************************ */
328
329 void unlink_actuator(bActuator *act)
330 {
331         bController *cont;
332         Object *ob;
333         int a, removed;
334         
335         /* check for actuator pointers in controllers */
336         ob= G.main->object.first;
337         while(ob) {
338                 cont= ob->controllers.first;
339                 while(cont) {
340                         removed= 0;
341                         for(a=0; a<cont->totlinks; a++) {
342                                 if(removed) (cont->links)[a-1] = (cont->links)[a];
343                                 else if((cont->links)[a] == act) removed= 1;
344                         }
345                         if(removed) {
346                                 cont->totlinks--;
347                                 
348                                 if(cont->totlinks==0) {
349                                         MEM_freeN(cont->links);
350                                         cont->links= NULL;
351                                 }
352                         }
353                         cont= cont->next;
354                 }
355                 ob= ob->id.next;
356         }
357 }
358
359 void unlink_actuators(ListBase *lb)
360 {
361         bActuator *act;
362         
363         for (act= lb->first; act; act= act->next)
364                 unlink_actuator(act);
365 }
366
367 void free_actuator(bActuator *act)
368 {
369         if(act->data) MEM_freeN(act->data);
370         MEM_freeN(act);
371 }
372
373 void free_actuators(ListBase *lb)
374 {
375         bActuator *act;
376         
377         while((act= lb->first)) {
378                 BLI_remlink(lb, act);
379                 free_actuator(act);
380         }
381 }
382
383 bActuator *copy_actuator(bActuator *act)
384 {
385         bActuator *actn;
386         
387         act->mynew=actn= MEM_dupallocN(act);
388         actn->flag |= ACT_NEW;
389         if(act->data) {
390                 actn->data= MEM_dupallocN(act->data);
391         }
392         
393         return actn;
394 }
395
396 void copy_actuators(ListBase *lbn, ListBase *lbo)
397 {
398         bActuator *act, *actn;
399         
400         lbn->first= lbn->last= 0;
401         act= lbo->first;
402         while(act) {
403                 actn= copy_actuator(act);
404                 BLI_addtail(lbn, actn);
405                 act= act->next;
406         }
407 }
408
409 void init_actuator(bActuator *act)
410 {
411         /* also use when actuator changes type */
412         bObjectActuator *oa;
413         
414         if(act->data) MEM_freeN(act->data);
415         act->data= 0;
416         
417         switch(act->type) {
418 #ifdef __NLA
419         case ACT_ACTION:
420         case ACT_SHAPEACTION:
421                 act->data= MEM_callocN(sizeof(bActionActuator), "actionact");
422                 break;
423 #endif
424         case ACT_SOUND:
425                 act->data= MEM_callocN(sizeof(bSoundActuator), "soundact");
426                 break;
427         case ACT_CD:
428                 act->data= MEM_callocN(sizeof(bCDActuator), "cdact");
429                 break;
430         case ACT_OBJECT:
431                 act->data= MEM_callocN(sizeof(bObjectActuator), "objectact");
432                 oa= act->data;
433                 oa->flag= 15;
434                 break;
435         case ACT_IPO:
436                 act->data= MEM_callocN(sizeof(bIpoActuator), "ipoact");
437                 break;
438         case ACT_PROPERTY:
439                 act->data= MEM_callocN(sizeof(bPropertyActuator), "propact");
440                 break;
441         case ACT_CAMERA:
442                 act->data= MEM_callocN(sizeof(bCameraActuator), "camact");
443                 break;
444         case ACT_EDIT_OBJECT:
445                 act->data= MEM_callocN(sizeof(bEditObjectActuator), "editobact");
446                 break;
447         case ACT_CONSTRAINT:
448                 act->data= MEM_callocN(sizeof(bConstraintActuator), "cons act");
449                 break;
450         case ACT_SCENE:
451                 act->data= MEM_callocN(sizeof(bSceneActuator), "scene act");
452                 break;
453         case ACT_GROUP:
454                 act->data= MEM_callocN(sizeof(bGroupActuator), "group act");
455                 break;
456         case ACT_RANDOM:
457                 act->data= MEM_callocN(sizeof(bRandomActuator), "random act");
458                 break;
459         case ACT_MESSAGE:
460                 act->data= MEM_callocN(sizeof(bMessageActuator), "message act");
461                 break;
462         case ACT_GAME:
463                 act->data= MEM_callocN(sizeof(bGameActuator), "game act");
464                 break;
465         case ACT_VISIBILITY:
466                 act->data= MEM_callocN(sizeof(bVisibilityActuator), "visibility act");
467                 break;
468     case ACT_2DFILTER:
469         act->data = MEM_callocN(sizeof( bTwoDFilterActuator ), "2d filter act");
470         break;
471     case ACT_PARENT:
472         act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
473         break;
474         case ACT_STATE:
475         act->data = MEM_callocN(sizeof( bStateActuator ), "state act");
476         break;
477         default:
478                 ; /* this is very severe... I cannot make any memory for this        */
479                 /* logic brick...                                                    */
480         }
481 }
482
483 bActuator *new_actuator(int type)
484 {
485         bActuator *act;
486
487         act= MEM_callocN(sizeof(bActuator), "Actuator");
488         act->type= type;
489         act->flag= ACT_SHOW;
490         
491         init_actuator(act);
492         
493         strcpy(act->name, "act");
494 // XXX  make_unique_prop_names(act->name);
495         
496         return act;
497 }
498
499 /* ******************** GENERAL ******************* */
500
501 void clear_sca_new_poins_ob(Object *ob)
502 {
503         bSensor *sens;
504         bController *cont;
505         bActuator *act;
506         
507         sens= ob->sensors.first;
508         while(sens) {
509                 sens->flag &= ~SENS_NEW;
510                 sens= sens->next;
511         }
512         cont= ob->controllers.first;
513         while(cont) {
514                 cont->mynew= NULL;
515                 cont->flag &= ~CONT_NEW;
516                 cont= cont->next;
517         }
518         act= ob->actuators.first;
519         while(act) {
520                 act->mynew= NULL;
521                 act->flag &= ~ACT_NEW;
522                 act= act->next;
523         }
524 }
525
526 void clear_sca_new_poins()
527 {
528         Object *ob;
529         
530         ob= G.main->object.first;
531         while(ob) {
532                 clear_sca_new_poins_ob(ob);
533                 ob= ob->id.next;        
534         }
535 }
536
537 void set_sca_new_poins_ob(Object *ob)
538 {
539         bSensor *sens;
540         bController *cont;
541         bActuator *act;
542         int a;
543         
544         sens= ob->sensors.first;
545         while(sens) {
546                 if(sens->flag & SENS_NEW) {
547                         for(a=0; a<sens->totlinks; a++) {
548                                 if(sens->links[a] && sens->links[a]->mynew)
549                                         sens->links[a]= sens->links[a]->mynew;
550                         }
551                 }
552                 sens= sens->next;
553         }
554
555         cont= ob->controllers.first;
556         while(cont) {
557                 if(cont->flag & CONT_NEW) {
558                         for(a=0; a<cont->totlinks; a++) {
559                                 if( cont->links[a] && cont->links[a]->mynew)
560                                         cont->links[a]= cont->links[a]->mynew;
561                         }
562                 }
563                 cont= cont->next;
564         }
565         
566         
567         act= ob->actuators.first;
568         while(act) {
569                 if(act->flag & ACT_NEW) {
570                         if(act->type==ACT_EDIT_OBJECT) {
571                                 bEditObjectActuator *eoa= act->data;
572                                 ID_NEW(eoa->ob);
573                         }
574                         else if(act->type==ACT_SCENE) {
575                                 bSceneActuator *sca= act->data;
576                                 ID_NEW(sca->camera);
577                         }
578                         else if(act->type==ACT_CAMERA) {
579                                 bCameraActuator *ca= act->data;
580                                 ID_NEW(ca->ob);
581                         }
582                         else if(act->type==ACT_OBJECT) {
583                                 bObjectActuator *oa= act->data;
584                                 ID_NEW(oa->reference);
585                         }
586                         else if(act->type==ACT_SCENE) {
587                                 bSceneActuator *sca= act->data;
588                                 ID_NEW(sca->camera);
589                         }
590                 }
591                 act= act->next;
592         }
593 }
594
595
596 void set_sca_new_poins()
597 {
598         Object *ob;
599         
600         ob= G.main->object.first;
601         while(ob) {
602                 set_sca_new_poins_ob(ob);
603                 ob= ob->id.next;        
604         }
605 }
606
607 void sca_remove_ob_poin(Object *obt, Object *ob)
608 {
609         bSensor *sens;
610         bMessageSensor *ms;
611         bActuator *act;
612         bCameraActuator *ca;
613         bObjectActuator *oa;
614         bSceneActuator *sa;
615         bEditObjectActuator *eoa;
616         bPropertyActuator *pa;
617         bMessageActuator *ma;
618
619         sens= obt->sensors.first;
620         while(sens) {
621                 switch(sens->type) {
622                 case SENS_MESSAGE:
623                         ms= sens->data;
624                         if(ms->fromObject==ob) ms->fromObject= NULL;
625                 }
626                 sens= sens->next;
627         }
628
629         act= obt->actuators.first;
630         while(act) {
631                 switch(act->type) {
632                 case ACT_CAMERA:
633                         ca= act->data;
634                         if(ca->ob==ob) ca->ob= NULL;
635                         break;
636                 case ACT_OBJECT:
637                         oa= act->data;
638                         if(oa->reference==ob) oa->reference= NULL;
639                         break;
640                 case ACT_PROPERTY:
641                         pa= act->data;
642                         if(pa->ob==ob) pa->ob= NULL;
643                         break;
644                 case ACT_SCENE:
645                         sa= act->data;
646                         if(sa->camera==ob) sa->camera= NULL;
647                         break;
648                 case ACT_EDIT_OBJECT:
649                         eoa= act->data;
650                         if(eoa->ob==ob) eoa->ob= NULL;
651                         break;
652                 case ACT_MESSAGE:
653                         ma= act->data;
654                         if(ma->toObject==ob) ma->toObject= NULL;
655                         break;
656
657                 }
658                 act= act->next;
659         }       
660 }