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