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