== Global 'Delete Key' Tool ==
[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, short add)
1782 {
1783         /* lib-linked data is not appropriate here */
1784         if ((from==NULL) || (from->lib))
1785                 return NULL;
1786         
1787         /* first check action ipos */
1788         if (actname && actname[0]) {
1789                 Object *ob= (Object *)from;
1790                 bActionChannel *achan;
1791                 
1792                 if (GS(from->name)!=ID_OB) {
1793                         printf("called ipo system for action with wrong base pointer\n");
1794                         return NULL;
1795                 }
1796                 
1797                 if ((ob->action==NULL) && (add))
1798                         ob->action= add_empty_action("Action");
1799                 
1800                 if (add)
1801                         achan= verify_action_channel(ob->action, actname);
1802                 else    
1803                         achan= get_action_channel(ob->action, actname);
1804                 
1805                 if (achan) {
1806                         /* automatically assign achan to act-group based on pchan's grouping */
1807                         if ((blocktype == ID_PO) && (add))
1808                                 verify_pchan2achan_grouping(ob->action, ob->pose, actname);
1809                         
1810                         /* constraint exception */
1811                         if (blocktype==ID_CO) {
1812                                 bConstraintChannel *conchan;
1813                                 
1814                                 if (add)
1815                                         conchan= verify_constraint_channel(&achan->constraintChannels, constname);
1816                                 else
1817                                         conchan= get_constraint_channel(&achan->constraintChannels, constname);
1818                                         
1819                                 if (conchan) {
1820                                         if ((conchan->ipo==NULL) && (add))
1821                                                 conchan->ipo= add_ipo("CoIpo", ID_CO);  
1822                                         return conchan->ipo;
1823                                 }
1824                         }
1825                         else {
1826                                 if ((achan->ipo==NULL) && (add))
1827                                         achan->ipo= add_ipo("ActIpo", blocktype);
1828                                 return achan->ipo;
1829                         }
1830                 }
1831         }
1832         else {
1833                 switch (GS(from->name)) {
1834                 case ID_OB:
1835                         {
1836                                 Object *ob= (Object *)from;
1837                                 
1838                                 /* constraint exception */
1839                                 if (blocktype==ID_CO) {
1840                                         /* check the local constraint ipo */
1841                                         if (bonename && bonename[0] && ob->pose) {
1842                                                 bPoseChannel *pchan= get_pose_channel(ob->pose, bonename);
1843                                                 bConstraint *con;
1844                                                 
1845                                                 for (con= pchan->constraints.first; con; con= con->next) {
1846                                                         if (strcmp(con->name, constname)==0)
1847                                                                 break;
1848                                                 }
1849                                                 
1850                                                 if (con) {
1851                                                         if ((con->ipo==NULL) && (add))
1852                                                                 con->ipo= add_ipo("CoIpo", ID_CO);
1853                                                         return con->ipo;
1854                                                 }
1855                                         }
1856                                         else { /* the actionchannel */
1857                                                 bConstraintChannel *conchan;
1858                                                 
1859                                                 if (add)
1860                                                         conchan= verify_constraint_channel(&ob->constraintChannels, constname);
1861                                                 else
1862                                                         conchan= get_constraint_channel(&ob->constraintChannels, constname);
1863                                                         
1864                                                 if (conchan) {
1865                                                         if ((conchan->ipo==NULL) && (add))
1866                                                                 conchan->ipo= add_ipo("CoIpo", ID_CO);  
1867                                                         return conchan->ipo;
1868                                                 }
1869                                         }
1870                                 }
1871                                 else if (blocktype==ID_OB) {
1872                                         if ((ob->ipo==NULL) && (add))
1873                                                 ob->ipo= add_ipo("ObIpo", ID_OB);
1874                                         return ob->ipo;
1875                                 }
1876                                 else if (blocktype==ID_KE) {
1877                                         Key *key= ob_get_key((Object *)from);
1878                                         
1879                                         if (key) {
1880                                                 if ((key->ipo==NULL) && (add))
1881                                                         key->ipo= add_ipo("KeyIpo", ID_KE);
1882                                                 return key->ipo;
1883                                         }
1884                                         return NULL;
1885                                 }
1886                                 else if (blocktype== ID_FLUIDSIM) {
1887                                         Object *ob= (Object *)from;
1888                                         
1889                                         if (ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) {
1890                                                 FluidsimSettings *fss= ob->fluidsimSettings;
1891                                                 
1892                                                 if ((fss->ipo==NULL) && (add))
1893                                                         fss->ipo= add_ipo("FluidsimIpo", ID_FLUIDSIM);
1894                                                 return fss->ipo;
1895                                         }
1896                                 }
1897                                 else if(blocktype== ID_PA) {
1898                                         Object *ob= (Object *)from;
1899                                         ParticleSystem *psys= psys_get_current(ob);
1900                                         
1901                                         if (psys) {
1902                                                 if ((psys->part->ipo==NULL) && (add))
1903                                                         psys->part->ipo= add_ipo("ParticleIpo", ID_PA);
1904                                                 return psys->part->ipo;
1905                                         }
1906                                         return NULL;
1907                                 }
1908                         }
1909                         break;
1910                 case ID_MA:
1911                         {
1912                                 Material *ma= (Material *)from;
1913                                 
1914                                 if ((ma->ipo==NULL) && (add))
1915                                         ma->ipo= add_ipo("MatIpo", ID_MA);
1916                                 return ma->ipo;
1917                         }
1918                         break;
1919                 case ID_TE:
1920                         {
1921                                 Tex *tex= (Tex *)from;
1922                                 
1923                                 if ((tex->ipo==NULL) && (add))
1924                                         tex->ipo= add_ipo("TexIpo", ID_TE);
1925                                 return tex->ipo;
1926                         }
1927                         break;
1928                 case ID_SEQ:
1929                         {
1930                                 Sequence *seq= (Sequence *)from;        /* note, sequence is mimicing Id */
1931                                 
1932                                 if ((seq->ipo==NULL) && (add))
1933                                         seq->ipo= add_ipo("SeqIpo", ID_SEQ);
1934                                 update_seq_ipo_rect(seq);
1935                                 return seq->ipo;
1936                         }
1937                         break;
1938                 case ID_CU:
1939                         {
1940                                 Curve *cu= (Curve *)from;
1941                                 
1942                                 if ((cu->ipo==NULL) && (add))
1943                                         cu->ipo= add_ipo("CuIpo", ID_CU);
1944                                 return cu->ipo;
1945                         }
1946                         break;
1947                 case ID_WO:
1948                         {
1949                                 World *wo= (World *)from;
1950                                 
1951                                 if ((wo->ipo==NULL) && (add))
1952                                         wo->ipo= add_ipo("WoIpo", ID_WO);
1953                                 return wo->ipo;
1954                         }
1955                         break;
1956                 case ID_LA:
1957                         {
1958                                 Lamp *la= (Lamp *)from;
1959                                 
1960                                 if ((la->ipo==NULL) && (add))
1961                                         la->ipo= add_ipo("LaIpo", ID_LA);
1962                                 return la->ipo;
1963                         }
1964                         break;
1965                 case ID_CA:
1966                         {
1967                                 Camera *ca= (Camera *)from;
1968                                 
1969                                 if ((ca->ipo==NULL) && (add))
1970                                         ca->ipo= add_ipo("CaIpo", ID_CA);
1971                                 return ca->ipo;
1972                         }
1973                         break;
1974                 case ID_SO:
1975                         {
1976                                 bSound *snd= (bSound *)from;
1977                                 
1978                                 if ((snd->ipo==NULL) && (add))
1979                                         snd->ipo= add_ipo("SndIpo", ID_SO);
1980                                 return snd->ipo;
1981                         }
1982                 }
1983         }
1984         
1985         return NULL;    
1986 }
1987
1988 /* returns and creates
1989  * Make sure functions check for NULL or they will crash!
1990  *  */
1991 IpoCurve *verify_ipocurve(ID *from, short blocktype, char *actname, char *constname, char *bonename, int adrcode, short add)
1992 {
1993         Ipo *ipo;
1994         IpoCurve *icu= NULL;
1995         
1996         /* return 0 if lib */
1997         /* creates ipo too (if add) */
1998         ipo= verify_ipo(from, blocktype, actname, constname, bonename, add);
1999         
2000         if (ipo && ipo->id.lib==NULL && from->lib==NULL) {
2001                 /* try to find matching curve */
2002                 for (icu= ipo->curve.first; icu; icu= icu->next) {
2003                         if (icu->adrcode==adrcode) 
2004                                 break;
2005                 }
2006                 
2007                 /* make a new one if none found (and can add) */
2008                 if ((icu==NULL) && (add)) {
2009                         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
2010                         
2011                         icu->flag |= (IPO_VISIBLE|IPO_AUTO_HORIZ);
2012                         if (ipo->curve.first==NULL) 
2013                                 icu->flag |= IPO_ACTIVE;        /* first one added active */
2014                         
2015                         icu->blocktype= blocktype;
2016                         icu->adrcode= adrcode;
2017                         
2018                         set_icu_vars(icu);
2019                         
2020                         BLI_addtail(&ipo->curve, icu);
2021                         
2022                         switch (GS(from->name)) {
2023                                 case ID_SEQ: {
2024                                         Sequence *seq= (Sequence *)from;
2025                                         
2026                                         update_seq_icu_rects(seq);
2027                                         break;
2028                                 }
2029                         }
2030                 }
2031         }
2032         
2033         /* return ipo-curve */
2034         return icu;
2035 }
2036
2037
2038 void add_vert_ipo(void)
2039 {
2040         EditIpo *ei;
2041         float x, y;
2042         int val;
2043         short mval[2];
2044
2045         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
2046         if(G.sipo->showkey) {
2047                 G.sipo->showkey= 0;
2048                 free_ipokey(&G.sipo->ipokey);
2049         }
2050         
2051         getmouseco_areawin(mval);
2052         
2053         if(mval[0]>G.v2d->mask.xmax) return;
2054         
2055         ei= get_active_editipo();
2056         if(ei==NULL) {
2057                 error("No active Ipo curve");
2058                 return;
2059         }
2060         ei->flag |= IPO_VISIBLE;        /* can happen it is active but not visible */
2061         
2062         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
2063         
2064         /* convert click-time to ipo-time */
2065         if (NLA_IPO_SCALED) {
2066                 x= get_action_frame(OBACT, x);
2067         }
2068         
2069         if(ei->icu==NULL) {
2070                 if(G.sipo->from) {
2071                         ei->icu= verify_ipocurve(G.sipo->from, G.sipo->blocktype, G.sipo->actname, G.sipo->constname, G.sipo->bonename, ei->adrcode, 1);
2072                         if (ei->icu)
2073                                 ei->flag |= ei->icu->flag & IPO_AUTO_HORIZ;     /* new curve could have been added, weak... */
2074                         else
2075                                 error("Cannot create an IPO curve, you may be using libdata");
2076                 }
2077         }
2078         if(ei->icu==NULL) return;
2079
2080         if(ei->disptype==IPO_DISPBITS) {
2081                 ei->icu->vartype= IPO_BITS;
2082                 val= (int)floor(y-0.5);
2083                 if(val<0) val= 0;
2084                 y= (float)(1 << val);
2085         }
2086         
2087         insert_vert_icu(ei->icu, x, y, 0);
2088
2089         /* to be sure: if icu was 0, or only 1 curve visible */
2090         ei->flag |= IPO_SELECT;
2091         ei->icu->flag= ei->flag;
2092         
2093         editipo_changed(G.sipo, 1);
2094         BIF_undo_push("Add Ipo vertex");
2095 }
2096
2097
2098 void insertkey_editipo(void)
2099 {
2100         EditIpo *ei;
2101         IpoKey *ik;
2102         ID *id;
2103         float *fp, cfra, *insertvals;
2104         int a, nr, ok, tot;
2105         short event;
2106         
2107         ei= get_active_editipo();
2108         if(ei && ei->icu && ei->icu->driver) 
2109                 event= pupmenu("Insert Curve %t|Default one-to-one mapping %x3");
2110         else if(G.sipo->showkey)
2111                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1|Selected Keys %x2");
2112         else 
2113                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1");
2114         
2115         if(event<1) return;
2116         
2117         if(event==3) {
2118                 IpoDriver *driver= ei->icu->driver;
2119                 
2120                 if(ei->icu->bezt) MEM_freeN(ei->icu->bezt);
2121                 ei->icu->totvert= 0;
2122                 ei->icu->bezt= NULL;
2123                 
2124                 insert_vert_icu(ei->icu, 0.0f, 0.0f, 0);
2125                 
2126                 if(ELEM3(driver->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) {
2127                         if(ei->disptype==IPO_DISPDEGR)
2128                                 insert_vert_icu(ei->icu, 18.0f, 18.0f, 0);
2129                         else
2130                                 insert_vert_icu(ei->icu, 18.0f, 1.0f, 0);
2131                 }
2132                 else
2133                         insert_vert_icu(ei->icu, 1.0f, 1.0f, 0);
2134                 
2135                 ei->flag |= IPO_SELECT|IPO_VISIBLE;
2136                 ei->icu->flag= ei->flag;
2137                 ei->icu->extrap= IPO_DIR;
2138
2139                 do_ipo_buttons(B_IPOHOME);
2140         }
2141         else {
2142                 ei= G.sipo->editipo;
2143                 for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
2144                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
2145                         
2146                                 ok= 0;
2147                                 if(G.sipo->showkey) ok= 1;
2148                                 else if(ei->flag & IPO_SELECT) ok= 1;
2149
2150                                 if(ok) {
2151                                         /* count amount */
2152                                         if(event==1) tot= 1;
2153                                         else {
2154                                                 ik= G.sipo->ipokey.first;
2155                                                 tot= 0;
2156                                                 while(ik) {
2157                                                         if(ik->flag & 1) tot++;
2158                                                         ik= ik->next;
2159                                                 }
2160                                         }
2161                                         if(tot) {
2162                                         
2163                                                 /* correction for ob timeoffs */
2164                                                 cfra= frame_to_float(CFRA);
2165                                                 id= G.sipo->from;       
2166                                                 if(id && GS(id->name)==ID_OB ) {
2167                                                         Object *ob= (Object *)id;
2168                                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2169                                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2170                                                         }
2171                                                 }
2172                                                 else if(id && GS(id->name)==ID_SEQ) {
2173                                                         Sequence *last_seq = get_last_seq();    /* editsequence.c */
2174                                                         
2175                                                         if(last_seq && (last_seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
2176                                                                 cfra= (float)(100.0*(cfra-last_seq->startdisp)/((float)(last_seq->enddisp-last_seq->startdisp)));
2177                                                         }
2178                                                 }
2179                                                 
2180                                                 /* convert cfra to ipo-time */
2181                                                 if (NLA_IPO_SCALED) {
2182                                                         cfra= get_action_frame(OBACT, cfra);
2183                                                 }
2184                                 
2185                                                 insertvals= MEM_mallocN(sizeof(float)*2*tot, "insertkey_editipo");
2186                                                 /* make sure icu->curval is correct */
2187                                                 calc_ipo(G.sipo->ipo, cfra);
2188                                                 
2189                                                 if(event==1) {
2190                                                         insertvals[0]= cfra;
2191                                                         
2192                                                         insertvals[1]= ei->icu->curval;
2193                                                 }
2194                                                 else {
2195                                                         fp= insertvals;
2196                                                         ik= G.sipo->ipokey.first;
2197                                                         while(ik) {
2198                                                                 if(ik->flag & 1) {
2199                                                                         calc_ipo(G.sipo->ipo, ik->val);
2200
2201                                                                         fp[0]= ik->val;
2202                                                                         fp[1]= ei->icu->curval;
2203                                                                         fp+= 2;
2204                                                                 }
2205                                                                 ik= ik->next;
2206                                                         }
2207                                                 }
2208                                                 fp= insertvals;
2209                                                 for(a=0; a<tot; a++, fp+=2) {
2210                                                         insert_vert_icu(ei->icu, fp[0], fp[1], 0);
2211                                                 }
2212                                                 
2213                                                 MEM_freeN(insertvals);
2214                                                 calc_ipo(G.sipo->ipo, (float)CFRA);
2215                                         }
2216                                 }
2217                         }
2218                 }
2219         }
2220         BIF_undo_push("Insert Key Ipo");
2221         allqueue (REDRAWACTION, 0);
2222         allqueue(REDRAWNLA, 0);
2223         allqueue(REDRAWIPO, 0);
2224         allspace(REMAKEIPO, 0);
2225 }
2226
2227
2228 /* ****************************************************************************** */
2229
2230 void add_duplicate_editipo(void)
2231 {
2232         Object *ob;
2233         EditIpo *ei;
2234         IpoCurve *icu;
2235         BezTriple *bezt, *beztn, *newb;
2236         int tot, a, b;
2237         
2238         get_status_editipo();
2239         if(totipo_vertsel==0) return;
2240         
2241         ei= G.sipo->editipo;
2242         for(a=0; a<G.sipo->totipo; a++, ei++) {
2243                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2244                         if(G.sipo->showkey || (ei->flag & IPO_EDIT)) {
2245                                 icu= ei->icu;
2246                                 
2247                                 /* how many points */
2248                                 tot= 0;
2249                                 b= icu->totvert;
2250                                 bezt= icu->bezt;
2251                                 while(b--) {
2252                                         if(bezt->f2 & SELECT) tot++;
2253                                         bezt++;
2254                                 }
2255                                 
2256                                 if(tot) {
2257                                         icu->totvert+= tot;
2258                                         newb= beztn= MEM_mallocN(icu->totvert*sizeof(BezTriple), "bezt");
2259                                         bezt= icu->bezt;
2260                                         b= icu->totvert-tot;
2261                                         while(b--) {
2262                                                 *beztn= *bezt;
2263                                                 if(bezt->f2 & SELECT) {
2264                                                         beztn->f1= beztn->f2= beztn->f3= 0;
2265                                                         beztn++;
2266                                                         *beztn= *bezt;
2267                                                 }
2268                                                 beztn++;
2269                                                 bezt++;
2270                                         }
2271                                         MEM_freeN(icu->bezt);
2272                                         icu->bezt= newb;
2273                                         
2274                                         calchandles_ipocurve(icu);
2275                                 }
2276                         }
2277                 }
2278         }
2279         
2280         if(G.sipo->showkey) {
2281                 make_ipokey();
2282                 if(G.sipo->blocktype==ID_OB) {
2283                         ob= OBACT;
2284                         if(ob && (ob->ipoflag & OB_DRAWKEY)) allqueue(REDRAWVIEW3D, 0);
2285                 }
2286         }
2287         BIF_undo_push("Duplicate Ipo");
2288         transform_ipo('g');
2289 }
2290
2291 void remove_doubles_ipo(void)
2292 {
2293         EditIpo *ei;
2294         IpoKey *ik, *ikn;
2295         BezTriple *bezt, *newb, *new1;
2296         float val;
2297         int mode, a, b;
2298         
2299         ei= G.sipo->editipo;
2300         for(a=0; a<G.sipo->totipo; a++, ei++) {
2301                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2302                         
2303                         /* OR the curve is selected OR in editmode OR in keymode */
2304                         mode= 0;
2305                         if(G.sipo->showkey || (ei->flag & IPO_EDIT)) mode= 1;
2306                         else if(ei->flag & IPO_SELECT) mode= 2;
2307                         
2308                         if(mode) {
2309                                 bezt= ei->icu->bezt;
2310                                 newb= new1= MEM_mallocN(ei->icu->totvert*sizeof(BezTriple), "newbezt");
2311                                 *newb= *bezt;
2312                                 b= ei->icu->totvert-1;
2313                                 bezt++;
2314                                 while(b--) {
2315                                         
2316                                         /* can we remove? */
2317                                         if(mode==2 || (bezt->f2 & SELECT)) {
2318                                         
2319                                                 /* are the points different? */
2320                                                 if( fabs( bezt->vec[1][0]-newb->vec[1][0] ) > 0.9 ) {
2321                                                         newb++;
2322                                                         *newb= *bezt;
2323                                                 }
2324                                                 else {
2325                                                         /* median */
2326                                                         VecMidf(newb->vec[0], newb->vec[0], bezt->vec[0]);
2327                                                         VecMidf(newb->vec[1], newb->vec[1], bezt->vec[1]);
2328                                                         VecMidf(newb->vec[2], newb->vec[2], bezt->vec[2]);
2329                                                         
2330                                                         newb->h1= newb->h2= HD_FREE;
2331                                                         
2332                                                         ei->icu->totvert--;
2333                                                 }
2334                                                 
2335                                         }
2336                                         else {
2337                                                 newb++;
2338                                                 *newb= *bezt;
2339                                         }
2340                                         bezt++;
2341                                 }
2342                                 
2343                                 MEM_freeN(ei->icu->bezt);
2344                                 ei->icu->bezt= new1;
2345                                 
2346                                 calchandles_ipocurve(ei->icu);                          
2347                         }
2348                 }
2349         }
2350         
2351         editipo_changed(G.sipo, 1);     /* makes ipokeys again! */
2352
2353         /* remove double keys */
2354         if(G.sipo->showkey) {
2355                 ik= G.sipo->ipokey.first;
2356                 ikn= ik->next;
2357                 
2358                 while(ik && ikn) {
2359                         if( (ik->flag & 1) && (ikn->flag & 1) ) {
2360                                 if( fabs(ik->val-ikn->val) < 0.9 ) {
2361                                         val= (float)((ik->val + ikn->val)/2.0);
2362                                         
2363                                         for(a=0; a<G.sipo->totipo; a++) {
2364                                                 if(ik->data[a]) ik->data[a]->vec[1][0]= val;
2365                                                 if(ikn->data[a]) ikn->data[a]->vec[1][0]= val;                                          
2366                                         }
2367                                 }
2368                         }
2369                         ik= ikn;
2370                         ikn= ikn->next;
2371
2372                 }
2373                 
2374                 editipo_changed(G.sipo, 1);     /* makes ipokeys agian! */
2375
2376         }
2377         deselectall_editipo();
2378         BIF_undo_push("Remove Doubles (IPO)");
2379 }
2380
2381
2382 void clean_ipo(void) 
2383 {
2384         EditIpo *ei;
2385         short ok;
2386         int b;
2387         
2388         ok= fbutton(&G.scene->toolsettings->clean_thresh, 
2389                                 0.0000001f, 1.0, 0.001, 0.1,
2390                                 "Threshold");
2391         if (!ok) return;
2392         
2393         get_status_editipo();
2394
2395         ei= G.sipo->editipo;
2396         for(b=0; b<G.sipo->totipo; b++, ei++) {
2397                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2398                 
2399                         ok= 0;
2400                         if(G.sipo->showkey) ok= 1;
2401                         else if(totipo_vert && (ei->flag & IPO_EDIT)) ok= 2;
2402                         else if(totipo_vert==0 && (ei->flag & IPO_SELECT)) ok= 3;
2403                         
2404                         if(ok) {
2405                                 /* only clean if ok */
2406                                 clean_ipo_curve(ei->icu);
2407                         }
2408                 }
2409         }
2410         
2411         editipo_changed(G.sipo, 1);
2412         BIF_undo_push("Clean IPO");
2413 }
2414
2415 void clean_ipo_curve(IpoCurve *icu)
2416 {
2417         BezTriple *old_bezts, *bezt, *beztn;
2418         BezTriple *lastb;
2419         int totCount, i;
2420         float thresh;
2421         
2422         /* check if any points  */
2423         if (icu == NULL || icu->totvert <= 1) 
2424                 return;
2425         
2426         /* get threshold for match-testing */
2427         thresh= G.scene->toolsettings->clean_thresh;
2428         
2429         /* make a copy of the old BezTriples, and clear IPO curve */
2430         old_bezts = icu->bezt;
2431         totCount = icu->totvert;        
2432         icu->bezt = NULL;
2433         icu->totvert = 0;
2434         
2435         /* now insert first keyframe, as it should be ok */
2436         bezt = old_bezts;
2437         insert_vert_icu(icu, bezt->vec[1][0], bezt->vec[1][1], 0);
2438         
2439         /* Loop through BezTriples, comparing them. Skip any that do 
2440          * not fit the criteria for "ok" points.
2441          */
2442         for (i=1; i<totCount; i++) {    
2443                 float prev[2], cur[2], next[2];
2444                 
2445                 /* get BezTriples and their values */
2446                 if (i < (totCount - 1)) {
2447                         beztn = (old_bezts + (i+1));
2448                         next[0]= beztn->vec[1][0]; next[1]= beztn->vec[1][1];
2449                 }
2450                 else {
2451                         beztn = NULL;
2452                         next[0] = next[1] = 0.0f;
2453                 }
2454                 lastb= (icu->bezt + (icu->totvert - 1));
2455                 bezt= (old_bezts + i);
2456                 
2457                 /* get references for quicker access */
2458                 prev[0] = lastb->vec[1][0]; prev[1] = lastb->vec[1][1];
2459                 cur[0] = bezt->vec[1][0]; cur[1] = bezt->vec[1][1];
2460                 
2461                 /* check if current bezt occurs at same time as last ok */
2462                 if (IS_EQT(cur[0], prev[0], thresh)) {
2463                         /* If there is a next beztriple, and if occurs at the same time, only insert 
2464                          * if there is a considerable distance between the points, and also if the 
2465                          * current is further away than the next one is to the previous.
2466                          */
2467                         if (beztn && (IS_EQT(cur[0], next[0], thresh)) && 
2468                                 (IS_EQT(next[1], prev[1], thresh)==0)) 
2469                         {
2470                                 /* only add if current is further away from previous */
2471                                 if (cur[1] > next[1]) {
2472                                         if (IS_EQT(cur[1], prev[1], thresh) == 0) {
2473                                                 /* add new keyframe */
2474                                                 insert_vert_icu(icu, cur[0], cur[1], 0);
2475                                         }
2476                                 }
2477                         }
2478                         else {
2479                                 /* only add if values are a considerable distance apart */
2480                                 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
2481                                         /* add new keyframe */
2482                                         insert_vert_icu(icu, cur[0], cur[1], 0);
2483                                 }
2484                         }
2485                 }
2486                 else {
2487                         /* checks required are dependent on whether this is last keyframe or not */
2488                         if (beztn) {
2489                                 /* does current have same value as previous and next? */
2490                                 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
2491                                         /* add new keyframe*/
2492                                         insert_vert_icu(icu, cur[0], cur[1], 0);
2493                                 }
2494                                 else if (IS_EQT(cur[1], next[1], thresh) == 0) {
2495                                         /* add new keyframe */
2496                                         insert_vert_icu(icu, cur[0], cur[1], 0);
2497                                 }
2498                         }
2499                         else {  
2500                                 /* add if value doesn't equal that of previous */
2501                                 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
2502                                         /* add new keyframe */
2503                                         insert_vert_icu(icu, cur[0], cur[1], 0);
2504                                 }
2505                         }
2506                 }
2507         }
2508         
2509         /* now free the memory used by the old BezTriples */
2510         if (old_bezts)
2511                 MEM_freeN(old_bezts);
2512 }
2513
2514
2515 /* temp struct used for smooth_ipo */
2516 typedef struct tSmooth_Bezt {
2517         float *h1, *h2, *h3;    /* bezt->vec[0,1,2][1] */
2518 } tSmooth_Bezt;
2519
2520 void smooth_ipo(void)
2521 {
2522         EditIpo *ei;
2523         short ok;
2524         int b;
2525         
2526         get_status_editipo();
2527
2528         ei= G.sipo->editipo;
2529         for(b=0; b<G.sipo->totipo; b++, ei++) {
2530                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2531                         ok= 0;
2532                         if(G.sipo->showkey) ok= 1;
2533                         else if(totipo_vert && (ei->flag & IPO_EDIT)) ok= 2;
2534                         else if(totipo_vert==0 && (ei->flag & IPO_SELECT)) ok= 3;
2535                         
2536                         if(ok) {
2537                                 IpoCurve *icu= ei->icu;
2538                                 BezTriple *bezt;
2539                                 int i, x, totSel = 0;
2540                                 
2541                                 /* check if enough points */
2542                                 if (icu->totvert >= 3) {
2543                                         /* first loop through - count how many verts are selected, and fix up handles */
2544                                         bezt= icu->bezt;
2545                                         for (i=0; i < icu->totvert; i++, bezt++) {                                              
2546                                                 if (BEZSELECTED(bezt)) {                                                        
2547                                                         /* line point's handles up with point's vertical position */
2548                                                         bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1];
2549                                                         if ((bezt->h1==HD_AUTO) || (bezt->h1==HD_VECT)) bezt->h1= HD_ALIGN;
2550                                                         if ((bezt->h2==HD_AUTO) || (bezt->h2==HD_VECT)) bezt->h2= HD_ALIGN;
2551                                                         
2552                                                         /* add value to total */
2553                                                         totSel++;
2554                                                 }
2555                                         }
2556                                         
2557                                         /* if any points were selected, allocate tSmooth_Bezt points to work on */
2558                                         if (totSel >= 3) {
2559                                                 tSmooth_Bezt *tarray, *tsb;
2560                                                 
2561                                                 /* allocate memory in one go */
2562                                                 tsb= tarray= MEM_callocN(totSel*sizeof(tSmooth_Bezt), "tSmooth_Bezt Array");
2563                                                 
2564                                                 /* populate tarray with data of selected points */
2565                                                 bezt= icu->bezt;
2566                                                 for (i=0, x=0; (i < icu->totvert) && (x < totSel); i++, bezt++) {
2567                                                         if (BEZSELECTED(bezt)) {
2568                                                                 /* tsb simply needs pointer to vec, and index */
2569                                                                 tsb->h1 = &bezt->vec[0][1];
2570                                                                 tsb->h2 = &bezt->vec[1][1];
2571                                                                 tsb->h3 = &bezt->vec[2][1];
2572                                                                 
2573                                                                 /* advance to the next tsb to populate */
2574                                                                 if (x < totSel- 1) 
2575                                                                         tsb++;
2576                                                                 else
2577                                                                         break;
2578                                                         }
2579                                                 }
2580                                                 
2581                                                 /* calculate the new smoothed ipo's with weighted averages:
2582                                                  *      - this is done with two passes
2583                                                  *      - uses 5 points for each operation (which stores in the relevant handles)
2584                                                  *      -       previous: w/a ratio = 3:5:2:1:1
2585                                                  *      -       next: w/a ratio = 1:1:2:5:3
2586                                                  */
2587                                                 
2588                                                 /* round 1: calculate previous and next */ 
2589                                                 tsb= tarray;
2590                                                 for (i=0; i < totSel; i++, tsb++) {
2591                                                         /* don't touch end points (otherwise, curves slowly explode) */
2592                                                         if (ELEM(i, 0, (totSel-1)) == 0) {
2593                                                                 tSmooth_Bezt *tP1 = tsb - 1;
2594                                                                 tSmooth_Bezt *tP2 = (i-2 > 0) ? (tsb - 2) : (NULL);
2595                                                                 tSmooth_Bezt *tN1 = tsb + 1;
2596                                                                 tSmooth_Bezt *tN2 = (i+2 < totSel) ? (tsb + 2) : (NULL);
2597                                                                 
2598                                                                 float p1 = *tP1->h2;
2599                                                                 float p2 = (tP2) ? (*tP2->h2) : (*tP1->h2);
2600                                                                 float c1 = *tsb->h2;
2601                                                                 float n1 = *tN1->h2;
2602                                                                 float n2 = (tN2) ? (*tN2->h2) : (*tN1->h2);
2603                                                                 
2604                                                                 /* calculate previous and next */
2605                                                                 *tsb->h1= (3*p2 + 5*p1 + 2*c1 + n1 + n2) / 12;
2606                                                                 *tsb->h3= (p2 + p1 + 2*c1 + 5*n1 + 3*n2) / 12;
2607                                                         }
2608                                                 }
2609                                                 
2610                                                 /* round 2: calculate new values and reset handles */
2611                                                 tsb= tarray;
2612                                                 for (i=0; i < totSel; i++, tsb++) {
2613                                                         /* calculate new position by averaging handles */
2614                                                         *tsb->h2 = (*tsb->h1 + *tsb->h3) / 2;
2615                                                         
2616                                                         /* reset handles now */
2617                                                         *tsb->h1 = *tsb->h2;
2618                                                         *tsb->h3 = *tsb->h2;
2619                                                 }
2620                                                 
2621                                                 /* free memory required for tarray */
2622                                                 MEM_freeN(tarray);
2623                                         }
2624                                 }
2625                                 
2626                                 /* recalculate handles */
2627                                 calchandles_ipocurve(icu);
2628                         }
2629                 }
2630         }
2631         
2632         editipo_changed(G.sipo, 1);
2633         BIF_undo_push("Smooth IPO");
2634 }
2635
2636 void join_ipo_menu(void)
2637 {
2638         int mode = 0;
2639         mode= pupmenu("Join %t|All Selected %x1|Selected Doubles %x2");
2640         
2641         if (mode == -1) return;
2642         
2643         join_ipo(mode);
2644 }
2645
2646 void join_ipo(int mode)
2647 {
2648         EditIpo *ei;
2649         IpoKey *ik;
2650         IpoCurve *icu;
2651         BezTriple *bezt, *beztn, *newb;
2652         float val;
2653         int tot, a, b;
2654         
2655         get_status_editipo();
2656         
2657         /* Mode events:
2658          * All Selected: 1
2659          * Selected Doubles: 2
2660          */
2661         
2662         if( mode==2 ) {
2663                 remove_doubles_ipo();
2664                 return;
2665         }
2666         
2667         /* first: multiple selected verts in 1 curve */
2668         ei= G.sipo->editipo;
2669         for(a=0; a<G.sipo->totipo; a++, ei++) {
2670                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2671                         if(G.sipo->showkey || (ei->flag & IPO_EDIT)) {
2672                                 icu= ei->icu;
2673                                 
2674                                 /* how many points */
2675                                 tot= 0;
2676                                 b= icu->totvert;
2677                                 bezt= icu->bezt;
2678                                 while(b--) {
2679                                         if(bezt->f2 & SELECT) tot++;
2680                                         bezt++;
2681                                 }
2682                                 
2683                                 if(tot>1) {
2684                                         tot--;
2685                                         icu->totvert-= tot;
2686                                         
2687                                         newb= MEM_mallocN(icu->totvert*sizeof(BezTriple), "bezt");
2688                                         /* the first point is the new one */
2689                                         beztn= newb+1;
2690                                         tot= 0;
2691                                         
2692                                         bezt= icu->bezt;
2693                                         b= icu->totvert+tot+1;
2694                                         while(b--) {
2695                                                 
2696                                                 if(bezt->f2 & SELECT) {
2697                                                         if(tot==0) *newb= *bezt;
2698                                                         else {
2699                                                                 VecAddf(newb->vec[0], newb->vec[0], bezt->vec[0]);
2700                                                                 VecAddf(newb->vec[1], newb->vec[1], bezt->vec[1]);
2701                                                                 VecAddf(newb->vec[2], newb->vec[2], bezt->vec[2]);
2702                                                         }
2703                                                         tot++;
2704                                                 }
2705                                                 else {
2706                                                         *beztn= *bezt;
2707                                                         beztn++;
2708                                                 }
2709                                                 bezt++;
2710                                         }
2711                                         
2712                                         VecMulf(newb->vec[0], (float)(1.0/((float)tot)));
2713                                         VecMulf(newb->vec[1], (float)(1.0/((float)tot)));
2714                                         VecMulf(newb->vec[2], (float)(1.0/((float)tot)));
2715                                         
2716                                         MEM_freeN(icu->bezt);
2717                                         icu->bezt= newb;
2718                                         
2719                                         sort_time_ipocurve(icu);
2720                                         calchandles_ipocurve(icu);
2721                                 }
2722                         }
2723                 }
2724         }
2725         
2726         /* next: in keymode: join multiple selected keys */
2727         
2728         editipo_changed(G.sipo, 1);     /* makes ipokeys again! */
2729         
2730         if(G.sipo->showkey) {
2731                 ik= G.sipo->ipokey.first;
2732                 val= 0.0;
2733                 tot= 0;
2734                 while(ik) {
2735                         if(ik->flag & 1) {
2736                                 for(a=0; a<G.sipo->totipo; a++) {
2737                                         if(ik->data[a]) {
2738                                                 val+= ik->data[a]->vec[1][0];
2739                                                 break;
2740                                         }
2741                                 }
2742                                 tot++;
2743                         }
2744                         ik= ik->next;
2745                 }
2746                 if(tot>1) {
2747                         val/= (float)tot;
2748                         
2749                         ik= G.sipo->ipokey.first;
2750                         while(ik) {
2751                                 if(ik->flag & 1) {
2752                                         for(a=0; a<G.sipo->totipo; a++) {
2753                                                 if(ik->data[a]) {
2754                                                         ik->data[a]->vec[1][0]= val;
2755                                                 }
2756                                         }
2757                                 }
2758                                 ik= ik->next;
2759                         }
2760                         editipo_changed(G.sipo, 0);
2761                 }
2762         }
2763         deselectall_editipo();
2764         BIF_undo_push("Join Ipo");
2765 }
2766
2767 void ipo_snap_menu(void)
2768 {
2769         short event;
2770         
2771         event= pupmenu("Snap %t|Horizontal %x1|To Next %x2|To Frame %x3|To Current Frame%x4");
2772         if(event < 1) return;
2773
2774         ipo_snap(event);
2775 }
2776
2777 void ipo_snap(short event)
2778 {
2779         EditIpo *ei;
2780         BezTriple *bezt;
2781         float dx = 0.0;
2782         int a, b;
2783         short ok, ok2;
2784         
2785         /* events:
2786          * Horizontal : 1
2787          * To Next: 2
2788          * To Frame: 3
2789          * To Current Frame: 4
2790          */
2791          
2792         get_status_editipo();
2793         
2794         /* map ipo-points for editing if scaled ipo */
2795         if (NLA_IPO_SCALED) {
2796                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 0, 0);
2797         }
2798
2799         ei= G.sipo->editipo;
2800         for(b=0; b<G.sipo->totipo; b++, ei++) {
2801                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2802                 
2803                         ok2= 0;
2804                         if(G.sipo->showkey) ok2= 1;
2805                         else if(totipo_vert && (ei->flag & IPO_EDIT)) ok2= 2;
2806                         else if(totipo_vert==0 && (ei->flag & IPO_SELECT)) ok2= 3;
2807                         
2808                         if(ok2) {
2809                                 bezt= ei->icu->bezt;
2810                                 a= ei->icu->totvert;
2811                                 while(a--) {
2812                                         ok= 0;
2813                                         if(totipo_vert) {
2814                                                 if(bezt->f2 & SELECT) ok= 1;
2815                                         }
2816                                         else ok= 1;
2817                                         
2818                                         if(ok) {
2819                                                 if(event==1) {
2820                                                         bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1];
2821                                                         if(bezt->h1==HD_AUTO || bezt->h1==HD_VECT) bezt->h1= HD_ALIGN;
2822                                                         if(bezt->h2==HD_AUTO || bezt->h2==HD_VECT) bezt->h2= HD_ALIGN;
2823                                                 }
2824                                                 else if(event==2) {
2825                                                         if(a) {
2826                                                                 bezt->vec[0][1]= bezt->vec[1][1]= bezt->vec[2][1]= (bezt+1)->vec[1][1];
2827                                                                 if(bezt->h1==HD_AUTO || bezt->h1==HD_VECT) bezt->h1= HD_ALIGN;
2828                                                                 if(bezt->h2==HD_AUTO || bezt->h2==HD_VECT) bezt->h2= HD_ALIGN;
2829                                                         }
2830                                                 }
2831                                                 else if(event==3) {
2832                                                         bezt->vec[1][0]= (float)(floor(bezt->vec[1][0]+0.5));
2833                                                 }
2834                                                 else if(event==4) {     /* to current frame */
2835                                                         
2836                                                         if(ok2==1 || ok2==2) {
2837                                                                 
2838                                                                 if(G.sipo->blocktype==ID_SEQ) {
2839                                                                         Sequence *seq;
2840                                                         
2841                                                                         seq= (Sequence *)G.sipo->from;
2842                                                                         if(seq && (seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
2843                                                                                 dx= (float)(CFRA-seq->startdisp);
2844                                                                                 dx= (float)(100.0*dx/((float)(seq->enddisp-seq->startdisp)));
2845                                                                                 
2846                                                                                 dx-= bezt->vec[1][0];
2847                                                                         } else {
2848                                                                                 dx= G.scene->r.framelen*CFRA - bezt->vec[1][0];
2849                                                                         }
2850                                                                 }
2851                                                                 else dx= G.scene->r.framelen*CFRA - bezt->vec[1][0];
2852                                                                 
2853                                                                 bezt->vec[0][0]+= dx;
2854                                                                 bezt->vec[1][0]+= dx;
2855                                                                 bezt->vec[2][0]+= dx;
2856                                                         }
2857                                                 }
2858                                         }
2859                                         
2860                                         bezt++;
2861                                 }
2862                                 calchandles_ipocurve(ei->icu);
2863                         }
2864                 }
2865         }
2866         
2867         /* undo mapping of ipo-points for editing if scaled ipo */
2868         if (NLA_IPO_SCALED) {
2869                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 1, 0);
2870         }
2871         
2872         editipo_changed(G.sipo, 1);
2873         BIF_undo_push("Snap Ipo");
2874 }
2875
2876 void ipo_mirror_menu(void)
2877 {
2878         int mode = 0;
2879         mode= pupmenu("Mirror Over%t|Current Frame%x1|Vertical Axis%x2|Horizontal Axis%x3");
2880         
2881         if (mode == -1) return;
2882         
2883         ipo_mirror(mode);
2884 }
2885
2886 void ipo_mirror(short mode)
2887 {
2888         EditIpo *ei;
2889         BezTriple *bezt;
2890         
2891         int a, b;
2892         short ok, ok2, i;
2893         float diff;
2894         
2895         /* what's this for? */
2896         get_status_editipo();
2897
2898         /* get edit ipo */
2899         ei= G.sipo->editipo;
2900         if (!ei) return;
2901         
2902         /* map ipo-points for editing if scaled ipo */
2903         if (NLA_IPO_SCALED) {
2904                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 0, 0);
2905         }
2906         
2907         /* look throught ipo curves */
2908         for(b=0; b<G.sipo->totipo; b++, ei++) {
2909                 if (ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt)) {
2910                 
2911                         ok2= 0;
2912                         if(G.sipo->showkey) ok2= 1;
2913                         else if(totipo_vert && (ei->flag & IPO_EDIT)) ok2= 2;
2914                         else if(totipo_vert==0 && (ei->flag & IPO_SELECT)) ok2= 3;
2915                         
2916                         if(ok2) {
2917                                 bezt= ei->icu->bezt;
2918                                 a= ei->icu->totvert;
2919                                 
2920                                 /* loop through beztriples, mirroring them */
2921                                 while(a--) {
2922                                         ok= 0;
2923                                   &nb