f08c78cb0c215abcde410f4338511ca4d306bf53
[blender-staging.git] / source / blender / src / editipo.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2005. Full recode
24  *
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_armature.h"
76 #include "BKE_anim.h"
77 #include "BKE_constraint.h"
78 #include "BKE_depsgraph.h"
79 #include "BKE_global.h"
80 #include "BKE_group.h"
81 #include "BKE_ipo.h"
82 #include "BKE_key.h"
83 #include "BKE_material.h"
84 #include "BKE_texture.h"
85 #include "BKE_utildefines.h"
86
87 #include "BIF_butspace.h"
88 #include "BIF_editaction.h"
89 #include "BIF_editconstraint.h"
90 #include "BIF_editkey.h"
91 #include "BIF_editnla.h"
92 #include "BIF_editseq.h"
93 #include "BIF_editview.h"
94 #include "BIF_interface.h"
95 #include "BIF_mywindow.h"
96 #include "BIF_poseobject.h"
97 #include "BIF_screen.h"
98 #include "BIF_space.h"
99 #include "BIF_toolbox.h"
100 #include "BIF_poseobject.h"
101
102 #include "BDR_drawobject.h"
103 #include "BDR_editobject.h"
104 #include "BDR_editcurve.h"      // for bezt_compare 
105
106 #include "BSE_trans_types.h"
107 #include "BSE_editipo_types.h"
108 #include "BSE_drawipo.h"
109 #include "BSE_editipo.h"
110 #include "BSE_edit.h"
111 #include "BSE_drawview.h"
112 #include "BSE_headerbuttons.h"
113 #include "BSE_node.h"
114 #include "BSE_sequence.h"
115 #include "BSE_time.h"
116
117 #include "blendef.h"
118 #include "mydevice.h"
119
120 extern int ob_ar[];
121 extern int ma_ar[];
122 extern int seq_ar[];
123 extern int cu_ar[];
124 extern int wo_ar[];
125 extern int la_ar[];
126 extern int cam_ar[];
127 extern int snd_ar[];
128 extern int ac_ar[];
129 extern int co_ar[];
130 extern int te_ar[];
131 extern int fluidsim_ar[]; // NT
132
133 /* forwards */
134 #define IPOTHRESH       0.9
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) free_imbuf_seq_with_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         /* sets globals, bad stuff but we need these variables in other parts of code */
924         get_status_editipo();
925         
926         if(G.sipo->ipo) {
927
928                 if (G.sipo->pin)
929                         rf= &(G.sipo->v2d.cur);
930                 else
931                         rf= &(G.sipo->ipo->cur);
932                 
933                 if(rf->xmin<rf->xmax && rf->ymin<rf->ymax) G.v2d->cur= *rf;
934                 else ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
935         }
936         else {
937                 ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
938         }
939         
940         view2d_do_locks(curarea, V2D_LOCK_COPY);
941 }
942
943 /* evaluates context in the current UI */
944 /* blocktype is type of ipo */
945 /* from is the base pointer to find data to change (ob in case of action or pose) */
946 static void get_ipo_context(short blocktype, ID **from, Ipo **ipo, char *actname, char *constname)
947 {
948         Object *ob= OBACT;
949         
950         *from= NULL;
951         *ipo= NULL;
952         
953         if(blocktype==ID_CO) {
954                 if (ob) {
955                         bConstraintChannel *chan;
956                         bConstraint *con= get_active_constraint(ob);
957                         
958                         if(con) {
959                                 BLI_strncpy(constname, con->name, 32);
960                                 
961                                 chan= get_active_constraint_channel(ob);
962                                 if(chan) {
963                                         *ipo= chan->ipo;
964                                         BLI_strncpy(constname, con->name, 32);
965                                 }
966                                 
967                                 *from= &ob->id;
968                                 
969                                 /* set actname if in posemode */
970                                 if(ob->action) {
971                                         if(ob->flag & OB_POSEMODE) {
972                                                 bPoseChannel *pchan= get_active_posechannel(ob);
973                                                 if(pchan)
974                                                         BLI_strncpy(actname, pchan->name, 32);
975                                         }
976                                         else if(ob->ipoflag & OB_ACTION_OB)
977                                                 strcpy(actname, "Object");
978                                 }
979                         }
980                 }
981         }
982         else if(blocktype==ID_PO) {
983                 if (ob && ob->action && ob->type==OB_ARMATURE) {
984                         bPoseChannel *pchan= get_active_posechannel(ob);
985                         
986                         *from= (ID *)ob;
987                         if (pchan) {
988                                 bActionChannel *achan;
989                                 
990                                 BLI_strncpy(actname, pchan->name, 32);  /* also set when no channel yet */
991                                 
992                                 achan= get_action_channel(ob->action, pchan->name);
993                                 if(achan)
994                                         *ipo= achan->ipo;
995                         }
996                 } 
997                 
998         }
999         else if(blocktype==ID_OB) {
1000                 if(ob) {
1001                         *from= (ID *)ob;
1002                         if(ob->ipoflag & OB_ACTION_OB) {
1003                                 if (ob->action) {
1004                                         bActionChannel *achan= get_action_channel(ob->action, "Object");
1005                                         if(achan) {
1006                                                 *ipo= achan->ipo;
1007                                                 BLI_strncpy(actname, achan->name, 32);
1008                                         }
1009                                 }
1010                         }
1011                         else {
1012                                 *ipo= ob->ipo;
1013                         }
1014                 }
1015         }
1016         else if(blocktype==ID_SEQ) {
1017                 Sequence *last_seq = get_last_seq();
1018                 
1019                 if(last_seq && ((last_seq->type & SEQ_EFFECT)||(last_seq->type == SEQ_HD_SOUND)||(last_seq->type == SEQ_RAM_SOUND))) {
1020                         *from= (ID *)last_seq;
1021                         *ipo= last_seq->ipo;
1022                 }
1023         }
1024         else if(blocktype==ID_WO) {
1025                 World *wo= G.scene->world;
1026                 *from= (ID *)wo;
1027                 if(wo) *ipo= wo->ipo;
1028         }
1029         else if(blocktype==ID_TE) {
1030                 if(ob) {
1031                         Tex *tex= give_current_texture(ob, ob->actcol);
1032                         *from= (ID *)tex;
1033                         if(tex) *ipo= tex->ipo;
1034                 }
1035         }
1036         else if(blocktype==ID_MA) {
1037                 if(ob) {
1038                         Material *ma= give_current_material(ob, ob->actcol);
1039                         ma= editnode_get_active_material(ma);
1040                         *from= (ID *)ma;
1041                         if(ma) *ipo= ma->ipo;
1042                 }
1043         }
1044         else if(blocktype==ID_KE) {
1045                 if(ob) {
1046                         Key *key= ob_get_key(ob);
1047                         
1048                         if(ob->ipoflag & OB_ACTION_KEY) {
1049                                 if (ob->action) {
1050                                         bActionChannel *achan= get_action_channel(ob->action, "Shape");
1051                                         if(achan) {
1052                                                 *ipo= achan->ipo;
1053                                                 BLI_strncpy(actname, achan->name, 32);
1054                                         }
1055                                 }
1056                         }
1057                         else if(key) *ipo= key->ipo;
1058                         
1059                         *from= (ID *)ob;
1060                 }
1061         }
1062         else if(blocktype==ID_CU) {
1063                 if(ob && ob->type==OB_CURVE) {
1064                         Curve *cu= ob->data;
1065                         *from= (ID *)cu;
1066                         *ipo= cu->ipo;
1067                 }
1068         }
1069         else if(blocktype==ID_LA) {
1070                 if(ob && ob->type==OB_LAMP) {
1071                         Lamp *la= ob->data;
1072                         *from= (ID *)la;
1073                         *ipo= la->ipo;
1074                 }
1075         }
1076         else if(blocktype==ID_CA) {
1077                 if(ob && ob->type==OB_CAMERA) {
1078                         Camera *ca= ob->data;
1079                         *from= (ID *)ca;
1080                         if(ca) *ipo= ca->ipo;
1081                 }
1082         }
1083         else if(blocktype==ID_SO) {
1084                 
1085                 //              if (G.buts && G.buts->mainb == BUTS_SOUND) {
1086                 //                      bSound *sound = G.buts->lockpoin;
1087                 //                      *from= (ID *)sound;
1088                 //                      if(sound) *ipo= sound->ipo;
1089                 //              }
1090         }
1091         else if(blocktype==ID_FLUIDSIM) {
1092                 if(ob && ( ob->fluidsimFlag & OB_FLUIDSIM_ENABLE)) {
1093                         FluidsimSettings *fss= ob->fluidsimSettings;
1094                         *from= (ID *)ob;
1095                         if(fss) *ipo= fss->ipo;
1096                 }
1097         }
1098 }
1099
1100 /* called on each redraw, check if editipo data has to be remade */
1101 /* if doit already set, it always makes (in case no ipo exists, we need to see the channels */
1102 void test_editipo(int doit)
1103 {
1104         
1105         if(G.sipo->pin==0) {
1106                 Ipo *ipo;
1107                 ID *from;
1108                 char actname[32]="", constname[32]="";
1109                 
1110                 get_ipo_context(G.sipo->blocktype, &from, &ipo, actname, constname);
1111                 
1112                 if(G.sipo->ipo != ipo) {
1113                         G.sipo->ipo= ipo;
1114                         if(ipo) G.v2d->cur= ipo->cur;
1115                         doit= 1;
1116                 }
1117                 if(G.sipo->from != from) {
1118                         G.sipo->from= from;
1119                         doit= 1;
1120                 }
1121                 if( strcmp(G.sipo->actname, actname)) {
1122                         BLI_strncpy(G.sipo->actname, actname, 32);
1123                         doit= 1;
1124                 }
1125                 if( strcmp(G.sipo->constname, constname)) {
1126                         BLI_strncpy(G.sipo->constname, constname, 32);
1127                         doit= 1;
1128                 }
1129                 
1130                 if(G.sipo->ipo)
1131                         G.sipo->ipo->cur = G.v2d->cur;
1132                 
1133         }
1134                 
1135         if(G.sipo->editipo==NULL || doit) {
1136                 make_editipo();
1137         }
1138 }
1139
1140 /* ****************** EditIpo ************************ */
1141
1142 int totipo_edit=0, totipo_sel=0, totipo_curve=0, totipo_vis=0, totipo_vert=0, totipo_vertsel=0, totipo_key=0, totipo_keysel=0;
1143
1144 void get_status_editipo(void)
1145 {
1146         EditIpo *ei;
1147         IpoKey *ik;
1148         BezTriple *bezt;
1149         int a, b;
1150         
1151         totipo_vis= 0;
1152         totipo_curve= 0;
1153         totipo_sel= 0;
1154         totipo_edit= 0;
1155         totipo_vert= 0;
1156         totipo_vertsel= 0;
1157         totipo_key= 0;
1158         totipo_keysel= 0;
1159         
1160         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1161         
1162         ei= G.sipo->editipo;
1163         if(ei==0) return;
1164         for(a=0; a<G.sipo->totipo; a++) {
1165                 if( ei->flag & IPO_VISIBLE ) {
1166                         totipo_vis++;
1167                         if(ei->flag & IPO_SELECT) totipo_sel++;
1168                         if(ei->icu && ei->icu->totvert) totipo_curve++;
1169                         if(G.sipo->showkey || (ei->flag & IPO_EDIT)) {
1170                                 
1171                                 /* if showkey: do count the vertices (for grab) */
1172                                 if(G.sipo->showkey==0) totipo_edit++;
1173                                 
1174                                 if(ei->icu) {
1175                                         if(ei->icu->bezt) {
1176                                                 bezt= ei->icu->bezt;
1177                                                 b= ei->icu->totvert;
1178                                                 while(b--) {
1179                                                         if(ei->icu->ipo==IPO_BEZ) {
1180                                                                 if(bezt->f1 & 1) totipo_vertsel++;
1181                                                                 if(bezt->f3 & 1) totipo_vertsel++;
1182                                                                 totipo_vert+= 2;
1183                                                         }
1184                                                         if(bezt->f2 & 1) totipo_vertsel++;
1185                                                         
1186                                                         totipo_vert++;
1187                                                         bezt++;
1188                                                 }
1189                                         }
1190                                 }
1191                         }
1192                 }
1193                 ei++;
1194         }
1195         
1196         if(G.sipo->showkey) {
1197                 ik= G.sipo->ipokey.first;
1198                 while(ik) {
1199                         totipo_key++;
1200                         if(ik->flag & 1) totipo_keysel++;
1201                         ik= ik->next;
1202                 }
1203         }
1204 }
1205
1206 /* synchronize editipo flag with icu flag and ipokey flags */
1207 void update_editipo_flags(void)
1208 {
1209         EditIpo *ei;
1210         IpoKey *ik;
1211         int a;
1212         
1213         ei= G.sipo->editipo;
1214         if(ei) {
1215                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1216                         if(ei->icu) ei->icu->flag= ei->flag;
1217                 }
1218         }
1219         if(G.sipo->showkey) {
1220                 ik= G.sipo->ipokey.first;
1221                 while(ik) {
1222                         for(a=0; a<G.sipo->totipo; a++) {
1223                                 if(ik->data[a]) {
1224                                         if(ik->flag & 1) {
1225                                                 ik->data[a]->f1 |= 1;
1226                                                 ik->data[a]->f2 |= 1;
1227                                                 ik->data[a]->f3 |= 1;
1228                                         }
1229                                         else {
1230                                                 ik->data[a]->f1 &= ~1;
1231                                                 ik->data[a]->f2 &= ~1;
1232                                                 ik->data[a]->f3 &= ~1;
1233                                         }
1234                                 }
1235                         }
1236                         ik= ik->next;
1237                 }
1238         }
1239 }
1240
1241 /* sort of enter/leave editmode for curves */
1242 void set_editflag_editipo(void)
1243 {
1244         EditIpo *ei;
1245         int a; /*  , tot= 0, ok= 0; */
1246         
1247         /* after showkey immediately go to editing of selected points */
1248         if(G.sipo->showkey) {
1249                 G.sipo->showkey= 0;
1250                 if(G.sipo->ipo) G.sipo->ipo->showkey= 0;
1251                 ei= G.sipo->editipo;
1252                 for(a=0; a<G.sipo->totipo; a++, ei++) ei->flag |= IPO_SELECT;
1253                 scrarea_queue_headredraw(curarea);
1254                 allqueue(REDRAWVIEW3D, 0);
1255         }
1256         
1257         get_status_editipo();
1258         
1259         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1260         
1261         ei= G.sipo->editipo;
1262         for(a=0; a<G.sipo->totipo; a++, ei++) {         
1263                 if(ei->icu) {
1264                         if(ei->flag & IPO_VISIBLE) {
1265                                 
1266                                 if(totipo_edit==0 && (ei->flag & IPO_SELECT)) {
1267                                         ei->flag |= IPO_EDIT;
1268                                         ei->icu->flag= ei->flag;
1269                                 }
1270                                 else if(totipo_edit && (ei->flag & IPO_EDIT)) {
1271                                         ei->flag -= IPO_EDIT;
1272                                         ei->icu->flag= ei->flag;
1273                                 }
1274                                 else if(totipo_vis==1) {
1275                                         if(ei->flag & IPO_EDIT) ei->flag -= IPO_EDIT;
1276                                         else ei->flag |= IPO_EDIT;
1277                                         ei->icu->flag= ei->flag;
1278                                 }
1279                         }
1280                 }
1281         }
1282         scrarea_queue_headredraw(curarea);
1283         scrarea_queue_winredraw(curarea);
1284 }
1285
1286 static short findnearest_ipovert(IpoCurve **icu, BezTriple **bezt)
1287 {
1288         /* selected verts get a disadvantage */
1289         /* in icu and (bezt or bp) the nearest is written */
1290         /* return 0 1 2: handlepunt */
1291         EditIpo *ei;
1292         BezTriple *bezt1;
1293         int dist= 100, temp, a, b;
1294         short mval[2], hpoint=0, sco[3][2];
1295
1296         *icu= 0;
1297         *bezt= 0;
1298
1299         getmouseco_areawin(mval);
1300
1301         ei= G.sipo->editipo;
1302         for(a=0; a<G.sipo->totipo; a++, ei++) {
1303                 if (ISPOIN3(ei, flag & IPO_VISIBLE, flag & IPO_EDIT, icu)) {
1304                         
1305                         if(ei->icu->bezt) {
1306                                 bezt1= ei->icu->bezt;
1307                                 b= ei->icu->totvert;
1308                                 while(b--) {
1309
1310                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[0], sco[0]);
1311                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[1], sco[1]);
1312                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[2], sco[2]);
1313                                                                                 
1314                                         if(ei->disptype==IPO_DISPBITS) {
1315                                                 temp= abs(mval[0]- sco[1][0]);
1316                                         }
1317                                         else temp= abs(mval[0]- sco[1][0])+ abs(mval[1]- sco[1][1]);
1318
1319                                         if( bezt1->f2 & 1) temp+=5;
1320                                         if(temp<dist) { 
1321                                                 hpoint= 1; 
1322                                                 *bezt= bezt1; 
1323                                                 dist= temp; 
1324                                                 *icu= ei->icu; 
1325                                         }
1326                                         
1327                                         if(ei->disptype!=IPO_DISPBITS && ei->icu->ipo==IPO_BEZ) {
1328                                                 /* middle points get an advantage */
1329                                                 temp= -3+abs(mval[0]- sco[0][0])+ abs(mval[1]- sco[0][1]);
1330                                                 if( bezt1->f1 & 1) temp+=5;
1331                                                 if(temp<dist) { 
1332                                                         hpoint= 0; 
1333                                                         *bezt= bezt1; 
1334                                                         dist= temp; 
1335                                                         *icu= ei->icu; 
1336                                                 }
1337                 
1338                                                 temp= abs(mval[0]- sco[2][0])+ abs(mval[1]- sco[2][1]);
1339                                                 if( bezt1->f3 & 1) temp+=5;
1340                                                 if(temp<dist) { 
1341                                                         hpoint= 2; 
1342                                                         *bezt=bezt1; 
1343                                                         dist= temp; 
1344                                                         *icu= ei->icu; 
1345                                                 }
1346                                         }
1347                                         bezt1++;
1348                                 }
1349                         }
1350                 }
1351         }
1352
1353         return hpoint;
1354 }
1355
1356 void mouse_select_ipo(void)
1357 {
1358         Object *ob;
1359         KeyBlock *actkb=NULL;
1360         EditIpo *ei, *actei= 0;
1361         IpoCurve *icu;
1362         IpoKey *ik, *actik;
1363         BezTriple *bezt;
1364         TimeMarker *marker;
1365         float x, y, dist, mindist;
1366         int a, oldflag = 0, hand, ok;
1367         short mval[2], xo, yo;
1368         
1369         if(G.sipo->editipo==0) return;
1370         
1371         get_status_editipo();
1372         marker=find_nearest_marker(1);
1373         
1374         /* map ipo-points for editing if scaled ipo */
1375         if (NLA_IPO_SCALED) {
1376                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 0, 0);
1377         }
1378         
1379         if(G.sipo->showkey) {
1380                 getmouseco_areawin(mval);
1381                 
1382                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1383                 actik= 0;
1384                 mindist= 1000.0;
1385                 ik= G.sipo->ipokey.first;
1386                 while(ik) {
1387                         dist= (float)(fabs(ik->val-x));
1388                         if(ik->flag & 1) dist+= 1.0;
1389                         if(dist < mindist) {
1390                                 actik= ik;
1391                                 mindist= dist;
1392                         }
1393                         ik= ik->next;
1394                 }
1395                 if(actik) {
1396                         oldflag= actik->flag;
1397                         
1398                         if(G.qual & LR_SHIFTKEY);
1399                         else deselectall_editipo();
1400                         
1401                         if(G.qual & LR_SHIFTKEY) {
1402                                 if(oldflag & 1) actik->flag &= ~1;
1403                                 else actik->flag |= 1;
1404                         }
1405                         else {
1406                                 actik->flag |= 1;
1407                         }
1408                 }
1409         }
1410         else if(totipo_edit) {
1411                 
1412                 hand= findnearest_ipovert(&icu, &bezt);
1413                 
1414                 if(G.qual & LR_SHIFTKEY) {
1415                         if(bezt) {
1416                                 if(hand==1) {
1417                                         if(BEZSELECTED(bezt)) {
1418                                                 bezt->f1= bezt->f2= bezt->f3= 0;
1419                                         }
1420                                         else {
1421                                                 bezt->f1= bezt->f2= bezt->f3= 1;
1422                                         }
1423                                 }
1424                                 else if(hand==0) {
1425                                         if(bezt->f1 & 1) bezt->f1= 0;
1426                                         else bezt->f1= 1;
1427                                 }
1428                                 else {
1429                                         if(bezt->f3 & 1) bezt->f3= 0;
1430                                         else bezt->f3= 1;
1431                                 }
1432                         }                               
1433                 }
1434                 else {
1435                         deselectall_editipo();
1436                         
1437                         if(bezt) {
1438                                 if(hand==1) {
1439                                         bezt->f1|= 1; bezt->f2|= 1; bezt->f3|= 1;
1440                                 }
1441                                 else if(hand==0) bezt->f1|= 1;
1442                                 else bezt->f3|= 1;
1443                         }
1444                 }
1445         }
1446         else if (marker) {
1447                 /* select timeline marker */
1448                 if ((G.qual & LR_SHIFTKEY)==0) {
1449                         oldflag= marker->flag;
1450                         deselect_markers(0, 0);
1451                         
1452                         if (oldflag & SELECT)
1453                                 marker->flag &= ~SELECT;
1454                         else
1455                                 marker->flag |= SELECT;
1456                 }
1457                 else {
1458                         marker->flag |= SELECT;                         
1459                 }               
1460         }
1461         else {
1462                 
1463                 /* vertex keys ? */
1464                 if(G.sipo->blocktype==ID_KE && G.sipo->from) {
1465                         Key *key;
1466                         KeyBlock *kb, *curkb;
1467                         int i, index= 1;
1468                         
1469                         ob= (Object *)G.sipo->from;
1470                         key= ob_get_key(ob);
1471                         curkb= BLI_findlink(&key->block, ob->shapenr-1);
1472                         
1473                         ei= G.sipo->editipo;
1474                         if(key->type==KEY_NORMAL || (ei->flag & IPO_VISIBLE)) {
1475                                 getmouseco_areawin(mval);
1476                                 
1477                                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1478                                 /* how much is 20 pixels? */
1479                                 mindist= (float)(20.0*(G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)curarea->winy);
1480                                 
1481                                 for(i=1, kb= key->block.first; kb; kb= kb->next, i++) {
1482                                         dist= (float)(fabs(kb->pos-y));
1483                                         if(kb==curkb) dist+= (float)0.01;
1484                                         if(dist < mindist) {
1485                                                 actkb= kb;
1486                                                 mindist= dist;
1487                                                 index= i;
1488                                         }
1489                                 }
1490                                 if(actkb) {
1491                                         ok= TRUE;
1492                                         if(G.obedit && actkb!=curkb) {
1493                                                 ok= okee("Copy key after leaving Edit Mode");
1494                                         }
1495                                         if(ok) {
1496                                                 /* also does all keypos */
1497                                                 deselectall_editipo();
1498                                                 set_active_key(index);
1499                                                 set_active_editipo(ei+index-1);
1500                                         }
1501                                 }
1502                         }
1503                 }
1504                 
1505                 /* select curve */
1506                 if(actkb==NULL) {
1507                         if(totipo_vis==1) {
1508                                 ei= G.sipo->editipo;
1509                                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1510                                         if(ei->icu) {
1511                                                 if(ei->flag & IPO_VISIBLE) actei= ei;
1512                                         }
1513                                 }
1514                         }
1515                         else if(totipo_vis>1) {
1516                                 actei= select_proj_ipo(0, 0);
1517                         }
1518                         
1519                         if(actei) oldflag= actei->flag;
1520                         
1521                         if(G.qual & LR_SHIFTKEY);
1522                         else deselectall_editipo();
1523                         
1524                         if(actei) {
1525                                 if(G.qual & LR_SHIFTKEY) {
1526                                         if(oldflag & IPO_SELECT) actei->flag &= ~IPO_SELECT;
1527                                         else actei->flag |= IPO_SELECT;
1528                                 }
1529                                 else {
1530                                         actei->flag |= IPO_SELECT;
1531                                 }
1532                                 set_active_editipo(actei);
1533                         }
1534                 }
1535         }
1536         
1537         /* undo mapping of ipo-points for editing if scaled ipo */
1538         if (NLA_IPO_SCALED) {
1539                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 1, 0);
1540         }
1541         
1542         update_editipo_flags();
1543         
1544         force_draw(0);
1545         BIF_undo_push("Select Ipo");
1546         
1547         if(G.sipo->showkey && G.sipo->blocktype==ID_OB) {
1548                 ob= OBACT;
1549                 if(ob && (ob->ipoflag & OB_DRAWKEY)) allqueue(REDRAWVIEW3D, 0);
1550         }
1551         /* points inside of curve are drawn selected too */
1552         if(G.sipo->blocktype==ID_CU)
1553                 allqueue(REDRAWVIEW3D, 0);
1554         
1555         getmouseco_areawin(mval);
1556         xo= mval[0]; 
1557         yo= mval[1];
1558         
1559         while(get_mbut()&R_MOUSE) {             
1560                 getmouseco_areawin(mval);
1561                 if(abs(mval[0]-xo)+abs(mval[1]-yo) > 4) {
1562                         
1563                         if (marker) {
1564                                 transform_markers('g', 0);
1565                         }
1566                         else {
1567                                 if(actkb) move_keys(OBACT);
1568                                 else transform_ipo('g');
1569                         }
1570                         
1571                         return;
1572                 }
1573                 BIF_wait_for_statechange();
1574         }
1575 }
1576
1577
1578 /* *********************************** */
1579
1580 /* handling of right-hand channel/curve buttons in ipo window */
1581 void do_ipowin_buts(short event)
1582 {
1583         EditIpo *ei = NULL;
1584         int a;
1585
1586         /* without shift, all other channels are made invisible */
1587         if((G.qual & LR_SHIFTKEY)==0) {
1588                 if(event>G.sipo->totipo) return;
1589                 ei = G.sipo->editipo;
1590                 for(a=0; a<G.sipo->totipo; a++) {
1591                         if(a!=event) ei->flag &= ~IPO_VISIBLE;
1592                         else ei->flag |= IPO_VISIBLE;
1593                         ei++;
1594                 }
1595         }
1596         
1597         /* set active */
1598         if(event>=0 && event<G.sipo->totipo) {
1599                 ei= G.sipo->editipo;    // void pointer...
1600                 set_active_editipo(ei+event);
1601                 set_active_key(event+1);        // only if there's a key, of course
1602         }
1603         scrarea_queue_winredraw(curarea);
1604         
1605         update_editipo_flags();
1606         get_status_editipo();
1607
1608         if(G.sipo->showkey) {
1609                 make_ipokey();
1610                 if(G.sipo->blocktype==ID_OB) allqueue(REDRAWVIEW3D, 0);
1611         }
1612
1613 }
1614
1615 /* the fake buttons to the left of channel names, for select/deselect curves */
1616 void do_ipo_selectbuttons(void)
1617 {
1618         EditIpo *ei, *ei1;
1619         int a, nr;
1620         short mval[2];
1621         
1622         if(G.sipo->showkey) return;
1623         
1624         /* do not allow editipo here: convert editipos to selected */
1625         get_status_editipo();
1626         if(totipo_edit) {
1627                 set_editflag_editipo();
1628         }
1629         
1630         /* which */
1631         getmouseco_areawin(mval);
1632
1633         nr= -(mval[1]-curarea->winy+30-G.sipo->butofs-IPOBUTY)/IPOBUTY;
1634         if(G.sipo->blocktype==ID_KE) nr--;              /* keys show something else in first channel */
1635         
1636         if(nr>=0 && nr<G.sipo->totipo) {
1637                 ei= G.sipo->editipo;
1638                 ei+= nr;
1639                 
1640                 set_active_editipo(ei);
1641                 set_active_key(nr+1);
1642
1643                 if(ei->icu) {
1644                         if((ei->flag & IPO_VISIBLE)==0) {
1645                                 ei->flag |= IPO_VISIBLE|IPO_SELECT;
1646                         }
1647         
1648                         if((G.qual & LR_SHIFTKEY)==0) {
1649                                 ei1= G.sipo->editipo;
1650                                 for(a=0; a<G.sipo->totipo; a++) {
1651                                         ei1->flag &= ~IPO_SELECT;
1652                                         ei1++;
1653                                 }
1654                         }
1655
1656                         if(ei->flag & IPO_SELECT) {
1657                                 ei->flag &= ~IPO_SELECT;
1658                         }
1659                         else {
1660                                 ei->flag |= IPO_SELECT;
1661                         }
1662                         
1663                         update_editipo_flags();
1664                         scrarea_queue_winredraw(curarea);
1665                 }
1666         }
1667         BIF_undo_push("Select Ipo curve");
1668 }
1669
1670 /* ********************************* Inserting keys ********************************************* */
1671
1672 /* depending type, it returns ipo, if needed it creates one */
1673 /* returns either action ipo or "real" ipo */
1674 /* arguments define full context;
1675    - *from has to be set always, to Object in case of Actions
1676    - blocktype defines available channels of Ipo struct (blocktype ID_OB can be in action too)
1677    - if actname, use this to locate action, and optional constname to find the channel 
1678 */
1679
1680 /* note; check header_ipo.c, spaceipo_assign_ipo() too */
1681 Ipo *verify_ipo(ID *from, short blocktype, char *actname, char *constname)
1682 {
1683
1684         if(from==NULL || from->lib) return NULL;
1685         
1686         /* first check action ipos */
1687         if(actname && actname[0]) {
1688                 Object *ob= (Object *)from;
1689                 bActionChannel *achan;
1690                 
1691                 if(GS(from->name)!=ID_OB) {
1692                         printf("called ipo system for action with wrong base pointer\n");
1693                         return NULL;
1694                 }
1695                 
1696                 if(ob->action==NULL)
1697                         ob->action= add_empty_action("Action");
1698                 
1699                 achan= verify_action_channel(ob->action, actname);
1700                 
1701                 if(achan) {
1702                         /* constraint exception */
1703                         if(blocktype==ID_CO) {
1704                                 bConstraintChannel *conchan= verify_constraint_channel(&achan->constraintChannels, constname);
1705                                 if(conchan->ipo==NULL) {
1706                                         conchan->ipo= add_ipo("CoIpo", ID_CO);  
1707                                 }
1708                                 return conchan->ipo;
1709                         }
1710                         else {
1711                                 if(achan->ipo==NULL) {
1712                                         achan->ipo= add_ipo("ActIpo", blocktype);
1713                                 }
1714                                 
1715                                 return achan->ipo;
1716                         }
1717                 }
1718         }
1719         else {
1720                 
1721                 switch(GS(from->name)) {
1722                 case ID_OB:
1723                         {
1724                                 Object *ob= (Object *)from;
1725                                 /* constraint exception */
1726                                 if(blocktype==ID_CO) {
1727                                         bConstraintChannel *conchan= verify_constraint_channel(&ob->constraintChannels, constname);
1728                                         if(conchan->ipo==NULL) {
1729                                                 conchan->ipo= add_ipo("CoIpo", ID_CO);  
1730                                         }
1731                                         return conchan->ipo;
1732                                 }
1733                                 else if(blocktype==ID_OB) {
1734                                         if(ob->ipo==NULL) {
1735                                                 ob->ipo= add_ipo("ObIpo", ID_OB);
1736                                         }
1737                                         return ob->ipo;
1738                                 }
1739                                 else if(blocktype==ID_KE) {
1740                                         Key *key= ob_get_key((Object *)from);
1741                                         
1742                                         if(key) {
1743                                                 if(key->ipo==NULL) {
1744                                                         key->ipo= add_ipo("KeyIpo", ID_KE);
1745                                                 }
1746                                                 return key->ipo;
1747                                         }
1748                                         return NULL;
1749                                 }
1750                                 else if(blocktype== ID_FLUIDSIM) {
1751                                         Object *ob= (Object *)from;
1752                                         if(ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) {
1753                                                 FluidsimSettings *fss= ob->fluidsimSettings;
1754                                                 if(fss->ipo==NULL) {
1755                                                         fss->ipo= add_ipo("FluidsimIpo", ID_FLUIDSIM);
1756                                                         //fprintf(stderr,"FSIPO NEW!\n");
1757                                                 }
1758                                                 return fss->ipo;
1759                                         }
1760                                 }
1761                         }
1762                         break;
1763                 case ID_MA:
1764                         {
1765                                 Material *ma= (Material *)from;
1766
1767                                 if(ma->ipo==NULL) {
1768                                         ma->ipo= add_ipo("MatIpo", ID_MA);
1769                                 }
1770                                 return ma->ipo;
1771                         }
1772                         break;
1773                 case ID_TE:
1774                         {
1775                                 Tex *tex= (Tex *)from;
1776
1777                                 if(tex->ipo==NULL) {
1778                                         tex->ipo= add_ipo("TexIpo", ID_TE);
1779                                 }
1780                                 return tex->ipo;
1781                         }
1782                         break;
1783                 case ID_SEQ:
1784                         {
1785                                 Sequence *seq= (Sequence *)from;        /* note, sequence is mimicing Id */
1786
1787                                 if((seq->type & SEQ_EFFECT)||
1788                                    (seq->type == SEQ_RAM_SOUND)||
1789                                    (seq->type == SEQ_HD_SOUND)) {
1790                                         if(seq->ipo==NULL) {
1791                                                 seq->ipo= add_ipo("SeqIpo", ID_SEQ);
1792                                         }
1793                                         update_seq_ipo_rect(seq);
1794                                         return seq->ipo;
1795                                 }
1796                         }
1797                         break;
1798                 case ID_CU:
1799                         {
1800                                 Curve *cu= (Curve *)from;
1801                                 
1802                                 if(cu->ipo==NULL) {
1803                                         cu->ipo= add_ipo("CuIpo", ID_CU);
1804                                 }
1805                                 return cu->ipo;
1806                         }
1807                         break;
1808                 case ID_WO:
1809                         {
1810                                 World *wo= (World *)from;
1811
1812                                 if(wo->ipo==NULL) {
1813                                         wo->ipo= add_ipo("WoIpo", ID_WO);
1814                                 }
1815                                 return wo->ipo;
1816                         }
1817                         break;
1818                 case ID_LA:
1819                         {
1820                                 Lamp *la= (Lamp *)from;
1821                                 
1822                                 if(la->ipo==NULL) {
1823                                         la->ipo= add_ipo("LaIpo", ID_LA);
1824                                 }
1825                                 return la->ipo;
1826                         }
1827                         break;
1828                 case ID_CA:
1829                         {
1830                                 Camera *ca= (Camera *)from;
1831
1832                                 if(ca->ipo==NULL) {
1833                                         ca->ipo= add_ipo("CaIpo", ID_CA);
1834                                 }
1835                                 return ca->ipo;
1836                         }
1837                         break;
1838                 case ID_SO:
1839                         {
1840                                 bSound *snd= (bSound *)from;
1841
1842                                 if(snd->ipo==NULL) {
1843                                         snd->ipo= add_ipo("SndIpo", ID_SO);
1844                                 }
1845                                 return snd->ipo;
1846                         }
1847                 }
1848         }
1849         
1850         return NULL;    
1851 }
1852
1853 /* returns and creates
1854  * Make sure functions check for NULL or they will crash!
1855  *  */
1856 IpoCurve *verify_ipocurve(ID *from, short blocktype, char *actname, char *constname, int adrcode)
1857 {
1858         Ipo *ipo;
1859         IpoCurve *icu= NULL;
1860         
1861         /* return 0 if lib */
1862         /* creates ipo too */
1863         ipo= verify_ipo(from, blocktype, actname, constname);
1864         
1865         if(ipo && ipo->id.lib==NULL && from->lib==NULL) {
1866                 
1867                 for(icu= ipo->curve.first; icu; icu= icu->next) {
1868                         if(icu->adrcode==adrcode) break;
1869                 }
1870                 if(icu==NULL) {
1871                         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
1872
1873                         icu->flag |= IPO_VISIBLE|IPO_AUTO_HORIZ;
1874                         icu->blocktype= blocktype;
1875                         icu->adrcode= adrcode;
1876                         
1877                         set_icu_vars(icu);
1878
1879                         BLI_addtail( &(ipo->curve), icu);
1880
1881                         switch (GS(from->name)) {
1882                         case ID_SEQ: {
1883                                 Sequence *seq= (Sequence *)from;
1884
1885                                 update_seq_icu_rects(seq);
1886                                 break;
1887                         }
1888                         }
1889                 }
1890         }
1891
1892         return icu;
1893 }
1894
1895 void insert_vert_ipo(IpoCurve *icu, float x, float y)
1896 {
1897         BezTriple *bezt, beztr, *newbezt;
1898         int a = 0, h1, h2;
1899         
1900         memset(&beztr, 0, sizeof(BezTriple));
1901         beztr.vec[0][0]= x; // set all three points, for nicer start position
1902         beztr.vec[0][1]= y;
1903         beztr.vec[1][0]= x;
1904         beztr.vec[1][1]= y;
1905         beztr.vec[2][0]= x;
1906         beztr.vec[2][1]= y;
1907         beztr.hide= IPO_BEZ;
1908         beztr.f1= beztr.f2= beztr.f3= SELECT;
1909         beztr.h1= beztr.h2= HD_AUTO;
1910                 
1911         bezt= icu->bezt;
1912                 
1913         if(bezt==NULL) {
1914                 icu->bezt= MEM_callocN( sizeof(BezTriple), "beztriple");
1915                 *(icu->bezt)= beztr;
1916                 icu->totvert= 1;
1917         }
1918         else {
1919                 /* all vertices deselect */
1920                 for(a=0; a<icu->totvert; a++, bezt++) {
1921                         bezt->f1= bezt->f2= bezt->f3= 0;
1922                 }
1923         
1924                 bezt= icu->bezt;
1925                 for(a=0; a<=icu->totvert; a++, bezt++) {
1926                         
1927                         /* no double points */
1928                         if(a<icu->totvert && IS_EQ(bezt->vec[1][0], x)) {
1929                                 *(bezt)= beztr;
1930                                 break;
1931                         }
1932                         if(a==icu->totvert || bezt->vec[1][0] > x) {
1933                                 newbezt= MEM_callocN( (icu->totvert+1)*sizeof(BezTriple), "beztriple");
1934                                 
1935                                 if(a>0) memcpy(newbezt, icu->bezt, a*sizeof(BezTriple));
1936                                 
1937                                 bezt= newbezt+a;
1938                                 *(bezt)= beztr;
1939                                 
1940                                 if(a<icu->totvert) memcpy(newbezt+a+1, icu->bezt+a, (icu->totvert-a)*sizeof(BezTriple));
1941                                 
1942                                 MEM_freeN(icu->bezt);
1943                                 icu->bezt= newbezt;
1944                                 
1945                                 icu->totvert++;
1946                                 break;
1947                         }
1948                 }
1949         }
1950         
1951         
1952         calchandles_ipocurve(icu);
1953         
1954         /* set handletype */
1955         if(icu->totvert>2) {
1956                 h1= h2= HD_AUTO;
1957                 if(a>0) h1= (bezt-1)->h2;
1958                 if(a<icu->totvert-1) h2= (bezt+1)->h1;
1959                 bezt->h1= h1;
1960                 bezt->h2= h2;
1961
1962                 calchandles_ipocurve(icu);
1963         }
1964 }
1965
1966 void add_vert_ipo(void)
1967 {
1968         EditIpo *ei;
1969         float x, y;
1970         int val;
1971         short mval[2];
1972
1973         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1974         if(G.sipo->showkey) {
1975                 G.sipo->showkey= 0;
1976                 free_ipokey(&G.sipo->ipokey);
1977         }
1978         
1979         getmouseco_areawin(mval);
1980         
1981         if(mval[0]>G.v2d->mask.xmax) return;
1982         
1983         ei= get_active_editipo();
1984         if(ei==NULL) {
1985                 error("No active Ipo curve");
1986                 return;
1987         }
1988         ei->flag |= IPO_VISIBLE;        /* can happen it is active but not visible */
1989         
1990         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1991         
1992         /* convert click-time to ipo-time */
1993         if (NLA_IPO_SCALED) {
1994                 x= get_action_frame(OBACT, x);
1995         }
1996         
1997         if(ei->icu==NULL) {
1998                 if(G.sipo->from) {
1999                         ei->icu= verify_ipocurve(G.sipo->from, G.sipo->blocktype, G.sipo->actname, G.sipo->constname, ei->adrcode);
2000                         if (ei->icu)
2001                                 ei->flag |= ei->icu->flag & IPO_AUTO_HORIZ;     /* new curve could have been added, weak... */
2002                         else
2003                                 error("Cannot create an IPO curve, you may be using libdata");
2004                 }
2005         }
2006         if(ei->icu==NULL) return;
2007
2008         if(ei->disptype==IPO_DISPBITS) {
2009                 ei->icu->vartype= IPO_BITS;
2010                 val= (int)floor(y-0.5);
2011                 if(val<0) val= 0;
2012                 y= (float)(1 << val);
2013         }
2014         
2015         insert_vert_ipo(ei->icu, x, y);
2016
2017         /* to be sure: if icu was 0, or only 1 curve visible */
2018         ei->flag |= IPO_SELECT;
2019         ei->icu->flag= ei->flag;
2020         
2021         editipo_changed(G.sipo, 1);
2022         BIF_undo_push("Add Ipo vertex");
2023 }
2024
2025 static void *get_context_ipo_poin(ID *id, int blocktype, char *actname, IpoCurve *icu, int *vartype)
2026 {
2027         if(blocktype==ID_PO) {
2028                 if(GS(id->name)==ID_OB) {
2029                         Object *ob= (Object *)id;
2030                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2031                         
2032                         *vartype= IPO_FLOAT;
2033                         return get_pchan_ipo_poin(pchan, icu->adrcode);
2034                 }
2035                 return NULL;
2036         }
2037         else
2038                 return get_ipo_poin(id, icu, vartype);
2039
2040 }
2041
2042 #define KEYNEEDED_DONTADD       0
2043 #define KEYNEEDED_JUSTADD       1
2044 #define KEYNEEDED_DELPREV       2
2045 #define KEYNEEDED_DELNEXT       3
2046
2047 static int new_key_needed(IpoCurve *icu, float cFrame, float nValue) 
2048 {
2049         /* This function determines whether a new keyframe is needed */
2050         /* Cases where keyframes should not be added:
2051          *      1. Keyframe to be added bewteen two keyframes with similar values
2052          *      2. Keyframe to be added on frame where two keyframes are already situated
2053          *      3. Keyframe lies at point that intersects the linear line between two keyframes
2054          */
2055         
2056         BezTriple *bezt=NULL, *prev=NULL;
2057         int totCount, i;
2058         float valA = 0.0f, valB = 0.0f;
2059         
2060         /* safety checking */
2061         if (!icu) return KEYNEEDED_JUSTADD;
2062         totCount= icu->totvert;
2063         if (totCount==0) return KEYNEEDED_JUSTADD;
2064         
2065         /* loop through checking if any are the same */
2066         bezt= icu->bezt;
2067         for (i=0; i<totCount; i++) {
2068                 float prevPosi=0.0f, prevVal=0.0f;
2069                 float beztPosi=0.0f, beztVal=0.0f;
2070                         
2071                 /* get current time+value */    
2072                 beztPosi= bezt->vec[1][0];
2073                 beztVal= bezt->vec[1][1];
2074                         
2075                 if (prev) {
2076                         /* there is a keyframe before the one currently being examined */               
2077                         
2078                         /* get previous time+value */
2079                         prevPosi= prev->vec[1][0];
2080                         prevVal= prev->vec[1][1];
2081                         
2082                         /* keyframe to be added at point where there are already two similar points? */
2083                         if (IS_EQ(prevPosi, cFrame) && IS_EQ(beztPosi, cFrame) && IS_EQ(beztPosi, prevPosi)) {
2084                                 return KEYNEEDED_DONTADD;
2085                         }
2086                         
2087                         /* keyframe between prev+current points ? */
2088                         if ((prevPosi <= cFrame) && (cFrame <= beztPosi)) {
2089                                 /* is the value of keyframe to be added the same as keyframes on either side ? */
2090                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal)) {
2091                                         return KEYNEEDED_DONTADD;
2092                                 }
2093                                 else {
2094                                         float realVal;
2095                                         
2096                                         /* get real value of curve at that point */
2097                                         realVal= eval_icu(icu, cFrame);
2098                                 
2099                                         /* compare whether it's the same as proposed */
2100                                         if (IS_EQ(realVal, nValue)) 
2101                                                 return KEYNEEDED_DONTADD;
2102                                         else 
2103                                                 return KEYNEEDED_JUSTADD;
2104                                 }
2105                         }
2106                         
2107                         /* new keyframe before prev beztriple? */
2108                         if (cFrame < prevPosi) {
2109                                 /* A new keyframe will be added. However, whether the previous beztriple
2110                                  * stays around or not depends on whether the values of previous/current
2111                                  * beztriples and new keyframe are the same.
2112                                  */
2113                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal))
2114                                         return KEYNEEDED_DELNEXT;
2115                                 else 
2116                                         return KEYNEEDED_JUSTADD;
2117                         }
2118                 }
2119                 else {
2120                         /* just add a keyframe if there's only one keyframe 
2121                          * and the new one occurs before the exisiting one does.
2122                          */
2123                         if ((cFrame < beztPosi) && (totCount==1))
2124                                 return KEYNEEDED_JUSTADD;
2125                 }
2126                 
2127                 /* continue. frame to do not yet passed (or other conditions not met) */
2128                 if (i < (totCount-1)) {
2129                         prev= bezt;
2130                         bezt++;
2131                 }
2132                 else
2133                         break;
2134         }
2135         
2136         /* Frame in which to add a new-keyframe occurs after all other keys
2137          * -> If there are at least two existing keyframes, then if the values of the
2138          *       last two keyframes and the new-keyframe match, the last existing keyframe
2139          *       gets deleted as it is no longer required.
2140          * -> Otherwise, a keyframe is just added. 1.0 is added so that fake-2nd-to-last
2141          *       keyframe is not equal to last keyframe.
2142          */
2143         bezt= (icu->bezt + (icu->totvert - 1));
2144         valA= bezt->vec[1][1];
2145         
2146         if (prev)
2147                 valB= prev->vec[1][1];
2148         else 
2149                 valB= bezt->vec[1][1] + 1.0f; 
2150                 
2151         if (IS_EQ(valA, nValue) && IS_EQ(valA, valB)) 
2152                 return KEYNEEDED_DELPREV;
2153         else 
2154                 return KEYNEEDED_JUSTADD;
2155 }
2156
2157 void insertkey(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2158 {
2159         IpoCurve *icu;
2160         Object *ob;
2161         void *poin= NULL;
2162         float curval, cfra;
2163         int vartype;
2164         
2165         icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2166         
2167         if(icu) {
2168                 
2169                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2170                 
2171                 if(poin) {
2172                         curval= read_ipo_poin(poin, vartype);
2173                         
2174                         cfra= frame_to_float(CFRA);
2175                         
2176                         /* if action is mapped in NLA, it returns a correction */
2177                         if(actname && actname[0] && GS(id->name)==ID_OB)
2178                                 cfra= get_action_frame((Object *)id, cfra);
2179                         
2180                         if( GS(id->name)==ID_OB ) {
2181                                 ob= (Object *)id;
2182                                 if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2183                                         /* actually frametofloat calc again! */
2184                                         cfra-= ob->sf*G.scene->r.framelen;
2185                                 }
2186                         }
2187                         
2188                         insert_vert_ipo(icu, cfra, curval);
2189                 }
2190         }
2191 }
2192
2193 /* This function is a 'smarter' version of the insert key code.
2194  * It uses an auxilliary function to check whether a keyframe is really needed */
2195 void insertkey_smarter(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2196 {
2197         IpoCurve *icu;
2198         Object *ob;
2199         void *poin= NULL;
2200         float curval, cfra;
2201         int vartype;
2202         int insert_mode;
2203         
2204         icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2205         
2206         if(icu) {
2207                 
2208                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2209                 
2210                 if(poin) {
2211                         curval= read_ipo_poin(poin, vartype);
2212                         
2213                         cfra= frame_to_float(CFRA);
2214                         
2215                         /* if action is mapped in NLA, it returns a correction */
2216                         if(actname && actname[0] && GS(id->name)==ID_OB)
2217                                 cfra= get_action_frame((Object *)id, cfra);
2218                         
2219                         if( GS(id->name)==ID_OB ) {
2220                                 ob= (Object *)id;
2221                                 if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2222                                         /* actually frametofloat calc again! */
2223                                         cfra-= ob->sf*G.scene->r.framelen;
2224                                 }
2225                         }
2226                         
2227                         /* check whether this curve really needs a new keyframe */
2228                         insert_mode= new_key_needed(icu, cfra, curval);
2229                         
2230                         /* insert new keyframe at current frame */
2231                         if (insert_mode) 
2232                                 insert_vert_ipo(icu, cfra, curval);
2233                         
2234                         /* delete keyframe immediately before/after newly added */
2235                         switch (insert_mode) {
2236                                 case KEYNEEDED_DELPREV:
2237                                         delete_icu_key(icu, icu->totvert-2);
2238                                         break;
2239                                 case KEYNEEDED_DELNEXT:
2240                                         delete_icu_key(icu, 1);
2241                                         break;
2242                         }
2243                 }
2244         }
2245 }
2246
2247 /* For inserting keys based on the object matrix - not on the current IPO value
2248    Generically - it inserts the passed float value into the appropriate IPO */
2249 void insertmatrixkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, float matrixvalue)
2250 {
2251         IpoCurve *icu;
2252         Object *ob;
2253         void *poin= NULL;
2254         float cfra;
2255         int vartype;
2256         
2257         icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2258         
2259         if(icu) {
2260                 
2261                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2262                 
2263                 if(poin) {
2264                         
2265                         cfra= frame_to_float(CFRA);
2266                         
2267                         /* if action is mapped in NLA, it returns a correction */
2268                         if(actname && actname[0] && GS(id->name)==ID_OB)
2269                                 cfra= get_action_frame((Object *)id, cfra);
2270                         
2271                         if( GS(id->name)==ID_OB ) {
2272                                 ob= (Object *)id;
2273                                 if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2274                                         /* actually frametofloat calc again! */
2275                                         cfra-= ob->sf*G.scene->r.framelen;
2276                                 }
2277                         }
2278                         insert_vert_ipo(icu, cfra, matrixvalue);
2279                 }
2280         }
2281 }
2282
2283 void insertkey_editipo(void)
2284 {
2285         EditIpo *ei;
2286         IpoKey *ik;
2287         ID *id;
2288         float *fp, cfra, *insertvals;
2289         int a, nr, ok, tot;
2290         short event;
2291         
2292         ei= get_active_editipo();
2293         if(ei && ei->icu && ei->icu->driver) 
2294                 event= pupmenu("Insert Curve %t|Default one-to-one mapping %x3");
2295         else if(G.sipo->showkey)
2296                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1|Selected Keys %x2");
2297         else 
2298                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1");
2299         
2300         if(event<1) return;
2301         
2302         if(event==3) {
2303                 IpoDriver *driver= ei->icu->driver;
2304                 
2305                 if(ei->icu->bezt) MEM_freeN(ei->icu->bezt);
2306                 ei->icu->totvert= 0;
2307                 ei->icu->bezt= NULL;
2308                 
2309                 insert_vert_ipo(ei->icu, 0.0f, 0.0f);
2310                 
2311                 if(ELEM3(driver->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) {
2312                         if(ei->disptype==IPO_DISPDEGR)
2313                                 insert_vert_ipo(ei->icu, 18.0f, 18.0f);
2314                         else
2315                                 insert_vert_ipo(ei->icu, 18.0f, 1.0f);
2316                 }
2317                 else
2318                         insert_vert_ipo(ei->icu, 1.0f, 1.0f);
2319                 
2320                 ei->flag |= IPO_SELECT|IPO_VISIBLE;
2321                 ei->icu->flag= ei->flag;
2322                 ei->icu->extrap= IPO_DIR;
2323
2324                 do_ipo_buttons(B_IPOHOME);
2325         }
2326         else {
2327                 ei= G.sipo->editipo;
2328                 for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
2329                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
2330                         
2331                                 ok= 0;
2332                                 if(G.sipo->showkey) ok= 1;
2333                                 else if(ei->flag & IPO_SELECT) ok= 1;
2334
2335                                 if(ok) {
2336                                         /* count amount */
2337                                         if(event==1) tot= 1;
2338                                         else {
2339                                                 ik= G.sipo->ipokey.first;
2340                                                 tot= 0;
2341                                                 while(ik) {
2342                                                         if(ik->flag & 1) tot++;
2343                                                         ik= ik->next;
2344                                                 }
2345                                         }
2346                                         if(tot) {
2347                                         
2348                                                 /* correction for ob timeoffs */
2349                                                 cfra= frame_to_float(CFRA);
2350                                                 id= G.sipo->from;       
2351                                                 if(id && GS(id->name)==ID_OB ) {
2352                                                         Object *ob= (Object *)id;
2353                                                         if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2354                                                                 cfra-= ob->sf*G.scene->r.framelen;
2355                                                         }
2356                                                 }
2357                                                 else if(id && GS(id->name)==ID_SEQ) {
2358                                                         Sequence *last_seq = get_last_seq();    /* editsequence.c */
2359                                                         
2360                                                         if(last_seq && (last_seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
2361                                                                 cfra= (float)(100.0*(cfra-last_seq->startdisp)/((float)(last_seq->enddisp-last_seq->startdisp)));
2362                                                         }
2363                                                 }
2364                                                 
2365                                                 /* convert cfra to ipo-time */
2366                                                 if (NLA_IPO_SCALED) {
2367                                                         cfra= get_action_frame(OBACT, cfra);
2368                                                 }
2369                                 
2370                                                 insertvals= MEM_mallocN(sizeof(float)*2*tot, "insertkey_editipo");
2371                                                 /* make sure icu->curval is correct */
2372                                                 calc_ipo(G.sipo->ipo, cfra);
2373                                                 
2374                                                 if(event==1) {
2375                                                         insertvals[0]= cfra;
2376                                                         
2377                                                         insertvals[1]= ei->icu->curval;
2378                                                 }
2379                                                 else {
2380                                                         fp= insertvals;
2381                                                         ik= G.sipo->ipokey.first;
2382                                                         while(ik) {
2383                                                                 if(ik->flag & 1) {
2384                                                                         calc_ipo(G.sipo->ipo, ik->val);
2385
2386                                                                         fp[0]= ik->val;
2387                                                                         fp[1]= ei->icu->curval;
2388                                                                         fp+= 2;
2389                                                                 }
2390                                                                 ik= ik->next;
2391                                                         }
2392                                                 }
2393                                                 fp= insertvals;
2394                                                 for(a=0; a<tot; a++, fp+=2) {
2395                                                         insert_vert_ipo(ei->icu, fp[0], fp[1]);
2396                                                 }
2397                                                 
2398                                                 MEM_freeN(insertvals);
2399                                                 calc_ipo(G.sipo->ipo, (float)CFRA);
2400                                         }
2401                                 }
2402                         }
2403                 }
2404         }
2405         BIF_undo_push("Insert Key Ipo");
2406         allqueue (REDRAWACTION, 0);
2407         allqueue(REDRAWNLA, 0);
2408         allqueue(REDRAWIPO, 0);
2409         allspace(REMAKEIPO, 0);
2410 }
2411
2412
2413 void common_insertkey(void)
2414 {
2415         Base *base;
2416         Object *ob;
2417         Material *ma;
2418         ID *id;
2419         IpoCurve *icu;
2420         World *wo;
2421         Lamp *la;
2422         Tex *te;
2423         int tlay, map, event;
2424         char menustr[256];
2425
2426         if(curarea->spacetype==SPACE_IPO) {
2427                 insertkey_editipo();
2428         }
2429         else if(curarea->spacetype==SPACE_ACTION) {
2430                 insertkey_action();
2431         }
2432         else if(curarea->spacetype==SPACE_BUTS) {
2433                 if(G.buts->mainb==CONTEXT_SHADING) {
2434                         int tab= G.buts->tab[CONTEXT_SHADING];
2435
2436                         if(tab==TAB_SHADING_MAT) {
2437                                 ma = G.buts->lockpoin;
2438                                 ma = editnode_get_active_material(ma);
2439                                 id = (ID *)ma;
2440                                 
2441                                 if(id) {
2442                                         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");
2443                                         if(event== -1) return;
2444
2445                                         map= texchannel_to_adrcode(ma->texact);
2446
2447                                         if(event==0 || event==10) {
2448                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_R);
2449                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_G);
2450                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_B);
2451                                         }
2452                                         if(event==1 || event==10) {
2453                                                 insertkey(id, ID_MA, NULL, NULL, MA_ALPHA);
2454                                         }
2455                                         if(event==2 || event==10) {
2456                                                 insertkey(id, ID_MA, NULL, NULL, MA_HASIZE);
2457                                         }
2458                                         if(event==3 || event==10) {
2459                                                 insertkey(id, ID_MA, NULL, NULL, MA_MODE);
2460                                         }
2461                                         if(event==10) {
2462                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_R);
2463                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_G);
2464                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_B);
2465                                                 insertkey(id, ID_MA, NULL, NULL, MA_REF);
2466                                                 insertkey(id, ID_MA, NULL, NULL, MA_EMIT);
2467                                                 insertkey(id, ID_MA, NULL, NULL, MA_AMB);
2468                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC);
2469                                                 insertkey(id, ID_MA, NULL, NULL, MA_HARD);
2470                                                 insertkey(id, ID_MA, NULL, NULL, MA_MODE);
2471                                                 insertkey(id, ID_MA, NULL, NULL, MA_TRANSLU);
2472                                                 insertkey(id, ID_MA, NULL, NULL, MA_ADD);
2473                                         }
2474                                         if(event==14) {
2475                                                 insertkey(id, ID_MA, NULL, NULL, MA_RAYM);
2476                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESMIR);
2477                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESMIRI);
2478                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESTRA);
2479                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESTRAI);
2480                                         }
2481                                         if(event==12 || event==11) {
2482                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_X);
2483                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_Y);
2484                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_Z);
2485                                         }
2486                                         if(event==13 || event==11) {
2487                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_X);
2488                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_Y);
2489                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_Z);
2490                                         }
2491                                         if(event==11) {
2492                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_R);
2493                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_G);
2494                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_B);
2495                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_DVAR);
2496                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_COLF);
2497                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_NORF);
2498                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_VARF);
2499                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_DISP);
2500                                         }
2501                                 }
2502                         }
2503                         else if(tab==TAB_SHADING_WORLD) {
2504                                 id= G.buts->lockpoin;
2505                                 wo= G.buts->lockpoin;
2506                                 if(id) {
2507                                         event= pupmenu("Insert Key %t|Zenith RGB%x0|Horizon RGB%x1|Mist%x2|Stars %x3|Offset%x12|Size%x13");
2508                                         if(event== -1) return;
2509
2510                                         map= texchannel_to_adrcode(wo->texact);
2511
2512                                         if(event==0) {
2513                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_R);
2514                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_G);
2515                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_B);
2516                                         }
2517                                         if(event==1) {
2518                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_R);
2519                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_G);
2520                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_B);
2521                                         }
2522                                         if(event==2) {
2523                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISI);
2524                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTDI);
2525                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTSTA);
2526                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTHI);
2527                                         }
2528                                         if(event==3) {
2529                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_R);
2530                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_G);
2531                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_B);
2532                                                 insertkey(id, ID_WO, NULL, NULL, WO_STARDIST);
2533                                                 insertkey(id, ID_WO, NULL, NULL, WO_STARSIZE);
2534                                         }
2535                                         if(event==12) {
2536                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_X);
2537                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_Y);
2538                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_Z);
2539                                         }
2540                                         if(event==13) {
2541                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_X);
2542                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_Y);
2543                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_Z);
2544                                         }
2545                                 }
2546                         }
2547                         else if(tab==TAB_SHADING_LAMP) {
2548                                 id= G.buts->lockpoin;
2549                                 la= G.buts->lockpoin;
2550                                 if(id) {
2551                                         event= pupmenu("Insert Key %t|RGB%x0|Energy%x1|Spot Size%x2|Offset%x12|Size%x13");
2552                                         if(event== -1) return;
2553
2554                                         map= texchannel_to_adrcode(la->texact);
2555
2556                                         if(event==0) {
2557                                                 insertkey(id, ID_LA, NULL, NULL, LA_COL_R);
2558                                                 insertkey(id, ID_LA, NULL, NULL, LA_COL_G);
2559                                                 insertkey(id, ID_LA, NULL, NULL, LA_COL_B);
2560                                         }
2561                                         if(event==1) {
2562                                                 insertkey(id, ID_LA, NULL, NULL, LA_ENERGY);
2563                                         }
2564                                         if(event==2) {
2565                                                 insertkey(id, ID_LA, NULL, NULL, LA_SPOTSI);
2566                                         }
2567                                         if(event==12) {
2568                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_OFS_X);
2569                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_OFS_Y);
2570                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_OFS_Z);
2571                                         }
2572                                         if(event==13) {
2573                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_SIZE_X);
2574                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_SIZE_Y);
2575                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_SIZE_Z);
2576                                         }
2577
2578                                 }
2579                         }
2580                         else if(tab==TAB_SHADING_TEX) {
2581                                 id= G.buts->lockpoin;
2582                                 te= G.buts->lockpoin;
2583                                 if(id) {
2584                                         event= pupmenu("Insert Key %t|Clouds%x0|Marble%x1|Stucci%x2|Wood%x3|Magic%x4|Blend%x5|Musgrave%x6|Voronoi%x7|DistortedNoise%x8|ColorFilter%x9");
2585                                         if(event== -1) return;
2586
2587                                         if(event==0) {
2588                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE);
2589                                                 insertkey(id, ID_TE, NULL, NULL, TE_NDEPTH);
2590                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE);
2591                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP);
2592                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1);
2593                                         }
2594                                         if(event==1) {
2595                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE);
2596                                                 insertkey(id, ID_TE, NULL, NULL, TE_NDEPTH);
2597                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE);
2598                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB);
2599                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP);
2600                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1);
2601                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS2);
2602                                         }
2603                                         if(event==2) {
2604                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE);
2605                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE);
2606                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB);
2607                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP);
2608                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1);
2609                                         }
2610                                         if(event==3) {
2611                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE);
2612                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE);
2613                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB);
2614                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP);
2615                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1);
2616                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS2);
2617                                         }
2618                                         if(event==4) {
2619                                                 insertkey(id, ID_TE, NULL, NULL, TE_NDEPTH);
2620                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB);
2621                                         }
2622                                         if(event==5) {
2623                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP);
2624                                         } 
2625                                         if(event==6) {
2626                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP);
2627                                                 insertkey(id, ID_TE, NULL, NULL, TE_MGH);
2628                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_LAC);
2629                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OCT);
2630                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OFF);
2631                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_GAIN);
2632                                         }
2633                                         if(event==7) {
2634                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW1);
2635                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW2);
2636                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW3);
2637                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW4);
2638                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNMEXP);
2639                                                 insertkey(id, ID_TE, NULL, NULL, TE_VN_DISTM);
2640                                                 insertkey(id, ID_TE, NULL, NULL, TE_VN_COLT);
2641                                                 insertkey(id, ID_TE, NULL, NULL, TE_ISCA);
2642                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE);
2643                                         }
2644                                         if(event==8) {
2645                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OCT);
2646                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OFF);
2647                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_GAIN);
2648                                                 insertkey(id, ID_TE, NULL, NULL, TE_DISTA);
2649                                         }
2650                                         if(event==9) {
2651                                                 insertkey(id, ID_TE, NULL, NULL, TE_COL_R);
2652                                                 insertkey(id, ID_TE, NULL, NULL, TE_COL_G);
2653                                                 insertkey(id, ID_TE, NULL, NULL, TE_COL_B);
2654                                                 insertkey(id, ID_TE, NULL, NULL, TE_BRIGHT);
2655                                                 insertkey(id, ID_TE, NULL, NULL, TE_CONTRA);
2656                                         }
2657                                 }
2658                         }
2659                 }
2660                 else if(G.buts->mainb==CONTEXT_OBJECT) {
2661                         ob= OBACT;
2662                         if(ob) {
2663                                 id= (ID *) (ob);
2664                                 if(id) {
2665                                         if(ob->type==OB_MESH) 
2666                                                 event= pupmenu("Insert Key %t|Surface Damping%x0|Random Damping%x1|Permeability%x2|Force Strength%x3|Force Falloff%x4");
2667                                         else
2668                                                 event= pupmenu("Insert Key %t|Force Strength%x3|Force Falloff%x4");
2669                                         if(event == -1) return;
2670
2671                                         if(event==0) {
2672                                                 insertkey(id, ID_OB, NULL, NULL, OB_PD_SDAMP);
2673                                         }
2674                                         if(event==1) {
2675                                                 insertkey(id, ID_OB, NULL, NULL, OB_PD_RDAMP);
2676                                         }
2677                                         if(event==2) {
2678                                                 insertkey(id, ID_OB, NULL, NULL, OB_PD_PERM);
2679                                         }
2680                                         if(event==3) {
2681                                                 insertkey(id, ID_OB, NULL, NULL, OB_PD_FSTR);
2682                                         }
2683                                         if(event==4) {
2684                                                 insertkey(id, ID_OB, NULL, NULL, OB_PD_FFALL);
2685                                         }
2686
2687                                 }
2688                         }
2689                 }
2690                 else if(G.buts->mainb==CONTEXT_EDITING) {
2691                         ob= OBACT;
2692                         if(ob && ob->type==OB_CAMERA) {
2693                                 id= G.buts->lockpoin;
2694                                 if(id) {
2695                                         /* yafray: insert key extended with aperture and focal distance */
2696                                         /* qdn: FocalDistance now enabled for Blender as wel, for use with defocus node */
2697                                         if (G.scene->r.renderer==R_INTERN)
2698                                                 event= pupmenu("Insert Key %t|Lens%x0|Clipping%x1|FocalDistance%x3|Viewplane Shift%x4");
2699                                         else
2700                                                 event= pupmenu("Insert Key %t|Lens%x0|Clipping%x1|Aperture%x2|FocalDistance%x3");
2701                                         if(event== -1) return;
2702
2703                                         if(event==0) {
2704                                                 insertkey(id, ID_CA, NULL, NULL, CAM_LENS);
2705                                         }
2706                                         else if(event==1) {
2707                                                 insertkey(id, ID_CA, NULL, NULL, CAM_STA);
2708                                                 insertkey(id, ID_CA, NULL, NULL, CAM_END);
2709                                         }
2710                                         else if(event==2) {
2711                                                 insertkey(id, ID_CA, NULL, NULL, CAM_YF_APERT);
2712                                         }
2713                                         else if(event==3) {
2714                                                 insertkey(id, ID_CA, NULL, NULL, CAM_YF_FDIST);
2715                                         }
2716                                         else if(event==4) {
2717                                                 insertkey(id, ID_CA, NULL, NULL, CAM_SHIFT_X);
2718                                                 insertkey(id, ID_CA, NULL, NULL, CAM_SHIFT_Y);
2719                                         }
2720                                 }
2721                         }
2722                 }
2723                 else if(FALSE /* && G.buts->mainb==BUTS_SOUND */) {
2724                         if(G.ssound) {
2725                                 id= G.buts->lockpoin;
2726                                 if(id) {
2727                                         event= pupmenu("Insert Key %t|Volume%x0|Pitch%x1|Panning%x2|Attennuation%x3");
2728                                         if(event== -1) return;
2729
2730                                         if(event==0) {
2731                                                 insertkey(id, ID_SO, NULL, NULL, SND_VOLUME);
2732                                         }
2733                                         if(event==1) {
2734                                                 insertkey(id, ID_SO, NULL, NULL, SND_PITCH);
2735                                         }
2736                                         if(event==2) {
2737                                                 insertkey(id, ID_SO, NULL, NULL, SND_PANNING);
2738                                         }
2739                                         if(event==3) {
2740                                                 insertkey(id, ID_SO, NULL, NULL, SND_ATTEN);
2741                                         }
2742                                 }
2743                         }
2744                 }
2745
2746                 BIF_undo_push("Insert Key Buttons");
2747
2748                 allqueue(REDRAWACTION, 0);
2749                 allqueue(REDRAWNLA, 0);
2750                 allqueue(REDRAWIPO, 0);
2751                 allspace(REMAKEIPO, 0);
2752
2753         }
2754         else if(curarea->spacetype==SPACE_VIEW3D) {
2755                 ob= OBACT;
2756
2757                 if (ob && (ob->flag & OB_POSEMODE)) {
2758                         bPoseChannel *pchan;
2759                         
2760                         set_pose_keys(ob);  // sets pchan->flag to POSE_KEY if bone selected
2761                         for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next)
2762                                 if (pchan->flag & POSE_KEY)
2763                                         break;
2764                         if(pchan==NULL) return;
2765                         strcpy(menustr, "Insert Key%t|Loc%x0|Rot%x1|Scale%x2|LocRot%x3|LocRotScale%x4|Avail%x9|Needed%x15|VisualLoc%x11|VisualRot%x12|VisualLocRot%x13");
2766                 }
2767                 else {
2768                         base= FIRSTBASE;
2769                         while(base) {
2770                                 if (TESTBASELIB(base)) break;
2771                                 base= base->next;
2772                         }
2773                         if(base==NULL) return;
2774                         strcpy(menustr, "Insert Key%t|Loc%x0|Rot%x1|Scale%x2|LocRot%x3|LocRotScale%x4|Layer%x5|Avail%x9|Needed%x15|VisualLoc%x11|VisualRot%x12|VisualLocRot%x13");
2775                 }
2776
2777                 if(ob) {
2778                         if(ob->type==OB_MESH) strcat(menustr, "| %x6|Mesh%x7");
2779                         else if(ob->type==OB_LATTICE) strcat(menustr, "| %x6|Lattice%x7");
2780                         else if(ob->type==OB_CURVE) strcat(menustr, "| %x6|Curve%x7");
2781                         else if(ob->type==OB_SURF) strcat(menustr, "| %x6|Surface%x7");
2782                 }
2783
2784                 event= pupmenu(menustr);
2785                 if(event== -1) return;
2786
2787                 if(event==7) { // ob != NULL
2788                         insert_shapekey(ob);
2789                         return;
2790                 }
2791
2792                 if (ob && (ob->flag & OB_POSEMODE)){
2793                         bPoseChannel *pchan;
2794
2795                         if (ob->action && ob->action->id.lib) {
2796                                 error ("Can't key libactions");
2797                                 return;
2798                         }
2799
2800                         id= &ob->id;
2801                         for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
2802                                 if (pchan->flag & POSE_KEY){
2803                                         if(event==0 || event==3 ||event==4) {
2804                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X);
2805                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y);
2806                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z);
2807                                         }
2808                                         if(event==1 || event==3 ||event==4) {
2809                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X);
2810                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y);
2811                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z);
2812                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W);
2813                                         }
2814                                         if(event==2 || event==4) {
2815                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X);
2816                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y);
2817                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z);
2818                                         }
2819                                         if (event==9 && ob->action) {
2820                                                 bActionChannel *achan;
2821
2822                                                 for (achan = ob->action->chanbase.first; achan; achan=achan->next){
2823                                                         if (achan->ipo && !strcmp (achan->name, pchan->name)){
2824                                                                 for (icu = achan->ipo->curve.first; icu; icu=icu->next){
2825                                                                         insertkey(id, ID_PO, achan->name, NULL, icu->adrcode);
2826                                                                 }
2827                                                                 break;
2828                                                         }
2829                                                 }
2830                                         }
2831                                         if(event==11 || event==13) {
2832                                                 float delta_mat[4][4]; 
2833                                                 
2834                                                 armature_mat_pose_to_delta(delta_mat, pchan->pose_mat, pchan->bone->arm_mat);
2835                                                 insertmatrixkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, delta_mat[3][0]);
2836                                                 insertmatrixkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, delta_mat[3][1]);
2837                                                 insertmatrixkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, delta_mat[3][2]);
2838                                         }
2839                                         if(event==12 || event==13) {
2840                                                 float delta_mat[4][4];
2841                                                 float localQuat[4], oldQuat[4];
2842                                                 
2843                                                 /* obtain rotation caused by constraints/IK*/
2844                                                 armature_mat_pose_to_delta(delta_mat, pchan->pose_mat, pchan->bone->arm_mat);
2845                                                 Mat4ToQuat(delta_mat, localQuat);
2846                                                 
2847                                                 /* bad hack warning:
2848                                                  * Write the 'visual' rotation onto the
2849                                                  * bone's quat/rotation values and use standard 
2850                                                  * keyframing method to insert a keyframe with this
2851                                                  * value. 
2852                     &nb