26aa3e1cbae87d6cdfff30c884558358916b117a
[blender.git] / source / blender / src / edittime.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36 #include <string.h>
37 #include <math.h>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_arithb.h"
47
48 #include "DNA_action_types.h"
49 #include "DNA_ipo_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_userdef_types.h"
56
57 #include "BKE_ipo.h"
58 #include "BKE_utildefines.h"
59 #include "BKE_global.h"
60 #include "BKE_main.h"
61 #include "BKE_material.h"
62 #include "BKE_library.h"
63
64 #include "BIF_space.h"
65 #include "BIF_screen.h"
66 #include "BIF_interface.h"
67 #include "BIF_toolbox.h"
68 #include "BIF_mywindow.h"
69
70 #include "BSE_drawipo.h"
71 #include "BSE_headerbuttons.h"
72 #include "BSE_time.h"
73
74 #include "BDR_editobject.h"
75
76 #include "blendef.h"
77
78 #include "mydevice.h"
79
80 #include "PIL_time.h"
81
82 /* declarations */
83 void winqreadtimespace(ScrArea *, void *, BWinEvent *);
84
85 /* ************* Timeline marker code **************** */
86
87 /* add TimeMarker at curent frame */
88 void add_timeline_marker(int frame)
89 {
90         TimeMarker *marker;
91         
92         /* two markers can't be at the same place */
93         for(marker= G.scene->markers.first; marker; marker= marker->next)
94                 if(marker->frame == frame) return;
95         /* deselect all */
96         for(marker= G.scene->markers.first; marker; marker= marker->next)
97                 marker->flag &= ~SELECT;
98                 
99         marker = MEM_callocN(sizeof(TimeMarker), "TimeMarker");
100         marker->flag= SELECT;
101         marker->frame= frame;
102         BLI_addtail(&(G.scene->markers), marker);
103         
104         BIF_undo_push("Add Timeline Marker");
105 }
106
107 /* remove TimeMarker */
108 void remove_timeline_marker(void)
109 {
110         TimeMarker *marker;
111         
112         for(marker= G.scene->markers.first; marker; marker= marker->next) {
113                 if(marker->flag & SELECT){
114                         BLI_freelinkN(&G.scene->markers, marker);
115                 }
116         }
117         
118         BIF_undo_push("Remove Timeline Marker");
119 }
120
121 /* rename first selected TimeMarker */
122 void rename_timeline_marker(void)
123 {
124         TimeMarker *marker;
125         char name[64];
126         
127         for(marker= G.scene->markers.first; marker; marker= marker->next) {
128                 if(marker->flag & SELECT) {
129                         sprintf(name, marker->name);
130                         if (sbutton(name, 0, sizeof(name)-1, "Name: "))
131                                 BLI_strncpy(marker->name, name, sizeof(marker->name));
132                         break;
133                 }
134         }
135         
136         BIF_undo_push("Rename Timeline Marker");
137 }
138
139 /* duplicate selected TimeMarkers */
140 void duplicate_timeline_marker(void)
141 {
142         TimeMarker *marker, *newmarker;
143
144         /* go through the list of markers, duplicate selected markers and add duplicated copies
145          * to the begining of the list (unselect original markers) */
146         for(marker= G.scene->markers.first; marker; marker= marker->next) {
147                 if(marker->flag & SELECT){
148                         /* unselect selected marker */
149                         marker->flag &= ~SELECT;
150                         /* create and set up new marker */
151                         newmarker = MEM_callocN(sizeof(TimeMarker), "TimeMarker");
152                         newmarker->flag= SELECT;
153                         newmarker->frame= marker->frame;
154                         BLI_strncpy(newmarker->name, marker->name, sizeof(marker->name));
155                         /* new marker is added to the begining of list */
156                         BLI_addhead(&(G.scene->markers), newmarker);
157                 }
158         }
159         
160         timeline_grab('g', 0);
161 }
162
163
164
165 static int find_nearest_marker(float dx)
166 {
167         TimeMarker *marker, *nearest= NULL;
168         float dist, min_dist= 1000000;
169         
170         for(marker= G.scene->markers.first; marker; marker= marker->next) {
171                 dist = ABS((float)marker->frame - dx);
172                 if(dist < min_dist){
173                         min_dist= dist;
174                         nearest= marker;
175                 }
176         }
177         
178         if(nearest) return nearest->frame;
179         else return (int)floor(dx);
180 }
181
182 /* select/deselect TimeMarker at current frame */
183 static void select_timeline_marker_frame(int frame, unsigned char shift)
184 {
185         TimeMarker *marker;
186         int select=0;
187         
188         for(marker= G.scene->markers.first; marker; marker= marker->next) {
189                 /* if Shift is not set, then deselect Markers */
190                 if(!shift) marker->flag &= ~SELECT;
191                 /* this way a not-shift select will allways give 1 selected marker */
192                 if((marker->frame == frame) && (!select)) {
193                         if(marker->flag & SELECT) 
194                                 marker->flag &= ~SELECT;
195                         else
196                                 marker->flag |= SELECT;
197                         select = 1;
198                 }
199         }
200 }
201
202 /* select/deselect all TimeMarkers */
203 void select_timeline_markers(void)
204 {
205         TimeMarker *marker;
206         char any_selected= 0;
207         
208         for(marker= G.scene->markers.first; marker; marker= marker->next) {
209                 if(marker->flag & SELECT) any_selected= 1;
210                 marker->flag &= ~SELECT;
211         }
212         
213         /* no TimeMarker selected, then select all TimeMarkers */
214         if(!any_selected){
215                 for(marker= G.scene->markers.first; marker; marker= marker->next) {
216                         marker->flag |= SELECT;
217                 }
218         }
219 }
220
221 void nextprev_timeline_marker(short dir)
222 {
223         TimeMarker *marker, *cur=NULL, *first, *last;
224         int mindist= MAXFRAME, dist;
225         
226         first= last= G.scene->markers.first; 
227         for(marker= G.scene->markers.first; marker; marker= marker->next) {
228                 /* find closest to current frame first */
229                 dist= (marker->frame/G.scene->r.framelen) - CFRA;
230                 if(dir==1 && dist>0 && dist<mindist) {
231                         mindist= dist;
232                         cur= marker;
233                 }
234                 else if(dir==-1 && dist<0 && -dist<mindist) {
235                         mindist= -dist;
236                         cur= marker;
237                 }
238                 /* find first/last */
239                 if(marker->frame > last->frame) last= marker;
240                 if(marker->frame < first->frame) first= marker;
241         }
242         
243         if(cur==NULL) {
244                 if(dir==1) cur= first;
245                 else cur= last;
246         }
247         if(cur) {
248                 CFRA= cur->frame/G.scene->r.framelen;
249                 update_for_newframe();
250                 allqueue(REDRAWALL, 0);
251         }
252 }
253
254 /* *********** end Markers *************** */
255
256 static int float_to_frame(float frame) 
257 {
258         int to= (int) floor(0.5 + frame/G.scene->r.framelen );
259         
260         return to;      
261 }
262
263 static float find_closest_cfra_elem(ListBase elems, int dir, float closest)
264 {
265         CfraElem *ce;
266         
267         for(ce= elems.first; ce; ce= ce->next) {
268                 if (dir==-1) {
269                         if( float_to_frame(ce->cfra)<CFRA) {
270                                 if ((ce->cfra > closest) || (closest == CFRA)) {
271                                         closest= ce->cfra;
272                                 }
273                         }
274                 } 
275                 else {
276                         if(float_to_frame(ce->cfra)>CFRA) {
277                                 if ((ce->cfra < closest) || (closest == CFRA)) {
278                                         closest= ce->cfra;
279                                 }
280                         }
281                 }
282         }
283         return closest;
284 }
285
286 void nextprev_timeline_key(short dir)
287 {
288         /*mostly copied from drawobject.c, draw_object() AND editipo.c, movekey_obipo() */
289         Object *ob;
290         bActionChannel *achan;
291         bAction *act;
292         ListBase elems;
293         float closest= CFRA;
294         int a;
295         
296         if (OBACT) {
297                 ob = OBACT;
298                 
299                 if(ob) {
300                         if(ob!=G.obedit) {
301                                 if(ob->ipo) {
302                                         /* convert the ipo to a list of 'current frame elements' */
303                                         
304                                         elems.first= elems.last= NULL;
305                                         make_cfra_list(ob->ipo, &elems);
306                                         
307                                         closest= find_closest_cfra_elem(elems, dir, closest);
308                                         
309                                         BLI_freelistN(&elems);
310                                 }
311                                 
312                                 if(ob->action) {
313                                         act = ob->action;
314                                         /* go through each channel in the action */
315                                         for (achan=act->chanbase.first; achan; achan=achan->next){
316                                                 /* convert the ipo to a list of 'current frame elements' */
317                                                 
318                                                 elems.first= elems.last= NULL;
319                                                 make_cfra_list(achan->ipo, &elems);
320                                                 
321                                                 closest= find_closest_cfra_elem(elems, dir, closest);
322                                                 
323                                                 BLI_freelistN(&elems);
324                                         }
325                                 }
326                                 
327                                 for(a=0; a<ob->totcol; a++) {
328                                         Material *ma= give_current_material(ob, a+1);
329                                         if(ma && ma->ipo) {
330                                                 elems.first= elems.last= NULL;
331                                                 make_cfra_list(ma->ipo, &elems);
332                                                 
333                                                 closest= find_closest_cfra_elem(elems, dir, closest);
334                                                 
335                                                 BLI_freelistN(&elems);
336                                         }
337                                 }
338                         }
339                 }
340                 
341                 a= float_to_frame(closest);
342                 
343                 if (a!=CFRA) {
344                         CFRA= a;
345                         update_for_newframe();
346                 }       
347                 
348                 BIF_undo_push("Next/Prev Key");
349                 allqueue(REDRAWALL, 0);
350         }
351 }
352
353 void timeline_frame_to_center(void)
354 {
355         float dtime;
356         
357         dtime= CFRA*(G.scene->r.framelen) - (G.v2d->cur.xmin + G.v2d->cur.xmax)/2.0; 
358         G.v2d->cur.xmin += dtime;
359         G.v2d->cur.xmax += dtime;
360         scrarea_queue_winredraw(curarea);
361 }
362
363 void timeline_grab(int mode, int smode) // mode and smode unused here, for callback
364 {
365         SpaceTime *stime= curarea->spacedata.first;
366         TimeMarker *marker, *selmarker=NULL;
367         float dx, fac;
368         int a, ret_val= 0, totmark=0, *oldframe, offs, firsttime=1;
369         unsigned short event;
370         short val, pmval[2], mval[2], mvalo[2];
371         char str[32];
372         
373         for(marker= G.scene->markers.first; marker; marker= marker->next) {
374                 if(marker->flag & SELECT) totmark++;
375         }
376         if(totmark==0) return;
377         
378         oldframe= MEM_mallocN(totmark*sizeof(int), "marker array");
379         for(a=0, marker= G.scene->markers.first; marker; marker= marker->next) {
380                 if(marker->flag & SELECT) {
381                         oldframe[a]= marker->frame;
382                         selmarker= marker;      // used for hederprint
383                         a++;
384                 }
385         }
386         
387         dx= G.v2d->mask.xmax-G.v2d->mask.xmin;
388         dx= (G.v2d->cur.xmax-G.v2d->cur.xmin)/dx;
389         
390         getmouseco_areawin(pmval);
391         mvalo[0]= pmval[0];
392         
393         while(ret_val == 0) {
394                 
395                 getmouseco_areawin(mval);
396                 
397                 if (mval[0] != mvalo[0] || firsttime) {
398                         mvalo[0]= mval[0];
399                         firsttime= 0;
400                         
401                         fac= (((float)(mval[0] - pmval[0]))*dx);
402                         
403                         apply_keyb_grid(&fac, 0.0, (float)G.scene->r.frs_sec, 0.1*(float)G.scene->r.frs_sec, 0);
404                         offs= (int)fac;
405                         
406                         for(a=0, marker= G.scene->markers.first; marker; marker= marker->next) {
407                                 if(marker->flag & SELECT) {
408                                         marker->frame= oldframe[a] + offs;
409                                         a++;
410                                 }
411                         }
412                         
413                         if(totmark==1) {        // we print current marker value
414                                 if(stime->flag & TIME_DRAWFRAMES) 
415                                         sprintf(str, "Marker %d offset %d", selmarker->frame, offs);
416                                 else 
417                                         sprintf(str, "Marker %.2f offset %.2f", (selmarker->frame/(float)G.scene->r.frs_sec), (offs/(float)G.scene->r.frs_sec));
418                         }
419                         else {
420                                 if(stime->flag & TIME_DRAWFRAMES) 
421                                         sprintf(str, "Marker offset %d ", offs);
422                                 else 
423                                         sprintf(str, "Marker offset %.2f ", (offs/(float)G.scene->r.frs_sec));
424                         }
425                         headerprint(str);
426                         
427                         force_draw(0);  // areas identical to this, 0 = no header
428                 }
429                 else PIL_sleep_ms(10);  // idle
430                 
431                 /* emptying queue and reading events */
432                 while( qtest() ) {
433                         event= extern_qread(&val);
434                         
435                         if(val) {
436                                 if(event==ESCKEY || event==RIGHTMOUSE) ret_val= 2;
437                                 else if(event==LEFTMOUSE || event==RETKEY || event==SPACEKEY) ret_val= 1;
438                         }
439                 }
440         }
441         
442         /* restore? */
443         if(ret_val==2) {
444                 for(a=0, marker= G.scene->markers.first; marker; marker= marker->next) {
445                         if(marker->flag & SELECT) {
446                                 marker->frame= oldframe[a];
447                                 a++;
448                         }
449                 }
450         }
451         else {
452                 BIF_undo_push("Move Markers");
453         }
454         MEM_freeN(oldframe);
455         allqueue(REDRAWTIME, 0);
456 }
457
458 /* copy of this is actually in editscreen.c, but event based */
459 static void timeline_force_draw(short val)
460 {
461         ScrArea *sa, *tempsa, *samin= NULL;
462         int dodraw;
463         
464         if(val & TIME_LEFTMOST_3D_WIN) {
465                 ScrArea *sa= G.curscreen->areabase.first;
466                 int min= 10000;
467                 for(; sa; sa= sa->next) {
468                         if(sa->spacetype==SPACE_VIEW3D) {
469                                 if(sa->winrct.xmin - sa->winrct.ymin < min) {
470                                         samin= sa;
471                                         min= sa->winrct.xmin - sa->winrct.ymin;
472                                 }
473                         }
474                 }
475         }
476         
477         tempsa= curarea;
478         sa= G.curscreen->areabase.first;
479         while(sa) {
480                 dodraw= 0;
481                 if(sa->spacetype==SPACE_VIEW3D) {
482                         if(sa==samin || (val & TIME_ALL_3D_WIN)) dodraw= 1;
483                 }
484                 else if(ELEM6(sa->spacetype, SPACE_NLA, SPACE_IPO, SPACE_SEQ, SPACE_BUTS, SPACE_ACTION, SPACE_SOUND)) {
485                         if(val & TIME_ALL_ANIM_WIN) dodraw= 1;
486                 }
487                 else if(sa->spacetype==SPACE_BUTS) {
488                         if(val & TIME_ALL_BUTS_WIN) dodraw= 1;
489                 }
490                 else if(sa->spacetype==SPACE_TIME) dodraw= 2;
491                 
492                 if(dodraw) {
493                         areawinset(sa->win);
494                         scrarea_do_windraw(sa);
495                         if(dodraw==2) scrarea_do_headdraw(sa);
496                 }
497                 sa= sa->next;
498         }
499         areawinset(tempsa->win);
500         
501         screen_swapbuffers();
502
503 }
504
505 /* ***************************** */
506
507 /* Right. Now for some implementation: */
508 void winqreadtimespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
509 {
510         SpaceTime *stime= spacedata;
511         unsigned short event= evt->event;
512         short val= evt->val;
513         float dx, dy;
514         int doredraw= 0, cfra, first = 0;
515         short mval[2], nr;
516         short mousebut = L_MOUSE;
517         
518         if(sa->win==0) return;
519
520         if(val) {
521                 
522                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
523
524                 /* swap mouse buttons based on user preference */
525                 if (U.flag & USER_LMOUSESELECT) {
526                         if (event == LEFTMOUSE) {
527                                 event = RIGHTMOUSE;
528                                 mousebut = L_MOUSE;
529                         } else if (event == RIGHTMOUSE) {
530                                 event = LEFTMOUSE;
531                                 mousebut = R_MOUSE;
532                         }
533                 }
534
535                 switch(event) {
536                 case LEFTMOUSE:
537                         stime->flag |= TIME_CFRA_NUM;
538                         do {
539                                 getmouseco_areawin(mval);
540                                 areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
541                                 
542                                 cfra = (int)dx;
543                                 if(cfra< MINFRAME) cfra= MINFRAME;
544                                 
545                                 if( cfra!=CFRA || first )
546                                 {
547                                         first= 0;
548                                         CFRA= cfra;
549                                         update_for_newframe_nodraw(1);  // 1= nosound
550                                         timeline_force_draw(stime->redraws);
551                                 }
552                                 else PIL_sleep_ms(30);
553                         
554                         } while(get_mbut() & mousebut);
555                         
556                         stime->flag &= ~TIME_CFRA_NUM;
557                         allqueue(REDRAWALL, 0);
558                         break;
559                         
560                 case RIGHTMOUSE: /* select/deselect marker */
561                         getmouseco_areawin(mval);
562                         areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
563
564                         cfra= find_nearest_marker(dx);
565
566                         if (G.qual && LR_SHIFTKEY)
567                                 select_timeline_marker_frame(cfra, 1);
568                         else
569                                 select_timeline_marker_frame(cfra, 0);
570                         
571                         force_draw(0);
572                         std_rmouse_transform(timeline_grab);
573
574                         break;
575                 case MIDDLEMOUSE:
576                 case WHEELUPMOUSE:
577                 case WHEELDOWNMOUSE:
578                         view2dmove(event);      /* in drawipo.c */
579                         break;
580                 case PADPLUSKEY:
581                         dx= (float)(0.1154*(G.v2d->cur.xmax-G.v2d->cur.xmin));
582                         G.v2d->cur.xmin+= dx;
583                         G.v2d->cur.xmax-= dx;
584                         test_view2d(G.v2d, sa->winx, sa->winy);
585                         view2d_do_locks(curarea, V2D_LOCK_COPY);
586                         doredraw= 1;
587                         break;
588                 case PADMINUS:
589                         dx= (float)(0.15*(G.v2d->cur.xmax-G.v2d->cur.xmin));
590                         G.v2d->cur.xmin-= dx;
591                         G.v2d->cur.xmax+= dx;
592                         test_view2d(G.v2d, sa->winx, sa->winy);
593                         view2d_do_locks(curarea, V2D_LOCK_COPY);
594                         doredraw= 1;
595                         break;
596                 case HOMEKEY:
597                         first= G.scene->r.sfra;
598                         if(first >= G.scene->r.efra) first= G.scene->r.efra;
599                         G.v2d->cur.xmin=G.v2d->tot.xmin= (float)first-2;
600                         G.v2d->cur.xmax=G.v2d->tot.xmax= (float)G.scene->r.efra+2;
601                         doredraw= 1;
602                         break;
603                         
604                 case PAGEUPKEY: /* next keyframe */
605                         if(G.qual==LR_CTRLKEY)
606                                 nextprev_timeline_key(1);
607                         else
608                                 nextprev_timeline_marker(1);
609                         break;
610                 case PAGEDOWNKEY: /* prev keyframe */
611                         if(G.qual==LR_CTRLKEY)
612                                 nextprev_timeline_key(-1);
613                         else
614                                 nextprev_timeline_marker(-1);
615                         break;
616                         
617                 case AKEY:
618                         /* deselect all TimeMarkers */
619                         select_timeline_markers();
620                         doredraw= 1;
621                         break;
622                 case DKEY:
623                         if(G.qual==LR_SHIFTKEY)
624                                 duplicate_timeline_marker();
625                         break;
626                 case CKEY:
627                         timeline_frame_to_center();
628                         break;
629                 case GKEY: /* move marker */
630                         timeline_grab('g', 0);
631                         break;
632                 case EKEY: /* set end frame */
633                         G.scene->r.efra = CFRA;
634                         allqueue(REDRAWBUTSALL, 0);
635                         allqueue(REDRAWTIME, 1);
636                         break;
637                 case MKEY: /* add, rename marker */
638                         if (G.qual & LR_CTRLKEY)
639                                 rename_timeline_marker();
640                         else
641                                 add_timeline_marker(CFRA);
642                         allqueue(REDRAWTIME, 0);
643                         break;
644                 case SKEY: /* set start frame */
645                         G.scene->r.sfra = CFRA;
646                         allqueue(REDRAWBUTSALL, 0);
647                         allqueue(REDRAWTIME, 1);
648                         break;
649                 case TKEY: /* popup menu */
650                         nr= pupmenu("Time value%t|Frames %x1|Seconds%x2");
651                         if (nr>0) {
652                                 if(nr==1) stime->flag |= TIME_DRAWFRAMES;
653                                 else stime->flag &= ~TIME_DRAWFRAMES;
654                                 doredraw= 1;
655                         }
656                         break;
657                 case DELKEY:
658                 case XKEY:
659                         if( okee("Erase selected")==0 ) break;
660
661                         remove_timeline_marker();
662                         allqueue(REDRAWTIME, 0);
663                         break;
664                 }
665         }
666
667         if(doredraw)
668                 scrarea_queue_winredraw(sa);
669 }
670
671