#include "main.h" #include "assets.h" #include "thirdparty/CLI11.hpp" 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; } void MainLoop::Init() { #ifdef PORTALS g_set_application_name("Looper"); #endif // Our state show_demo_window = false; FileBrowser fileDialog(false, ImGuiFileBrowserFlags_NoTitleBar|ImGuiFileBrowserFlags_NoMove|ImGuiFileBrowserFlags_NoResize); fileDialog.SetPwd(path(userdir) / path("Music")); fileDialog.SetWindowSize(window_width, window_height); //fileDialog.SetWindowPos(0, 0); playback = new Playback(); position = 0.0; prefs_window = false; theme_editor = false; stopped = true; about_window = false; string lang; { Json::Value config; std::ifstream stream; stream.open(path(prefPath) / "config.json"); if (stream.is_open()) { stream >> config; if (config.isMember("theme_name")) { path themePath = theme->themeDir / config["theme_name"].asString(); if (exists(themePath)) { delete theme; theme = new Theme(themePath); } } if (config.isMember("accent_color")) { if (config["accent_color"].isNumeric()) { accent_color.x = config["accent_color"].asFloat() / 360.0; } else { Json::Value accentColor = config["accent_color"]; accent_color = ImVec4(accentColor["h"].asFloat(), accentColor["s"].asFloat(), accentColor["v"].asFloat(), accentColor["a"].asFloat()); } } if (config.isMember("demo_window")) { show_demo_window = config["demo_window"].asBool(); } if (config.isMember("vsync")) { vsync = config["vsync"].asBool(); } if (config.isMember("framerate")) { framerate = config["framerate"].asUInt(); } if (config.isMember("lang")) { Json::Value langValue; if (langValue.isNull()) { lang = DEFAULT_LANG; } else { lang = config["lang"].asString(); } SET_LANG(lang.c_str()); } stream.close(); } if (is_empty(Theme::themeDir)) { path lightPath = Theme::themeDir / "light.toml"; path darkPath = Theme::themeDir / "dark.toml"; string builtinDescription = _TRS_CTX("Built-in themes | Theme default strings | name", "(built-in)"); if (!exists(lightPath)) { Theme light(false); ThemeStrings &strings = light.strings["fallback"]; strings.name = _TRS_CTX("Built-in light theme | Theme default strings | name", "Default light"); strings.description = builtinDescription; light.strings[CURRENT_LANGUAGE] = strings; light.Save(lightPath); } if (!exists(darkPath)) { Theme dark(true); ThemeStrings &strings = dark.strings["fallback"]; strings.name = _TRS_CTX("Built-in dark theme | Theme default strings | name", "Default dark"); strings.description = builtinDescription; dark.strings[CURRENT_LANGUAGE] = strings; dark.Save(darkPath); } delete theme; theme = new Theme(darkPath); } } theme->Apply(accent_color); CLI::App app{"An audio player that can play files that need special handling to loop seamlessly."}; std::string filename = ""; app.allow_extras(); double new_speed = 1.0; double new_tempo = 1.0; double new_pitch = 1.0; app.add_option("-s,--speed", new_speed, "Set the initial speed of the playback.")->default_val(1.0); app.add_option("-t,--tempo", new_tempo, "Set the initial tempo of the playback.")->default_val(1.0); app.add_option("-p,--pitch", new_pitch, "Set the initial pitch of the playback.")->default_val(1.0); try { app.parse(args); } catch (const CLI::ParseError &e) { exit(app.exit(e)); } playback->speed = new_speed; playback->tempo = new_tempo; playback->pitch = new_pitch; args = app.remaining(); if (args.size() > 0) { LoadFile(args[0]); } } void MainLoop::Drop(std::string file) { LoadFile(file); } void MainLoop::GuiFunction() { position = playback->GetPosition(); auto dockid = ImGui::DockSpaceOverViewport(nullptr, ImGuiDockNodeFlags_PassthruCentralNode|ImGuiDockNodeFlags_AutoHideTabBar); // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); if (ImGui::BeginMainMenuBar()) { if (ImGui::BeginMenu(_TRI_CTX(ICON_FK_FILE, "Main menu", "File"))) { if (ImGui::MenuItem(_TRI_CTX(ICON_FK_FOLDER_OPEN, "Main menu | File", "Open"))) { // Set translatable strings here so that they are in the correct language even when it changes at runtime. fileDialog.SetTitle(_TR_CTX("File dialog title", "Open...")); fileDialog.SetTypeFilters(_TR_CTX("File dialog filter name", "Audio files"), { ".wav", ".ogg", ".mp3", ".qoa", ".flac", ".xm", ".mod"}); fileDialog.Open(); } if (ImGui::MenuItem(_TRI_CTX(ICON_FK_WINDOW_CLOSE, "Main menu | File", "Quit"))) { done = true; } ImGui::EndMenu(); } if (ImGui::BeginMenu(_TRI_CTX(ICON_FK_SCISSORS,"Main menu", "Edit"))) { if (ImGui::MenuItem(_TRI_CTX(ICON_FK_COG, "Main menu | Edit", "Preferences..."))) { prefs_window = true; } ImGui::EndMenu(); } #ifdef DEBUG if (ImGui::BeginMenu(_TRI_CTX(ICON_FK_COG, "Main menu (in debug builds)", "Debug"))) { if (ImGui::MenuItem(_TR_CTX("Main menu | Debug", "Show ImGui Demo Window"), nullptr, show_demo_window)) { show_demo_window = !show_demo_window; } ImGui::EndMenu(); } #endif if (ImGui::BeginMenu(_TRI_CTX(ICON_FK_INFO_CIRCLE, "Main menu", "Help"))) { if (ImGui::MenuItem(_TRI_CTX(ICON_FK_INFO, "Main menu | Help", "About"), nullptr, about_window)) { about_window = !about_window; } ImGui::EndMenu(); } ImGui::EndMainMenuBar(); } ImGui::SetNextWindowDockID(dockid); ImGui::Begin(_TRI_CTX(ICON_FK_PLAY, "Main window title", "Player"), nullptr, 0); { ImGui::SetCursorPosY(ImGui::GetWindowHeight() - ImGui::GetFrameHeightWithSpacing() - ImGui::GetFrameHeight() - ImGui::GetStyle().WindowPadding.y); if (ImGui::Button(playback->IsPaused() ? ICON_FK_PLAY "##Pause" : ICON_FK_PAUSE "##Pause")) { playback->Pause(); } ImGui::SameLine(); if (ImGui::Button(ICON_FK_REFRESH "##Restart")) { playback->Seek(0.0); } ImGui::SameLine(); const int NEXT_SLIDER_COUNT = 1; ImGui::SetNextItemWidth(-(ImGui::GetFontSize() * (1 + (8 * NEXT_SLIDER_COUNT))) - ((ImGui::GetStyle().ItemSpacing.x + ImGui::GetStyle().FramePadding.x) * (NEXT_SLIDER_COUNT + 1))); 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")) { playback->Stop(); } ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); if (ImGui::SliderFloat("##Volume", &playback->volume, 0.0, 100.0, ICON_FK_VOLUME_UP ": %.0f%%")) { playback->Update(); } const float items = 3.0f; const float between_items = items - 1.0f; ImGui::PushItemWidth((ImGui::GetWindowWidth() / items) - (ImGui::GetStyle().ItemSpacing.x / (items / between_items)) - ((ImGui::GetStyle().WindowPadding.x / items) * 2.0f)); if (ImGui::SliderFloat("##Speed", &playback->speed, 0.25, 4.0, _TR_CTX("Playback controls | slider", "Speed: %.2fx"), ImGuiSliderFlags_Logarithmic)) { playback->Update(); } ImGui::SameLine(); if (ImGui::SliderFloat("##Tempo", &playback->tempo, 0.25, 4.0, _TR_CTX("Playback controls | slider", "Tempo: %.2fx"), ImGuiSliderFlags_Logarithmic)) { playback->Update(); } ImGui::SameLine(); if (ImGui::SliderFloat("##Pitch", &playback->pitch, 0.25, 4.0, _TR_CTX("Playback controls | slider", "Pitch: %.2fx"), ImGuiSliderFlags_Logarithmic)) { playback->Update(); } ImGui::PopItemWidth(); } ImGui::End(); if (prefs_window) { ImGui::SetNextWindowDockID(dockid); ImGui::Begin(_TRI_CTX(ICON_FK_COG, "Window title, window opened by menu item", "Preferences..."), &prefs_window); { if (ImGui::Checkbox(_TR_CTX("Preference | VSync checkbox", "Enable VSync"), &vsync)) { SDL_GL_SetSwapInterval(vsync ? 1 : 0); } ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - ImGui::GetCursorPosX() - ImGui::GetStyle().WindowPadding.x); ImGui::SliderInt("##Framerate", &framerate, 10, 480, _TR_CTX("Preferences | Framerate slider", "Max framerate without VSync: %d")); if (ImGui::Button(_TRI_CTX(ICON_FK_MAGIC, "Preference | Related non-preference button", "Theme Editor"), ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2.0f), 0))) { theme_editor = true; } static bool override_lang = lang != DEFAULT_LANG; if (ImGui::Checkbox(_TR_CTX("Preference | override enable checkbox", "Override language"), &override_lang)) { if (!override_lang) { lang = DEFAULT_LANG; SET_LANG(lang.c_str()); } } if (override_lang) { ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - ImGui::GetCursorPosX() - (ImGui::GetFontSize()) - ((ImGui::GetStyle().ItemSpacing.x + (ImGui::GetStyle().FramePadding.x * 2.0f))) - (ImGui::GetStyle().WindowPadding.x)); ImGui::InputText("##LanguageOverrideTextBox", &lang); ImGui::SameLine(); if (ImGui::Button(ICON_FK_CHECK)) { SET_LANG(lang.c_str()); } } static string filter = ""; ImGui::Text(_TR_CTX("Preference | Theme selector | Filter label", "Filter:")); ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - ImGui::GetCursorPosX() - ImGui::GetStyle().WindowPadding.x); ImGui::InputText("##FilterInput", &filter); ImGui::Text(_TR_CTX("Preferences | Theme selector | Selector label", "Select a theme...")); ImVec2 ChildSize = ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2.0f), -ImGui::GetFrameHeightWithSpacing()); if (ImGui::BeginChildFrame(ImGui::GetID("##ThemesContainer"), ChildSize)) { ImVec2 TableSize = ImVec2(0, 0); if (ImGui::BeginTable("##Themes", 2, ImGuiTableFlags_SizingFixedFit|ImGuiTableFlags_NoSavedSettings|ImGuiTableFlags_ScrollY, TableSize)) { // Text in TableSetupColumn calls not translated because they're not visible to the user. ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); ImGui::TableSetupColumn("Remove", 0); for (auto themePath : Theme::availableThemes) { string themeStem = themePath.stem().string(); if (themeStem.starts_with(filter)) { ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); const bool is_selected = themePath == theme->file_path; if (ImGui::Selectable((theme->themeStrings[themePath].name + string(" (") + string(themeStem) + string(")")).c_str(), is_selected, 0)) { delete theme; theme = new Theme(themePath); theme->Apply(accent_color); break; } if (is_selected) { ImGui::SetItemDefaultFocus(); } else { ImGui::TableSetColumnIndex(1); if (ImGui::SmallButton((string(ICON_FK_WINDOW_CLOSE "##") + themeStem).c_str())) { std::filesystem::remove(themePath); Theme::updateAvailableThemes(); break; } } } } ImGui::EndTable(); } } ImGui::EndChildFrame(); ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2)); ImGui::ColorEdit4("##AccentColor", &accent_color.x, ImGuiColorEditFlags_InputHSV|ImGuiColorEditFlags_DisplayHSV|ImGuiColorEditFlags_Float); theme->Apply(accent_color); } ImGui::End(); } if (about_window) { ImGui::SetNextWindowDockID(dockid); if (ImGui::Begin(_TRI_CTX(ICON_FK_INFO, "Window title, window opened by menu item", "About and Licenses"), &about_window)) { ImGui::PushFont(title); static const string APP_NAME_STR = _TR_CTX("Application name.", "Looper"); ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, 0.0f, APP_NAME_STR.c_str()).x) / 2.0f); ImGui::Text(APP_NAME_STR.c_str()); ImGui::PopFont(); static const string VER_STRING = _TR_CTX("Version string format specifier", "Version ") + string(TAG) + _TRS_CTX("Suffix to the version string in the about window, if needed", " "); ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, 0.0f, VER_STRING.c_str()).x) / 2.0f); ImGui::Text(VER_STRING.c_str()); ImGui::NewLine(); static vector projects = { LicenseData(APP_NAME_STR, "MIT"), LicenseData(_TR_CTX("Library name", "SDL Mixer X"), "Zlib"), LicenseData(_TR_CTX("Library name", "CLI11"), "BSD-3-Clause"), LicenseData(_TR_CTX("Library name", "JsonCpp"), "MIT"), LicenseData(_TR_CTX("Library name", "SoundTouch"), "LGPL-2.1-only"), LicenseData(_TR_CTX("Library name", "libintl"), "LGPL-2.1-only"), LicenseData(_TR_CTX("Library name", "Dear ImGui"), "MIT"), LicenseData(_TR_CTX("Library name", "imgui-filebrowser"), "MIT"), #ifdef PORTALS LicenseData(_TR_CTX("Library name", "libportal"), "LGPL-3.0-only"), // Only include the license if it applies. #endif LicenseData(_TR_CTX("Library name", "Noto Sans"), "OFL-1.1-RFN"), LicenseData(_TR_CTX("Library name", "Fork Awesome"), "OFL-1.1-RFN"), LicenseData(_TR_CTX("Library name", "IconFontCppHeaders"), "Zlib"), LicenseData(_TR_CTX("Library name", "TOML++"), "MIT") }; // Do this in an inner scope so that 'i' isn't accidentally used outside it, // and so that 'i' can refer to another variable such as in a for loop. { int i = 0; // Use a variable instead of hardcoding so that a #ifdef can change the indices later on. LOAD_LICENSE(projects[i], looper); i++; LOAD_LICENSE(projects[i], sdl_mixer_x); i++; LOAD_LICENSE(projects[i], cli11); i++; LOAD_LICENSE(projects[i], jsoncpp); i++; LOAD_LICENSE(projects[i], soundtouch); i++; LOAD_LICENSE(projects[i], libintl); i++; LOAD_LICENSE(projects[i], imgui); i++; LOAD_LICENSE(projects[i], imgui_filebrowser); i++; #ifdef PORTALS LOAD_LICENSE(projects[i], libportal); i++; #endif LOAD_LICENSE(projects[i], notosans); i++; LOAD_LICENSE(projects[i], forkawesome); i++; LOAD_LICENSE(projects[i], icnfntcpphdrs); i++; LOAD_LICENSE(projects[i], tomlplusplus); i++; } // Left static LicenseData selected = projects[0]; { ImGui::BeginGroup(); ImGui::TextUnformatted(_TR_CTX("Project selector label.", "Project")); // Next string is internal. ImGui::BeginChild("project selector", ImVec2(150, 0), true); for (auto project : projects) { if (ImGui::Selectable(project.Project.c_str(), selected.Project == project.Project)) selected = project; } ImGui::EndChild(); ImGui::EndGroup(); } ImGui::SameLine(); // Right { ImGui::BeginGroup(); ImGui::TextUnformatted(_TR_CTX("License viewer label", "License")); // Next string is internal. ImGui::BeginChild("license view", ImVec2(0, 0), true); // *don't* leave room for the nonexistant line below us! ImGui::Text(_TR_CTX("License viewer | information above license - string 1: selected project, string 2: SPDX license identifier", "%s: %s"), selected.Project.c_str(), selected.Spdx.c_str()); ImGui::Separator(); ImGui::TextWrapped("%s", selected.LicenseContents.c_str()); ImGui::EndChild(); ImGui::EndGroup(); } } ImGui::End(); } // Display the theme editor. if (theme_editor) { Theme::ShowEditor(&theme_editor, theme, dockid, window_width, window_height); // Immediately apply any changes made in the theme editor. theme->Apply(accent_color); } if (fileDialog.IsOpened()) { // Make the fallback file dialog fill the window. fileDialog.SetWindowSize(window_width, window_height); fileDialog.SetWindowPos(0, 0); } // Display the file dialog fileDialog.Display(); // Load a new file when it has been selected. if (fileDialog.HasSelected()) { LoadFile(fileDialog.GetSelected().string()); // Make sure to not load the file unnecessarily. fileDialog.ClearSelected(); } if (playback->IsStopped() && !stopped) { // Update the window title to reflect that no file is playing. SetWindowTitle(NAME); } } void MainLoop::LoadFile(std::string file) { playback->Start(file); // Update the window title. SetWindowTitle((file + std::string(" - ") + std::string(NAME)).c_str()); } void MainLoop::Deinit() { delete playback; { Json::Value config; std::ofstream stream; stream.open(path(prefPath) / "config.json"); path themePath(theme->file_path); themePath = themePath.filename(); if (!themePath.empty()) { config["theme_name"] = themePath.filename().string(); } { Json::Value accentColor; accentColor["h"] = accent_color.x; accentColor["s"] = accent_color.y; accentColor["v"] = accent_color.z; accentColor["a"] = accent_color.w; config["accent_color"] = accentColor; } config["demo_window"] = show_demo_window; config["vsync"] = vsync; config["framerate"] = framerate; if (lang == DEFAULT_LANG) { config["lang"] = Json::Value::nullSingleton(); } else { config["lang"] = lang; } stream << config; stream.close(); } } MainLoop::MainLoop() : RendererBackend() { } // Main code int main(int argc, char** argv) { MainLoop loop; vector args; for (int i = 1; i < argc; i++) { args.push_back(std::string(argv[i])); } loop.args = args; return loop.Run(); }