From f7d262b7b09841a85fc3601014860626bbe110bc Mon Sep 17 00:00:00 2001 From: tobid7 Date: Mon, 3 Feb 2025 16:15:37 +0100 Subject: [PATCH] # Stage 1.8.1 - Add Removable to containers (Only used for ID Objs) - Add Counts for specific Object types to have for example multiple buttons with same name - Readd Background as accidently deleted it --- include/pd/ui7/container/container.hpp | 3 +++ include/pd/ui7/menu.hpp | 2 ++ source/ui7/container/container.cpp | 5 +++++ source/ui7/menu.cpp | 18 +++++++++++++++--- test/app/main.cpp | 24 ++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/include/pd/ui7/container/container.hpp b/include/pd/ui7/container/container.hpp index b8ad57a..7a3c329 100644 --- a/include/pd/ui7/container/container.hpp +++ b/include/pd/ui7/container/container.hpp @@ -28,6 +28,7 @@ class Container : public SmartCtor { vec2 GetSize() { return size; } bool Skippable() const { return skippable; } + bool Removable() const { return rem; } void HandleScrolling(vec2 scrolling, vec4 viewport); virtual void HandleInput(Hid::Ref inp) {} @@ -41,6 +42,8 @@ class Container : public SmartCtor { protected: /// used to skip Input/Render preocessing ot not bool skippable = false; + bool rem = false; + u64 last_use = 0; bool inp_done = false; Screen::Ref screen; vec2 pos; diff --git a/include/pd/ui7/menu.hpp b/include/pd/ui7/menu.hpp index 84d3b98..d866360 100644 --- a/include/pd/ui7/menu.hpp +++ b/include/pd/ui7/menu.hpp @@ -140,6 +140,8 @@ class Menu : public SmartCtor { std::vector objects; std::vector idobjs; std::vector join; + int count_btn = 0; + int count_cbx = 0; // DrawLists DrawList::Ref back; diff --git a/source/ui7/container/container.cpp b/source/ui7/container/container.cpp index 14bffad..a379358 100644 --- a/source/ui7/container/container.cpp +++ b/source/ui7/container/container.cpp @@ -1,8 +1,13 @@ +#include #include namespace PD { namespace UI7 { void Container::HandleScrolling(vec2 scrolling, vec4 viewport) { + if (last_use != 0 && Sys::GetTime() - last_use > 5000) { + rem = true; + } + last_use = Sys::GetTime(); pos -= vec2(0, scrolling.y()); if (!LI::Renderer::InBox(pos, size, viewport)) { skippable = true; diff --git a/source/ui7/menu.cpp b/source/ui7/menu.cpp index 992fe67..a4cd23d 100644 --- a/source/ui7/menu.cpp +++ b/source/ui7/menu.cpp @@ -14,7 +14,7 @@ void UI7::Menu::Label(const std::string& label) { bool UI7::Menu::Button(const std::string& label) { bool ret = false; - u32 id = Strings::FastHash("btn" + label); + u32 id = Strings::FastHash("btn" + label + std::to_string(count_btn++)); Container::Ref r = FindIDObj(id); if (!r) { r = ObjectPush(PD::New(label, Cursor(), this->back->ren)); @@ -33,7 +33,7 @@ bool UI7::Menu::Button(const std::string& label) { } void UI7::Menu::Checkbox(const std::string& label, bool& v) { - u32 id = Strings::FastHash("cbx" + label); + u32 id = Strings::FastHash("cbx" + label + std::to_string(count_cbx++)); Container::Ref r = FindIDObj(id); if (!r) { r = ObjectPush(PD::New(label, Cursor(), v, this->back->ren)); @@ -81,7 +81,9 @@ void UI7::Menu::DebugLabels() { void UI7::Menu::Update(float delta) { TT::Scope st("MUPT_" + name); - for (auto& it : objects) { + std::vector tbr; + for (int i = 0; i < (int)objects.size(); i++) { + auto& it = objects[i]; if (it->GetID() != 0 && !FindIDObj(it->GetID())) { idobjs.push_back(it); } @@ -92,6 +94,14 @@ void UI7::Menu::Update(float delta) { it->Draw(); } } + for (int i = 0; i < (int)idobjs.size(); i++) { + if (idobjs[i]->Removable()) { + tbr.push_back(i); + } + } + for (auto it : tbr) { + idobjs.erase(idobjs.begin() + it); + } this->back->Process(); this->main->Process(); this->front->Process(); @@ -116,6 +126,8 @@ void UI7::Menu::PreHandler(UI7MenuFlags flags) { this->back->BaseLayer(30); this->main->BaseLayer(40); this->front->BaseLayer(50); + count_btn = 0; + count_cbx = 0; Cursor(vec2(5, 5)); this->flags = flags; this->scrolling[0] = flags & UI7MenuFlags_HzScrolling; diff --git a/test/app/main.cpp b/test/app/main.cpp index 8aedd28..7cf1aea 100644 --- a/test/app/main.cpp +++ b/test/app/main.cpp @@ -102,6 +102,30 @@ class Test : public PD::App { void Deinit() override {} + void DrawFancyBG(float time) { + ren->DrawRect(vec2(0, 0), vec2(400, 240), 0xff64c9fd); + for (int i = 0; i < 44; i++) Append(i, vec2(0, 0), vec2(400, 240), time); + } + + float Offset(float x) { + float y = cos(x) * 42; + return y - floor(y); + } + void Append(int index, vec2 position, vec2 size, float time) { + float offset = Offset(index) * 62; + float x_position = position.x() + size.x() / 8 * ((index % 11) - 1) + + cos(offset + time) * 10; + float y_position = position.y() + size.y() / 8 * (index / 11) + 40 + + sin(offset + time) * 10 + 30; + float color_effect = 1 - exp(-(index / 11) / 3.0f); + + ren->DrawTriangle( + vec2(x_position, y_position), vec2(x_position + 300, y_position + (90)), + vec2(x_position - 300, y_position + (90)), + PD::Color(.94f - .17f * color_effect, .61f - .25f * color_effect, + .36f + .38f * color_effect)); + } + private: /// Shorter Acess to Renderer / Input PD::LI::Renderer::Ref ren;