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