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