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