- added GCC warning -Wstrict-prototypes
[blender.git] / source / blender / editors / space_action / action_draw.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Joshua Leung
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /* System includes ----------------------------------------------------- */
31
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <float.h>
36
37
38 #include "BLI_blenlib.h"
39 #include "BLI_math.h"
40
41 /* Types --------------------------------------------------------------- */
42
43 #include "DNA_anim_types.h"
44 #include "DNA_screen_types.h"
45
46 #include "BKE_action.h"
47 #include "BKE_context.h"
48 #include "BKE_utildefines.h"
49
50 /* Everything from source (BIF, BDR, BSE) ------------------------------ */ 
51
52 #include "BIF_gl.h"
53
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
57
58 #include "ED_anim_api.h"
59 #include "ED_keyframes_draw.h"
60
61
62 /* ************************************************************************* */
63 /* Channel List */
64
65 /* left hand part */
66 void draw_channel_names(bContext *C, bAnimContext *ac, ARegion *ar) 
67 {
68         ListBase anim_data = {NULL, NULL};
69         bAnimListElem *ale;
70         int filter;
71         
72         View2D *v2d= &ar->v2d;
73         float y= 0.0f;
74         int items, height;
75         
76         /* build list of channels to draw */
77         filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CHANNELS);
78         items= ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
79         
80         /* Update max-extent of channels here (taking into account scrollers):
81          *      - this is done to allow the channel list to be scrollable, but must be done here
82          *        to avoid regenerating the list again and/or also because channels list is drawn first
83          *      - offset of ACHANNEL_HEIGHT*2 is added to the height of the channels, as first is for 
84          *        start of list offset, and the second is as a correction for the scrollers.
85          */
86         height= ((items*ACHANNEL_STEP) + (ACHANNEL_HEIGHT*2));
87         if (height > (v2d->mask.ymax - v2d->mask.ymin)) {
88                 /* don't use totrect set, as the width stays the same 
89                  * (NOTE: this is ok here, the configuration is pretty straightforward) 
90                  */
91                 v2d->tot.ymin= (float)(-height);
92         }
93         /* need to do a view-sync here, so that the keys area doesn't jump around (it must copy this) */
94         UI_view2d_sync(NULL, ac->sa, v2d, V2D_LOCK_COPY);
95         
96         /* loop through channels, and set up drawing depending on their type  */        
97         {       /* first pass: just the standard GL-drawing for backdrop + text */
98                 y= (float)ACHANNEL_FIRST;
99                 
100                 for (ale= anim_data.first; ale; ale= ale->next) {
101                         float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
102                         float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
103                         
104                         /* check if visible */
105                         if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
106                                  IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
107                         {
108                                 /* draw all channels using standard channel-drawing API */
109                                 ANIM_channel_draw(ac, ale, yminc, ymaxc);
110                         }
111                         
112                         /* adjust y-position for next one */
113                         y -= ACHANNEL_STEP;
114                 }
115         }
116         {       /* second pass: widgets */
117                 uiBlock *block= uiBeginBlock(C, ar, "dopesheet channel buttons", UI_EMBOSS);
118                 
119                 y= (float)ACHANNEL_FIRST;
120                 
121                 for (ale= anim_data.first; ale; ale= ale->next) {
122                         float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
123                         float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
124                         
125                         /* check if visible */
126                         if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
127                                  IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
128                         {
129                                 /* draw all channels using standard channel-drawing API */
130                                 ANIM_channel_draw_widgets(ac, ale, block, yminc, ymaxc);
131                         }
132                         
133                         /* adjust y-position for next one */
134                         y -= ACHANNEL_STEP;
135                 }
136                 
137                 uiEndBlock(C, block);
138                 uiDrawBlock(C, block);
139         }
140         
141         /* free tempolary channels */
142         BLI_freelistN(&anim_data);
143 }
144
145 /* ************************************************************************* */
146 /* Keyframes */
147
148 /* extra padding for lengths (to go under scrollers) */
149 #define EXTRA_SCROLL_PAD        100.0f
150
151 /* draw keyframes in each channel */
152 void draw_channel_strips(bAnimContext *ac, SpaceAction *saction, ARegion *ar)
153 {
154         ListBase anim_data = {NULL, NULL};
155         bAnimListElem *ale;
156         int filter;
157         
158         View2D *v2d= &ar->v2d;
159         bDopeSheet *ads= &saction->ads;
160         AnimData *adt= NULL;
161         
162         float act_start, act_end, y;
163         int height, items;
164         
165         char col1[3], col2[3];
166         char col1a[3], col2a[3];
167         char col1b[3], col2b[3];
168         
169         
170         /* get theme colors */
171         UI_GetThemeColor3ubv(TH_BACK, col2);
172         UI_GetThemeColor3ubv(TH_HILITE, col1);
173         
174         UI_GetThemeColor3ubv(TH_GROUP, col2a);
175         UI_GetThemeColor3ubv(TH_GROUP_ACTIVE, col1a);
176         
177         UI_GetThemeColor3ubv(TH_DOPESHEET_CHANNELOB, col1b);
178         UI_GetThemeColor3ubv(TH_DOPESHEET_CHANNELSUBOB, col2b);
179         
180         /* set view-mapping rect (only used for x-axis), for NLA-scaling mapping with less calculation */
181
182         /* if in NLA there's a strip active, map the view */
183         if (ac->datatype == ANIMCONT_ACTION) {
184                 adt= ANIM_nla_mapping_get(ac, NULL);
185                 
186                 /* start and end of action itself */
187                 calc_action_range(ac->data, &act_start, &act_end, 0);
188         }
189         
190         /* build list of channels to draw */
191         filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CHANNELS);
192         items= ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
193         
194         /* Update max-extent of channels here (taking into account scrollers):
195          *      - this is done to allow the channel list to be scrollable, but must be done here
196          *        to avoid regenerating the list again and/or also because channels list is drawn first
197          *      - offset of ACHANNEL_HEIGHT*2 is added to the height of the channels, as first is for 
198          *        start of list offset, and the second is as a correction for the scrollers.
199          */
200         height= ((items*ACHANNEL_STEP) + (ACHANNEL_HEIGHT*2));
201         /* don't use totrect set, as the width stays the same 
202          * (NOTE: this is ok here, the configuration is pretty straightforward) 
203          */
204         v2d->tot.ymin= (float)(-height);
205         
206         /* first backdrop strips */
207         y= (float)(-ACHANNEL_HEIGHT);
208         glEnable(GL_BLEND);
209         
210         for (ale= anim_data.first; ale; ale= ale->next) {
211                 const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
212                 const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
213                 
214                 /* check if visible */
215                 if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
216                          IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
217                 {
218                         bAnimChannelType *acf= ANIM_channel_get_typeinfo(ale);
219                         int sel=0;
220                         
221                         /* determine if any need to draw channel */
222                         if (ale->datatype != ALE_NONE) {
223                                 /* determine if channel is selected */
224                                 if (acf->has_setting(ac, ale, ACHANNEL_SETTING_SELECT))
225                                         sel= ANIM_channel_setting_get(ac, ale, ACHANNEL_SETTING_SELECT);
226                                 
227                                 if (ELEM3(ac->datatype, ANIMCONT_ACTION, ANIMCONT_DOPESHEET, ANIMCONT_SHAPEKEY)) {
228                                         switch (ale->type) {
229                                                 case ANIMTYPE_SUMMARY:
230                                                 {
231                                                         // FIXME: hardcoded colors - reddish color from NLA
232                                                         glColor4f(0.8f, 0.2f, 0.0f, 0.4f);
233                                                 }
234                                                         break;
235                                                 
236                                                 case ANIMTYPE_SCENE:
237                                                 case ANIMTYPE_OBJECT:
238                                                 {
239                                                         if (sel) glColor4ub(col1b[0], col1b[1], col1b[2], 0x45); 
240                                                         else glColor4ub(col1b[0], col1b[1], col1b[2], 0x22); 
241                                                 }
242                                                         break;
243                                                 
244                                                 case ANIMTYPE_FILLACTD:
245                                                 case ANIMTYPE_FILLMATD:
246                                                 case ANIMTYPE_FILLPARTD:
247                                                 case ANIMTYPE_DSSKEY:
248                                                 case ANIMTYPE_DSWOR:
249                                                 {
250                                                         if (sel) glColor4ub(col2b[0], col2b[1], col2b[2], 0x45); 
251                                                         else glColor4ub(col2b[0], col2b[1], col2b[2], 0x22); 
252                                                 }
253                                                         break;
254                                                 
255                                                 case ANIMTYPE_GROUP:
256                                                 {
257                                                         if (sel) glColor4ub(col1a[0], col1a[1], col1a[2], 0x22);
258                                                         else glColor4ub(col2a[0], col2a[1], col2a[2], 0x22);
259                                                 }
260                                                         break;
261                                                 
262                                                 default:
263                                                 {
264                                                         if (sel) glColor4ub(col1[0], col1[1], col1[2], 0x22);
265                                                         else glColor4ub(col2[0], col2[1], col2[2], 0x22);
266                                                 }
267                                                         break;
268                                         }
269                                         
270                                         /* draw region twice: firstly backdrop, then the current range */
271                                         glRectf(v2d->cur.xmin,  (float)y-ACHANNEL_HEIGHT_HALF,  v2d->cur.xmax+EXTRA_SCROLL_PAD,  (float)y+ACHANNEL_HEIGHT_HALF);
272                                         
273                                         if (ac->datatype == ANIMCONT_ACTION)
274                                                 glRectf(act_start,  (float)y-ACHANNEL_HEIGHT_HALF,  act_end,  (float)y+ACHANNEL_HEIGHT_HALF);
275                                 }
276                                 else if (ac->datatype == ANIMCONT_GPENCIL) {
277                                         /* frames less than one get less saturated background */
278                                         if (sel) glColor4ub(col1[0], col1[1], col1[2], 0x22);
279                                         else glColor4ub(col2[0], col2[1], col2[2], 0x22);
280                                         glRectf(0.0f, (float)y-ACHANNEL_HEIGHT_HALF, v2d->cur.xmin, (float)y+ACHANNEL_HEIGHT_HALF);
281                                         
282                                         /* frames one and higher get a saturated background */
283                                         if (sel) glColor4ub(col1[0], col1[1], col1[2], 0x44);
284                                         else glColor4ub(col2[0], col2[1], col2[2], 0x44);
285                                         glRectf(v2d->cur.xmin, (float)y-ACHANNEL_HEIGHT_HALF, v2d->cur.xmax+EXTRA_SCROLL_PAD,  (float)y+ACHANNEL_HEIGHT_HALF);
286                                 }
287                         }
288                 }
289                 
290                 /*      Increment the step */
291                 y -= ACHANNEL_STEP;
292         }               
293         glDisable(GL_BLEND);
294         
295         /* Draw keyframes 
296          *      1) Only channels that are visible in the Action Editor get drawn/evaluated.
297          *         This is to try to optimise this for heavier data sets
298          *      2) Keyframes which are out of view horizontally are disregarded 
299          */
300         y= (float)(-ACHANNEL_HEIGHT);
301         
302         for (ale= anim_data.first; ale; ale= ale->next) {
303                 const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
304                 const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
305                 
306                 /* check if visible */
307                 if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
308                          IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
309                 {
310                         /* check if anything to show for this channel */
311                         if (ale->datatype != ALE_NONE) {
312                                 adt= ANIM_nla_mapping_get(ac, ale);
313                                 
314                                 /* draw 'keyframes' for each specific datatype */
315                                 switch (ale->datatype) {
316                                         case ALE_ALL:
317                                                 draw_summary_channel(v2d, ale->data, y);
318                                                 break;
319                                         case ALE_SCE:
320                                                 draw_scene_channel(v2d, ads, ale->key_data, y);
321                                                 break;
322                                         case ALE_OB:
323                                                 draw_object_channel(v2d, ads, ale->key_data, y);
324                                                 break;
325                                         case ALE_ACT:
326                                                 draw_action_channel(v2d, adt, ale->key_data, y);
327                                                 break;
328                                         case ALE_GROUP:
329                                                 draw_agroup_channel(v2d, adt, ale->data, y);
330                                                 break;
331                                         case ALE_FCURVE:
332                                                 draw_fcurve_channel(v2d, adt, ale->key_data, y);
333                                                 break;
334                                         case ALE_GPFRAME:
335                                                 draw_gpl_channel(v2d, ads, ale->data, y);
336                                                 break;
337                                 }
338                         }
339                 }
340                 
341                 y-= ACHANNEL_STEP;
342         }
343         
344         /* free tempolary channels used for drawing */
345         BLI_freelistN(&anim_data);
346
347         /* black line marking 'current frame' for Time-Slide transform mode */
348         if (saction->flag & SACTION_MOVING) {
349                 glColor3f(0.0f, 0.0f, 0.0f);
350                 
351                 glBegin(GL_LINES);
352                         glVertex2f(saction->timeslide, v2d->cur.ymin-EXTRA_SCROLL_PAD);
353                         glVertex2f(saction->timeslide, v2d->cur.ymax);
354                 glEnd();
355         }
356 }