1
0
mirror of synced 2024-11-30 18:34:29 +01:00

impr: Refactor init logic

This commit is contained in:
WerWolv 2023-12-17 20:33:17 +01:00
parent a315ecb831
commit 7ad7ea061c
4 changed files with 202 additions and 18 deletions

View File

@ -74,7 +74,7 @@ namespace hex::init {
ImGuiExt::Texture splashBackgroundTexture;
ImGuiExt::Texture splashTextTexture;
std::future<bool> tasksSucceeded;
std::array<Highlight, 3> highlights;
std::array<Highlight, 4> highlights;
float progressLerp = 0.0F;
};

View File

@ -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 ]
}
}
]

View File

@ -7,6 +7,7 @@
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <fmt/chrono.h>
#include <romfs/romfs.hpp>
@ -24,7 +25,8 @@
#include <future>
#include <numeric>
#include <random>
#include <ranges>
#include <nlohmann/json.hpp>
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<nlohmann::json> 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<std::string>();
log::debug("Using '{}' highlight color theme", selectedConfig["name"].get<std::string>());
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;

View File

@ -14,8 +14,64 @@
#include <romfs/romfs.hpp>
#include <wolv/utils/string.hpp>
#include <complex>
namespace hex::plugin::builtin {
class PopupEE : public Popup<PopupEE> {
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);