diff --git a/include/pd/ui7/container/container.hpp b/include/pd/ui7/container/container.hpp index a7d0a64..bc0748c 100644 --- a/include/pd/ui7/container/container.hpp +++ b/include/pd/ui7/container/container.hpp @@ -111,6 +111,25 @@ class PD_API Container { virtual void Draw() {} /** Template function to update internal data (if needed) */ virtual void Update() {} + /** Template as well as base func for Pool based Containers */ + virtual void Reset() { + pos = 0; + size = 0; + parent = nullptr; + id = 0; + pFlags = 0; + skippable = false; + rem = false; + inp_done = false; + pSelected = false; + pPressed = false; + pPressedTwice = false; + pCLipRectUsed = false; + pClipRect = 0; + io = nullptr; + list = nullptr; + last_use = 0; + } /** Internal function */ void PreDraw(); diff --git a/include/pd/ui7/container/dynobj.hpp b/include/pd/ui7/container/dynobj.hpp index f7214ad..2298e41 100755 --- a/include/pd/ui7/container/dynobj.hpp +++ b/include/pd/ui7/container/dynobj.hpp @@ -38,6 +38,7 @@ namespace UI7 { */ class PD_API DynObj : public Container { public: + DynObj() {} /** * Button Object constructor * @param label Label of the Button @@ -69,6 +70,14 @@ class PD_API DynObj : public Container { /** Function to Update Size if framepadding changes */ void Update() override; + void Reset() override { + Container::Reset(); + color = UI7Color_Button; + pressed = false; + pRenFun = nullptr; + pInp = nullptr; + } + private: UI7Color color = UI7Color_Button; ///< current button color bool pressed = false; ///< ispressed value diff --git a/include/pd/ui7/container/image.hpp b/include/pd/ui7/container/image.hpp index d58cb32..7e7e15d 100755 --- a/include/pd/ui7/container/image.hpp +++ b/include/pd/ui7/container/image.hpp @@ -32,6 +32,7 @@ namespace UI7 { */ class PD_API Image : public Container { public: + Image() {} /** * Constructor for the Image Object * @param img Image Texture Reference @@ -57,6 +58,13 @@ class PD_API Image : public Container { * */ void Draw() override; + void Reset() override { + Container::Reset(); + img = Li::Texture(); + newsize = 0.f; + cuv = Li::Rect(); + } + private: Li::Texture img; ///< Texture fvec2 newsize = 0.f; ///< New Size diff --git a/include/pd/ui7/container/label.hpp b/include/pd/ui7/container/label.hpp index b0ce750..2bcf1c6 100755 --- a/include/pd/ui7/container/label.hpp +++ b/include/pd/ui7/container/label.hpp @@ -32,6 +32,7 @@ namespace UI7 { */ class PD_API Label : public Container { public: + Label() {} /** * Constructor for Label Object * @param label Label [Text] to Draw @@ -54,6 +55,13 @@ class PD_API Label : public Container { */ void Update() override; + void Reset() override { + Container::Reset(); + tdim = 0; + color = UI7Color_Text; + label.clear(); + } + private: fvec2 tdim; ///< Text Size UI7Color color = UI7Color_Text; ///< Color diff --git a/include/pd/ui7/io.hpp b/include/pd/ui7/io.hpp index 2191cf1..55b4424 100644 --- a/include/pd/ui7/io.hpp +++ b/include/pd/ui7/io.hpp @@ -31,16 +31,14 @@ SOFTWARE. #include namespace PD { -class Context; namespace UI7 { +class Label; +class Image; +class DynObj; class PD_API IO { public: - IO() : DeltaStats(60), CurrentViewPort("", 0) { - /** Probably not the best solution i guess */ - // CurrentViewPort = - // ViewPort::New("Default", ivec4(ivec2(0, 0), pCtx.Gfx()->ViewPort)); - } - ~IO() {} + IO(); + ~IO(); /** * IO Update Internal Variables @@ -84,6 +82,11 @@ class PD_API IO { u32 NumIndices = 0; ///< Debug Indices Num std::vector MenuOrder; + // Pools + PD::Pool LabelPool; + PD::Pool ImagePool; + PD::Pool DynObjPool; + // DrawlistApi void RegisterDrawlist(const UI7::ID& id, Li::Drawlist* v) { DrawlistRegestry.push_back(std::make_pair(id, v)); diff --git a/source/ui7/container/coloredit.cpp b/source/ui7/container/coloredit.cpp index 1b37bb3..8af2d6f 100755 --- a/source/ui7/container/coloredit.cpp +++ b/source/ui7/container/coloredit.cpp @@ -53,26 +53,27 @@ PD_API void ColorEdit::Draw() { layout = new UI7::Layout(GetID(), *io); } layout->SetPosition(FinalPos()); - layout->AddObjectEx( - new DynObj([=, this](UI7::IO* io, Li::Drawlist* l, Container* thiz) { - thiz->SetSize(layout->GetSize()); - // l->Layer(30); - l->PathRect(thiz->GetPos(), thiz->GetPos() + thiz->GetSize(), - io->FrameRounding); - l->PathFill(io->Theme.Get(UI7Color_FrameBackground)); - }), - UI7LytAdd_Front | UI7LytAdd_NoCursorUpdate | UI7LytAdd_NoScrollHandle); - auto obj = - new DynObj([=, this](UI7::IO* io, Li::Drawlist* l, Container* thiz) { - l->PathRect(thiz->FinalPos(), thiz->FinalPos() + io->ItemRowHeight, - io->FrameRounding); - l->PathFill(*color_ref); - l->DrawText( - thiz->FinalPos() + fvec2(io->ItemSpace.x + io->ItemRowHeight, 0), - label.c_str(), io->Theme.Get(UI7Color_Text)); - }); - obj->SetSize(PD::fvec2(200, io->ItemRowHeight)); - layout->AddObject(obj); + DynObj* r = io->DynObjPool.Allocate(); + *r = UI7::DynObj([=, this](UI7::IO* io, Li::Drawlist* l, Container* thiz) { + thiz->SetSize(layout->GetSize()); + // l->Layer(30); + l->PathRect(thiz->GetPos(), thiz->GetPos() + thiz->GetSize(), + io->FrameRounding); + l->PathFill(io->Theme.Get(UI7Color_FrameBackground)); + }); + layout->AddObjectEx(r, UI7LytAdd_Front | UI7LytAdd_NoCursorUpdate | + UI7LytAdd_NoScrollHandle); + r = io->DynObjPool.Allocate(); + *r = UI7::DynObj([=, this](UI7::IO* io, Li::Drawlist* l, Container* thiz) { + l->PathRect(thiz->FinalPos(), thiz->FinalPos() + io->ItemRowHeight, + io->FrameRounding); + l->PathFill(*color_ref); + l->DrawText( + thiz->FinalPos() + fvec2(io->ItemSpace.x + io->ItemRowHeight, 0), + label.c_str(), io->Theme.Get(UI7Color_Text)); + }); + r->SetSize(PD::fvec2(200, io->ItemRowHeight)); + layout->AddObject(r); layout->Label("RGBA: ({}, {}, {}, {})", *((u8*)color_ref), *(((u8*)color_ref) + 1), *(((u8*)color_ref) + 2), *(((u8*)color_ref) + 3)); diff --git a/source/ui7/io.cpp b/source/ui7/io.cpp index 979c246..6256fdc 100644 --- a/source/ui7/io.cpp +++ b/source/ui7/io.cpp @@ -23,9 +23,22 @@ SOFTWARE. #include #include +#include #include namespace PD { + +PD_API UI7::IO::IO() : DeltaStats(60), CurrentViewPort("", 0) { + /** Probably not the best solution i guess */ + // CurrentViewPort = + // ViewPort::New("Default", ivec4(ivec2(0, 0), pCtx.Gfx()->ViewPort)); + // Start a little larger on these + LabelPool.Init(512); + DynObjPool.Init(512); +} + +PD_API UI7::IO::~IO() {} + PD_API void UI7::IO::Update() { /** Todo: find out if we even still use the Drawlist regestry */ u64 current = PD::Os::GetTimeNano(); @@ -41,5 +54,8 @@ PD_API void UI7::IO::Update() { // RegisterDrawList("CtxBackList", Back); NumIndices = 0; // FDL.pNumIndices; NumVertices = 0; // FDL.pNumVertices; + LabelPool.ResetFast(); + DynObjPool.ResetFast(); + ImagePool.ResetFast(); } } // namespace PD \ No newline at end of file diff --git a/source/ui7/layout.cpp b/source/ui7/layout.cpp index 22db057..d94049f 100644 --- a/source/ui7/layout.cpp +++ b/source/ui7/layout.cpp @@ -161,7 +161,8 @@ PD_API void Layout::Update() { PD_API void Layout::Label(const std::string& label) { // Layout API - auto r = new UI7::Label(label, IO); + auto r = IO.LabelPool.Allocate(); + *r = UI7::Label(label, IO); r->SetClipRect(fvec4(GetPosition(), GetPosition() + GetSize())); AddObject(r); } @@ -192,7 +193,8 @@ PD_API void Layout::Checkbox(const std::string& label, bool& v) { } PD_API void Layout::Image(Li::Texture img, fvec2 size, Li::Rect uv) { - Container* r = new UI7::Image(img, size, uv); + auto r = IO.ImagePool.Allocate(); + *r = UI7::Image(img, size, uv); AddObject(r); } diff --git a/source/ui7/menu.cpp b/source/ui7/menu.cpp index 0b77a40..f6b273b 100644 --- a/source/ui7/menu.cpp +++ b/source/ui7/menu.cpp @@ -36,7 +36,8 @@ Menu::Menu(const ID& id, IO& io) : pIO(io), pID(id), pLayout(id, io) { PD_API void Menu::Label(const std::string& label) { // Layout API - auto r = new UI7::Label(label, pIO); + auto r = pIO.LabelPool.Allocate(); + *r = UI7::Label(label, pIO); pLayout.AddObject(r); } @@ -68,7 +69,8 @@ PD_API void Menu::Checkbox(const std::string& label, bool& v) { } PD_API void Menu::Image(Li::Texture img, fvec2 size, Li::Rect uv) { - Container* r = new UI7::Image(img, size, uv); + auto r = pIO.ImagePool.Allocate(); + *r = UI7::Image(img, size, uv); pLayout.AddObject(r); } @@ -84,8 +86,9 @@ PD_API void Menu::ColorEdit(const std::string& label, u32& clr) { PD_API void Menu::Separator() { // Dynamic Objects are very simple... - Container* r = - new DynObj([=, this](UI7::IO* io, Li::Drawlist* l, UI7::Container* self) { + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj( + [=, this](UI7::IO* io, Li::Drawlist* l, UI7::Container* self) { l->DrawRectFilled(self->FinalPos(), self->GetSize(), pIO.Theme.Get(UI7Color_TextDead)); }); @@ -98,8 +101,9 @@ PD_API void Menu::Separator() { PD_API void Menu::SeparatorText(const std::string& label) { // Also note to use [=] instead of [&] to not undefined access label - Container* r = new DynObj([=, this](UI7::IO* io, Li::Drawlist* l, - UI7::Container* self) { + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj([=, this](UI7::IO* io, Li::Drawlist* l, + UI7::Container* self) { fvec2 size = self->GetSize(); fvec2 tdim = io->Font->GetTextBounds(label.c_str(), io->FontScale); fvec2 pos = self->FinalPos(); @@ -251,14 +255,14 @@ PD_API void Menu::DrawBaseLayout() { if (pIsOpen) { /** Resize Sym (Render on Top of Everything) */ if (!(Flags & UI7MenuFlags_NoResize)) { - Container* r = - new DynObj([](IO* io, Li::Drawlist* l, UI7::Container* self) { - // //l->Layer(1); - l->PathAdd(self->FinalPos() + self->GetSize() - fvec2(0, 20)); - l->PathAdd(self->FinalPos() + self->GetSize()); - l->PathAdd(self->FinalPos() + self->GetSize() - fvec2(20, 0)); - l->PathFill(io->Theme.Get(UI7Color_Button)); - }); + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj([](IO* io, Li::Drawlist* l, UI7::Container* self) { + // //l->Layer(1); + l->PathAdd(self->FinalPos() + self->GetSize() - fvec2(0, 20)); + l->PathAdd(self->FinalPos() + self->GetSize()); + l->PathAdd(self->FinalPos() + self->GetSize() - fvec2(20, 0)); + l->PathFill(io->Theme.Get(UI7Color_Button)); + }); r->SetSize( fvec2(pLayout.GetSize().x, pLayout.GetSize().y - TitleBarHeight)); r->SetPos(fvec2(0, TitleBarHeight)); @@ -267,8 +271,8 @@ PD_API void Menu::DrawBaseLayout() { } /** Background */ - Container* r = new DynObj([](IO* io, Li::Drawlist* l, - UI7::Container* self) { + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj([](IO* io, Li::Drawlist* l, UI7::Container* self) { // l->Layer(0); l->PathRectEx(self->FinalPos(), self->FinalPos() + self->GetSize(), 10.f, LiPathRectFlags_KeepTop | LiPathRectFlags_KeepBot); @@ -284,7 +288,8 @@ PD_API void Menu::DrawBaseLayout() { UI7LytAdd_Front); } if (!(Flags & UI7MenuFlags_NoTitlebar)) { - Container* r = new DynObj( + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj( [=, this](UI7::IO* io, Li::Drawlist* l, UI7::Container* self) { // l->Layer(20); /** Header Bar */ @@ -307,8 +312,9 @@ PD_API void Menu::DrawBaseLayout() { /** Collapse Sym */ if (!(Flags & UI7MenuFlags_NoCollapse)) { - r = new DynObj([=, this](UI7::IO* io, Li::Drawlist* l, - UI7::Container* self) { + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj([=, this](UI7::IO* io, Li::Drawlist* l, + UI7::Container* self) { /** This sym actually requires layer 21 (i dont know why) */ // l->Layer(21); /** @@ -383,7 +389,8 @@ PD_API bool Menu::BeginTreeNode(const ID& id) { } // Object - auto r = new DynObj([=, this](IO* io, Li::Drawlist* l, Container* self) { + DynObj* r = pIO.DynObjPool.Allocate(); + *r = UI7::DynObj([=, this](IO* io, Li::Drawlist* l, Container* self) { fvec2 ts = self->FinalPos() + fvec2(0, 7); fvec2 pl[2] = {fvec2(10, 5), fvec2(0, 10)}; if (n->second) {