From f399f032e7785f9f8a30054c8bec742c9d156fc4 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Wed, 5 Jun 2024 22:34:00 +0200 Subject: [PATCH] Chanmges: Add CopyOtherTheme Add Mor Stuff to ThemeEditor --- include/renderd7/Color.hpp | 5 +- include/renderd7/ThemeEditor.hpp | 5 ++ source/Color.cpp | 11 +++++ source/ThemeEditor.cpp | 83 +++++++++++++++++++++++++++++--- source/UI7.cpp | 11 +++-- 5 files changed, 103 insertions(+), 12 deletions(-) diff --git a/include/renderd7/Color.hpp b/include/renderd7/Color.hpp index 794ef1c..272231d 100644 --- a/include/renderd7/Color.hpp +++ b/include/renderd7/Color.hpp @@ -28,7 +28,7 @@ #define UNPACK_BGRA(col) (uint8_t)(col >> 8), (col >> 16), (col >> 24), (col) inline uint32_t RGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) { -#define ISIMPLEPAK(x, y) (((x) & 0xff) << y) +#define ISIMPLEPAK(x, y) (((x)&0xff) << y) return (ISIMPLEPAK(r, 0) | ISIMPLEPAK(g, 8) | ISIMPLEPAK(b, 16) | ISIMPLEPAK(a, 24)); } @@ -101,6 +101,9 @@ class Theme { using Ref = std::shared_ptr; static Ref New() { return std::make_shared(); } + // Loader method + void CopyOther(Theme::Ref theme); + private: struct change { change(RD7Color a, unsigned int f, unsigned int t) diff --git a/include/renderd7/ThemeEditor.hpp b/include/renderd7/ThemeEditor.hpp index a956470..2b06eea 100644 --- a/include/renderd7/ThemeEditor.hpp +++ b/include/renderd7/ThemeEditor.hpp @@ -38,5 +38,10 @@ class ThemeEditor : public RenderD7::Scene { mutable bool cm; mutable std::string inpt; mutable int menu = 0; + + // Keyboard + mutable RD7KeyboardState kbd_state; + mutable std::string kbd_text; + mutable std::vector theme_list; }; } // namespace RenderD7 \ No newline at end of file diff --git a/source/Color.cpp b/source/Color.cpp index 4492eb6..da8a191 100644 --- a/source/Color.cpp +++ b/source/Color.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -124,6 +125,14 @@ void RenderD7::Theme::Default() { } } +void RenderD7::Theme::CopyOther(Theme::Ref theme) { + this->clr_tab.clear(); + this->clr_tab.resize(RD7Color_Len); + for (int i = 0; i < (int)RD7Color_Len; i++) { + this->clr_tab[i] = theme->Get(i); + } +} + unsigned int RenderD7::Theme::Get(RD7Color clr) { if (clr < 0 || clr >= RD7Color_Len) return 0; return this->clr_tab[clr]; @@ -172,11 +181,13 @@ void RenderD7::Theme::UndoAll() { void RenderD7::Theme::Save(const std::string& path) { if (std::filesystem::path(path).filename().string() == "renderd7.theme") { if (!rd7i_amdt) { + RenderD7::PushMessage("Theme", "Default Theme cannot\nbe overwritten!"); return; } } std::ofstream file(path); if (!file.is_open()) { + RenderD7::PushMessage("Theme", "Unable to\ncreate file!"); return; } nlohmann::json js; diff --git a/source/ThemeEditor.cpp b/source/ThemeEditor.cpp index 286e403..29bcb72 100644 --- a/source/ThemeEditor.cpp +++ b/source/ThemeEditor.cpp @@ -16,7 +16,9 @@ * along with this program. If not, see . */ +#include #include +#include #include #include @@ -36,8 +38,8 @@ std::map color_names = { {RD7Color_Progressbar, "Progressbar"}, {RD7Color_Selector, "Selector"}, {RD7Color_SelectorFade, "SelectorFade"}, - {RD7Color_Text2, "Text2"}, - {RD7Color_Text, "Text"}, + {RD7Color_Text2, "Text Light"}, + {RD7Color_Text, "Text Dark"}, {RD7Color_TextDisabled, "Text Disabled"}, }; @@ -45,7 +47,7 @@ RenderD7::ThemeEditor::ThemeEditor() { // Backup active Theme and create New one to edit temp_theme = RenderD7::ThemeActive(); edit_theme = RenderD7::Theme::New(); - edit_theme->Default(); + edit_theme->CopyOther(temp_theme); RenderD7::ThemeSet(edit_theme); } @@ -56,7 +58,7 @@ RenderD7::ThemeEditor::~ThemeEditor() { void RenderD7::ThemeEditor::Draw() const { RenderD7::OnScreen(Top); - if (UI7::BeginMenu("Theme Editor")) { + if (UI7::BeginMenu("RenderD7 -> Theme Editor")) { UI7::Label("Sample Text"); UI7::Checkbox("Checkbox", cm); UI7::InputText("Input Text", inpt, "Input Text"); @@ -69,17 +71,86 @@ void RenderD7::ThemeEditor::Draw() const { RenderD7::OnScreen(Bottom); if (UI7::BeginMenu("Theme", R7Vec2(), UI7MenuFlags_Scrolling)) { if (menu == 0) { + if (UI7::Button("Create New")) { + menu = 1; + edit_theme->Default(); + } else if (UI7::Button("Edit Current")) { + menu = 1; + } else if (UI7::Button("Select Theme")) { + menu = 2; + theme_list.clear(); + for (const auto& it : std::filesystem::directory_iterator( + RenderD7::GetAppDirectory() + "/themes")) { + theme_list.push_back(it.path().filename().string()); + } + } + } else if (menu == 1) { + if (UI7::Button("Go back")) { + edit_theme->CopyOther(temp_theme); + menu = 0; + } else if (UI7::Button("Save")) { + RenderD7::AddOvl(std::make_unique(kbd_text, kbd_state, + ".theme")); + } for (auto& it : color_names) { UI7::ColorSelector(it.second, edit_theme->GetTableRef()[it.first]); } - } else if (menu == 1) { + } else if (menu == 2) { + if (UI7::Button("Go back")) { + menu = 0; + } + for (auto& it : theme_list) { + if (UI7::Button(it)) { + edit_theme->Load(RenderD7::GetAppDirectory() + "/themes/" + it); + menu = 1; + } + UI7::SameLine(); + if (UI7::Button("Make Current")) { + edit_theme->Load(RenderD7::GetAppDirectory() + "/themes/" + it); + temp_theme->CopyOther(edit_theme); + menu = 0; + } + UI7::SameLine(); + if (UI7::Button("Delete")) { + if (std::string(it) != "renderd7.theme") { + std::filesystem::remove(RenderD7::GetAppDirectory() + "/themes/" + + it); + theme_list.clear(); + for (const auto& it : std::filesystem::directory_iterator( + RenderD7::GetAppDirectory() + "/themes")) { + theme_list.push_back(it.path().filename().string()); + } + } else { + RenderD7::PushMessage("ThemeEditor", + "Cannot Delete\nrenderd7.theme!"); + } + } + } } UI7::EndMenu(); } } void RenderD7::ThemeEditor::Logic() { + if (kbd_state) { + if (kbd_state == RD7KeyboardState_Confirm) { + auto path = + RenderD7::GetAppDirectory() + "/themes/" + kbd_text + ".theme"; + kbd_text = ""; + if (std::filesystem::exists(path)) { + // Prompt Override + return; + } + edit_theme->Save(path); + } + kbd_state = RD7KeyboardState_None; + } if (Hid::IsEvent("cancel", Hid::Down)) { - RenderD7::Scene::Back(); + if (menu == 0) { + RenderD7::Scene::Back(); + } else { + if (menu == 1) edit_theme->CopyOther(temp_theme); + menu = 0; + } } } \ No newline at end of file diff --git a/source/UI7.cpp b/source/UI7.cpp index 635f292..fc2204c 100644 --- a/source/UI7.cpp +++ b/source/UI7.cpp @@ -553,6 +553,8 @@ void BrowserList(const std::vector &entrys, int &selection, if (size.x == 0) size.x = (rd7i_current_screen ? 400 : 320) - (pos.x * 2); if (size.y == 0) size.y = (max_entrys * 15); UI7CtxCursorMove(size); + int selindex = (selection < max_entrys ? selection : (max_entrys - 1)); + for (int i = 0; i < max_entrys; i++) { ui7_ctx->cm->main.push_back( DrawCmd::New(R7Vec4(pos + R7Vec2(0, 15 * i), R7Vec2(size.x, 15)), @@ -562,7 +564,6 @@ void BrowserList(const std::vector &entrys, int &selection, i < ((entrys.size() < (size_t)max_entrys) ? entrys.size() : (size_t)max_entrys); i++) { - int selindex = (selection < max_entrys ? selection : (max_entrys - 1)); int list_index = (selection < max_entrys ? i : (i + selection - (max_entrys - 1))); if (i == (size_t)selindex) { @@ -933,7 +934,7 @@ void ColorSelector(const std::string &label, unsigned int &color) { 0xff0000ff)); auto ncmd = DrawCmd::New(npos + R7Vec2(2, cbs.y * 3 + 4), - std::to_string(clr.m_r), RD7Color_Text); + "R: " + std::to_string(clr.m_r), RD7Color_Text); ncmd->text_flags |= RD7TextFlags_AlignMid; ncmd->text_box = R7Vec2(50, 0); ui7_ctx->cm->front.push_back(ncmd); @@ -948,7 +949,7 @@ void ColorSelector(const std::string &label, unsigned int &color) { R7Vec2(50 * ((float)clr.m_g / 255.f), cbs.y)), 0xff00ff00)); auto ncmd = DrawCmd::New(npos + R7Vec2(54, cbs.y * 3 + 4), - std::to_string(clr.m_g), RD7Color_Text); + "G: " + std::to_string(clr.m_g), RD7Color_Text); ncmd->text_flags |= RD7TextFlags_AlignMid; ncmd->text_box = R7Vec2(50, 0); ui7_ctx->cm->front.push_back(ncmd); @@ -964,7 +965,7 @@ void ColorSelector(const std::string &label, unsigned int &color) { 0xffff0000)); auto ncmd = DrawCmd::New(npos + R7Vec2(2, cbs.y * 4 + 4), - std::to_string(clr.m_b), RD7Color_Text); + "B: " + std::to_string(clr.m_b), RD7Color_Text); ncmd->text_flags |= RD7TextFlags_AlignMid; ncmd->text_box = R7Vec2(50, 0); ui7_ctx->cm->front.push_back(ncmd); @@ -980,7 +981,7 @@ void ColorSelector(const std::string &label, unsigned int &color) { 0xffffffff)); auto ncmd = DrawCmd::New(npos + R7Vec2(54, cbs.y * 4 + 4), - std::to_string(clr.m_a), RD7Color_Text); + "A: " + std::to_string(clr.m_a), RD7Color_Text); ncmd->text_flags |= RD7TextFlags_AlignMid; ncmd->text_box = R7Vec2(50, 0); ui7_ctx->cm->front.push_back(ncmd);