From fd32101a4121072ddabf0004dd518b7ee9d43148 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Sun, 9 Jul 2023 10:57:48 -0700 Subject: [PATCH] Make time display show a time code and silence warnings --- main.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- meson.build | 2 +- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 4c5113b..853f426 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #if defined(IMGUI_IMPL_OPENGL_ES2) @@ -26,6 +27,7 @@ static const char* NAME = "Neko Player"; #endif using namespace std::filesystem; using namespace std::numbers; +using std::string; static float accent_color = 280.0; float GetHue(ImVec4 rgba){ float r = rgba.x, g = rgba.y, b = rgba.z; @@ -83,6 +85,39 @@ void UpdateStyle(bool dark) { change_accent_color(color, accent_color); } } +string PadZeros(string input, size_t required_length) { + return std::string(required_length - std::min(required_length, input.length()), '0') + input; +} +uint8_t TimeToComponentCount(double time_code) { + int seconds = (int)time_code; + int minutes = seconds / 60; + seconds -= minutes * 60; + int hours = minutes / 60; + minutes -= hours * 60; + if (hours > 0) { + return 3; + } else if (minutes > 0) { + return 2; + } else { + return 1; + } +} +string TimeToString(double time_code, uint8_t min_components = 1) { + uint8_t components = std::max(TimeToComponentCount(time_code), min_components); + int seconds = (int)time_code; + int minutes = seconds / 60; + seconds -= minutes * 60; + int hours = minutes / 60; + minutes -= hours * 60; + string output = PadZeros(std::to_string(seconds), components < 2 ? 1 : 2); + if (components >= 2) { + output = PadZeros(std::to_string(minutes), components == 2 ? 1 : 2) + ":" + output; + } + if (components >= 3) { + output = PadZeros(std::to_string(hours), components == 3 ? 1 : 2) + ":" + output; + } + return output; +} // Main code int main(int, char**) { @@ -314,7 +349,9 @@ int main(int, char**) } ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - (ImGui::GetFontSize() * 22) - (ImGui::GetStyle().FramePadding.x * 10)); - if (ImGui::SliderFloat("##Seek", &position, 0.0f, playback->GetLength(), "%.0fs", ImGuiSliderFlags_NoRoundToFormat)) + uint8_t components = TimeToComponentCount(playback->GetLength()); + string time_str = TimeToString(position, components); + if (ImGui::SliderFloat("##Seek", &position, 0.0f, playback->GetLength(), time_str.c_str(), ImGuiSliderFlags_NoRoundToFormat)) playback->Seek(position); ImGui::SameLine(); if (ImGui::Button(ICON_FK_STOP "##Stop")) { @@ -339,7 +376,7 @@ int main(int, char**) min_size.x = ImGui::GetFontSize() * 18; min_size.y = (ImGui::GetStyle().FramePadding.y * 5) + (ImGui::GetFontSize() * 5); ImVec2 max_size; - max_size.x = 99999999999; + max_size.x = 99999997952; // The compiler says that if this were just 9s it would be turned into this anyways. max_size.y = min_size.y; ImGui::SetNextWindowSizeConstraints(min_size, max_size); ImGui::Begin("Preferences...", &prefs_window); diff --git a/meson.build b/meson.build index 2f914b5..0eedbc4 100644 --- a/meson.build +++ b/meson.build @@ -5,7 +5,7 @@ project('neko-player', ['c', 'cpp'], if get_option('debug') add_global_arguments('-DDEBUG', language: 'cpp') endif -raudio_lib = static_library('raudio', 'raudio/src/raudio.c', c_args: ['-DRAUDIO_STANDALONE', '-DSUPPORT_MODULE_RAUDIO', '-DSUPPORT_FILEFORMAT_WAV', '-DSUPPORT_FILEFORMAT_OGG', '-DSUPPORT_FILEFORMAT_MP3', '-DSUPPORT_FILEFORMAT_QOA', '-DSUPPORT_FILEFORMAT_FLAC', '-DSUPPORT_FILEFORMAT_XM', '-DSUPPORT_FILEFORMAT_MOD']) +raudio_lib = static_library('raudio', 'raudio/src/raudio.c', c_args: ['-DRAUDIO_STANDALONE', '-DSUPPORT_MODULE_RAUDIO', '-DSUPPORT_FILEFORMAT_WAV', '-DSUPPORT_FILEFORMAT_OGG', '-DSUPPORT_FILEFORMAT_MP3', '-DSUPPORT_FILEFORMAT_QOA', '-DSUPPORT_FILEFORMAT_FLAC', '-DSUPPORT_FILEFORMAT_XM', '-DSUPPORT_FILEFORMAT_MOD', '-w']) raudio_include = include_directories('raudio/src') raudio_dep = declare_dependency(include_directories: raudio_include, link_with: raudio_lib) deps = [