2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Joshua Leung
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/animation/anim_draw.c
28 * \ingroup edanimation
31 #include "BLO_sys_types.h"
33 #include "DNA_anim_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
38 #include "BKE_context.h"
39 #include "BKE_global.h"
41 #include "BKE_object.h"
43 #include "ED_anim_api.h"
44 #include "ED_keyframes_edit.h"
46 #include "RNA_access.h"
50 #include "UI_interface.h"
51 #include "UI_resources.h"
52 #include "UI_view2d.h"
55 extern void ui_rasterpos_safe(float x, float y, float aspect);
57 /* *************************************************** */
58 /* TIME CODE FORMATTING */
60 /* Generate timecode/frame number string and store in the supplied string
61 * - buffer: must be at least 13 chars long
62 * - power: special setting for View2D grid drawing,
63 * used to specify how detailed we need to be
64 * - timecodes: boolean specifying whether timecodes or
65 * frame numbers get drawn
66 * - cfra: time in frames or seconds, consistent with the values shown by timecodes
68 // TODO: have this in kernel instead under scene?
69 void ANIM_timecode_string_from_frame (char *str, Scene *scene, int power, short timecodes, float cfra)
72 int hours=0, minutes=0, seconds=0, frames=0;
73 float raw_seconds= cfra;
78 /* correction for negative cfraues */
84 /* XXX should we only display a single digit for hours since clips are
85 * VERY UNLIKELY to be more than 1-2 hours max? However, that would
86 * go against conventions...
88 hours= (int)cfra / 3600;
89 cfra= (float)fmod(cfra, 3600);
93 minutes= (int)cfra / 60;
94 cfra= (float)fmod(cfra, 60);
98 * Frames are derived from 'fraction' of second. We need to perform some additional rounding
99 * to cope with 'half' frames, etc., which should be fine in most cases
102 frames= (int)floor( (((double)cfra - (double)seconds) * FPS) + 0.5 );
105 /* seconds (with pixel offset rounding) */
106 seconds= (int)floor(cfra + 0.375f);
109 switch (U.timecode_style) {
110 case USER_TIMECODE_MINIMAL:
112 /* - In general, minutes and seconds should be shown, as most clips will be
113 * within this length. Hours will only be included if relevant.
114 * - Only show frames when zoomed in enough for them to be relevant
115 * (using separator of '+' for frames).
116 * When showing frames, use slightly different display to avoid confusion with mm:ss format
119 /* include "frames" in display */
120 if (hours) sprintf(str, "%s%02d:%02d:%02d+%02d", neg, hours, minutes, seconds, frames);
121 else if (minutes) sprintf(str, "%s%02d:%02d+%02d", neg, minutes, seconds, frames);
122 else sprintf(str, "%s%d+%02d", neg, seconds, frames);
125 /* don't include 'frames' in display */
126 if (hours) sprintf(str, "%s%02d:%02d:%02d", neg, hours, minutes, seconds);
127 else sprintf(str, "%s%02d:%02d", neg, minutes, seconds);
132 case USER_TIMECODE_SMPTE_MSF:
134 /* reduced SMPTE format that always shows minutes, seconds, frames. Hours only shown as needed. */
135 if (hours) sprintf(str, "%s%02d:%02d:%02d:%02d", neg, hours, minutes, seconds, frames);
136 else sprintf(str, "%s%02d:%02d:%02d", neg, minutes, seconds, frames);
140 case USER_TIMECODE_MILLISECONDS:
142 /* reduced SMPTE. Instead of frames, milliseconds are shown */
143 int ms_dp= (power <= 0) ? (1 - power) : 1; /* precision of decimal part */
144 int s_pad= ms_dp+3; /* to get 2 digit whole-number part for seconds display (i.e. 3 is for 2 digits + radix, on top of full length) */
146 if (hours) sprintf(str, "%s%02d:%02d:%0*.*f", neg, hours, minutes, s_pad, ms_dp, cfra);
147 else sprintf(str, "%s%02d:%0*.*f", neg, minutes, s_pad, ms_dp, cfra);
151 case USER_TIMECODE_SECONDS_ONLY:
153 /* only show the original seconds display */
154 /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */
155 if (power <= 0) sprintf(str, "%.*f", 1-power, raw_seconds);
156 else sprintf(str, "%d", (int)floor(raw_seconds + 0.375f));
160 case USER_TIMECODE_SMPTE_FULL:
163 /* full SMPTE format */
164 sprintf(str, "%s%02d:%02d:%02d:%02d", neg, hours, minutes, seconds, frames);
170 /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */
171 if (power <= 0) sprintf(str, "%.*f", 1-power, cfra);
172 else sprintf(str, "%d", (int)floor(cfra + 0.375f));
176 /* *************************************************** */
177 /* CURRENT FRAME DRAWING */
179 /* Draw current frame number in a little green box beside the current frame indicator */
180 static void draw_cfra_number (Scene *scene, View2D *v2d, float cfra, short time)
182 float xscale, yscale, x, y;
183 char str[32] = " t"; /* t is the character to start replacing from */
186 /* because the frame number text is subject to the same scaling as the contents of the view */
187 UI_view2d_getscale(v2d, &xscale, &yscale);
188 glScalef(1.0f/xscale, 1.0f, 1.0f);
190 /* get timecode string
191 * - padding on str-buf passed so that it doesn't sit on the frame indicator
192 * - power = 0, gives 'standard' behaviour for time
193 * but power = 1 is required for frames (to get integer frames)
196 ANIM_timecode_string_from_frame(&str[4], scene, 0, time, FRA2TIME(cfra));
198 ANIM_timecode_string_from_frame(&str[4], scene, 1, time, cfra);
199 slen= (short)UI_GetStringWidth(str) - 1;
201 /* get starting coordinates for drawing */
205 /* draw green box around/behind text */
206 UI_ThemeColorShade(TH_CFRAME, 0);
207 glRectf(x, y, x+slen, y+15);
209 /* draw current frame number - black text */
210 UI_ThemeColor(TH_TEXT);
211 UI_DrawString(x-5, y+3, str);
213 /* restore view transform */
214 glScalef(xscale, 1.0, 1.0);
217 /* General call for drawing current frame indicator in animation editor */
218 void ANIM_draw_cfra (const bContext *C, View2D *v2d, short flag)
220 Scene *scene= CTX_data_scene(C);
223 /* Draw a light green line to indicate current frame */
224 vec[0]= (float)(scene->r.cfra * scene->r.framelen);
226 UI_ThemeColor(TH_CFRAME);
227 if (flag & DRAWCFRA_WIDE)
232 glBegin(GL_LINE_STRIP);
233 vec[1]= v2d->cur.ymin-500.0f; /* XXX arbitrary... want it go to bottom */
236 vec[1]= v2d->cur.ymax;
240 /* Draw dark green line if slow-parenting/time-offset is enabled */
241 if (flag & DRAWCFRA_SHOW_TIMEOFS) {
244 float timeoffset= give_timeoffset(ob);
245 // XXX ob->ipoflag is depreceated!
246 if ((ob->ipoflag & OB_OFFS_OB) && (timeoffset != 0.0f)) {
247 vec[0]-= timeoffset; /* could avoid calling twice */
249 UI_ThemeColorShade(TH_CFRAME, -30);
251 glBegin(GL_LINE_STRIP);
252 /*vec[1]= v2d->cur.ymax;*/ // this is set already. this line is only included
255 vec[1]= v2d->cur.ymin;
264 /* Draw current frame number in a little box */
265 if (flag & DRAWCFRA_SHOW_NUMBOX) {
266 UI_view2d_view_orthoSpecial(CTX_wm_region(C), v2d, 1);
267 draw_cfra_number(scene, v2d, vec[0], (flag & DRAWCFRA_UNIT_SECONDS));
271 /* *************************************************** */
272 /* PREVIEW RANGE 'CURTAINS' */
273 /* Note: 'Preview Range' tools are defined in anim_ops.c */
275 /* Draw preview range 'curtains' for highlighting where the animation data is */
276 void ANIM_draw_previewrange (const bContext *C, View2D *v2d)
278 Scene *scene= CTX_data_scene(C);
280 /* only draw this if preview range is set */
282 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
284 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
286 /* only draw two separate 'curtains' if there's no overlap between them */
288 glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
289 glRectf((float)PEFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
292 glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
299 /* *************************************************** */
300 /* NLA-MAPPING UTILITIES (required for drawing and also editing keyframes) */
302 /* Obtain the AnimData block providing NLA-mapping for the given channel (if applicable) */
303 // TODO: do not supply return this if the animdata tells us that there is no mapping to perform
304 AnimData *ANIM_nla_mapping_get(bAnimContext *ac, bAnimListElem *ale)
310 /* abort if rendering - we may get some race condition issues... */
311 if (G.rendering) return NULL;
313 /* handling depends on the type of animation-context we've got */
320 /* ------------------- */
322 /* helper function for ANIM_nla_mapping_apply_fcurve() -> "restore", i.e. mapping points back to action-time */
323 static short bezt_nlamapping_restore(KeyframeEditData *ked, BezTriple *bezt)
325 /* AnimData block providing scaling is stored in 'data', only_keys option is stored in i1 */
326 AnimData *adt= (AnimData *)ked->data;
327 short only_keys= (short)ked->i1;
329 /* adjust BezTriple handles only if allowed to */
330 if (only_keys == 0) {
331 bezt->vec[0][0]= BKE_nla_tweakedit_remap(adt, bezt->vec[0][0], NLATIME_CONVERT_UNMAP);
332 bezt->vec[2][0]= BKE_nla_tweakedit_remap(adt, bezt->vec[2][0], NLATIME_CONVERT_UNMAP);
335 bezt->vec[1][0]= BKE_nla_tweakedit_remap(adt, bezt->vec[1][0], NLATIME_CONVERT_UNMAP);
340 /* helper function for ANIM_nla_mapping_apply_fcurve() -> "apply", i.e. mapping points to NLA-mapped global time */
341 static short bezt_nlamapping_apply(KeyframeEditData *ked, BezTriple *bezt)
343 /* AnimData block providing scaling is stored in 'data', only_keys option is stored in i1 */
344 AnimData *adt= (AnimData*)ked->data;
345 short only_keys= (short)ked->i1;
347 /* adjust BezTriple handles only if allowed to */
348 if (only_keys == 0) {
349 bezt->vec[0][0]= BKE_nla_tweakedit_remap(adt, bezt->vec[0][0], NLATIME_CONVERT_MAP);
350 bezt->vec[2][0]= BKE_nla_tweakedit_remap(adt, bezt->vec[2][0], NLATIME_CONVERT_MAP);
353 bezt->vec[1][0]= BKE_nla_tweakedit_remap(adt, bezt->vec[1][0], NLATIME_CONVERT_MAP);
359 /* Apply/Unapply NLA mapping to all keyframes in the nominated F-Curve
360 * - restore = whether to map points back to non-mapped time
361 * - only_keys = whether to only adjust the location of the center point of beztriples
363 void ANIM_nla_mapping_apply_fcurve (AnimData *adt, FCurve *fcu, short restore, short only_keys)
365 KeyframeEditData ked= {{NULL}};
366 KeyframeEditFunc map_cb;
369 * - AnimData is stored in 'data'
370 * - only_keys is stored in 'i1'
372 ked.data= (void *)adt;
373 ked.i1= (int)only_keys;
375 /* get editing callback */
377 map_cb= bezt_nlamapping_restore;
379 map_cb= bezt_nlamapping_apply;
381 /* apply to F-Curve */
382 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, map_cb, NULL);
385 /* *************************************************** */
386 /* UNITS CONVERSION MAPPING (required for drawing and editing keyframes) */
388 /* Get unit conversion factor for given ID + F-Curve */
389 float ANIM_unit_mapping_get_factor (Scene *scene, ID *id, FCurve *fcu, short restore)
392 if (id && fcu && fcu->rna_path)
394 PointerRNA ptr, id_ptr;
397 /* get RNA property that F-Curve affects */
398 RNA_id_pointer_create(id, &id_ptr);
399 if (RNA_path_resolve(&id_ptr, fcu->rna_path, &ptr, &prop))
401 /* rotations: radians <-> degrees? */
402 if (RNA_SUBTYPE_UNIT(RNA_property_subtype(prop)) == PROP_UNIT_ROTATION)
404 /* if the radians flag is not set, default to using degrees which need conversions */
405 if ((scene) && (scene->unit.system_rotation == USER_UNIT_ROT_RADIANS) == 0) {
407 return M_PI / 180.0; /* degrees to radians */
409 return 180.0 / M_PI; /* radians to degrees */
413 // TODO: other rotation types here as necessary
417 /* no mapping needs to occur... */
421 /* ----------------------- */
423 /* helper function for ANIM_unit_mapping_apply_fcurve -> mapping callback for unit mapping */
424 static short bezt_unit_mapping_apply (KeyframeEditData *ked, BezTriple *bezt)
426 /* mapping factor is stored in f1, flags are stored in i1 */
427 short only_keys= (ked->i1 & ANIM_UNITCONV_ONLYKEYS);
428 short sel_vs= (ked->i1 & ANIM_UNITCONV_SELVERTS);
431 /* adjust BezTriple handles only if allowed to */
432 if (only_keys == 0) {
433 if ((sel_vs==0) || (bezt->f1 & SELECT))
434 bezt->vec[0][1] *= fac;
435 if ((sel_vs==0) || (bezt->f3 & SELECT))
436 bezt->vec[2][1] *= fac;
439 if ((sel_vs == 0) || (bezt->f2 & SELECT))
440 bezt->vec[1][1] *= fac;
445 /* Apply/Unapply units conversions to keyframes */
446 void ANIM_unit_mapping_apply_fcurve (Scene *scene, ID *id, FCurve *fcu, short flag)
448 KeyframeEditData ked;
449 KeyframeEditFunc sel_cb;
452 /* abort if rendering - we may get some race condition issues... */
453 if (G.rendering) return;
455 /* calculate mapping factor, and abort if nothing to change */
456 fac= ANIM_unit_mapping_get_factor(scene, id, fcu, (flag & ANIM_UNITCONV_RESTORE));
461 * - mapping factor is stored in f1
462 * - flags are stored in 'i1'
464 memset(&ked, 0, sizeof(KeyframeEditData));
469 if (flag & ANIM_UNITCONV_ONLYSEL)
470 sel_cb= ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
474 /* apply to F-Curve */
475 ANIM_fcurve_keyframes_loop(&ked, fcu, sel_cb, bezt_unit_mapping_apply, NULL);
477 // FIXME: loop here for samples should be generalised
483 for (i=0, fpt=fcu->fpt; i < fcu->totvert; i++, fpt++) {
484 /* apply unit mapping */
490 /* *************************************************** */