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