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