diff options
author | Campbell Barton <ideasman42@gmail.com> | 2014-01-29 13:01:30 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-01-29 13:44:01 +0400 |
commit | 39eb314cb922b805e9126d5f0352f31c2f84f151 (patch) | |
tree | 9fc861802eeaab149539eb3692a975969cea03db /source/blender/editors | |
parent | f70d9660474c2be5f56d65247df3be5af0479e08 (diff) |
UI: Refactor timecode functions into BLI_timecode
- deduplicate timecode_simple_string from image.c
- replace V2D_UNIT_SECONDSSEQ with V2D_UNIT_SECONDS
- avoid possible buffer overflow bugs (sprintf -> BLI_snprintf)
- remove option not to use timecode and split into 2 functions
Patch D227 by Andrew Buttery with own refactoring.
Diffstat (limited to 'source/blender/editors')
-rw-r--r-- | source/blender/editors/animation/anim_draw.c | 130 | ||||
-rw-r--r-- | source/blender/editors/include/ED_anim_api.h | 3 | ||||
-rw-r--r-- | source/blender/editors/include/UI_view2d.h | 1 | ||||
-rw-r--r-- | source/blender/editors/interface/view2d.c | 20 | ||||
-rw-r--r-- | source/blender/editors/space_sequencer/sequencer_draw.c | 2 |
5 files changed, 18 insertions, 138 deletions
diff --git a/source/blender/editors/animation/anim_draw.c b/source/blender/editors/animation/anim_draw.c index 25fcd76b513..b1789d25b88 100644 --- a/source/blender/editors/animation/anim_draw.c +++ b/source/blender/editors/animation/anim_draw.c @@ -37,6 +37,7 @@ #include "DNA_userdef_types.h" #include "BLI_math.h" +#include "BLI_timecode.h" #include "BKE_context.h" #include "BKE_blender.h" @@ -56,125 +57,10 @@ #include "UI_view2d.h" /* *************************************************** */ -/* TIME CODE FORMATTING */ - -/* Generate timecode/frame number string and store in the supplied string - * - buffer: must be at least 13 chars long - * - power: special setting for View2D grid drawing, - * used to specify how detailed we need to be - * - timecodes: boolean specifying whether timecodes or - * frame numbers get drawn - * - cfra: time in frames or seconds, consistent with the values shown by timecodes - */ -// TODO: have this in kernel instead under scene? -void ANIM_timecode_string_from_frame(char *str, Scene *scene, int power, short timecodes, float cfra) -{ - if (timecodes) { - int hours = 0, minutes = 0, seconds = 0, frames = 0; - float raw_seconds = cfra; - char neg[2] = {'\0'}; - - /* get cframes */ - if (cfra < 0) { - /* correction for negative cfraues */ - neg[0] = '-'; - cfra = -cfra; - } - if (cfra >= 3600) { - /* hours */ - /* XXX should we only display a single digit for hours since clips are - * VERY UNLIKELY to be more than 1-2 hours max? However, that would - * go against conventions... - */ - hours = (int)cfra / 3600; - cfra = (float)fmod(cfra, 3600); - } - if (cfra >= 60) { - /* minutes */ - minutes = (int)cfra / 60; - cfra = (float)fmod(cfra, 60); - } - if (power <= 0) { - /* seconds + frames - * Frames are derived from 'fraction' of second. We need to perform some additional rounding - * to cope with 'half' frames, etc., which should be fine in most cases - */ - seconds = (int)cfra; - frames = (int)floor( (((double)cfra - (double)seconds) * FPS) + 0.5); - } - else { - /* seconds (with pixel offset rounding) */ - seconds = (int)floor(cfra + GLA_PIXEL_OFS); - } - - switch (U.timecode_style) { - case USER_TIMECODE_MINIMAL: - { - /* - In general, minutes and seconds should be shown, as most clips will be - * within this length. Hours will only be included if relevant. - * - Only show frames when zoomed in enough for them to be relevant - * (using separator of '+' for frames). - * When showing frames, use slightly different display to avoid confusion with mm:ss format - */ - if (power <= 0) { - /* include "frames" in display */ - if (hours) sprintf(str, "%s%02d:%02d:%02d+%02d", neg, hours, minutes, seconds, frames); - else if (minutes) sprintf(str, "%s%02d:%02d+%02d", neg, minutes, seconds, frames); - else sprintf(str, "%s%d+%02d", neg, seconds, frames); - } - else { - /* don't include 'frames' in display */ - if (hours) sprintf(str, "%s%02d:%02d:%02d", neg, hours, minutes, seconds); - else sprintf(str, "%s%02d:%02d", neg, minutes, seconds); - } - break; - } - case USER_TIMECODE_SMPTE_MSF: - { - /* reduced SMPTE format that always shows minutes, seconds, frames. Hours only shown as needed. */ - if (hours) sprintf(str, "%s%02d:%02d:%02d:%02d", neg, hours, minutes, seconds, frames); - else sprintf(str, "%s%02d:%02d:%02d", neg, minutes, seconds, frames); - break; - } - case USER_TIMECODE_MILLISECONDS: - { - /* reduced SMPTE. Instead of frames, milliseconds are shown */ - int ms_dp = (power <= 0) ? (1 - power) : 1; /* precision of decimal part */ - 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) */ - - if (hours) sprintf(str, "%s%02d:%02d:%0*.*f", neg, hours, minutes, s_pad, ms_dp, cfra); - else sprintf(str, "%s%02d:%0*.*f", neg, minutes, s_pad, ms_dp, cfra); - break; - } - case USER_TIMECODE_SECONDS_ONLY: - { - /* only show the original seconds display */ - /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */ - if (power <= 0) sprintf(str, "%.*f", 1 - power, raw_seconds); - else sprintf(str, "%d", (int)floor(raw_seconds + GLA_PIXEL_OFS)); - break; - } - case USER_TIMECODE_SMPTE_FULL: - default: - { - /* full SMPTE format */ - sprintf(str, "%s%02d:%02d:%02d:%02d", neg, hours, minutes, seconds, frames); - break; - } - } - } - else { - /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */ - if (power <= 0) sprintf(str, "%.*f", 1 - power, cfra); - else sprintf(str, "%d", (int)floor(cfra + GLA_PIXEL_OFS)); - } -} - -/* *************************************************** */ /* CURRENT FRAME DRAWING */ /* Draw current frame number in a little green box beside the current frame indicator */ -static void draw_cfra_number(Scene *scene, View2D *v2d, float cfra, short time) +static void draw_cfra_number(Scene *scene, View2D *v2d, const float cfra, const bool time) { float xscale, yscale, x, y; char numstr[32] = " t"; /* t is the character to start replacing from */ @@ -189,10 +75,12 @@ static void draw_cfra_number(Scene *scene, View2D *v2d, float cfra, short time) * - power = 0, gives 'standard' behavior for time * but power = 1 is required for frames (to get integer frames) */ - if (time) - ANIM_timecode_string_from_frame(&numstr[4], scene, 0, time, FRA2TIME(cfra)); - else - ANIM_timecode_string_from_frame(&numstr[4], scene, 1, time, cfra); + if (time) { + BLI_timecode_string_from_time(&numstr[4], sizeof(numstr) - 4, 0, FRA2TIME(cfra), FPS, U.timecode_style); + } + else { + BLI_timecode_string_from_time_simple(&numstr[4], sizeof(numstr) - 4, 1, cfra); + } slen = (short)UI_GetStringWidth(numstr) - 1; /* get starting coordinates for drawing */ @@ -239,7 +127,7 @@ void ANIM_draw_cfra(const bContext *C, View2D *v2d, short flag) /* Draw current frame number in a little box */ if (flag & DRAWCFRA_SHOW_NUMBOX) { UI_view2d_view_orthoSpecial(CTX_wm_region(C), v2d, 1); - draw_cfra_number(scene, v2d, vec[0], (flag & DRAWCFRA_UNIT_SECONDS)); + draw_cfra_number(scene, v2d, vec[0], (flag & DRAWCFRA_UNIT_SECONDS) != 0); } } diff --git a/source/blender/editors/include/ED_anim_api.h b/source/blender/editors/include/ED_anim_api.h index dff5069d991..c13d0c45f21 100644 --- a/source/blender/editors/include/ED_anim_api.h +++ b/source/blender/editors/include/ED_anim_api.h @@ -471,9 +471,6 @@ void ANIM_fcurve_delete_from_animdata(bAnimContext *ac, struct AnimData *adt, st /* DRAWING API */ /* anim_draw.c */ -/* Get string representing the given frame number as an appropriately represented frame or timecode */ -void ANIM_timecode_string_from_frame(char *str, struct Scene *scene, int power, short timecodes, float cfra); - /* ---------- Current Frame Drawing ---------------- */ /* flags for Current Frame Drawing */ diff --git a/source/blender/editors/include/UI_view2d.h b/source/blender/editors/include/UI_view2d.h index 0d5198ea338..3bae255f297 100644 --- a/source/blender/editors/include/UI_view2d.h +++ b/source/blender/editors/include/UI_view2d.h @@ -78,7 +78,6 @@ enum eView2D_Units { V2D_UNIT_VALUES, V2D_UNIT_DEGREES, V2D_UNIT_TIME, - V2D_UNIT_SECONDSSEQ }; /* clamping of grid values to whole numbers */ diff --git a/source/blender/editors/interface/view2d.c b/source/blender/editors/interface/view2d.c index aadb1b30f5b..ea350fd08df 100644 --- a/source/blender/editors/interface/view2d.c +++ b/source/blender/editors/interface/view2d.c @@ -41,6 +41,7 @@ #include "BLI_blenlib.h" #include "BLI_math.h" #include "BLI_utildefines.h" +#include "BLI_timecode.h" #include "BKE_context.h" #include "BKE_screen.h" @@ -1613,7 +1614,13 @@ static void scroll_printstr(Scene *scene, float x, float y, float val, int power } /* get string to print */ - ANIM_timecode_string_from_frame(timecode_str, scene, power, (unit == V2D_UNIT_SECONDS), val); + if (unit == V2D_UNIT_SECONDS) { + /* not neces*/ + BLI_timecode_string_from_time(timecode_str, sizeof(timecode_str), power, val, FPS, U.timecode_style); + } + else { + BLI_timecode_string_from_time_simple(timecode_str, sizeof(timecode_str), power, val); + } /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */ len = strlen(timecode_str); @@ -1737,17 +1744,6 @@ void UI_view2d_scrollers_draw(const bContext *C, View2D *v2d, View2DScrollers *v scroll_printstr(scene, fac, h, fac2, grid->powerx, V2D_UNIT_SECONDS, 'h'); break; - case V2D_UNIT_SECONDSSEQ: /* seconds with special calculations (only used for sequencer only) */ - { - float time; - - fac2 = val / (float)FPS; - time = (float)floor(fac2); - fac2 = fac2 - time; - - scroll_printstr(scene, fac, h, time + (float)FPS * fac2 / 100.0f, grid->powerx, V2D_UNIT_SECONDSSEQ, 'h'); - break; - } case V2D_UNIT_DEGREES: /* Graph Editor for rotation Drivers */ /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */ scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_DEGREES, 'v'); diff --git a/source/blender/editors/space_sequencer/sequencer_draw.c b/source/blender/editors/space_sequencer/sequencer_draw.c index 42f7e1b45b2..caade2dc59f 100644 --- a/source/blender/editors/space_sequencer/sequencer_draw.c +++ b/source/blender/editors/space_sequencer/sequencer_draw.c @@ -1483,7 +1483,7 @@ void draw_timeline_seq(const bContext *C, ARegion *ar) UI_view2d_view_restore(C); /* scrollers */ - unit = (sseq->flag & SEQ_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDSSEQ; + unit = (sseq->flag & SEQ_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDS; scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_UNIT_VALUES, V2D_GRID_CLAMP); UI_view2d_scrollers_draw(C, v2d, scrollers); UI_view2d_scrollers_free(scrollers); |