From 2863a1f3c92c17a7c34773060b754571ca769430 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Tue, 4 Aug 2020 19:05:03 +0200 Subject: [PATCH] improved text display of time with 3 modes depending on the needs. --- GstToolkit.cpp | 28 +++++++++++++++++----------- GstToolkit.h | 16 +++++++++++----- ImGuiToolkit.cpp | 13 ++++++++----- UserInterfaceManager.cpp | 2 +- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/GstToolkit.cpp b/GstToolkit.cpp index 4d77836..d35656c 100644 --- a/GstToolkit.cpp +++ b/GstToolkit.cpp @@ -4,19 +4,26 @@ using namespace std; #include "GstToolkit.h" -string GstToolkit::time_to_string(guint64 t, bool stripped) +string GstToolkit::time_to_string(guint64 t, time_string_mode m) { - if (t == GST_CLOCK_TIME_NONE) - return "00:00:00.00"; + if (t == GST_CLOCK_TIME_NONE) { + switch (m) { + case TIME_STRING_FIXED: + return "00:00:00.00"; + case TIME_STRING_MINIMAL: + return "0.0"; + default: + return "00.00"; + } + } guint ms = GST_TIME_AS_MSECONDS(t); guint s = ms / 1000; - ostringstream oss; - if (stripped) { + // MINIMAL: keep only the 2 higher values (most significant) + if (m == TIME_STRING_MINIMAL) { int count = 0; - if (s / 3600) { oss << s / 3600 << ':'; count++; @@ -31,19 +38,18 @@ string GstToolkit::time_to_string(guint64 t, bool stripped) } if (count < 2 ) oss << '.'<< setw(1) << setfill('0') << (ms % 1000) / 10; - } else { - if (s / 3600) + // TIME_STRING_FIXED : fixed length string (11 chars) HH:mm:ss.ii" + // TIME_STRING_RIGHT : always show the right part (seconds), not the min or hours if none + if (m == TIME_STRING_FIXED || (s / 3600) ) oss << setw(2) << setfill('0') << s / 3600 << ':'; - if ((s % 3600) / 60) + if (m == TIME_STRING_FIXED || ((s % 3600) / 60) ) oss << setw(2) << setfill('0') << (s % 3600) / 60 << ':'; oss << setw(2) << setfill('0') << (s % 3600) % 60 << '.'; oss << setw(2) << setfill('0') << (ms % 1000) / 10; } - // non-stripped : fixed length string (11 chars) HH:mm:ss.ii" - // stripped : adapted to precision return oss.str(); } diff --git a/GstToolkit.h b/GstToolkit.h index b72c3c5..d60cc6e 100644 --- a/GstToolkit.h +++ b/GstToolkit.h @@ -9,13 +9,19 @@ namespace GstToolkit { - std::string time_to_string(guint64 t, bool stripped = false); +typedef enum { + TIME_STRING_FIXED = 0, + TIME_STRING_ADJUSTED, + TIME_STRING_MINIMAL +} time_string_mode; - std::string gst_version(); - std::list all_plugins(); - std::list all_plugin_features(std::string pluginname); +std::string time_to_string(guint64 t, time_string_mode m = TIME_STRING_ADJUSTED); - bool enable_feature (std::string name, bool enable); +std::string gst_version(); +std::list all_plugins(); +std::list all_plugin_features(std::string pluginname); + +bool enable_feature (std::string name, bool enable); } diff --git a/ImGuiToolkit.cpp b/ImGuiToolkit.cpp index ee4a182..8470de4 100644 --- a/ImGuiToolkit.cpp +++ b/ImGuiToolkit.cpp @@ -400,22 +400,25 @@ bool ImGuiToolkit::TimelineSlider(const char* label, guint64 *time, guint64 dura ImVec2 maxi = ImVec2(0.f, 0.f); // render text duration - ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%s", GstToolkit::time_to_string(duration).c_str()); + ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%s", + GstToolkit::time_to_string(duration, GstToolkit::TIME_STRING_MINIMAL).c_str()); overlay_size = ImGui::CalcTextSize(overlay_buf, NULL); ImVec2 duration_label = bbox.GetBR() - overlay_size - ImVec2(3.f, 3.f); if (overlay_size.x > 0.0f) ImGui::RenderTextClipped( duration_label, bbox.Max, overlay_buf, NULL, &overlay_size); - + // render tick marks while ( tick < duration) { - // large tick mark every large tick + // large tick mark float tick_length = !(tick%large_tick_step) ? fontsize - style.FramePadding.y : style.FramePadding.y; + // label tick mark if ( !(tick%label_tick_step) ) { tick_length = fontsize; - ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%s", GstToolkit::time_to_string(tick, true).c_str()); + ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%s", + GstToolkit::time_to_string(tick, GstToolkit::TIME_STRING_MINIMAL).c_str()); overlay_size = ImGui::CalcTextSize(overlay_buf, NULL); mini = ImVec2( pos.x - overlay_size.x / 2.f, pos.y + tick_length ); maxi = ImVec2( pos.x + overlay_size.x / 2.f, pos.y + tick_length + overlay_size.y ); @@ -424,7 +427,7 @@ bool ImGuiToolkit::TimelineSlider(const char* label, guint64 *time, guint64 dura ImGui::RenderTextClipped(mini, maxi, overlay_buf, NULL, &overlay_size); } - // draw a tick mark each step + // draw the tick mark each step window->DrawList->AddLine( pos, pos + ImVec2(0.f, tick_length), color); // next tick diff --git a/UserInterfaceManager.cpp b/UserInterfaceManager.cpp index 21a00aa..6161ea8 100644 --- a/UserInterfaceManager.cpp +++ b/UserInterfaceManager.cpp @@ -1168,7 +1168,7 @@ void MediaController::Render() // display time ImGuiToolkit::PushFont(ImGuiToolkit::FONT_LARGE); ImGui::SetCursorPos( ImVec2(return_to_pos.x + 5, return_to_pos.y - ImGui::GetTextLineHeightWithSpacing()) ); - ImGui::Text("%s", GstToolkit::time_to_string(mp_->position()).c_str()); + ImGui::Text("%s", GstToolkit::time_to_string(mp_->position(), GstToolkit::TIME_STRING_FIXED).c_str()); ImGui::PopFont(); ImGui::SetCursorPos(return_to_pos);