WIP commit to introduce channels
[blender.git] / source / blender / src / editipo.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  * Contributor(s): Blender Foundation, 2005. Full recode.
24  * Roland Hess, 2007. Visual Key refactor.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29
30 /* this code feels over-complex, mostly because I choose in the past to devise a system
31   that converts the Ipo blocks (linked to Object, Material, etc), into a copy of that
32   data which is being worked on;  the 'editipo'.
33   The editipo then can have 'ipokey' data, which is optimized for editing curves as if
34   it were key positions. This is still a great feature to work with, which makes ipo editing
35   in Blender still valuable. However, getting this beast under control was hard, even
36   for me... (ton) */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #ifndef WIN32
43 #include <unistd.h>
44 #else
45 #include <io.h>
46 #endif   
47 #include "MEM_guardedalloc.h"
48 #include "PIL_time.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52
53 #include "DNA_constraint_types.h"
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_curve_types.h"
58 #include "DNA_group_types.h"
59 #include "DNA_ipo_types.h"
60 #include "DNA_key_types.h"
61 #include "DNA_lamp_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_object_fluidsim.h"
65 #include "DNA_particle_types.h"
66 #include "DNA_screen_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_space_types.h"
69 #include "DNA_sequence_types.h"
70 #include "DNA_sound_types.h"
71 #include "DNA_texture_types.h"
72 #include "DNA_userdef_types.h"
73 #include "DNA_view3d_types.h"
74 #include "DNA_world_types.h"
75
76 #include "BKE_action.h"
77 #include "BKE_armature.h"
78 #include "BKE_anim.h"
79 #include "BKE_constraint.h"
80 #include "BKE_depsgraph.h"
81 #include "BKE_global.h"
82 #include "BKE_group.h"
83 #include "BKE_ipo.h"
84 #include "BKE_key.h"
85 #include "BKE_material.h"
86 #include "BKE_particle.h"
87 #include "BKE_texture.h"
88 #include "BKE_utildefines.h"
89 #include "BKE_object.h"
90
91 #include "BIF_butspace.h"
92 #include "BIF_editaction.h"
93 #include "BIF_editconstraint.h"
94 #include "BIF_editkey.h"
95 #include "BIF_editnla.h"
96 #include "BIF_editseq.h"
97 #include "BIF_editview.h"
98 #include "BIF_interface.h"
99 #include "BIF_mywindow.h"
100 #include "BIF_poseobject.h"
101 #include "BIF_screen.h"
102 #include "BIF_space.h"
103 #include "BIF_toolbox.h"
104 #include "BIF_poseobject.h"
105
106 #include "BDR_drawobject.h"
107 #include "BDR_editobject.h"
108 #include "BDR_editcurve.h"      // for bezt_compare 
109
110 #include "BSE_trans_types.h"
111 #include "BSE_editipo_types.h"
112 #include "BSE_drawipo.h"
113 #include "BSE_editipo.h"
114 #include "BSE_edit.h"
115 #include "BSE_drawview.h"
116 #include "BSE_headerbuttons.h"
117 #include "BSE_node.h"
118 #include "BSE_sequence.h"
119 #include "BSE_seqaudio.h"
120 #include "BSE_time.h"
121
122 #include "blendef.h"
123 #include "mydevice.h"
124 #include "transform.h"
125
126 extern int ob_ar[];
127 extern int ma_ar[];
128 extern int seq_ar[];
129 extern int cu_ar[];
130 extern int wo_ar[];
131 extern int la_ar[];
132 extern int cam_ar[];
133 extern int snd_ar[];
134 extern int ac_ar[];
135 extern int co_ar[];
136 extern int te_ar[];
137 extern int fluidsim_ar[]; // NT
138 extern int part_ar[];
139
140 /* forwards */
141 #define IPOTHRESH       0.9
142
143 /* tests if only one editipo is active */
144 static void check_active_editipo(void)
145 {
146         EditIpo *ei, *actei;
147         int a;
148         
149         actei= G.sipo->editipo;
150         if(actei) {
151                 for(a=0; a<G.sipo->totipo; a++, actei++) {
152                         if(actei->flag & IPO_ACTIVE) 
153                                 break;
154                 }
155                 if(actei==NULL) {
156                         /* set visible active */
157                         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++) {
158                                 if(ei->flag & IPO_VISIBLE)
159                                         break;
160                         }
161                         if(ei==NULL) ei=G.sipo->editipo;
162                         ei->flag |= IPO_ACTIVE;
163                         if(ei->icu) ei->icu->flag |= IPO_ACTIVE;
164                 }
165                 else {
166                         /* make sure no others are active */
167                         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++) {
168                                 if(ei!=actei) {
169                                         ei->flag &= ~IPO_ACTIVE;
170                                         if(ei->icu) ei->icu->flag &= ~IPO_ACTIVE;
171                                 }
172                         }
173                 }
174         }
175 }
176
177 /* sets this ei channel active */
178 static void set_active_editipo(EditIpo *actei)
179 {
180         EditIpo *ei;
181         int a;
182         
183         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++) {
184                 ei->flag &= ~IPO_ACTIVE;
185                 if(ei->icu) ei->icu->flag &= ~IPO_ACTIVE;
186         }
187         actei->flag |= IPO_ACTIVE;
188         if(actei->icu) actei->icu->flag |= IPO_ACTIVE;
189 }
190
191 EditIpo *get_active_editipo(void)
192 {
193         EditIpo *ei;
194         int a;
195         
196         if(G.sipo==NULL)
197                 return NULL;
198         
199         /* prevent confusing situations, like for sequencer */
200         if(G.sipo->totipo==1) {
201                 ei= G.sipo->editipo;
202                 ei->flag |= IPO_ACTIVE;
203                 return ei;
204         }
205         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++)
206                 if(ei->flag & IPO_ACTIVE)
207                         return ei;
208         
209         return NULL;
210 }
211
212 static void set_active_key(int index)
213 {
214         if(G.sipo->blocktype==ID_KE && G.sipo->from) {
215                 Object *ob= (Object *)G.sipo->from;
216                 Key *key= ob_get_key(ob);
217                 
218                 if(key) {
219                         KeyBlock *curkb;
220                         
221                         curkb= BLI_findlink(&key->block, index-1);
222                         if(curkb) {
223                                 ob->shapenr= index;
224                                 ob->shapeflag |= OB_SHAPE_TEMPLOCK;
225                                 
226                                 /* calc keypos */
227                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
228                                 allqueue(REDRAWVIEW3D, 0);
229                                 allqueue(REDRAWBUTSEDIT, 0);
230                         }
231                 }
232         }                       
233 }
234
235 void editipo_changed(SpaceIpo *si, int doredraw)
236 {
237         EditIpo *ei;
238         View2D *v2d;
239         Key *key;
240         KeyBlock *kb;
241         int a, first=1;
242
243         ei= si->editipo;
244         if(ei==0)
245                 return;
246         
247         for(a=0; a<si->totipo; a++, ei++) {
248                 
249                 if(ei->icu) {
250                         
251                                 /* twice because of ittererating new autohandle */
252                         calchandles_ipocurve(ei->icu);
253                         calchandles_ipocurve(ei->icu);
254                         
255                         if(ei->flag & IPO_VISIBLE) {
256                 
257                                 boundbox_ipocurve(ei->icu, 0);
258                                 sort_time_ipocurve(ei->icu);
259                                 if(first) {
260                                         si->v2d.tot= ei->icu->totrct;
261                                         first= 0;
262                                 }
263                                 else BLI_union_rctf(&(si->v2d.tot), &(ei->icu->totrct));
264                         }
265                 }
266         }
267         
268
269         v2d= &(si->v2d);        
270
271         /* keylines? */
272         if(si->blocktype==ID_KE) {
273                 key= ob_get_key((Object *)G.sipo->from);
274                 if(key && key->block.first) {
275                         kb= key->block.first;
276                         if(kb->pos < v2d->tot.ymin) v2d->tot.ymin= kb->pos;
277                         kb= key->block.last;
278                         if(kb->pos > v2d->tot.ymax) v2d->tot.ymax= kb->pos;
279                 }
280         }
281         
282         /* is there no curve? */
283         if(first) {
284                 v2d->tot.xmin= 0.0;
285                 v2d->tot.xmax= EFRA;
286                 v2d->tot.ymin= (float)-0.1;
287                 v2d->tot.ymax= (float)1.1;
288         
289                 if(si->blocktype==ID_SEQ) {
290                         v2d->tot.xmin= -5.0;
291                         v2d->tot.xmax= 105.0;
292                         v2d->tot.ymin= (float)-0.1;
293                         v2d->tot.ymax= (float)1.1;
294                 }
295         }
296         
297         si->tot= v2d->tot;      
298         
299         if(doredraw) {
300                 /* if you always call do_ipo: you get problems with insertkey, for example
301                  * when inserting only a 'loc' the 'ob->rot' value then is changed.
302                  */
303
304                 if(si->blocktype==ID_OB) {                      
305                                 /* clear delta loc,rot,size (when free/delete ipo) */
306                         clear_delta_obipo(si->ipo);
307                         
308                 }
309         
310                 do_ipo(si->ipo);
311
312                 allqueue(REDRAWIPO, 0);
313                 allqueue(REDRAWACTION, 0);
314                 allqueue(REDRAWTIME, 0);
315                 allqueue(REDRAWNLA, 0);
316                 allqueue(REDRAWBUTSOBJECT, 0);
317                 
318                 if(si->blocktype==ID_OB) {
319                         Object *ob= (Object *)si->from;                 
320                         if(ob) DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
321                         allqueue(REDRAWVIEW3D, 0);
322                         allqueue(REDRAWNLA, 0);
323                 }
324
325                 else if(si->blocktype==ID_MA) allqueue(REDRAWBUTSSHADING, 0);
326                 else if(si->blocktype==ID_TE) allqueue(REDRAWBUTSSHADING, 0);
327                 else if(si->blocktype==ID_WO) allqueue(REDRAWBUTSSHADING, 0);
328                 else if(si->blocktype==ID_LA) allqueue(REDRAWBUTSSHADING, 0);
329 //              else if(si->blocktype==ID_SO) allqueue(REDRAWBUTSSOUND, 0);
330                 else if(si->blocktype==ID_CA) {
331                         allqueue(REDRAWBUTSEDIT, 0);
332                         allqueue(REDRAWVIEW3D, 0);
333                 }
334                 else if(si->blocktype==ID_SEQ) free_imbuf_seq_with_ipo(si->ipo);
335                 else if(si->blocktype==ID_PO) {
336                         Object *ob= OBACT;
337                         if(ob && ob->pose) {
338                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
339                         }
340                         allqueue(REDRAWVIEW3D, 0);
341                         allqueue(REDRAWACTION, 0);
342                         allqueue(REDRAWNLA, 0);
343                 }
344                 else if(si->blocktype==ID_KE) {
345                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
346                         allqueue(REDRAWVIEW3D, 0);
347                 }
348                 else if(si->blocktype==ID_CU) {
349                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
350                         allqueue(REDRAWVIEW3D, 0);
351                 }
352                 else if(si->blocktype==ID_PA){
353                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
354                         allqueue(REDRAWVIEW3D, 0);
355                 }
356         }
357
358         if(si->showkey) make_ipokey();
359         
360         if(si->actname[0])
361                 synchronize_action_strips();
362 }
363
364 void scale_editipo(void)
365 {
366         /* comes from buttons, scale with G.sipo->tot rect */
367         
368         EditIpo *ei;
369         BezTriple *bezt;
370         float facx, facy;
371         int a, b;       
372         
373         facx= (G.sipo->tot.xmax-G.sipo->tot.xmin)/(G.sipo->v2d.tot.xmax-G.sipo->v2d.tot.xmin);
374         facy= (G.sipo->tot.ymax-G.sipo->tot.ymin)/(G.sipo->v2d.tot.ymax-G.sipo->v2d.tot.ymin);
375
376         ei= G.sipo->editipo;
377         if(ei==0) return;
378         for(a=0; a<G.sipo->totipo; a++, ei++) {
379                 if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
380                         bezt= ei->icu->bezt;
381                         b= ei->icu->totvert;
382                         while(b--) {
383                                 
384                                 bezt->vec[0][0]= facx*(bezt->vec[0][0] - G.sipo->v2d.tot.xmin) + G.sipo->tot.xmin;
385                                 bezt->vec[1][0]= facx*(bezt->vec[1][0] - G.sipo->v2d.tot.xmin) + G.sipo->tot.xmin;
386                                 bezt->vec[2][0]= facx*(bezt->vec[2][0] - G.sipo->v2d.tot.xmin) + G.sipo->tot.xmin;
387                         
388                                 bezt->vec[0][1]= facy*(bezt->vec[0][1] - G.sipo->v2d.tot.ymin) + G.sipo->tot.ymin;
389                                 bezt->vec[1][1]= facy*(bezt->vec[1][1] - G.sipo->v2d.tot.ymin) + G.sipo->tot.ymin;
390                                 bezt->vec[2][1]= facy*(bezt->vec[2][1] - G.sipo->v2d.tot.ymin) + G.sipo->tot.ymin;
391
392                                 bezt++;
393                         }
394                 }
395         }
396
397         editipo_changed(G.sipo, 1);
398
399         BIF_undo_push("Scale Edit Ipo");
400         allqueue(REDRAWNLA, 0);
401         allqueue (REDRAWACTION, 0);
402         allqueue(REDRAWIPO, 0);
403 }
404
405 static void make_ob_editipo(Object *ob, SpaceIpo *si)
406 {
407         EditIpo *ei;
408         int a, len, colipo=0;
409         char *name;
410         
411         if(ob->type==OB_MESH) colipo= 1;
412
413         ei= si->editipo= MEM_callocN(OB_TOTIPO*sizeof(EditIpo), "editipo");
414         
415         si->totipo= OB_TOTIPO;
416         
417         for(a=0; a<OB_TOTIPO; a++) {
418                 name = getname_ob_ei(ob_ar[a], colipo);
419                 strcpy(ei->name, name);
420                 ei->adrcode= ob_ar[a];
421                 
422                 if ELEM6(ei->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z, OB_DROT_X, OB_DROT_Y, OB_DROT_Z) ei->disptype= IPO_DISPDEGR;
423                 else if(ei->adrcode==OB_LAY) ei->disptype= IPO_DISPBITS;
424                 else if(ei->adrcode==OB_TIME) ei->disptype= IPO_DISPTIME;
425
426                 ei->col= ipo_rainbow(a, OB_TOTIPO);
427
428                 if(colipo) {
429                         len= strlen(ei->name);
430                         if(len) {
431                                 if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
432                                 else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
433                                 else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
434                         }
435                 }
436                 
437                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
438                 if(ei->icu) {
439                         ei->flag= ei->icu->flag;
440                 }
441                 
442                 
443                 ei++;
444         }
445         //fprintf(stderr,"FSIMAKE_OPBJ call %d \n", si->totipo);
446 }
447
448 static void make_part_editipo(SpaceIpo *si)
449 {
450         EditIpo *ei;
451         int a;
452         char *name;
453         
454         if(si->from==0) return;
455         
456         ei= si->editipo= MEM_callocN(PART_TOTIPO*sizeof(EditIpo), "editipo");
457         
458         si->totipo= PART_TOTIPO;
459         
460         for(a=0; a<PART_TOTIPO; a++) {
461                 name = getname_part_ei(part_ar[a]);
462                 strcpy(ei->name, name);
463                 ei->adrcode= part_ar[a];
464                 
465                 //if(ei->adrcode & MA_MAP1) {
466                 //      ei->adrcode-= MA_MAP1;
467                 //      ei->adrcode |= texchannel_to_adrcode(si->channel);
468                 //}
469                 //else {
470                 //      if(ei->adrcode==MA_MODE) ei->disptype= IPO_DISPBITS;
471                 //}
472                 
473                 ei->col= ipo_rainbow(a, PART_TOTIPO);
474                 
475                 //len= strlen(ei->name);
476                 //if(len) {
477                 //      if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
478                 //      else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
479                 //      else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
480                 //}
481                 
482                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
483                 if(ei->icu) {
484                         ei->flag= ei->icu->flag;
485                 }
486                 
487                 ei++;
488         }
489 }
490
491 // copied from make_seq_editipo
492 static void make_fluidsim_editipo(SpaceIpo *si, Object *ob) // NT
493 {
494         EditIpo *ei;
495         int a;
496         char *name;
497         FluidsimSettings *fss= ob->fluidsimSettings;
498         int numipos = FLUIDSIM_TOTIPO;
499         int ipo_start_index = 0;
500         
501         // we don't need all fluid ipos for all types! - dg
502         if(fss->type == OB_FLUIDSIM_CONTROL)
503         {
504                 numipos = 4; // there are 4 fluid control ipos
505                 ipo_start_index = 9;
506                 
507         }
508         else if(fss->type == OB_FLUIDSIM_DOMAIN)
509         {
510                 numipos = 5; // there are 5 ipos for fluid domains
511         }
512         else
513         {
514                 numipos = 4; // there are 4 for the rest
515                 ipo_start_index = 5;
516         }
517                 
518         ei= si->editipo= MEM_callocN(numipos*sizeof(EditIpo), "fluidsim_editipo");
519         si->totipo = numipos;
520         for(a=ipo_start_index; a<numipos; a++) {
521                 //fprintf(stderr,"FSINAME %d %d \n",a,fluidsim_ar[a], (int)(getname_fluidsim_ei(fluidsim_ar[a]))  );
522                 name = getname_fluidsim_ei(fluidsim_ar[a]);
523                 strcpy(ei->name, name);
524                 ei->adrcode= fluidsim_ar[a];
525                 ei->col= ipo_rainbow(a, numipos);
526                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
527                 if(ei->icu) {
528                         ei->flag = ei->icu->flag;
529                 } 
530                 //else { ei->flag |= IPO_VISIBLE; }
531                 //fprintf(stderr,"FSIMAKE eif%d,icuf%d icu%d %d|%d\n", ei->flag,ei->icu->flag, (int)ei->icu, IPO_VISIBLE,IPO_SELECT);
532                 //fprintf(stderr,"FSIMAKE eif%d icu%d %d|%d\n", ei->flag, (int)ei->icu, IPO_VISIBLE,IPO_SELECT);
533                 ei++;
534         }
535 }
536
537 static void make_seq_editipo(SpaceIpo *si)
538 {
539         EditIpo *ei;
540         int a;
541         char *name;
542         
543         ei= si->editipo= MEM_callocN(SEQ_TOTIPO*sizeof(EditIpo), "editipo");
544         
545         si->totipo= SEQ_TOTIPO;
546         
547         
548         for(a=0; a<SEQ_TOTIPO; a++) {
549                 name = getname_seq_ei(seq_ar[a]);
550                 strcpy(ei->name, name);
551                 ei->adrcode= seq_ar[a];
552                 
553                 ei->col= ipo_rainbow(a, SEQ_TOTIPO);
554                 
555                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
556                 if(ei->icu) {
557                         ei->flag= ei->icu->flag;
558                 }
559                 else ei->flag |= IPO_VISIBLE;
560                 
561                 ei++;
562         }
563 }
564
565 static void make_cu_editipo(SpaceIpo *si)
566 {
567         EditIpo *ei;
568         int a;
569         char *name;
570         
571         ei= si->editipo= MEM_callocN(CU_TOTIPO*sizeof(EditIpo), "editipo");
572         
573         si->totipo= CU_TOTIPO;
574         
575         
576         for(a=0; a<CU_TOTIPO; a++) {
577                 name = getname_cu_ei(cu_ar[a]);
578                 strcpy(ei->name, name);
579                 ei->adrcode= cu_ar[a];
580                 
581                 ei->col= ipo_rainbow(a, CU_TOTIPO);
582                 
583                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
584                 if(ei->icu) {
585                         ei->flag= ei->icu->flag;
586                 }
587                 else ei->flag |= IPO_VISIBLE;
588                  
589                 ei++;
590         }
591 }
592
593 static void make_key_editipo(SpaceIpo *si)
594 {
595         Key *key;
596         KeyBlock *kb=NULL;
597         EditIpo *ei;
598         int a;
599         
600         key= ob_get_key((Object *)G.sipo->from);
601         if(key==NULL) return;
602         
603         si->totipo= BLI_countlist(&key->block);
604         ei= si->editipo= MEM_callocN(si->totipo*sizeof(EditIpo), "editipo");
605         
606         for(a=0, kb= key->block.first; a<si->totipo; a++, ei++, kb= kb->next) {
607                 
608                 if(kb->name[0] != 0) strncpy(ei->name, kb->name, 31);   // length both same
609                 ei->adrcode= kb->adrcode;
610                 
611                 ei->col= ipo_rainbow(a, KEY_TOTIPO);
612                 
613                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
614                 if(ei->icu) {
615                         ei->flag= ei->icu->flag;
616                 }
617                 else if(a==0) 
618                         if(key && key->type==KEY_NORMAL)
619                                 ei->flag |= IPO_VISIBLE;
620                 
621                 /* active ipo is tied to active shape  */
622                 {
623                         Object *ob= OBACT;
624                         if(a==ob->shapenr-1)
625                                 set_active_editipo(ei);
626                 }
627         }
628         
629         ei= si->editipo;
630         if(key && key->type==KEY_RELATIVE) {
631                 strcpy(ei->name, "----");
632         }
633         else {
634                 ei->flag |= IPO_VISIBLE;
635         }
636 }
637
638 static void make_mat_editipo(SpaceIpo *si)
639 {
640         EditIpo *ei;
641         int a, len;
642         char *name;
643         
644         if(si->from==0) return;
645         
646         ei= si->editipo= MEM_callocN(MA_TOTIPO*sizeof(EditIpo), "editipo");
647         
648         si->totipo= MA_TOTIPO;
649         
650         for(a=0; a<MA_TOTIPO; a++) {
651                 name = getname_mat_ei(ma_ar[a]);
652                 strcpy(ei->name, name);
653                 ei->adrcode= ma_ar[a];
654                 
655                 if(ei->adrcode & MA_MAP1) {
656                         ei->adrcode-= MA_MAP1;
657                         ei->adrcode |= texchannel_to_adrcode(si->channel);
658                 }
659                 else {
660                         if(ei->adrcode==MA_MODE) ei->disptype= IPO_DISPBITS;
661                 }
662                 
663                 ei->col= ipo_rainbow(a, MA_TOTIPO);
664                 
665                 len= strlen(ei->name);
666                 if(len) {
667                         if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
668                         else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
669                         else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
670                 }
671                 
672                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
673                 if(ei->icu) {
674                         ei->flag= ei->icu->flag;
675                 }
676                 
677                 ei++;
678         }
679 }
680
681 static void make_texture_editipo(SpaceIpo *si)
682 {
683         EditIpo *ei;
684         int a, len;
685         char *name;
686         
687         if(si->from==0) return;    
688         
689         ei= si->editipo= MEM_callocN(TE_TOTIPO*sizeof(EditIpo), "editipo");
690         
691         si->totipo= TE_TOTIPO;
692         
693         for(a=0; a<TE_TOTIPO; a++) {
694                 name = getname_tex_ei(te_ar[a]);
695                 strcpy(ei->name, name);
696                         ei->adrcode= te_ar[a];
697
698                 ei->col= ipo_rainbow(a, TE_TOTIPO);
699         
700                 len= strlen(ei->name);
701                 if(len) {
702                         if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
703                         else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
704                         else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
705                 }       
706                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
707                 if(ei->icu) {
708                         ei->flag= ei->icu->flag;
709                 }
710                 
711                 ei++;
712         }
713 }
714
715 static void make_world_editipo(SpaceIpo *si)
716 {
717         EditIpo *ei;
718         int a, len;
719         char *name;
720         
721         if(si->from==0) return;
722         
723         ei= si->editipo= MEM_callocN(WO_TOTIPO*sizeof(EditIpo), "editipo");
724         
725         si->totipo= WO_TOTIPO;
726         
727         for(a=0; a<WO_TOTIPO; a++) {
728                 name = getname_world_ei(wo_ar[a]);
729                 
730                 strcpy(ei->name, name); 
731                 ei->adrcode= wo_ar[a];
732                 
733                 if(ei->adrcode & MA_MAP1) {
734                         ei->adrcode-= MA_MAP1;
735                         ei->adrcode |= texchannel_to_adrcode(si->channel);
736                 }
737                 else {
738                         if(ei->adrcode==MA_MODE) ei->disptype= IPO_DISPBITS;
739                 }
740                 
741                 ei->col= ipo_rainbow(a, WO_TOTIPO);
742                 
743                 len= strlen(ei->name);
744                 if(len) {
745                         if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
746                         else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
747                         else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
748                 }
749                 
750                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
751                 if(ei->icu) {
752                         ei->flag= ei->icu->flag;
753                 }
754                 
755                 ei++;
756         }
757 }
758
759 static void make_lamp_editipo(SpaceIpo *si)
760 {
761         EditIpo *ei;
762         int a;
763         char *name;
764         
765         ei= si->editipo= MEM_callocN(LA_TOTIPO*sizeof(EditIpo), "editipo");
766         
767         si->totipo= LA_TOTIPO;
768         
769         for(a=0; a<LA_TOTIPO; a++) {
770                 name = getname_la_ei(la_ar[a]);
771                 strcpy(ei->name, name);
772                 ei->adrcode= la_ar[a];
773
774                 if(ei->adrcode & MA_MAP1) {
775                         ei->adrcode-= MA_MAP1;
776                         ei->adrcode |= texchannel_to_adrcode(si->channel);
777                 }
778
779                 ei->col= ipo_rainbow(a, LA_TOTIPO);
780                 
781                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
782                 if(ei->icu) {
783                         ei->flag= ei->icu->flag;
784                 }
785                 
786                 ei++;
787         }
788 }
789
790 static void make_camera_editipo(SpaceIpo *si)
791 {
792         EditIpo *ei;
793         int a;
794         char *name;
795         
796         ei= si->editipo= MEM_callocN(CAM_TOTIPO*sizeof(EditIpo), "editipo");
797         
798         si->totipo= CAM_TOTIPO;
799         
800         
801         for(a=0; a<CAM_TOTIPO; a++) {
802                 name = getname_cam_ei(cam_ar[a]);
803                 strcpy(ei->name, name);
804                 ei->adrcode= cam_ar[a];
805
806                 ei->col= ipo_rainbow(a, CAM_TOTIPO);
807                 
808                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
809                 if(ei->icu) {
810                         ei->flag= ei->icu->flag;
811                 }
812                 
813                 ei++;
814         }
815 }
816
817 static int make_constraint_editipo(Ipo *ipo, EditIpo **si)
818 {
819         EditIpo *ei;
820         int a;
821         char *name;
822         
823         ei= *si= MEM_callocN(CO_TOTIPO*sizeof(EditIpo), "editipo");
824         
825         for(a=0; a<CO_TOTIPO; a++) {
826                 name = getname_co_ei(co_ar[a]);
827                 strcpy(ei->name, name);
828                 ei->adrcode= co_ar[a];
829
830                 ei->col= ipo_rainbow(a, CO_TOTIPO);
831                 
832                 ei->icu= find_ipocurve(ipo, ei->adrcode);
833                 if(ei->icu) {
834                         ei->flag= ei->icu->flag;
835                 }
836                 
837                 ei++;
838         }
839
840         return CO_TOTIPO;
841 }
842
843 static int make_bone_editipo(Ipo *ipo, EditIpo **si)
844 {
845         EditIpo *ei;
846         int a;
847         char *name;
848         
849         ei= *si= MEM_callocN(AC_TOTIPO*sizeof(EditIpo), "editipo");
850         
851         for(a=0; a<AC_TOTIPO; a++) {
852                 name = getname_ac_ei(ac_ar[a]);
853                 strcpy(ei->name, name);
854                 ei->adrcode= ac_ar[a];
855
856                 ei->col= ipo_rainbow(a, AC_TOTIPO);
857                 
858                 ei->icu= find_ipocurve(ipo, ei->adrcode);
859                 if(ei->icu) {
860                         ei->flag= ei->icu->flag;
861                 }
862                 
863                 ei++;
864         }
865
866         return AC_TOTIPO;
867 }
868
869 static void make_sound_editipo(SpaceIpo *si)
870 {
871         EditIpo *ei;
872         int a;
873         char *name;
874         
875         ei= si->editipo= MEM_callocN(SND_TOTIPO*sizeof(EditIpo), "editipo");
876         
877         si->totipo= SND_TOTIPO;
878         
879         
880         for(a=0; a<SND_TOTIPO; a++) {
881                 name = getname_snd_ei(snd_ar[a]);
882                 strcpy(ei->name, name);
883                 ei->adrcode= snd_ar[a];
884
885                 ei->col= ipo_rainbow(a, SND_TOTIPO);
886                 
887                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
888                 if(ei->icu) {
889                         ei->flag= ei->icu->flag;
890                 }
891                 
892                 ei++;
893         }
894 }
895
896 /* only called in test_editipo() below */
897 static void make_editipo(void)
898 {
899         EditIpo *ei;
900         Object *ob;
901         rctf *rf;
902         int a;
903
904         if(G.sipo->editipo)
905                 MEM_freeN(G.sipo->editipo);
906         
907         G.sipo->editipo= NULL;
908         G.sipo->totipo= 0;
909         
910         if(G.sipo->from==NULL) return;
911         
912         ob= OBACT;
913
914         if(G.sipo->ipo) G.sipo->showkey= G.sipo->ipo->showkey;
915
916         if(G.sipo->blocktype==ID_SEQ) {
917                 make_seq_editipo(G.sipo);
918         }
919         else if(G.sipo->blocktype==ID_WO) {
920                 make_world_editipo(G.sipo);
921         } 
922         else if(G.sipo->blocktype==ID_OB) {
923                 if (ob) {
924                         ob->ipowin= ID_OB;
925                         make_ob_editipo(ob, G.sipo);
926                 }
927         }
928         else if(G.sipo->blocktype==ID_MA) {
929                 if (ob) {
930                         ob->ipowin= ID_MA;
931                         make_mat_editipo(G.sipo);
932                 }
933         }
934         else if(G.sipo->blocktype==ID_CU) {
935                 if (ob) {
936                         ob->ipowin= ID_CU;
937                         make_cu_editipo(G.sipo);
938                 }
939         }
940         else if(G.sipo->blocktype==ID_KE) {
941                 if (ob) {
942                         ob->ipowin= ID_KE;
943                         make_key_editipo(G.sipo);
944                 }
945         }
946         else if(G.sipo->blocktype==ID_LA) {
947                 if (ob) {
948                         ob->ipowin= ID_LA;
949                         make_lamp_editipo(G.sipo);
950                 }
951         }
952         else if(G.sipo->blocktype==ID_TE) {
953                 if (ob) {
954                         ob->ipowin= ID_TE;
955                         make_texture_editipo(G.sipo);
956                 }
957         }
958         else if(G.sipo->blocktype==ID_CA) {
959                 if (ob) {
960                         ob->ipowin= ID_CA;
961                         make_camera_editipo(G.sipo);
962                 }
963         }
964         else if(G.sipo->blocktype==ID_SO) {
965                 if (ob) {
966                         ob->ipowin= ID_SO;
967                         make_sound_editipo(G.sipo);
968                 }
969         }
970         else if(G.sipo->blocktype==ID_CO){
971                 G.sipo->totipo = make_constraint_editipo(G.sipo->ipo, (EditIpo**)&G.sipo->editipo);
972                 if (ob) {
973                         ob->ipowin= ID_CO;
974                 }
975         }
976         else if(G.sipo->blocktype==ID_PO) {
977
978                 G.sipo->totipo = make_bone_editipo(G.sipo->ipo, (EditIpo**)&G.sipo->editipo);
979                 if (ob) {
980                         ob->ipowin= ID_PO;
981                 }
982         }
983         else if(G.sipo->blocktype==ID_FLUIDSIM) {
984                 if (ob) { // NT
985                         ob->ipowin= ID_FLUIDSIM;
986                         make_fluidsim_editipo(G.sipo, ob);
987                 }
988         }
989         else if(G.sipo->blocktype==ID_PA) {
990                 if (ob) {
991                         ob->ipowin= ID_PA;
992                         make_part_editipo(G.sipo);
993                 }
994         }
995
996         if(G.sipo->editipo==0) return;
997         
998         ei= G.sipo->editipo;
999         for(a=0; a<G.sipo->totipo; a++, ei++) {
1000                 if(ei->icu) ei->icu->flag= ei->flag;
1001         }
1002         editipo_changed(G.sipo, 0);
1003         
1004         /* sets globals, bad stuff but we need these variables in other parts of code */
1005         get_status_editipo();
1006         
1007         
1008         if (G.sipo->flag & SIPO_LOCK_VIEW) {
1009                 rf= &(G.v2d->cur); /* view is locked, dont move it, just to sanity check */
1010                 if(rf->xmin>=rf->xmax || rf->ymin>=rf->ymax) ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
1011         } else {
1012                 if(G.sipo->ipo) {
1013                         if (G.sipo->pin)
1014                                 rf= &(G.sipo->v2d.cur);
1015                         else
1016                                 rf= &(G.sipo->ipo->cur);
1017                         
1018                         if(rf->xmin<rf->xmax && rf->ymin<rf->ymax) G.v2d->cur= *rf;
1019                         else ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
1020                 }
1021                 else {
1022                         ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
1023                 }
1024         }
1025         view2d_do_locks(curarea, V2D_LOCK_COPY);
1026 }
1027
1028 /* evaluates context in the current UI */
1029 /* blocktype is type of ipo */
1030 /* from is the base pointer to find data to change (ob in case of action or pose) */
1031 /* bonename is for local bone ipos (constraint only now) */
1032 static void get_ipo_context(short blocktype, ID **from, Ipo **ipo, char *actname, char *constname, char *bonename)
1033 {
1034         Object *ob= OBACT;
1035         
1036         *from= NULL;
1037         *ipo= NULL;
1038         
1039         if(blocktype==ID_CO) {
1040                 if (ob) {
1041                         bConstraintChannel *chan;
1042                         bConstraint *con= get_active_constraint(ob);
1043                         
1044                         if(con) {
1045                                 *from= &ob->id;
1046                                 
1047                                 BLI_strncpy(constname, con->name, 32);
1048                                 
1049                                 /* a bit hackish, but we want con->ipo to work */
1050                                 if(con->flag & CONSTRAINT_OWN_IPO) {
1051                                         if(ob->flag & OB_POSEMODE) {
1052                                                 bPoseChannel *pchan= get_active_posechannel(ob);
1053                                                 if(pchan) {
1054                                                         BLI_strncpy(bonename, pchan->name, 32);
1055                                                         *ipo= con->ipo;
1056                                                 }
1057                                         }
1058                                 }
1059                                 else {
1060                                         chan= get_active_constraint_channel(ob);
1061                                         if(chan) {
1062                                                 *ipo= chan->ipo;
1063                                                 BLI_strncpy(constname, con->name, 32);
1064                                         }
1065                                         
1066                                         /* set actname if in posemode */
1067                                         if (ob->action) {
1068                                                 if (ob->flag & OB_POSEMODE) {
1069                                                         bPoseChannel *pchan= get_active_posechannel(ob);
1070                                                         if (pchan) {
1071                                                                 BLI_strncpy(actname, pchan->name, 32);
1072                                                                 BLI_strncpy(bonename, pchan->name, 32);
1073                                                         }
1074                                                 }
1075                                                 else if (ob->ipoflag & OB_ACTION_OB)
1076                                                         strcpy(actname, "Object");
1077                                         }
1078                                         else {
1079                                                 if (ob->flag & OB_POSEMODE) {
1080                                                         bPoseChannel *pchan= get_active_posechannel(ob);
1081                                                         if (pchan) {
1082                                                                 BLI_strncpy(actname, pchan->name, 32);
1083                                                                 BLI_strncpy(bonename, pchan->name, 32);
1084                                                         }
1085                                                 }
1086                                         }
1087                                 }
1088                         }
1089                 }
1090         }
1091         else if(blocktype==ID_PO) {
1092                 if (ob && ob->action && ob->type==OB_ARMATURE) {
1093                         bPoseChannel *pchan= get_active_posechannel(ob);
1094                         
1095                         *from= (ID *)ob;
1096                         if (pchan) {
1097                                 bActionChannel *achan;
1098                                 
1099                                 BLI_strncpy(actname, pchan->name, 32);  /* also set when no channel yet */
1100                                 
1101                                 achan= get_action_channel(ob->action, pchan->name);
1102                                 if(achan)
1103                                         *ipo= achan->ipo;
1104                         }
1105                 } 
1106                 
1107         }
1108         else if(blocktype==ID_OB) {
1109                 if(ob) {
1110                         *from= (ID *)ob;
1111                         if(ob->ipoflag & OB_ACTION_OB) {
1112                                 if (ob->action) {
1113                                         bActionChannel *achan= get_action_channel(ob->action, "Object");
1114                                         if(achan) {
1115                                                 *ipo= achan->ipo;
1116                                                 BLI_strncpy(actname, achan->name, 32);
1117                                         }
1118                                 }
1119                         }
1120                         else {
1121                                 *ipo= ob->ipo;
1122                         }
1123                 }
1124         }
1125         else if(blocktype==ID_SEQ) {
1126                 Sequence *last_seq = get_last_seq();
1127                 
1128                 if(last_seq) {
1129                         *from= (ID *)last_seq;
1130                         *ipo= last_seq->ipo;
1131                 }
1132         }
1133         else if(blocktype==ID_WO) {
1134                 World *wo= G.scene->world;
1135                 *from= (ID *)wo;
1136                 if(wo) *ipo= wo->ipo;
1137         }
1138         else if(blocktype==ID_TE) {
1139                 if(ob) {
1140                         Tex *tex= give_current_texture(ob, ob->actcol);
1141                         *from= (ID *)tex;
1142                         if(tex) *ipo= tex->ipo;
1143                 }
1144         }
1145         else if(blocktype==ID_MA) {
1146                 if(ob) {
1147                         Material *ma= give_current_material(ob, ob->actcol);
1148                         ma= editnode_get_active_material(ma);
1149                         *from= (ID *)ma;
1150                         if(ma) *ipo= ma->ipo;
1151                 }
1152         }
1153         else if(blocktype==ID_KE) {
1154                 if(ob) {
1155                         Key *key= ob_get_key(ob);
1156                         
1157                         if(ob->ipoflag & OB_ACTION_KEY) {
1158                                 if (ob->action) {
1159                                         bActionChannel *achan= get_action_channel(ob->action, "Shape");
1160                                         if(achan) {
1161                                                 *ipo= achan->ipo;
1162                                                 BLI_strncpy(actname, achan->name, 32);
1163                                         }
1164                                 }
1165                         }
1166                         else if(key) *ipo= key->ipo;
1167                         
1168                         *from= (ID *)ob;
1169                 }
1170         }
1171         else if(blocktype==ID_CU) {
1172                 if(ob && ob->type==OB_CURVE) {
1173                         Curve *cu= ob->data;
1174                         *from= (ID *)cu;
1175                         *ipo= cu->ipo;
1176                 }
1177         }
1178         else if(blocktype==ID_LA) {
1179                 if(ob && ob->type==OB_LAMP) {
1180                         Lamp *la= ob->data;
1181                         *from= (ID *)la;
1182                         *ipo= la->ipo;
1183                 }
1184         }
1185         else if(blocktype==ID_CA) {
1186                 if(ob && ob->type==OB_CAMERA) {
1187                         Camera *ca= ob->data;
1188                         *from= (ID *)ca;
1189                         if(ca) *ipo= ca->ipo;
1190                 }
1191         }
1192         else if(blocktype==ID_SO) {
1193                 
1194                 //              if (G.buts && G.buts->mainb == BUTS_SOUND) {
1195                 //                      bSound *sound = G.buts->lockpoin;
1196                 //                      *from= (ID *)sound;
1197                 //                      if(sound) *ipo= sound->ipo;
1198                 //              }
1199         }
1200         else if(blocktype==ID_FLUIDSIM) {
1201                 if(ob && ( ob->fluidsimFlag & OB_FLUIDSIM_ENABLE)) {
1202                         FluidsimSettings *fss= ob->fluidsimSettings;
1203                         *from= (ID *)ob;
1204                         if(fss) *ipo= fss->ipo;
1205                 }
1206         }
1207         else if(blocktype==ID_PA) {
1208                 ParticleSystem *psys = psys_get_current(ob);
1209                 if(psys){
1210                         *from= (ID *)ob;
1211                         *ipo= psys->part->ipo;
1212                 }
1213         }
1214 }
1215
1216 /* called on each redraw, check if editipo data has to be remade */
1217 /* if doit already set, it always makes (in case no ipo exists, we need to see the channels */
1218 void test_editipo(int doit)
1219 {
1220         
1221         if(G.sipo->pin==0) {
1222                 Ipo *ipo;
1223                 ID *from;
1224                 char actname[32]="", constname[32]="", bonename[32]="";
1225                 
1226                 get_ipo_context(G.sipo->blocktype, &from, &ipo, actname, constname, bonename);
1227                 
1228                 if(G.sipo->ipo != ipo) {
1229                         G.sipo->ipo= ipo;
1230                         /* if lock we don't copy from ipo, this makes the UI jump around confusingly */
1231                         if(G.v2d->flag & V2D_VIEWLOCK || G.sipo->flag & SIPO_LOCK_VIEW);
1232                         else if(ipo) G.v2d->cur= ipo->cur;
1233                         doit= 1;
1234                 }
1235                 if(G.sipo->from != from) {
1236                         G.sipo->from= from;
1237                         doit= 1;
1238                 }
1239                 if( strcmp(G.sipo->actname, actname)) {
1240                         BLI_strncpy(G.sipo->actname, actname, 32);
1241                         doit= 1;
1242                 }
1243                 if( strcmp(G.sipo->constname, constname)) {
1244                         BLI_strncpy(G.sipo->constname, constname, 32);
1245                         doit= 1;
1246                 }
1247                 if( strcmp(G.sipo->bonename, bonename)) {
1248                         BLI_strncpy(G.sipo->bonename, bonename, 32);
1249                         /* urmf; if bonename, then no action */
1250                         if(bonename[0]) G.sipo->actname[0]= 0;
1251                         doit= 1;
1252                 }
1253                 
1254                 if(G.sipo->ipo)
1255                         G.sipo->ipo->cur = G.v2d->cur;
1256                 
1257         }
1258                 
1259         if(G.sipo->editipo==NULL || doit) {
1260                 make_editipo();
1261         }
1262 }
1263
1264 /* ****************** EditIpo ************************ */
1265
1266 int totipo_edit=0, totipo_sel=0, totipo_curve=0, totipo_vis=0, totipo_vert=0, totipo_vertsel=0, totipo_key=0, totipo_keysel=0;
1267
1268 void get_status_editipo(void)
1269 {
1270         EditIpo *ei;
1271         IpoKey *ik;
1272         BezTriple *bezt;
1273         int a, b;
1274         
1275         totipo_vis= 0;
1276         totipo_curve= 0;
1277         totipo_sel= 0;
1278         totipo_edit= 0;
1279         totipo_vert= 0;
1280         totipo_vertsel= 0;
1281         totipo_key= 0;
1282         totipo_keysel= 0;
1283         
1284         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1285         
1286         ei= G.sipo->editipo;
1287         if(ei==0) return;
1288         for(a=0; a<G.sipo->totipo; a++) {
1289                 if( ei->flag & IPO_VISIBLE ) {
1290                         totipo_vis++;
1291                         if(ei->flag & IPO_SELECT) totipo_sel++;
1292                         if(ei->icu && ei->icu->totvert) totipo_curve++;
1293                         if(G.sipo->showkey || (ei->flag & IPO_EDIT)) {
1294                                 
1295                                 /* if showkey: do count the vertices (for grab) */
1296                                 if(G.sipo->showkey==0) totipo_edit++;
1297                                 
1298                                 if(ei->icu) {
1299                                         if(ei->icu->bezt) {
1300                                                 bezt= ei->icu->bezt;
1301                                                 b= ei->icu->totvert;
1302                                                 while(b--) {
1303                                                         if(ei->icu->ipo==IPO_BEZ) {
1304                                                                 if(bezt->f1 & SELECT) totipo_vertsel++;
1305                                                                 if(bezt->f3 & SELECT) totipo_vertsel++;
1306                                                                 totipo_vert+= 2;
1307                                                         }
1308                                                         if(bezt->f2 & SELECT) totipo_vertsel++;
1309                                                         
1310                                                         totipo_vert++;
1311                                                         bezt++;
1312                                                 }
1313                                         }
1314                                 }
1315                         }
1316                 }
1317                 ei++;
1318         }
1319         
1320         if(G.sipo->showkey) {
1321                 ik= G.sipo->ipokey.first;
1322                 while(ik) {
1323                         totipo_key++;
1324                         if(ik->flag & 1) totipo_keysel++;
1325                         ik= ik->next;
1326                 }
1327         }
1328 }
1329
1330 /* synchronize editipo flag with icu flag and ipokey flags */
1331 void update_editipo_flags(void)
1332 {
1333         EditIpo *ei;
1334         IpoKey *ik;
1335         int a;
1336         
1337         ei= G.sipo->editipo;
1338         if(ei) {
1339                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1340                         if(ei->icu) ei->icu->flag= ei->flag;
1341                 }
1342         }
1343         if(G.sipo->showkey) {
1344                 ik= G.sipo->ipokey.first;
1345                 while(ik) {
1346                         for(a=0; a<G.sipo->totipo; a++) {
1347                                 if(ik->data[a]) {
1348                                         if(ik->flag & 1) {
1349                                                 ik->data[a]->f1 |= SELECT;
1350                                                 ik->data[a]->f2 |= SELECT;
1351                                                 ik->data[a]->f3 |= SELECT;
1352                                         }
1353                                         else {
1354                                                 ik->data[a]->f1 &= ~SELECT;
1355                                                 ik->data[a]->f2 &= ~SELECT;
1356                                                 ik->data[a]->f3 &= ~SELECT;
1357                                         }
1358                                 }
1359                         }
1360                         ik= ik->next;
1361                 }
1362         }
1363 }
1364
1365 /* sort of enter/leave editmode for curves */
1366 void set_editflag_editipo(void)
1367 {
1368         EditIpo *ei;
1369         int a; /*  , tot= 0, ok= 0; */
1370         
1371         /* after showkey immediately go to editing of selected points */
1372         if(G.sipo->showkey) {
1373                 G.sipo->showkey= 0;
1374                 if(G.sipo->ipo) G.sipo->ipo->showkey= 0;
1375                 ei= G.sipo->editipo;
1376                 for(a=0; a<G.sipo->totipo; a++, ei++) ei->flag |= IPO_SELECT;
1377                 scrarea_queue_headredraw(curarea);
1378                 allqueue(REDRAWVIEW3D, 0);
1379         }
1380         
1381         get_status_editipo();
1382         
1383         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1384         
1385         ei= G.sipo->editipo;
1386         for(a=0; a<G.sipo->totipo; a++, ei++) {         
1387                 if(ei->icu) {
1388                         if(ei->flag & IPO_VISIBLE) {
1389                                 
1390                                 if(totipo_edit==0 && (ei->flag & IPO_SELECT)) {
1391                                         ei->flag |= IPO_EDIT;
1392                                         ei->icu->flag= ei->flag;
1393                                 }
1394                                 else if(totipo_edit && (ei->flag & IPO_EDIT)) {
1395                                         ei->flag -= IPO_EDIT;
1396                                         ei->icu->flag= ei->flag;
1397                                 }
1398                                 else if(totipo_vis==1) {
1399                                         if(ei->flag & IPO_EDIT) ei->flag -= IPO_EDIT;
1400                                         else ei->flag |= IPO_EDIT;
1401                                         ei->icu->flag= ei->flag;
1402                                 }
1403                         }
1404                 }
1405         }
1406         scrarea_queue_headredraw(curarea);
1407         scrarea_queue_winredraw(curarea);
1408 }
1409
1410 static short findnearest_ipovert(IpoCurve **icu, BezTriple **bezt)
1411 {
1412         /* selected verts get a disadvantage */
1413         /* in icu and (bezt or bp) the nearest is written */
1414         /* return 0 1 2: handlepunt */
1415         EditIpo *ei;
1416         BezTriple *bezt1;
1417         int dist= 100, temp, a, b;
1418         short mval[2], hpoint=0, sco[3][2];
1419
1420         *icu= 0;
1421         *bezt= 0;
1422
1423         getmouseco_areawin(mval);
1424
1425         ei= G.sipo->editipo;
1426         for(a=0; a<G.sipo->totipo; a++, ei++) {
1427                 if (ISPOIN3(ei, flag & IPO_VISIBLE, flag & IPO_EDIT, icu)) {
1428                         
1429                         if(ei->icu->bezt) {
1430                                 bezt1= ei->icu->bezt;
1431                                 b= ei->icu->totvert;
1432                                 while(b--) {
1433
1434                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[0], sco[0]);
1435                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[1], sco[1]);
1436                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[2], sco[2]);
1437                                                                                 
1438                                         if(ei->disptype==IPO_DISPBITS) {
1439                                                 temp= abs(mval[0]- sco[1][0]);
1440                                         }
1441                                         else temp= abs(mval[0]- sco[1][0])+ abs(mval[1]- sco[1][1]);
1442
1443                                         if( bezt1->f2 & SELECT) temp+=5;
1444                                         if(temp<dist) { 
1445                                                 hpoint= 1; 
1446                                                 *bezt= bezt1; 
1447                                                 dist= temp; 
1448                                                 *icu= ei->icu; 
1449                                         }
1450                                         
1451                                         if(ei->disptype!=IPO_DISPBITS && ei->icu->ipo==IPO_BEZ) {
1452                                                 /* middle points get an advantage */
1453                                                 temp= -3+abs(mval[0]- sco[0][0])+ abs(mval[1]- sco[0][1]);
1454                                                 if( bezt1->f1 & 1) temp+=5;
1455                                                 if(temp<dist) { 
1456                                                         hpoint= 0; 
1457                                                         *bezt= bezt1; 
1458                                                         dist= temp; 
1459                                                         *icu= ei->icu; 
1460                                                 }
1461                 
1462                                                 temp= abs(mval[0]- sco[2][0])+ abs(mval[1]- sco[2][1]);
1463                                                 if( bezt1->f3 & 1) temp+=5;
1464                                                 if(temp<dist) { 
1465                                                         hpoint= 2; 
1466                                                         *bezt=bezt1; 
1467                                                         dist= temp; 
1468                                                         *icu= ei->icu; 
1469                                                 }
1470                                         }
1471                                         bezt1++;
1472                                 }
1473                         }
1474                 }
1475         }
1476
1477         return hpoint;
1478 }
1479
1480 void mouse_select_ipo(void)
1481 {
1482         Object *ob;
1483         KeyBlock *actkb=NULL;
1484         EditIpo *ei, *actei= 0;
1485         IpoCurve *icu;
1486         IpoKey *ik, *actik;
1487         BezTriple *bezt;
1488         TimeMarker *marker;
1489         float x, y, dist, mindist;
1490         int a, oldflag = 0, hand, ok;
1491         short mval[2], xo, yo;
1492         
1493         if(G.sipo->editipo==0) return;
1494         
1495         get_status_editipo();
1496         marker=find_nearest_marker(SCE_MARKERS, 1);
1497         
1498         /* map ipo-points for editing if scaled ipo */
1499         if (NLA_IPO_SCALED) {
1500                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 0, 0);
1501         }
1502         
1503         if(G.sipo->showkey) {
1504                 float pixelwidth;
1505                 
1506                 view2d_getscale(G.v2d, &pixelwidth, NULL);
1507                 
1508                 getmouseco_areawin(mval);
1509                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1510                 actik= 0;
1511                 mindist= 1000.0;
1512                 ik= G.sipo->ipokey.first;
1513                 while(ik) {
1514                         dist= (float)(fabs(ik->val-x));
1515                         if(ik->flag & SELECT) dist+= pixelwidth;
1516                         if(dist < mindist) {
1517                                 actik= ik;
1518                                 mindist= dist;
1519                         }
1520                         ik= ik->next;
1521                 }
1522                 if(actik) {
1523                         oldflag= actik->flag;
1524                         
1525                         if(G.qual & LR_SHIFTKEY);
1526                         else deselectall_editipo();
1527                         
1528                         if(G.qual & LR_SHIFTKEY) {
1529                                 if(oldflag & 1) actik->flag &= ~1;
1530                                 else actik->flag |= 1;
1531                         }
1532                         else {
1533                                 actik->flag |= 1;
1534                         }
1535                 }
1536         }
1537         else if(totipo_edit) {
1538                 
1539                 hand= findnearest_ipovert(&icu, &bezt);
1540                 
1541                 if(G.qual & LR_SHIFTKEY) {
1542                         if(bezt) {
1543                                 if(hand==1) {
1544                                         if(BEZSELECTED(bezt)) {
1545                                                 bezt->f1= bezt->f2= bezt->f3= 0;
1546                                         }
1547                                         else {
1548                                                 bezt->f1= bezt->f2= bezt->f3= SELECT;
1549                                         }
1550                                 }
1551                                 else if(hand==0) {
1552                                         if(bezt->f1 & SELECT) bezt->f1= 0;
1553                                         else bezt->f1= SELECT;
1554                                 }
1555                                 else {
1556                                         if(bezt->f3 & SELECT) bezt->f3= 0;
1557                                         else bezt->f3= SELECT;
1558                                 }
1559                         }                               
1560                 }
1561                 else {
1562                         deselectall_editipo();
1563                         
1564                         if(bezt) {
1565                                 if(hand==1) {
1566                                         bezt->f1|= SELECT; bezt->f2|= SELECT; bezt->f3|= SELECT;
1567                                 }
1568                                 else if(hand==0) bezt->f1 |= SELECT;
1569                                 else bezt->f3 |= SELECT;
1570                         }
1571                 }
1572         }
1573         else if (marker) {
1574                 /* select timeline marker */
1575                 if ((G.qual & LR_SHIFTKEY)==0) {
1576                         oldflag= marker->flag;
1577                         deselect_markers(0, 0);
1578                         
1579                         if (oldflag & SELECT)
1580                                 marker->flag &= ~SELECT;
1581                         else
1582                                 marker->flag |= SELECT;
1583                 }
1584                 else {
1585                         marker->flag |= SELECT;                         
1586                 }               
1587         }
1588         else {
1589                 
1590                 /* vertex keys ? */
1591                 if(G.sipo->blocktype==ID_KE && G.sipo->from) {
1592                         Key *key;
1593                         KeyBlock *kb, *curkb;
1594                         int i, index= 1;
1595                         
1596                         ob= (Object *)G.sipo->from;
1597                         key= ob_get_key(ob);
1598                         curkb= BLI_findlink(&key->block, ob->shapenr-1);
1599                         
1600                         ei= G.sipo->editipo;
1601                         if(key->type==KEY_NORMAL || (ei->flag & IPO_VISIBLE)) {
1602                                 getmouseco_areawin(mval);
1603                                 
1604                                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1605                                 /* how much is 20 pixels? */
1606                                 mindist= (float)(20.0*(G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)curarea->winy);
1607                                 
1608                                 for(i=1, kb= key->block.first; kb; kb= kb->next, i++) {
1609                                         dist= (float)(fabs(kb->pos-y));
1610                                         if(kb==curkb) dist+= (float)0.01;
1611                                         if(dist < mindist) {
1612                                                 actkb= kb;
1613                                                 mindist= dist;
1614                                                 index= i;
1615                                         }
1616                                 }
1617                                 if(actkb) {
1618                                         ok= TRUE;
1619                                         if(G.obedit && actkb!=curkb) {
1620                                                 ok= okee("Copy key after leaving Edit Mode");
1621                                         }
1622                                         if(ok) {
1623                                                 /* also does all keypos */
1624                                                 deselectall_editipo();
1625                                                 set_active_key(index);
1626                                                 set_active_editipo(ei+index-1);
1627                                         }
1628                                 }
1629                         }
1630                 }
1631                 
1632                 /* select curve */
1633                 if(actkb==NULL) {
1634                         if(totipo_vis==1) {
1635                                 ei= G.sipo->editipo;
1636                                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1637                                         if(ei->icu) {
1638                                                 if(ei->flag & IPO_VISIBLE) actei= ei;
1639                                         }
1640                                 }
1641                         }
1642                         else if(totipo_vis>1) {
1643                                 actei= select_proj_ipo(0, 0);
1644                         }
1645                         
1646                         if(actei) oldflag= actei->flag;
1647                         
1648                         if(G.qual & LR_SHIFTKEY);
1649                         else deselectall_editipo();
1650                         
1651                         if(actei) {
1652                                 if(G.qual & LR_SHIFTKEY) {
1653                                         if(oldflag & IPO_SELECT) actei->flag &= ~IPO_SELECT;
1654                                         else actei->flag |= IPO_SELECT;
1655                                 }
1656                                 else {
1657                                         actei->flag |= IPO_SELECT;
1658                                 }
1659                                 set_active_editipo(actei);
1660                         }
1661                 }
1662         }
1663         
1664         /* undo mapping of ipo-points for editing if scaled ipo */
1665         if (NLA_IPO_SCALED) {
1666                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 1, 0);
1667         }
1668         
1669         update_editipo_flags();
1670         
1671         force_draw(0);
1672         BIF_undo_push("Select Ipo");
1673         
1674         if(G.sipo->showkey && G.sipo->blocktype==ID_OB) {
1675                 ob= OBACT;
1676                 if(ob && (ob->ipoflag & OB_DRAWKEY)) allqueue(REDRAWVIEW3D, 0);
1677         }
1678         /* points inside of curve are drawn selected too */
1679         if(G.sipo->blocktype==ID_CU)
1680                 allqueue(REDRAWVIEW3D, 0);
1681         
1682         getmouseco_areawin(mval);
1683         xo= mval[0]; 
1684         yo= mval[1];
1685         
1686         while (get_mbut() & ((U.flag & USER_LMOUSESELECT)?L_MOUSE:R_MOUSE)) {           
1687                 getmouseco_areawin(mval);
1688                 if(abs(mval[0]-xo)+abs(mval[1]-yo) > 4) {
1689                         
1690                         if (marker) {
1691                                 transform_markers('g', 0);
1692                         }
1693                         else {
1694                                 if(actkb) move_keys(OBACT);
1695                                 else transform_ipo('g');
1696                         }
1697                         
1698                         return;
1699                 }
1700                 BIF_wait_for_statechange();
1701         }
1702 }
1703
1704
1705 /* *********************************** */
1706
1707 /* handling of right-hand channel/curve buttons in ipo window */
1708 void do_ipowin_buts(short event)
1709 {
1710         EditIpo *ei = NULL;
1711         int a;
1712
1713         /* without shift, all other channels are made invisible */
1714         if((G.qual & LR_SHIFTKEY)==0) {
1715                 if(event>G.sipo->totipo) return;
1716                 ei = G.sipo->editipo;
1717                 for(a=0; a<G.sipo->totipo; a++) {
1718                         if(a!=event) ei->flag &= ~IPO_VISIBLE;
1719                         else ei->flag |= IPO_VISIBLE;
1720                         ei++;
1721                 }
1722         }
1723         
1724         /* set active */
1725         if(event>=0 && event<G.sipo->totipo) {
1726                 ei= G.sipo->editipo;    // void pointer...
1727                 set_active_editipo(ei+event);
1728                 set_active_key(event+1);        // only if there's a key, of course
1729         }
1730         scrarea_queue_winredraw(curarea);
1731         
1732         update_editipo_flags();
1733         get_status_editipo();
1734
1735         if(G.sipo->showkey) {
1736                 make_ipokey();
1737                 if(G.sipo->blocktype==ID_OB) allqueue(REDRAWVIEW3D, 0);
1738         }
1739
1740 }
1741
1742 /* the fake buttons to the left of channel names, for select/deselect curves */
1743 void do_ipo_selectbuttons(void)
1744 {
1745         EditIpo *ei, *ei1;
1746         int a, nr;
1747         short mval[2];
1748         
1749         if(G.sipo->showkey) return;
1750         
1751         /* do not allow editipo here: convert editipos to selected */
1752         get_status_editipo();
1753         if(totipo_edit) {
1754                 set_editflag_editipo();
1755         }
1756         
1757         /* which */
1758         getmouseco_areawin(mval);
1759
1760         nr= -(mval[1]-curarea->winy+30-G.sipo->butofs-IPOBUTY)/IPOBUTY;
1761         if(G.sipo->blocktype==ID_KE) nr--;              /* keys show something else in first channel */
1762         
1763         if(nr>=0 && nr<G.sipo->totipo) {
1764                 ei= G.sipo->editipo;
1765                 ei+= nr;
1766                 
1767                 set_active_editipo(ei);
1768                 set_active_key(nr+1);
1769
1770                 if(ei->icu) {
1771                         if((ei->flag & IPO_VISIBLE)==0) {
1772                                 ei->flag |= IPO_VISIBLE|IPO_SELECT;
1773                         }
1774         
1775                         if((G.qual & LR_SHIFTKEY)==0) {
1776                                 ei1= G.sipo->editipo;
1777                                 for(a=0; a<G.sipo->totipo; a++) {
1778                                         ei1->flag &= ~IPO_SELECT;
1779                                         ei1++;
1780                                 }
1781                         }
1782
1783                         if(ei->flag & IPO_SELECT) {
1784                                 ei->flag &= ~IPO_SELECT;
1785                         }
1786                         else {
1787                                 ei->flag |= IPO_SELECT;
1788                         }
1789                         
1790                         update_editipo_flags();
1791                         scrarea_queue_winredraw(curarea);
1792                 }
1793         }
1794         BIF_undo_push("Select Ipo curve");
1795 }
1796
1797 /* ********************************* Inserting keys ********************************************* */
1798
1799 /* depending type, it returns ipo, if needed it creates one */
1800 /* returns either action ipo or "real" ipo */
1801 /* arguments define full context;
1802    - *from has to be set always, to Object in case of Actions
1803    - blocktype defines available channels of Ipo struct (blocktype ID_OB can be in action too)
1804    - if actname, use this to locate actionchannel, and optional constname 
1805    - if bonename, the constname is the ipo to the constraint
1806 */
1807
1808 /* note; check header_ipo.c, spaceipo_assign_ipo() too */
1809 Ipo *verify_ipo(ID *from, short blocktype, char *actname, char *constname, char *bonename)
1810 {
1811
1812         if(from==NULL || from->lib) return NULL;
1813         
1814         /* first check action ipos */
1815         if(actname && actname[0]) {
1816                 Object *ob= (Object *)from;
1817                 bActionChannel *achan;
1818                 
1819                 if(GS(from->name)!=ID_OB) {
1820                         printf("called ipo system for action with wrong base pointer\n");
1821                         return NULL;
1822                 }
1823                 
1824                 if(ob->action==NULL)
1825                         ob->action= add_empty_action("Action");
1826                 
1827                 achan= verify_action_channel(ob->action, actname);
1828                 
1829                 if(achan) {
1830                         /* automatically assign achan to act-group based on pchan's grouping */
1831                         if (blocktype == ID_PO)
1832                                 verify_pchan2achan_grouping(ob->action, ob->pose, actname);
1833                         
1834                         /* constraint exception */
1835                         if(blocktype==ID_CO) {
1836                                 bConstraintChannel *conchan= verify_constraint_channel(&achan->constraintChannels, constname);
1837                                 if(conchan->ipo==NULL) {
1838                                         conchan->ipo= add_ipo("CoIpo", ID_CO);  
1839                                 }
1840                                 return conchan->ipo;
1841                         }
1842                         else {
1843                                 if(achan->ipo==NULL) {
1844                                         achan->ipo= add_ipo("ActIpo", blocktype);
1845                                 }
1846                                 
1847                                 return achan->ipo;
1848                         }
1849                 }
1850         }
1851         else {
1852                 
1853                 switch(GS(from->name)) {
1854                 case ID_OB:
1855                         {
1856                                 Object *ob= (Object *)from;
1857                                 
1858                                 /* constraint exception */
1859                                 if(blocktype==ID_CO) {
1860                                         /* check the local constraint ipo */
1861                                         if(bonename && bonename[0] && ob->pose) {
1862                                                 bPoseChannel *pchan= get_pose_channel(ob->pose, bonename);
1863                                                 bConstraint *con;
1864                                                 for(con= pchan->constraints.first; con; con= con->next)
1865                                                         if(strcmp(con->name, constname)==0)
1866                                                                 break;
1867                                                 if(con) {
1868                                                         if(con->ipo==NULL) {
1869                                                                 con->ipo= add_ipo("CoIpo", ID_CO);
1870                                                         }
1871                                                         return con->ipo;
1872                                                 }
1873                                         }
1874                                         else { /* the actionchannel */
1875                                                 bConstraintChannel *conchan= verify_constraint_channel(&ob->constraintChannels, constname);
1876                                                 if(conchan->ipo==NULL) {
1877                                                         conchan->ipo= add_ipo("CoIpo", ID_CO);  
1878                                                 }
1879                                                 return conchan->ipo;
1880                                         }
1881                                 }
1882                                 else if(blocktype==ID_OB) {
1883                                         if(ob->ipo==NULL) {
1884                                                 ob->ipo= add_ipo("ObIpo", ID_OB);
1885                                         }
1886                                         return ob->ipo;
1887                                 }
1888                                 else if(blocktype==ID_KE) {
1889                                         Key *key= ob_get_key((Object *)from);
1890                                         
1891                                         if(key) {
1892                                                 if(key->ipo==NULL) {
1893                                                         key->ipo= add_ipo("KeyIpo", ID_KE);
1894                                                 }
1895                                                 return key->ipo;
1896                                         }
1897                                         return NULL;
1898                                 }
1899                                 else if(blocktype== ID_FLUIDSIM) {
1900                                         Object *ob= (Object *)from;
1901                                         if(ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) {
1902                                                 FluidsimSettings *fss= ob->fluidsimSettings;
1903                                                 if(fss->ipo==NULL) {
1904                                                         fss->ipo= add_ipo("FluidsimIpo", ID_FLUIDSIM);
1905                                                         //fprintf(stderr,"FSIPO NEW!\n");
1906                                                 }
1907                                                 return fss->ipo;
1908                                         }
1909                                 }
1910                                 else if(blocktype== ID_PA){
1911                                         Object *ob= (Object *)from;
1912                                         ParticleSystem *psys= psys_get_current(ob);
1913                                         if(psys){
1914                                                 if(psys->part->ipo==0)
1915                                                         psys->part->ipo= add_ipo("ParticleIpo", ID_PA);
1916                                                 return psys->part->ipo;
1917                                         }
1918                                         return NULL;
1919                                 }
1920                         }
1921                         break;
1922                 case ID_MA:
1923                         {
1924                                 Material *ma= (Material *)from;
1925
1926                                 if(ma->ipo==NULL) {
1927                                         ma->ipo= add_ipo("MatIpo", ID_MA);
1928                                 }
1929                                 return ma->ipo;
1930                         }
1931                         break;
1932                 case ID_TE:
1933                         {
1934                                 Tex *tex= (Tex *)from;
1935
1936                                 if(tex->ipo==NULL) {
1937                                         tex->ipo= add_ipo("TexIpo", ID_TE);
1938                                 }
1939                                 return tex->ipo;
1940                         }
1941                         break;
1942                 case ID_SEQ:
1943                         {
1944                                 Sequence *seq= (Sequence *)from;        /* note, sequence is mimicing Id */
1945
1946                                 if(seq->ipo==NULL) {
1947                                         seq->ipo= add_ipo("SeqIpo", ID_SEQ);
1948                                 }
1949                                 update_seq_ipo_rect(seq);
1950                                 return seq->ipo;
1951                         }
1952                         break;
1953                 case ID_CU:
1954                         {
1955                                 Curve *cu= (Curve *)from;
1956                                 
1957                                 if(cu->ipo==NULL) {
1958                                         cu->ipo= add_ipo("CuIpo", ID_CU);
1959                                 }
1960                                 return cu->ipo;
1961                         }
1962                         break;
1963                 case ID_WO:
1964                         {
1965                                 World *wo= (World *)from;
1966
1967                                 if(wo->ipo==NULL) {
1968                                         wo->ipo= add_ipo("WoIpo", ID_WO);
1969                                 }
1970                                 return wo->ipo;
1971                         }
1972                         break;
1973                 case ID_LA:
1974                         {
1975                                 Lamp *la= (Lamp *)from;
1976                                 
1977                                 if(la->ipo==NULL) {
1978                                         la->ipo= add_ipo("LaIpo", ID_LA);
1979                                 }
1980                                 return la->ipo;
1981                         }
1982                         break;
1983                 case ID_CA:
1984                         {
1985                                 Camera *ca= (Camera *)from;
1986
1987                                 if(ca->ipo==NULL) {
1988                                         ca->ipo= add_ipo("CaIpo", ID_CA);
1989                                 }
1990                                 return ca->ipo;
1991                         }
1992                         break;
1993                 case ID_SO:
1994                         {
1995                                 bSound *snd= (bSound *)from;
1996
1997                                 if(snd->ipo==NULL) {
1998                                         snd->ipo= add_ipo("SndIpo", ID_SO);
1999                                 }
2000                                 return snd->ipo;
2001                         }
2002                 }
2003         }
2004         
2005         return NULL;    
2006 }
2007
2008 /* returns and creates
2009  * Make sure functions check for NULL or they will crash!
2010  *  */
2011 IpoCurve *verify_ipocurve(ID *from, short blocktype, char *actname, char *constname, char *bonename, int adrcode)
2012 {
2013         Ipo *ipo;
2014         IpoCurve *icu= NULL;
2015         
2016         /* return 0 if lib */
2017         /* creates ipo too */
2018         ipo= verify_ipo(from, blocktype, actname, constname, bonename);
2019         
2020         if(ipo && ipo->id.lib==NULL && from->lib==NULL) {
2021                 
2022                 for(icu= ipo->curve.first; icu; icu= icu->next) {
2023                         if(icu->adrcode==adrcode) break;
2024                 }
2025                 if(icu==NULL) {
2026                         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
2027                         
2028                         icu->flag |= IPO_VISIBLE|IPO_AUTO_HORIZ;
2029                         if(ipo->curve.first==NULL) icu->flag |= IPO_ACTIVE;     /* first one added active */
2030                         
2031                         icu->blocktype= blocktype;
2032                         icu->adrcode= adrcode;
2033                         
2034                         set_icu_vars(icu);
2035                         
2036                         BLI_addtail( &(ipo->curve), icu);
2037                         
2038                         switch (GS(from->name)) {
2039                                 case ID_SEQ: {
2040                                         Sequence *seq= (Sequence *)from;
2041                                         
2042                                         update_seq_icu_rects(seq);
2043                                         break;
2044                                 }
2045                         }
2046                 }
2047         }
2048
2049         return icu;
2050 }
2051
2052
2053 /* threshold for inserting keyframes - threshold here should be good enough for now, but should become userpref */
2054 #define BEZT_INSERT_THRESH      0.00001
2055
2056 /* Binary search algorithm for finding where to insert BezTriple. (for use by insert_bezt_icu)
2057  * Returns the index to insert at (data already at that index will be offset if replace is 0)
2058  */
2059 static int binarysearch_bezt_index (BezTriple array[], BezTriple *item, int arraylen, short *replace)
2060 {
2061         int start=0, end=arraylen;
2062         int loopbreaker= 0, maxloop= arraylen * 2;
2063         const float frame= (item)? item->vec[1][0] : 0.0f;
2064         
2065         /* initialise replace-flag first */
2066         *replace= 0;
2067         
2068         /* sneaky optimisations (don't go through searching process if...):
2069          *      - keyframe to be added is to be added out of current bounds
2070          *      - keyframe to be added would replace one of the existing ones on bounds
2071          */
2072         if ((arraylen <= 0) || ELEM(NULL, array, item)) {
2073                 printf("Warning: binarysearch_bezt_index encountered invalid array \n");
2074                 return 0;
2075         }
2076         else {
2077                 /* check whether to add before/after/on */
2078                 float framenum;
2079                 
2080                 /* 'First' Keyframe (when only one keyframe, this case is used) */
2081                 framenum= array[0].vec[1][0];
2082                 if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) {
2083                         *replace = 1;
2084                         return 0;
2085                 }
2086                 else if (frame < framenum)
2087                         return 0;
2088                         
2089                 /* 'Last' Keyframe */
2090                 framenum= array[(arraylen-1)].vec[1][0];
2091                 if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) {
2092                         *replace= 1;
2093                         return (arraylen - 1);
2094                 }
2095                 else if (frame > framenum)
2096                         return arraylen;
2097         }
2098         
2099         
2100         /* most of the time, this loop is just to find where to put it
2101          * 'loopbreaker' is just here to prevent infinite loops 
2102          */
2103         for (loopbreaker=0; (start <= end) && (loopbreaker < maxloop); loopbreaker++) {
2104                 /* compute and get midpoint */
2105                 int mid = (start + end) / 2;
2106                 float midfra= array[mid].vec[1][0];
2107                 
2108                 /* check if exactly equal to midpoint */
2109                 if (IS_EQT(frame, midfra, BEZT_INSERT_THRESH)) {
2110                         *replace = 1;
2111                         return mid;
2112                 }
2113                 
2114                 /* repeat in upper/lower half */
2115                 if (frame > midfra)
2116                         start= mid + 1;
2117                 else if (frame < midfra)
2118                         end= mid - 1;
2119         }
2120         
2121         /* print error if loop-limit exceeded */
2122         if (loopbreaker == (maxloop-1)) {
2123                 printf("Error: binarysearch_bezt_index was taking too long \n");
2124                 
2125                 // include debug info 
2126                 printf("\tround = %d: start = %d, end = %d, arraylen = %d \n", loopbreaker, start, end, arraylen);
2127         }
2128         
2129         /* not found, so return where to place it */
2130         return start;
2131 }
2132
2133 /* This function adds a given BezTriple to an IPO-Curve. It will allocate 
2134  * memory for the array if needed, and will insert the BezTriple into a
2135  * suitable place in chronological order.
2136  * 
2137  * NOTE: any recalculate of the IPO-Curve that needs to be done will need to 
2138  *              be done by the caller.
2139  */
2140 int insert_bezt_icu (IpoCurve *icu, BezTriple *bezt)
2141 {
2142         BezTriple *newb;
2143         int i= 0;
2144         
2145         if (icu->bezt == NULL) {
2146                 icu->bezt= MEM_callocN(sizeof(BezTriple), "beztriple");
2147                 *(icu->bezt)= *bezt;
2148                 icu->totvert= 1;
2149         }
2150         else {
2151                 short replace = -1;
2152                 i = binarysearch_bezt_index(icu->bezt, bezt, icu->totvert, &replace);
2153                 
2154                 if (replace) {                  
2155                         /* sanity check: 'i' may in rare cases exceed arraylen */
2156                         if ((i >= 0) && (i < icu->totvert))
2157                                 *(icu->bezt + i) = *bezt;
2158                 }
2159                 else {
2160                         /* add new */
2161                         newb= MEM_callocN((icu->totvert+1)*sizeof(BezTriple), "beztriple");
2162                         
2163                         /* add the beztriples that should occur before the beztriple to be pasted (originally in ei->icu) */
2164                         if (i > 0)
2165                                 memcpy(newb, icu->bezt, i*sizeof(BezTriple));
2166                         
2167                         /* add beztriple to paste at index i */
2168                         *(newb + i)= *bezt;
2169                         
2170                         /* add the beztriples that occur after the beztriple to be pasted (originally in icu) */
2171                         if (i < icu->totvert) 
2172                                 memcpy(newb+i+1, icu->bezt+i, (icu->totvert-i)*sizeof(BezTriple));
2173                         
2174                         /* replace (+ free) old with new */
2175                         MEM_freeN(icu->bezt);
2176                         icu->bezt= newb;
2177                         
2178                         icu->totvert++;
2179                 }
2180         }
2181         
2182         /* we need to return the index, so that some tools which do post-processing can 
2183          * detect where we added the BezTriple in the array
2184          */
2185         return i;
2186 }
2187
2188 /* This function is a wrapper for insert_bezt_icu, and should be used when
2189  * adding a new keyframe to a curve, when the keyframe doesn't exist anywhere
2190  * else yet. 
2191  * 
2192  * 'fast' - is only for the python API where importing BVH's would take an extreamly long time.
2193  */
2194 void insert_vert_icu (IpoCurve *icu, float x, float y, short fast)
2195 {
2196         BezTriple beztr;
2197         int a, h1, h2;
2198         
2199         /* set all three points, for nicer start position */
2200         memset(&beztr, 0, sizeof(BezTriple));
2201         beztr.vec[0][0]= x; 
2202         beztr.vec[0][1]= y;
2203         beztr.vec[1][0]= x;
2204         beztr.vec[1][1]= y;
2205         beztr.vec[2][0]= x;
2206         beztr.vec[2][1]= y;
2207         beztr.hide= IPO_BEZ;
2208         beztr.f1= beztr.f2= beztr.f3= SELECT;
2209         beztr.h1= beztr.h2= HD_AUTO;
2210         
2211         /* add temp beztriple to keyframes */
2212         a= insert_bezt_icu(icu, &beztr);
2213         if (!fast) calchandles_ipocurve(icu);
2214         
2215         /* set handletype */
2216         if (icu->totvert > 2) {
2217                 BezTriple *bezt;
2218                 
2219                 h1= h2= HD_AUTO;
2220                 bezt= (icu->bezt + a);
2221                 
2222                 if (a > 0) h1= (bezt-1)->h2;
2223                 if (a < icu->totvert-1) h2= (bezt+1)->h1;
2224                 
2225                 bezt->h1= h1;
2226                 bezt->h2= h2;
2227                 
2228                 if (!fast) calchandles_ipocurve(icu);
2229         }
2230 }
2231
2232 void add_vert_ipo(void)
2233 {
2234         EditIpo *ei;
2235         float x, y;
2236         int val;
2237         short mval[2];
2238
2239         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
2240         if(G.sipo->showkey) {
2241                 G.sipo->showkey= 0;
2242                 free_ipokey(&G.sipo->ipokey);
2243         }
2244         
2245         getmouseco_areawin(mval);
2246         
2247         if(mval[0]>G.v2d->mask.xmax) return;
2248         
2249         ei= get_active_editipo();
2250         if(ei==NULL) {
2251                 error("No active Ipo curve");
2252                 return;
2253         }
2254         ei->flag |= IPO_VISIBLE;        /* can happen it is active but not visible */
2255         
2256         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
2257         
2258         /* convert click-time to ipo-time */
2259         if (NLA_IPO_SCALED) {
2260                 x= get_action_frame(OBACT, x);
2261         }
2262         
2263         if(ei->icu==NULL) {
2264                 if(G.sipo->from) {
2265                         ei->icu= verify_ipocurve(G.sipo->from, G.sipo->blocktype, G.sipo->actname, G.sipo->constname, G.sipo->bonename, ei->adrcode);
2266                         if (ei->icu)
2267                                 ei->flag |= ei->icu->flag & IPO_AUTO_HORIZ;     /* new curve could have been added, weak... */
2268                         else
2269                                 error("Cannot create an IPO curve, you may be using libdata");
2270                 }
2271         }
2272         if(ei->icu==NULL) return;
2273
2274         if(ei->disptype==IPO_DISPBITS) {
2275                 ei->icu->vartype= IPO_BITS;
2276                 val= (int)floor(y-0.5);
2277                 if(val<0) val= 0;
2278                 y= (float)(1 << val);
2279         }
2280         
2281         insert_vert_icu(ei->icu, x, y, 0);
2282
2283         /* to be sure: if icu was 0, or only 1 curve visible */
2284         ei->flag |= IPO_SELECT;
2285         ei->icu->flag= ei->flag;
2286         
2287         editipo_changed(G.sipo, 1);
2288         BIF_undo_push("Add Ipo vertex");
2289 }
2290
2291 static void *get_context_ipo_poin(ID *id, int blocktype, char *actname, char *constname, IpoCurve *icu, int *vartype)
2292 {
2293         if (blocktype==ID_PO) {
2294                 if (GS(id->name)==ID_OB) {
2295                         Object *ob= (Object *)id;
2296                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2297                         
2298                         if (pchan) {
2299                                 *vartype= IPO_FLOAT;
2300                                 return get_pchan_ipo_poin(pchan, icu->adrcode);
2301                         }
2302                         else
2303                                 return NULL;
2304                 }
2305                 return NULL;
2306         }
2307         else if (blocktype==ID_CO) {
2308                 if ((GS(id->name)==ID_OB) && (constname && constname[0])) {
2309                         Object *ob= (Object *)id;
2310                         bConstraint *con;
2311                         
2312                         /* assume that we only want the influence (as only used for Constraint Channels) */
2313                         if ((ob->ipoflag & OB_ACTION_OB) && !strcmp(actname, "Object")) {
2314                                 for (con= ob->constraints.first; con; con= con->next) {
2315                                         if (strcmp(constname, con->name)==0) {
2316                                                 *vartype= IPO_FLOAT;
2317                                                 return &con->enforce;
2318                                         }
2319                                 }
2320                         }
2321                         else if (ob->pose) {
2322                                 bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2323                                 
2324                                 if (pchan) {
2325                                         for (con= pchan->constraints.first; con; con= con->next) {
2326                                                 if (strcmp(constname, con->name)==0) {
2327                                                         *vartype= IPO_FLOAT;
2328                                                         return &con->enforce;
2329                                                 }
2330                                         }
2331                                 }
2332                         }
2333                 }
2334                 return NULL;
2335         }
2336         else
2337                 return get_ipo_poin(id, icu, vartype);
2338
2339 }
2340
2341 #define KEYNEEDED_DONTADD       0
2342 #define KEYNEEDED_JUSTADD       1
2343 #define KEYNEEDED_DELPREV       2
2344 #define KEYNEEDED_DELNEXT       3
2345
2346 static int new_key_needed(IpoCurve *icu, float cFrame, float nValue) 
2347 {
2348         /* This function determines whether a new keyframe is needed */
2349         /* Cases where keyframes should not be added:
2350          *      1. Keyframe to be added bewteen two keyframes with similar values
2351          *      2. Keyframe to be added on frame where two keyframes are already situated
2352          *      3. Keyframe lies at point that intersects the linear line between two keyframes
2353          */
2354         
2355         BezTriple *bezt=NULL, *prev=NULL;
2356         int totCount, i;
2357         float valA = 0.0f, valB = 0.0f;
2358         
2359         /* safety checking */
2360         if (!icu) return KEYNEEDED_JUSTADD;
2361         totCount= icu->totvert;
2362         if (totCount==0) return KEYNEEDED_JUSTADD;
2363         
2364         /* loop through checking if any are the same */
2365         bezt= icu->bezt;
2366         for (i=0; i<totCount; i++) {
2367                 float prevPosi=0.0f, prevVal=0.0f;
2368                 float beztPosi=0.0f, beztVal=0.0f;
2369                         
2370                 /* get current time+value */    
2371                 beztPosi= bezt->vec[1][0];
2372                 beztVal= bezt->vec[1][1];
2373                         
2374                 if (prev) {
2375                         /* there is a keyframe before the one currently being examined */               
2376                         
2377                         /* get previous time+value */
2378                         prevPosi= prev->vec[1][0];
2379                         prevVal= prev->vec[1][1];
2380                         
2381                         /* keyframe to be added at point where there are already two similar points? */
2382                         if (IS_EQ(prevPosi, cFrame) && IS_EQ(beztPosi, cFrame) && IS_EQ(beztPosi, prevPosi)) {
2383                                 return KEYNEEDED_DONTADD;
2384                         }
2385                         
2386                         /* keyframe between prev+current points ? */
2387                         if ((prevPosi <= cFrame) && (cFrame <= beztPosi)) {
2388                                 /* is the value of keyframe to be added the same as keyframes on either side ? */
2389                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal)) {
2390                                         return KEYNEEDED_DONTADD;
2391                                 }
2392                                 else {
2393                                         float realVal;
2394                                         
2395                                         /* get real value of curve at that point */
2396                                         realVal= eval_icu(icu, cFrame);
2397                                 
2398                                         /* compare whether it's the same as proposed */
2399                                         if (IS_EQ(realVal, nValue)) 
2400                                                 return KEYNEEDED_DONTADD;
2401                                         else 
2402                                                 return KEYNEEDED_JUSTADD;
2403                                 }
2404                         }
2405                         
2406                         /* new keyframe before prev beztriple? */
2407                         if (cFrame < prevPosi) {
2408                                 /* A new keyframe will be added. However, whether the previous beztriple
2409                                  * stays around or not depends on whether the values of previous/current
2410                                  * beztriples and new keyframe are the same.
2411                                  */
2412                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal))
2413                                         return KEYNEEDED_DELNEXT;
2414                                 else 
2415                                         return KEYNEEDED_JUSTADD;
2416                         }
2417                 }
2418                 else {
2419                         /* just add a keyframe if there's only one keyframe 
2420                          * and the new one occurs before the exisiting one does.
2421                          */
2422                         if ((cFrame < beztPosi) && (totCount==1))
2423                                 return KEYNEEDED_JUSTADD;
2424                 }
2425                 
2426                 /* continue. frame to do not yet passed (or other conditions not met) */
2427                 if (i < (totCount-1)) {
2428                         prev= bezt;
2429                         bezt++;
2430                 }
2431                 else
2432                         break;
2433         }
2434         
2435         /* Frame in which to add a new-keyframe occurs after all other keys
2436          * -> If there are at least two existing keyframes, then if the values of the
2437          *       last two keyframes and the new-keyframe match, the last existing keyframe
2438          *       gets deleted as it is no longer required.
2439          * -> Otherwise, a keyframe is just added. 1.0 is added so that fake-2nd-to-last
2440          *       keyframe is not equal to last keyframe.
2441          */
2442         bezt= (icu->bezt + (icu->totvert - 1));
2443         valA= bezt->vec[1][1];
2444         
2445         if (prev)
2446                 valB= prev->vec[1][1];
2447         else 
2448                 valB= bezt->vec[1][1] + 1.0f; 
2449                 
2450         if (IS_EQ(valA, nValue) && IS_EQ(valA, valB)) 
2451                 return KEYNEEDED_DELPREV;
2452         else 
2453                 return KEYNEEDED_JUSTADD;
2454 }
2455
2456 /* a duplicate of insertkey that does not check for routing to insertmatrixkey 
2457         to avoid recursion problems */
2458 static void insertkey_nonrecurs(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2459 {
2460         IpoCurve *icu;
2461         Object *ob;
2462         void *poin= NULL;
2463         float curval, cfra;
2464         int vartype;
2465         int matset=0;
2466         
2467         if (matset==0) {
2468                 icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2469                 
2470                 if(icu) {
2471                         
2472                         poin= get_context_ipo_poin(id, blocktype, actname, constname, icu, &vartype);
2473                         
2474                         if(poin) {
2475                                 curval= read_ipo_poin(poin, vartype);
2476                                 
2477                                 cfra= frame_to_float(CFRA);
2478                                 
2479                                 /* if action is mapped in NLA, it returns a correction */
2480                                 if(actname && actname[0] && GS(id->name)==ID_OB)
2481                                         cfra= get_action_frame((Object *)id, cfra);
2482                                 
2483                                 if( GS(id->name)==ID_OB ) {
2484                                         ob= (Object *)id;
2485                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2486                                                 /* actually frametofloat calc again! */
2487                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2488                                         }
2489                                 }
2490                                 
2491                                 insert_vert_icu(icu, cfra, curval, 0);
2492                         }
2493                 }
2494         }
2495 }
2496
2497 int insertmatrixkey(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2498 {
2499         int matindex=0;
2500         /* branch on adrcode and blocktype, generating the proper matrix-based
2501         values to send to insertfloatkey */
2502         if (GS(id->name)==ID_OB) {
2503                 Object *ob= (Object *)id;
2504
2505                 if ( blocktype==ID_OB ){ //working with an object
2506                         if ((ob)&&!(ob->parent)) {
2507                                 if ((adrcode==OB_ROT_X)||(adrcode==OB_ROT_Y)||(adrcode==OB_ROT_Z)) { //get a rotation
2508                                         float eul[3];
2509                                         switch (adrcode) {
2510                                                 case OB_ROT_X:
2511                                                         matindex=0;
2512                                                         break;
2513                                                 case OB_ROT_Y:
2514                                                         matindex=1;
2515                                                         break;
2516                                                 case OB_ROT_Z:
2517                                                         matindex=2;
2518                                                         break;
2519                                         }
2520                                         Mat4ToEul(ob->obmat, eul);
2521                                         insertfloatkey(id, ID_OB, actname, NULL, adrcode, eul[matindex]*(5.72958));
2522                                         return 1;
2523                                 } else if ((adrcode==OB_LOC_X)||(adrcode==OB_LOC_Y)||(adrcode==OB_LOC_Z)) {//get a translation
2524                                         switch (adrcode) {
2525                                                 case OB_LOC_X:
2526                                                         matindex=0;
2527                                                         break;
2528                                                 case OB_LOC_Y:
2529                                                         matindex=1;
2530                                                         break;
2531                                                 case OB_LOC_Z:
2532                                                         matindex=2;
2533                                                         break;
2534                                         }
2535                                         insertfloatkey(id, ID_OB, actname, NULL, adrcode, ob->obmat[3][matindex]);
2536                                         return 1;
2537                                 }
2538                         }
2539                 } else if ( blocktype==ID_PO) { //working with a pose channel
2540                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2541                         if (pchan) {
2542                                 if ((adrcode==AC_LOC_X)||(adrcode==AC_LOC_Y)||(adrcode==AC_LOC_Z)) {
2543                                         switch (adrcode) {
2544                                                 case AC_LOC_X:
2545                                                         matindex=0;
2546                                                         break;
2547                                                 case AC_LOC_Y:
2548                                                         matindex=1;
2549                                                         break;
2550                                                 case AC_LOC_Z:
2551                                                         matindex=2;
2552                                                         break;
2553                                         }
2554                                         if (!(pchan->bone->parent)||((pchan->bone->parent)&&!(pchan->bone->flag&BONE_CONNECTED))) { /* don't use for non-connected child bones */
2555                                                 float delta_mat[4][4]; 
2556                                                 armature_mat_pose_to_delta(delta_mat, pchan->pose_mat, pchan->bone->arm_mat);
2557                                                 insertfloatkey(id, ID_PO, pchan->name, NULL, adrcode, delta_mat[3][matindex]);
2558                                                 return 1;
2559                                         }
2560                                 } else if ((adrcode==AC_QUAT_W)||(adrcode==AC_QUAT_X)||(adrcode==AC_QUAT_Y)||(adrcode==AC_QUAT_Z)) { 
2561                                         float tmat[4][4], trimat[3][3], localQuat[4];
2562                                         
2563                                         switch (adrcode) {
2564                                                 case AC_QUAT_W:
2565                                                         matindex=0;
2566                                                         break;
2567                                                 case AC_QUAT_X:
2568                                                         matindex=1;
2569                                                         break;
2570                                                 case AC_QUAT_Y:
2571                                                         matindex=2;
2572                                                         break;
2573                                                 case AC_QUAT_Z:
2574                                                         matindex=3;
2575                                                         break;
2576                                         }
2577                                         
2578                                         /* it should be reasonable to assume that we are keyframing on the active object, although it is not 
2579                                          * strictly required for this particular space conversion, arg1 must not be null for this to work
2580                                          */
2581                                         Mat4CpyMat4(tmat, pchan->pose_mat);
2582                                         constraint_mat_convertspace(OBACT, pchan, tmat, CONSTRAINT_SPACE_POSE, CONSTRAINT_SPACE_LOCAL);
2583                                         
2584                                         Mat3CpyMat4(trimat, tmat);
2585                                         Mat3ToQuat_is_ok(trimat, localQuat);
2586                                         insertfloatkey(id, ID_PO, pchan->name, NULL, adrcode, localQuat[matindex]);
2587                                         
2588                                         return 1;
2589                                 }
2590                         }
2591                 }
2592         }
2593         /* failed to set a matrix key -- use traditional, but the non-recursing version */
2594         insertkey_nonrecurs(id,blocktype,actname,constname,adrcode);
2595         return 0;
2596 }
2597
2598 static int match_adr_constraint(ID * id, int blocktype, char *actname, int adrcode)
2599 {       /* This function matches constraint blocks with adrcodes to see if the
2600                 visual keying method should be used. For example, an object looking to key
2601                 location and having a CopyLoc constraint would return true. */
2602                 
2603         Object *ob=NULL;
2604         int foundmatch=0;
2605         int searchtype=0;
2606         bConstraint *conref=NULL, *con=NULL;
2607         
2608         /*Retrieve constraint list*/
2609         if( GS(id->name)==ID_OB ) 
2610                 ob= (Object *)id;
2611         if (ob) {
2612                 if (blocktype==ID_PO) {
2613                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2614                         conref=pchan->constraints.first;
2615                 } else if (blocktype==ID_OB) {
2616                         conref=ob->constraints.first;
2617                 }
2618                 
2619                 if (conref) {
2620                         /*Set search type: 1 is for translation contraints, 2 is for rotation*/
2621                         if ((adrcode==OB_LOC_X)||(adrcode==OB_LOC_Y)||(adrcode==OB_LOC_Z)||(adrcode==AC_LOC_X)||(adrcode==AC_LOC_Y)||(adrcode==AC_LOC_Z)) {
2622                                 searchtype=1;
2623                         } else if ((adrcode==OB_ROT_X)||(adrcode==OB_ROT_Y)||(adrcode==OB_ROT_Z)||(adrcode==AC_QUAT_W)||(adrcode==AC_QUAT_X)||(adrcode==AC_QUAT_Y)||(adrcode==AC_QUAT_Z)) {
2624                                 searchtype=2;
2625                         }
2626                         
2627                         if (searchtype>0) {
2628                                 for (con=conref; (con)&&(foundmatch==0); con=con->next) {
2629                                         switch (con->type) {
2630                                         /* match constraint types to which kinds of keying they would affect */
2631                                                 case CONSTRAINT_TYPE_CHILDOF:
2632                                                         foundmatch=1;
2633                                                         break;
2634                                                 case CONSTRAINT_TYPE_TRACKTO:
2635                                                         if (searchtype==2) foundmatch=1;
2636                                                         break;
2637                                                 case CONSTRAINT_TYPE_FOLLOWPATH:
2638                                                         foundmatch=1;
2639                                                         break;
2640                                                 case CONSTRAINT_TYPE_ROTLIMIT:
2641                                                         if (searchtype==2) foundmatch=1;
2642                                                         break;
2643                                                 case CONSTRAINT_TYPE_LOCLIMIT:
2644                                                         if (searchtype==1) foundmatch=1;
2645                                                         break;
2646                                                 case CONSTRAINT_TYPE_ROTLIKE:
2647                                                         if (searchtype==2) foundmatch=1;
2648                                                         break;
2649                                                 case CONSTRAINT_TYPE_LOCLIKE:
2650                                                         if (searchtype==1) foundmatch=1;
2651                                                         break;
2652                                                 case CONSTRAINT_TYPE_LOCKTRACK:
2653                                                         if (searchtype==2) foundmatch=1;
2654                                                         break;
2655                                                 case CONSTRAINT_TYPE_DISTLIMIT:
2656                                                         if (searchtype==1) foundmatch=1;
2657                                                         break;
2658                                                 case CONSTRAINT_TYPE_MINMAX:
2659                                                         if (searchtype==1) foundmatch=1;
2660                                                         break;
2661                                                 case CONSTRAINT_TYPE_TRANSFORM:
2662                                                         foundmatch=1;
2663                                                         break;
2664                                                 default:
2665                                                         break;
2666                                         }
2667                                 }
2668                         }
2669                 }
2670         }
2671         
2672         return foundmatch;
2673                         
2674 }
2675
2676 void insertkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, short fast)
2677 {
2678         IpoCurve *icu;
2679         Object *ob;
2680         void *poin= NULL;
2681         float curval, cfra;
2682         int vartype;
2683         int matset=0;
2684         
2685         if ((IS_AUTOKEY_FLAG(AUTOMATKEY))&&(match_adr_constraint(id, blocktype, actname, adrcode))) {
2686                 matset=insertmatrixkey(id, blocktype, actname, constname, adrcode);
2687         } 
2688         if (matset==0) {
2689                 icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2690                 
2691                 if(icu) {
2692                         
2693                         poin= get_context_ipo_poin(id, blocktype, actname, constname, icu, &vartype);
2694                         
2695                         if(poin) {
2696                                 curval= read_ipo_poin(poin, vartype);
2697                                 
2698                                 cfra= frame_to_float(CFRA);
2699                                 
2700                                 /* if action is mapped in NLA, it returns a correction */
2701                                 if(actname && actname[0] && GS(id->name)==ID_OB)
2702                                         cfra= get_action_frame((Object *)id, cfra);
2703                                 
2704                                 if( GS(id->name)==ID_OB ) {
2705                                         ob= (Object *)id;
2706                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2707                                                 /* actually frametofloat calc again! */
2708                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2709                                         }
2710                                 }
2711                                 
2712                                 insert_vert_icu(icu, cfra, curval, fast);
2713                         }
2714                 }
2715         }
2716 }
2717
2718
2719
2720 /* This function is a 'smarter' version of the insert key code.
2721  * It uses an auxilliary function to check whether a keyframe is really needed */
2722 void insertkey_smarter(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2723 {
2724         IpoCurve *icu;
2725         Object *ob;
2726         void *poin= NULL;
2727         float curval, cfra;
2728         int vartype;
2729         int insert_mode;
2730         
2731         icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2732         
2733         if(icu) {
2734                 
2735                 poin= get_context_ipo_poin(id, blocktype, actname, constname, icu, &vartype);
2736                 
2737                 if(poin) {
2738                         curval= read_ipo_poin(poin, vartype);
2739                         
2740                         cfra= frame_to_float(CFRA);
2741                         
2742                         /* if action is mapped in NLA, it returns a correction */
2743                         if(actname && actname[0] && GS(id->name)==ID_OB)
2744                                 cfra= get_action_frame((Object *)id, cfra);
2745                         
2746                         if( GS(id->name)==ID_OB ) {
2747                                 ob= (Object *)id;
2748                                 if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2749                                         /* actually frametofloat calc again! */
2750                                         cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2751                                 }
2752                         }
2753                         
2754                         /* check whether this curve really needs a new keyframe */
2755                         insert_mode= new_key_needed(icu, cfra, curval);
2756                         
2757                         /* insert new keyframe at current frame */
2758                         if (insert_mode) 
2759                                 insert_vert_icu(icu, cfra, curval, 0);
2760                         
2761                         /* delete keyframe immediately before/after newly added */
2762                         switch (insert_mode) {
2763                                 case KEYNEEDED_DELPREV:
2764                                         delete_icu_key(icu, icu->totvert-2, 1);
2765                                         break;
2766                                 case KEYNEEDED_DELNEXT:
2767                                         delete_icu_key(icu, 1, 1);
2768                                         break;
2769                         }
2770                 }
2771         }
2772 }
2773
2774 /* For inserting keys based on an arbitrary float value */
2775 void insertfloatkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, float floatkey)
2776 {
2777         IpoCurve *icu;
2778         Object *ob;
2779         void *poin= NULL;
2780         float cfra;
2781         int vartype;
2782         
2783         icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2784         
2785         if(icu) {
2786                 
2787                 poin= get_context_ipo_poin(id, blocktype, actname, constname, icu, &vartype);
2788                 
2789                 if(poin) {
2790                         
2791                         cfra= frame_to_float(CFRA);
2792                         
2793                         /* if action is mapped in NLA, it returns a correction */
2794                         if(actname && actname[0] && GS(id->name)==ID_OB)
2795                                 cfra= get_action_frame((Object *)id, cfra);
2796                         
2797                         if( GS(id->name)==ID_OB ) {
2798                                 ob= (Object *)id;
2799                                 if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2800                                         /* actually frametofloat calc again! */
2801                                         cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2802                                 }
2803                         }
2804                         
2805                         /* insert new keyframe at current frame */
2806                         insert_vert_icu(icu, cfra, floatkey, 0);
2807                 }
2808         }
2809 }
2810
2811 void insertkey_editipo(void)
2812 {
2813         EditIpo *ei;
2814         IpoKey *ik;
2815         ID *id;
2816         float *fp, cfra, *insertvals;
2817         int a, nr, ok, tot;
2818         short event;
2819         
2820         ei= get_active_editipo();
2821         if(ei && ei->icu && ei->icu->driver) 
2822                 event= pupmenu("Insert Curve %t|Default one-to-one mapping %x3");
2823         else if(G.sipo->showkey)
2824                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1|Selected Keys %x2");
2825         else 
2826                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1");
2827         
2828         if(event<1) return;
2829         
2830         if(event==3) {
2831                 IpoDriver *driver= ei->icu->driver;
2832                 
2833                 if(ei->icu->bezt) MEM_freeN(ei->icu->bezt);
2834                 ei->icu->totvert= 0;
2835                 ei->icu->bezt= NULL;
2836                 
2837                 insert_vert_icu(ei->icu, 0.0f, 0.0f, 0);
2838                 
2839                 if(ELEM3(driver->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) {
2840                         if(ei->disptype==IPO_DISPDEGR)
2841                                 insert_vert_icu(ei->icu, 18.0f, 18.0f, 0);
2842                         else
2843                                 insert_vert_icu(ei->icu, 18.0f, 1.0f, 0);
2844                 }
2845                 else
2846                         insert_vert_icu(ei->icu, 1.0f, 1.0f, 0);
2847                 
2848                 ei->flag |= IPO_SELECT|IPO_VISIBLE;
2849                 ei->icu->flag= ei->flag;
2850                 ei->icu->extrap= IPO_DIR;
2851
2852                 do_ipo_buttons(B_IPOHOME);
2853         }
2854         else {
2855                 ei= G.sipo->editipo;
2856                 for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
2857                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
2858                         
2859                                 ok= 0;
2860                                 if(G.sipo->showkey) ok= 1;
2861                                 else if(ei->flag & IPO_SELECT) ok= 1;
2862
2863                                 if(ok) {
2864                                         /* count amount */
2865                                         if(event==1) tot= 1;
2866                                         else {
2867                                                 ik= G.sipo->ipokey.first;
2868                                                 tot= 0;
2869                                                 while(ik) {
2870                                                         if(ik->flag & 1) tot++;
2871                                                         ik= ik->next;
2872                                                 }
2873                                         }
2874                                         if(tot) {
2875                                         
2876                                                 /* correction for ob timeoffs */
2877                                                 cfra= frame_to_float(CFRA);
2878                                                 id= G.sipo->from;       
2879                                                 if(id && GS(id->name)==ID_OB ) {
2880                                                         Object *ob= (Object *)id;
2881                                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2882                                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2883                                                         }
2884                                                 }
2885                                                 else if(id && GS(id->name)==ID_SEQ) {
2886                                                         Sequence *last_seq = get_last_seq();    /* editsequence.c */
2887                                                         
2888                                                         if(last_seq && (last_seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
2889                                                                 cfra= (float)(100.0*(cfra-last_seq->startdisp)/((float)(last_seq->enddisp-last_seq->startdisp)));
2890                                                         }
2891                                                 }
2892                                                 
2893                                                 /* convert cfra to ipo-time */
2894                                                 if (NLA_IPO_SCALED) {
2895                                                         cfra= get_action_frame(OBACT, cfra);
2896                                                 }
2897                                 
2898                                                 insertvals= MEM_mallocN(sizeof(float)*2*tot, "insertkey_editipo");
2899                                                 /* make sure icu->curval is correct */
2900                                                 calc_ipo(G.sipo->ipo, cfra);
2901                                                 
2902                                                 if(event==1) {
2903                                                         insertvals[0]= cfra;
2904                                                         
2905                                                         insertvals[1]= ei->icu->curval;
2906                                                 }
2907                                                 else {
2908                                                         fp= insertvals;
2909                                                         ik= G.sipo->ipokey.first;
2910                                                         while(ik) {
2911                                                                 if(ik->flag & 1) {
2912                                                                         calc_ipo(G.sipo->ipo, ik->val);
2913
2914                                                                         fp[0]= ik->val;
2915                                                                         fp[1]= ei->icu->curval;
2916                                                                         fp+= 2;
2917                                                                 }
2918                                                                 ik= ik->next;
2919                                                         }
2920                                                 }
2921                                                 fp= insertvals;
2922                                                 for(a=0; a<tot; a++, fp+=2) {
2923                                                         insert_vert_icu(ei->icu, fp[0], fp[1], 0);
2924                                                 }
2925                                                 
2926                                                 MEM_freeN(insertvals);
2927                                                 calc_ipo(G.sipo->ipo, (float)CFRA);
2928                                         }
2929                                 }
2930                         }
2931                 }
2932         }
2933         BIF_undo_push("Insert Key Ipo");
2934         allqueue (REDRAWACTION, 0);
2935         allqueue(REDRAWNLA, 0);
2936         allqueue(REDRAWIPO, 0);
2937         allspace(REMAKEIPO, 0);
2938 }
2939
2940
2941 void common_insertkey(void)
2942 {
2943         Base *base;
2944         Object *ob;
2945         Material *ma;
2946         ID *id;
2947         IpoCurve *icu;
2948         World *wo;
2949         Lamp *la;
2950         Tex *te;
2951         int tlay, map, event;
2952         char menustr[256];
2953
2954         if(curarea->spacetype==SPACE_IPO) {
2955                 insertkey_editipo();
2956         }
2957         else if(curarea->spacetype==SPACE_ACTION) {
2958                 insertkey_action();
2959         }
2960         else if(curarea->spacetype==SPACE_BUTS) {
2961                 if(G.buts->mainb==CONTEXT_SHADING) {
2962                         int tab= G.buts->tab[CONTEXT_SHADING];
2963
2964                         if(tab==TAB_SHADING_MAT) {
2965                                 ma = G.buts->lockpoin;
2966                                 ma = editnode_get_active_material(ma);
2967                                 id = (ID *)ma;
2968                                 
2969                                 if(id) {
2970                                         event= pupmenu("Insert Key %t|RGB%x0|Alpha%x1|Halo Size%x2|Mode %x3|All Color%x10|All Mirror%x14|Ofs%x12|Size%x13|All Mapping%x11");
2971                                         if(event== -1) return;
2972
2973                                         map= texchannel_to_adrcode(ma->texact);
2974
2975                                         if(event==0 || event==10) {
2976                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_R, 0);
2977                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_G, 0);
2978                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_B, 0);
2979                                         }
2980                                         if(event==1 || event==10) {
2981                                                 insertkey(id, ID_MA, NULL, NULL, MA_ALPHA, 0);
2982                                         }