looper/backends/ui/imgui/main.cpp
2024-04-09 10:15:05 -07:00

464 lines
No EOL
21 KiB
C++

#include "main.h"
#include <data.h>
#include <util.hpp>
#include <assets/assets.h>
#include <filesystem>
#include <options.hpp>
#include "ui_backend.hpp"
#include "thirdparty/CLI11.hpp"
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);
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);
}
void MainLoop::Drop(std::string file) {
LoadFile(file);
}
void MainLoop::GuiFunction() {
position = playback->GetPosition();
length = playback->GetLength();
// Set the window title if the file changed, or playback stopped.
if (playback->handle_signals(PlaybackSignalFileChanged|PlaybackSignalStopped)) {
auto file_maybe = playback->get_current_title();
if (file_maybe.has_value()) {
auto name = file_maybe.value();
SetWindowTitle((name + std::string(" - Looper")).c_str());
} else {
SetWindowTitle("Looper");
}
}
bool lengthKnown = length > 0.0;
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_MODE
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;
double seek_bar_width = -(ImGui::GetFontSize() * (1 + (8 * NEXT_SLIDER_COUNT))) - ((ImGui::GetStyle().ItemSpacing.x + ImGui::GetStyle().FramePadding.x) * (NEXT_SLIDER_COUNT + 1));
if (lengthKnown) {
ImGui::SetNextItemWidth(seek_bar_width);
uint8_t components = TimeToComponentCount(playback->GetLength());
string time_str = TimeToString(position, components) + "/" + TimeToString(length, components);
if (ImGui::SliderFloat("##Seek", &position, 0.0f, playback->GetLength(), time_str.c_str(), ImGuiSliderFlags_NoRoundToFormat))
playback->Seek(position);
} else {
ImVec2 size(seek_bar_width, ImGui::GetFrameHeight());
ImGui::InvisibleButton("##SeekIndeterminate", size);
}
ImGui::SameLine();
if (ImGui::Button(ICON_FK_STOP "##Stop")) {
playback->Stop();
}
float pitch = playback->GetPitch(), tempo = playback->GetTempo(), speed = playback->GetSpeed(), volume = playback->GetVolume();
ImGui::SameLine();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (ImGui::SliderFloat("##Volume", &volume, 0.0, 100.0, ICON_FK_VOLUME_UP ": %.0f%%")) {
playback->SetVolume(volume);
}
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", &speed, 0.25, 4.0, _TR_CTX("Playback controls | slider", "Speed: %.2fx"), ImGuiSliderFlags_Logarithmic)) {
playback->SetSpeed(speed);
}
ImGui::SameLine();
if (ImGui::SliderFloat("##Tempo", &tempo, 0.25, 4.0, _TR_CTX("Playback controls | slider", "Tempo: %.2fx"), ImGuiSliderFlags_Logarithmic)) {
playback->SetTempo(tempo);
}
ImGui::SameLine();
if (ImGui::SliderFloat("##Pitch", &pitch, 0.25, 4.0, _TR_CTX("Playback controls | slider", "Pitch: %.2fx"), ImGuiSliderFlags_Logarithmic)) {
playback->SetPitch(pitch);
}
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);
{
static std::string set_backend_name = cur_backend->get_name();
static std::string set_backend_id = cur_backend->get_id();
if (restart_needed) {
ImGui::TextUnformatted(_TR("A restart is needed to apply some changes."));
}
if (ImGui::BeginCombo(_TR("UI frontend"), set_backend_name.c_str())) {
for (auto &backend : backends) {
bool is_current = set_backend_id == backend->get_id();
if (ImGui::Selectable(backend->get_name().c_str(), &is_current)) {
set_backend_id = backend->get_id();
set_backend_name = backend->get_name();
Looper::Options::set_option("ui.frontend", set_backend_id);
restart_needed = true;
}
if (is_current) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
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();
auto &license_data = get_license_data();
// Left
static LicenseData selected = *license_data.begin();
{
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 : license_data)
{
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()) {
playback->Start(fileDialog.GetSelected().string());
// Make sure to not load the file unnecessarily.
fileDialog.ClearSelected();
}
if (exit_flag.load()) {
done = true;
}
}
void MainLoop::LoadFile(std::string file) {
playback->Start(file);
}
void MainLoop::Deinit() {
{
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() {
for (auto &kv : UIBackend::backends) {
backends.push_back(kv.second);
}
cur_backend = UIBackend::get_backend(UI_BACKEND()).value_or(UIBackend::get_first_backend());
}
std::string ImGuiUIBackend::get_id() {
return "imgui";
}
std::string ImGuiUIBackend::get_name() {
return "Dear ImGui frontend";
}
void ImGuiUIBackend::QuitHandler() {
if (main_loop == nullptr) {
return;
}
((MainLoop*)main_loop)->exit_flag.store(true);
}
// Main code
int ImGuiUIBackend::run(std::vector<std::string> realArgs, int argc, char** argv)
{
int possible_error = UIBackend::run(realArgs, argc, argv);
if (possible_error != 0) {
return possible_error;
}
MainLoop loop;
loop.playback = playback;
loop.args = args;
main_loop = &loop;
return loop.Run();
}
void ImGuiUIBackend::add_licenses() {
auto &license_data = get_license_data();
auto glib = LicenseData("Glib", "lgpl-2.1-or-later");
auto imgui = LicenseData("Dear ImGui", "MIT");
auto libintl = LicenseData("libintl", "LGPL-2.1-only");
auto imfb = LicenseData("imgui-filebrowser", "MIT");
auto noto = LicenseData("Noto Sans", "OFL-1.1-RFN");
auto noto_jp = LicenseData("Noto Sans JP", "OFL-1.1-RFN");
auto fork_awesome = LicenseData("Fork Awesome", "OFL-1.1-RFN");
auto ifch = LicenseData("IconFontCppHeaders", "Zlib");
LOAD_LICENSE(glib, lgpl_2_1);
LOAD_LICENSE(imgui, imgui);
LOAD_LICENSE(libintl, lgpl_2_1);
LOAD_LICENSE(imfb, imgui_filebrowser);
LOAD_LICENSE(noto, notosans);
LOAD_LICENSE(noto_jp, notosansjp);
LOAD_LICENSE(fork_awesome, forkawesome);
LOAD_LICENSE(ifch, icnfntcpphdrs);
license_data.insert(glib);
license_data.insert(imgui);
license_data.insert(libintl);
license_data.insert(imfb);
license_data.insert(noto);
license_data.insert(noto_jp);
license_data.insert(fork_awesome);
license_data.insert(ifch);
#ifdef PORTALS
auto libportal = LicenseData("libportal", "LGPL-3.0-only");
LOAD_LICENSE(libportal, lgpl_3_0);
license_data.insert(libportal);
#endif
}