Final merge of HEAD (bf-blender) into the orange branch.
[blender.git] / source / blender / src / drawtime.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) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "DNA_action_types.h"
42 #include "DNA_ipo_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_screen_types.h"
48
49 #include "BKE_ipo.h"
50 #include "BKE_object.h"
51 #include "BKE_material.h"
52 #include "BKE_utildefines.h"
53 #include "BKE_global.h"
54
55 #include "BIF_gl.h"
56 #include "BIF_interface_icons.h"
57 #include "BIF_mywindow.h"
58 #include "BIF_screen.h"
59 #include "BIF_resources.h"
60
61 #include "BSE_drawipo.h"
62 #include "BSE_view.h"
63 #include "BMF_Api.h"
64
65 #include "blendef.h"
66
67 /* ---- prototypes ------ */
68 void drawtimespace(ScrArea *, void *);
69
70
71 static void draw_cfra_time(SpaceTime *stime)
72 {
73         float vec[2];
74         
75         vec[0]=  (G.scene->r.cfra);
76         vec[0]*= G.scene->r.framelen;
77
78         vec[1]= G.v2d->cur.ymin;
79         glColor3ub(0x60, 0xc0, 0x40);   // no theme, should be global color once...
80         glLineWidth(3.0);
81
82         glBegin(GL_LINES);
83                 glVertex2fv(vec);
84                 vec[1]= G.v2d->cur.ymax;
85                 glVertex2fv(vec);
86         glEnd();
87         
88         glLineWidth(1.0);
89         
90         if(stime->flag & TIME_CFRA_NUM) {
91                 short mval[2];
92                 float x,  y;
93                 char str[32];
94                 /* little box with frame */
95                 
96                 glFlush();      // huhh... without this glColor won't work for the text...
97                 getmouseco_areawin(mval);
98                 
99                 if(mval[1]>curarea->winy-10) mval[1]= curarea->winy - 13;
100                 
101                 if (curarea->winy < 25) {       
102                         if (mval[1]<17) mval[1]= 17;
103                 } else if (mval[1]<22) mval[1]= 22;
104                 
105                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
106                 
107                 if(stime->flag & TIME_DRAWFRAMES) 
108                         sprintf(str, "   %d\n", (G.scene->r.cfra));
109                 else sprintf(str, "   %.2f\n", (G.scene->r.cfra/(float)G.scene->r.frs_sec));
110                 
111                 /* HACK! somehow the green color won't go away... */
112                 glColor4ub(0, 0, 0, 0);
113                 BIF_ThemeColor(TH_TEXT);
114                 
115                 glRasterPos2f(x, y);
116                 BMF_DrawString(G.fonts, str);
117                 
118         }
119         
120 }
121
122 static void draw_marker(TimeMarker *marker)
123 {
124         float xpos, xspace, yspace, xpixels, ypixels;
125
126         xpos = marker->frame;
127         /* no time correction for framelen! space is drawn with old values */
128         
129         xspace= G.v2d->cur.xmax - G.v2d->cur.xmin;
130         yspace= G.v2d->cur.ymax - G.v2d->cur.ymin;
131         xpixels= G.v2d->mask.xmax-G.v2d->mask.xmin;
132         ypixels= G.v2d->mask.ymax-G.v2d->mask.ymin;
133
134         glEnable(GL_BLEND);
135         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);                      
136         
137         /* 5 px to offset icon to align properly, space / pixels corrects for zoom */
138         if(marker->flag & SELECT)
139                 BIF_icon_draw_blended(xpos-(5.0*(xspace/xpixels)), 12.0*yspace/ypixels, ICON_MARKER_HLT, TH_BACK, 0);
140         else
141                 BIF_icon_draw_blended(xpos-(5.0*(xspace/xpixels)), 12.0*yspace/ypixels, ICON_MARKER, TH_BACK, 0);
142         
143         glBlendFunc(GL_ONE, GL_ZERO);
144         glDisable(GL_BLEND);            
145
146         /* and the marker name too, shifted slightly to the top-right */
147         if(marker->name && marker->name[0]) {
148                 if(marker->flag & SELECT) {
149                         BIF_ThemeColor(TH_TEXT_HI);
150                         glRasterPos2f(xpos+(4.0*(xspace/xpixels)),
151                                         ((ypixels<=39.0)?(ypixels-10.0):29.0)*yspace/ypixels);
152                 }
153                 else {
154                         BIF_ThemeColor(TH_TEXT);
155                         if((marker->frame <= G.scene->r.cfra) && (marker->frame+5 > G.scene->r.cfra))
156                                 glRasterPos2f(xpos+(4.0*(xspace/xpixels)),
157                                                 ((ypixels<=39.0)?(ypixels-10.0):29.0)*yspace/ypixels);
158                         else
159                                 glRasterPos2f(xpos+(4.0*(xspace/xpixels)), 17.0*yspace/ypixels);
160                 }
161                 BMF_DrawString(G.font, marker->name);
162         }
163 }
164
165 static void draw_markers_time( void )
166 {
167         TimeMarker *marker;
168
169         /* unselected markers are drawn at the first time */
170         for(marker= G.scene->markers.first; marker; marker= marker->next) {
171                 if(!(marker->flag & SELECT)) draw_marker(marker);
172         }
173
174         /* selected markers are drawn later ... selected markers have to cover unselected
175          * markers laying at the same position as selected markers
176          * (jiri: it is hack, it could be solved better) */
177         for(marker= G.scene->markers.first; marker; marker= marker->next) {
178                 if(marker->flag & SELECT) draw_marker(marker);
179         }
180 }
181
182 static void draw_sfra_efra()
183 {
184         BIF_ThemeColorShade(TH_BACK, -25);
185         
186         if (G.scene->r.sfra < G.scene->r.efra) {
187                 glRectf(G.v2d->cur.xmin, G.v2d->cur.ymin, G.scene->r.sfra, G.v2d->cur.ymax);
188                 
189                 glRectf(G.scene->r.efra, G.v2d->cur.ymin, G.v2d->cur.xmax, G.v2d->cur.ymax);    
190         } else {
191                 glRectf(G.v2d->cur.xmin, G.v2d->cur.ymin, G.v2d->cur.xmax, G.v2d->cur.ymax);
192         }
193         
194         BIF_ThemeColorShade(TH_BACK, -60);
195         /* thin lines where the actual frames are */
196         fdrawline(G.scene->r.sfra, G.v2d->cur.ymin, G.scene->r.sfra, G.v2d->cur.ymax);
197         fdrawline(G.scene->r.efra, G.v2d->cur.ymin, G.scene->r.efra, G.v2d->cur.ymax);
198         
199         glDisable(GL_BLEND);
200 }
201
202 /*draw all the keys in a list (elems) as lines */
203 static void draw_key_list(ListBase elems, char col[3]) 
204 {
205         CfraElem *ce;
206         float drawframe;
207
208         ce= elems.first;
209         while(ce) {
210                 drawframe = ce->cfra; //not correct for G.scene->r.framelen;
211                 glColor3ub(col[0], col[1], col[2]);
212
213                 fdrawline(drawframe, G.v2d->cur.ymin, drawframe, G.v2d->cur.ymax);
214                 
215                 ce= ce->next;
216         }
217 }
218
219 static void draw_ob_keys()
220 {
221         /*mostly copied from drawobject.c, draw_object() */
222         Object *ob;
223         bActionChannel *achan;
224         bAction *act;
225         ListBase elems;
226         int a;
227         char col[3];
228
229         if (OBACT) {
230                 ob = OBACT;
231                 
232                 if(ob) {
233                         if(ob!=G.obedit) {
234                                 if(ob->ipo) {
235                                         /* convert the ipo to a list of 'current frame elements' */
236                                                 
237                                         elems.first= elems.last= NULL;
238                                         make_cfra_list(ob->ipo, &elems);
239                                         
240                                         /* draw the list of current frame elements */
241                                         col[0] = 0xDD; col[1] = 0xD7; col[2] = 0x00;
242                                         draw_key_list(elems, col);
243                                         
244                                         BLI_freelistN(&elems);
245                                 }
246                                 
247                                 if(ob->action) {
248                                         act = ob->action;
249
250                                         /* go through each channel in the action */
251                                         for (achan=act->chanbase.first; achan; achan=achan->next){
252                                                 /* convert the ipo to a list of 'current frame elements' */
253                                                 if(achan->ipo) {
254                                                         elems.first= elems.last= NULL;
255                                                         make_cfra_list(achan->ipo, &elems);
256
257                                                         col[0] = 0x00; col[1] = 0x82; col[2] = 0x8B;
258                                                         draw_key_list(elems, col);
259                                                         
260                                                         BLI_freelistN(&elems);
261                                                 }
262                                         }
263                                 }
264                                 
265                                 for(a=0; a<ob->totcol; a++) {
266                                         Material *ma= give_current_material(ob, a+1);
267                                         
268                                         if(ma && ma->ipo) {
269                                                 elems.first= elems.last= NULL;
270                                                 make_cfra_list(ma->ipo, &elems);
271                                                 
272                                                 col[0] = 0xDD; col[1] = 0xA7; col[2] = 0x00;
273                                                 draw_key_list(elems, col);
274                                                 
275                                                 BLI_freelistN(&elems);
276                                         }
277                                 }
278                                 
279                         }
280                 }
281         }
282 }
283
284 void drawtimespace(ScrArea *sa, void *spacedata)
285 {
286         SpaceTime *stime= sa->spacedata.first;
287         float col[3];
288         
289         BIF_GetThemeColor3fv(TH_BACK, col);
290         glClearColor(col[0], col[1], col[2], 0.0);
291         glClear(GL_COLOR_BUFFER_BIT);
292
293         calc_scrollrcts(sa, &(stime->v2d), curarea->winx, curarea->winy);
294         
295         myortho2(stime->v2d.cur.xmin, stime->v2d.cur.xmax, stime->v2d.cur.ymin, stime->v2d.cur.ymax);
296
297         /* draw darkened area outside of active timeline */
298         draw_sfra_efra();
299         
300         /* boundbox_seq(); */
301         calc_ipogrid(); 
302         draw_ipogrid();
303
304         draw_cfra_time(spacedata);
305         draw_ob_keys();
306         draw_markers_time();
307
308         /* restore viewport */
309         mywinset(curarea->win);
310
311         /* ortho at pixel level curarea */
312         myortho2(-0.375, curarea->winx-0.375, -0.375, curarea->winy-0.375);
313         
314         /* the bottom with time values */
315         BIF_ThemeColor(TH_HEADER);
316         glRectf(0.0f, 0.0f, (float)curarea->winx, 12.0f);
317         BIF_ThemeColorShade(TH_HEADER, 50);
318         fdrawline(0.0f, 12.0f, (float)curarea->winx, 12.0f);
319         draw_view2d_numbers_horiz(stime->flag & TIME_DRAWFRAMES);
320         
321         draw_area_emboss(sa);
322         curarea->win_swap= WIN_BACK_OK;
323 }