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