From 3964785a1458ef5ba6d28485ff5f7343ccf3f61c Mon Sep 17 00:00:00 2001 From: Richard Antalik Date: Thu, 29 Jul 2021 10:24:28 +0200 Subject: UI: Fix time labels drawing Calculate frequency of time/frame label drawing, such that labels have at least 10px margin and don't overlap. Change timecode format: - Use at least `mm:ss` format - Don't display frames if all labels would end with +00 Reviewed By: Severin Differential Revision: https://developer.blender.org/D11792 --- source/blender/blenlib/intern/timecode.c | 2 +- source/blender/editors/interface/view2d_draw.c | 30 +++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/source/blender/blenlib/intern/timecode.c b/source/blender/blenlib/intern/timecode.c index a9665c0a9c2..69c383061fc 100644 --- a/source/blender/blenlib/intern/timecode.c +++ b/source/blender/blenlib/intern/timecode.c @@ -115,7 +115,7 @@ size_t BLI_timecode_string_from_time(char *str, str, maxncpy, "%s%02d:%02d+%02d", neg, minutes, seconds, frames); } else { - rlen = BLI_snprintf_rlen(str, maxncpy, "%s%d+%02d", neg, seconds, frames); + rlen = BLI_snprintf_rlen(str, maxncpy, "%s00:%02d+%02d", neg, seconds, frames); } } else { diff --git a/source/blender/editors/interface/view2d_draw.c b/source/blender/editors/interface/view2d_draw.c index f7ef8c06389..37c84b707fd 100644 --- a/source/blender/editors/interface/view2d_draw.c +++ b/source/blender/editors/interface/view2d_draw.c @@ -326,10 +326,27 @@ static void draw_horizontal_scale_indicators(const ARegion *region, const float xmin = rect->xmin; const float xmax = rect->xmax; - for (uint i = 0; i < steps; i++) { + char text[32]; + + /* Calculate max_label_count and draw_frequency based on largest visible label. */ + to_string(to_string_data, start, 0, sizeof(text), text); + const float left_text_width = BLF_width(font_id, text, strlen(text)); + to_string(to_string_data, start + steps * distance, 0, sizeof(text), text); + const float right_text_width = BLF_width(font_id, text, strlen(text)); + const float max_text_width = max_ff(left_text_width, right_text_width); + const float max_label_count = BLI_rcti_size_x(&v2d->mask) / (max_text_width + 10.0f); + const int draw_frequency = ceil((float)steps / max_label_count); + + if (draw_frequency == 0) { + BLF_batch_draw_end(); + GPU_matrix_pop_projection(); + return; + } + + const int start_index = abs(start / distance) % draw_frequency; + for (uint i = start_index; i < steps; i += draw_frequency) { const float xpos_view = start + i * distance; const float xpos_region = UI_view2d_view_to_region_x(v2d, xpos_view); - char text[32]; to_string(to_string_data, xpos_view, distance, sizeof(text), text); const float text_width = BLF_width(font_id, text, strlen(text)); @@ -339,7 +356,6 @@ static void draw_horizontal_scale_indicators(const ARegion *region, } BLF_batch_draw_end(); - GPU_matrix_pop_projection(); } @@ -413,11 +429,15 @@ static void view_to_string__frame_number( } static void view_to_string__time( - void *user_data, float v2d_pos, float UNUSED(v2d_step), uint max_len, char *r_str) + void *user_data, float v2d_pos, float v2d_step, uint max_len, char *r_str) { const Scene *scene = (const Scene *)user_data; - const int brevity_level = 0; + int brevity_level = 0; + if (U.timecode_style == USER_TIMECODE_MINIMAL && v2d_step >= FPS) { + brevity_level = 1; + } + BLI_timecode_string_from_time( r_str, max_len, brevity_level, v2d_pos / (float)FPS, FPS, U.timecode_style); } -- cgit v1.2.3