From 7ad7ea061c054fde7608688e4c8ffd4b92815c40 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 17 Dec 2023 20:33:17 +0100 Subject: [PATCH] impr: Refactor init logic --- main/gui/include/init/splash_window.hpp | 2 +- main/gui/romfs/splash_colors.json | 71 +++++++++++++++++ main/gui/source/init/splash_window.cpp | 79 +++++++++++++++---- .../source/content/views/view_about.cpp | 68 +++++++++++++++- 4 files changed, 202 insertions(+), 18 deletions(-) create mode 100644 main/gui/romfs/splash_colors.json diff --git a/main/gui/include/init/splash_window.hpp b/main/gui/include/init/splash_window.hpp index a8d0c2a8c..59d5a5cee 100644 --- a/main/gui/include/init/splash_window.hpp +++ b/main/gui/include/init/splash_window.hpp @@ -74,7 +74,7 @@ namespace hex::init { ImGuiExt::Texture splashBackgroundTexture; ImGuiExt::Texture splashTextTexture; std::future tasksSucceeded; - std::array highlights; + std::array highlights; float progressLerp = 0.0F; }; diff --git a/main/gui/romfs/splash_colors.json b/main/gui/romfs/splash_colors.json new file mode 100644 index 000000000..0a49e5ebc --- /dev/null +++ b/main/gui/romfs/splash_colors.json @@ -0,0 +1,71 @@ +[ + { + "name": "Default Colors", + "colors": [ + "random", + "random", + "random" + ] + }, + { + "name": "Christmas", + "colors": [ + "#d3011a", + "#00843b" + ], + "time": { + "start": [ 12, 24 ], + "end": [ 12, 26 ] + } + }, + { + "name": "Pride", + "colors": [ + "#5dcefa", + "#f6a6b9", + "#fdfdfd" + ], + "time": { + "start": [ 6, 1 ], + "end": [ 6, 1 ] + } + }, + { + "name": "Pride", + "colors": [ + "#e40001", + "#fe8a00", + "#fdec00", + "#007d22", + "#004bfe", + "#740585" + ], + "time": { + "start": [ 6, 1 ], + "end": [ 6, 1 ] + } + }, + { + "name": "Pride", + "colors": [ + "#fb0073", + "#b24b98", + "#0134ab" + ], + "time": { + "start": [ 6, 1 ], + "end": [ 6, 1 ] + } + }, + { + "name": "UA", + "colors": [ + "#005bbc", + "#ffd600" + ], + "time": { + "start": [ 2, 20 ], + "end": [ 2, 20 ] + } + } +] \ No newline at end of file diff --git a/main/gui/source/init/splash_window.cpp b/main/gui/source/init/splash_window.cpp index 55c9da828..35fa45d6c 100644 --- a/main/gui/source/init/splash_window.cpp +++ b/main/gui/source/init/splash_window.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -24,7 +25,8 @@ #include #include #include - +#include +#include using namespace std::literals::chrono_literals; @@ -78,6 +80,65 @@ namespace hex::init { glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2); } + static ImColor getHighlightColor(u32 index) { + static auto highlightConfig = nlohmann::json::parse(romfs::get("splash_colors.json").string()); + static std::list selectedConfigs; + + if (selectedConfigs.empty()) { + const auto now = []{ + const auto now = std::chrono::system_clock::now(); + const auto time = std::chrono::system_clock::to_time_t(now); + + return fmt::localtime(time); + }(); + + for (const auto &colorConfig : highlightConfig) { + if (!colorConfig.contains("time")) + selectedConfigs.push_back(colorConfig); + else { + const auto &time = colorConfig["time"]; + const auto &start = time["start"]; + const auto &end = time["end"]; + + if ((now.tm_mon + 1) >= start[0] && (now.tm_mon + 1) <= end[0]) { + if (now.tm_mday >= start[1] && now.tm_mday <= end[1]) { + selectedConfigs.push_back(colorConfig); + } + } + } + } + + // Remove the default color theme if there's another one available + if (selectedConfigs.size() != 1) + selectedConfigs.erase(selectedConfigs.begin()); + } + + std::mt19937 random(std::random_device{}()); + + static const auto &selectedConfig = *std::next(selectedConfigs.begin(), random() % selectedConfigs.size()); + + const auto colorString = selectedConfig["colors"][index % selectedConfig["colors"].size()].get(); + + log::debug("Using '{}' highlight color theme", selectedConfig["name"].get()); + if (colorString == "random") { + float r, g, b; + ImGui::ColorConvertHSVtoRGB( + float(random() % 360) / 100.0F, + float(25 + random() % 70) / 100.0F, + float(85 + random() % 10) / 100.0F, + r, g, b); + + return { r, g, b, 0x50 / 255.0F }; + } else if (colorString.starts_with("#")) { + u32 color = std::strtoul(colorString.substr(1).c_str(), nullptr, 16); + + return ImColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, 0x50); + } else { + log::error("Invalid color string '{}'", colorString); + return { 0xFF, 0x00, 0xFF, 0xFF }; + } + } + void WindowSplash::createTask(const Task& task) { auto runTask = [&, task] { try { @@ -453,28 +514,18 @@ namespace hex::init { std::exit(EXIT_FAILURE); } - - std::mt19937 rng(std::random_device{}()); u32 lastPos = 0; u32 lastCount = 0; - for (auto &highlight : this->highlights) { - u32 newPos = lastPos + lastCount + (rng() % 40); + for (const auto &[index, highlight] : this->highlights | std::views::enumerate) { + u32 newPos = lastPos + lastCount + (rng() % 35); u32 newCount = (rng() % 7) + 3; highlight.start.x = float(newPos % 13); highlight.start.y = float(newPos / 13); highlight.count = newCount; - { - float r, g, b; - ImGui::ColorConvertHSVtoRGB( - (rng() % 360) / 100.0F, - (25 + rng() % 70) / 100.0F, - (85 + rng() % 10) / 100.0F, - r, g, b); - highlight.color = ImColor(r, g, b, 0x50 / 255.0F); - } + highlight.color = getHighlightColor(index); lastPos = newPos; lastCount = newCount; diff --git a/plugins/builtin/source/content/views/view_about.cpp b/plugins/builtin/source/content/views/view_about.cpp index 1170b5459..ff458e1b5 100644 --- a/plugins/builtin/source/content/views/view_about.cpp +++ b/plugins/builtin/source/content/views/view_about.cpp @@ -14,8 +14,64 @@ #include #include +#include + namespace hex::plugin::builtin { + class PopupEE : public Popup { + public: + PopupEE() : Popup("Se" /* Not going to */ "cr" /* make it that easy */ "et") { + + } + + void drawContent() override { + ImGuiIO& io = ImGui::GetIO(); + ImVec2 size = scaled({ 320, 180 }); + ImGui::InvisibleButton("canvas", size); + ImVec2 p0 = ImGui::GetItemRectMin(); + ImVec2 p1 = ImGui::GetItemRectMax(); + + ImDrawList* drawList = ImGui::GetWindowDrawList(); + drawList->PushClipRect(p0, p1); + + ImVec4 mouseData; + mouseData.x = (io.MousePos.x - p0.x) / size.x; + mouseData.y = (io.MousePos.y - p0.y) / size.y; + mouseData.z = io.MouseDownDuration[0]; + mouseData.w = io.MouseDownDuration[1]; + + fx(drawList, p0, p1, size, mouseData, float(ImGui::GetTime())); + } + + void fx(ImDrawList* drawList, ImVec2 startPos, ImVec2 endPos, ImVec2, ImVec4, float t) { + const float CircleRadius = 5_scaled; + const float Gap = 1_scaled; + + constexpr static auto func = [](u32 x, u32 y, float t) { + return std::sin(t - std::sqrt(std::pow((x - 14), 2) + std::pow((y - 8), 2))); + }; + + float x = startPos.x + CircleRadius + Gap; + u32 ix = 0; + while (x < endPos.x) { + float y = startPos.y + CircleRadius + Gap; + u32 iy = 0; + while (y < endPos.y) { + const float result = func(ix, iy, t); + const float radius = CircleRadius * std::abs(result); + const auto color = result < 0 ? ImColor(0xFF, 0, 0, 0xFF) : ImColor(0xFF, 0xFF, 0xFF, 0xFF); + drawList->AddCircleFilled(ImVec2(x, y), radius, color); + + y += CircleRadius * 2 + Gap; + iy += 1; + } + + x += CircleRadius * 2 + Gap; + ix += 1; + } + } + }; + ViewAbout::ViewAbout() : View::Modal("hex.builtin.view.help.about.name") { // Add "About" menu item to the help menu ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.help", "hex.builtin.view.help.about.name" }, 1000, Shortcut::None, [this] { @@ -36,8 +92,7 @@ namespace hex::plugin::builtin { } - void ViewAbout::drawAboutMainPage() - { + void ViewAbout::drawAboutMainPage() { // Draw main about table if (ImGui::BeginTable("about_table", 2, ImGuiTableFlags_SizingFixedFit)) { ImGui::TableNextRow(); @@ -48,9 +103,16 @@ namespace hex::plugin::builtin { this->m_logoTexture = ImGuiExt::Texture(romfs::get("assets/common/logo.png").span(), ImGuiExt::Texture::Filter::Linear); ImGui::Image(this->m_logoTexture, scaled({ 100, 100 })); - if (ImGui::IsItemHovered() && ImGui::IsItemClicked()) { + if (ImGui::IsItemClicked()) { this->m_clickCount += 1; } + + if (this->m_clickCount >= (2 * 3 + 4)) { + this->getWindowOpenState() = false; + PopupEE::open(); + this->m_clickCount = 0; + } + ImGui::TableNextColumn(); ImGuiExt::BeginSubWindow("Build Information", ImVec2(0, 0), ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY);