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