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