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