From 21b45f5855916e9b4697335cb8d87b43ac94519d Mon Sep 17 00:00:00 2001 From: tobid7 Date: Wed, 28 Jan 2026 07:11:56 +0100 Subject: [PATCH] WIP: Add CLipRects to UI7 Layouts - Added Scissor Support to Font Rendering --- CMakeLists.txt | 1 + include/pd/lithium/font.hpp | 3 ++- include/pd/ui7/container/container.hpp | 5 +++++ include/pd/ui7/layout.hpp | 1 + source/lithium/drawlist.cpp | 4 ++-- source/lithium/font.cpp | 7 ++++--- source/ui7/container/container.cpp | 13 +++++++++++++ source/ui7/container/label.cpp | 6 ------ source/ui7/layout.cpp | 5 +++++ source/ui7/menu.cpp | 3 +-- 10 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d06cc1c..c57bc9d 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ file(GLOB_RECURSE PD_FMTFILES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/backends/3ds/include/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/backends/desktop/include/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/backends/3ds/include/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test/source*.cpp ) add_custom_target(pd-clang-format diff --git a/include/pd/lithium/font.hpp b/include/pd/lithium/font.hpp index 7726d7d..28dbafa 100644 --- a/include/pd/lithium/font.hpp +++ b/include/pd/lithium/font.hpp @@ -44,6 +44,7 @@ enum LiTextFlags_ { namespace PD { class Context; namespace Li { +class DrawList; class PD_API Font { public: /** Codepoint Data holder */ @@ -93,7 +94,7 @@ class PD_API Font { /** * Extended Draw Text Function that vreates a Command List */ - void CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color, float scale, + void CmdTextEx(DrawList& dl, const fvec2& pos, u32 color, float scale, const std::string& text, LiTextFlags flags = 0, const fvec2& box = 0); diff --git a/include/pd/ui7/container/container.hpp b/include/pd/ui7/container/container.hpp index 421e21e..b7351c9 100644 --- a/include/pd/ui7/container/container.hpp +++ b/include/pd/ui7/container/container.hpp @@ -113,6 +113,11 @@ class PD_API Container { /** Template function to update internal data (if needed) */ virtual void Update() {} + /** Internal function */ + void PreDraw(); + /** Internal function */ + void PostDraw(); + /** Internal Input Handler */ void HandleInternalInput(); diff --git a/include/pd/ui7/layout.hpp b/include/pd/ui7/layout.hpp index 8ada854..2ee6575 100644 --- a/include/pd/ui7/layout.hpp +++ b/include/pd/ui7/layout.hpp @@ -178,6 +178,7 @@ class PD_API Layout { UI7::ID ID; UI7::IO::Ref IO; Li::DrawList::Ref DrawList; + UI7LayoutFlags Flags; // Positioning fvec2 Pos; diff --git a/source/lithium/drawlist.cpp b/source/lithium/drawlist.cpp index 32aeed7..4c8347d 100644 --- a/source/lithium/drawlist.cpp +++ b/source/lithium/drawlist.cpp @@ -288,7 +288,7 @@ PD_API void DrawList::DrawText(const fvec2& pos, const std::string& text, if (!pCurrentFont) { return; } - pCurrentFont->CmdTextEx(pPool, pos, color, pFontScale, text); + pCurrentFont->CmdTextEx(*this, pos, color, pFontScale, text); } PD_API void DrawList::DrawTextEx(const fvec2& p, const std::string& text, @@ -297,7 +297,7 @@ PD_API void DrawList::DrawTextEx(const fvec2& p, const std::string& text, if (!pCurrentFont) { return; } - pCurrentFont->CmdTextEx(pPool, p, color, pFontScale, text, flags, box); + pCurrentFont->CmdTextEx(*this, p, color, pFontScale, text, flags, box); } PD_API void DrawList::DrawLine(const fvec2& a, const fvec2& b, u32 color, diff --git a/source/lithium/font.cpp b/source/lithium/font.cpp index 38d1dda..6bf16e7 100644 --- a/source/lithium/font.cpp +++ b/source/lithium/font.cpp @@ -28,6 +28,7 @@ SOFTWARE. #define PD_TRUETYPE_IMPLEMENTATION #endif #include +#include #include #ifdef PD_LI_INCLUDE_FONTS @@ -246,7 +247,7 @@ PD_API fvec2 Font::GetTextBounds(const std::string& text, float scale) { return res; } -PD_API void Font::CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color, +PD_API void Font::CmdTextEx(DrawList& dl, const fvec2& pos, u32 color, float scale, const std::string& text, LiTextFlags flags, const fvec2& box) { fvec2 off; @@ -291,7 +292,7 @@ PD_API void Font::CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color, it = pShortText(it, scale, box - pos, tmp_dim); } auto wline = Strings::MakeWstring(it); - auto cmd = cmds.NewCmd(); + auto cmd = dl.GetNewCmd(); auto Tex = GetCodepoint(wline[0]).Tex; if (Tex) { cmd->Tex = Tex->Address; @@ -303,7 +304,7 @@ PD_API void Font::CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color, continue; } if (Tex != cp.Tex) { - cmd = cmds.NewCmd(); + cmd = dl.GetNewCmd(); Tex = cp.Tex; if (Tex) { cmd->Tex = Tex->Address; diff --git a/source/ui7/container/container.cpp b/source/ui7/container/container.cpp index e12ab36..bc66a31 100755 --- a/source/ui7/container/container.cpp +++ b/source/ui7/container/container.cpp @@ -40,5 +40,18 @@ PD_API void Container::HandleScrolling(fvec2 scrolling, fvec4 viewport) { PD_API void Container::HandleInternalInput() { /** Requires Handle Scrolling First */ } + +/** Internal function */ +PD_API void Container::PreDraw() { + if (pCLipRectUsed) { + list->PushClipRect(pClipRect); + } +} +/** Internal function */ +PD_API void Container::PostDraw() { + if (pCLipRectUsed) { + list->PopClipRect(); + } +} } // namespace UI7 } // namespace PD \ No newline at end of file diff --git a/source/ui7/container/label.cpp b/source/ui7/container/label.cpp index 9cf9e75..b91cf07 100755 --- a/source/ui7/container/label.cpp +++ b/source/ui7/container/label.cpp @@ -28,14 +28,8 @@ namespace UI7 { PD_API void Label::Draw() { // Assert(io.get() && list.get(), "Did you run Container::Init correctly?"); // io->Ren->OnScreen(screen); - if (pCLipRectUsed) { - list->PushClipRect(pClipRect); - } list->DrawTextEx(FinalPos(), label, io->Theme->Get(UI7Color_Text), LiTextFlags_NoOOS, PD::fvec2(0, io->CurrentViewPort.w)); - if (pCLipRectUsed) { - list->PopClipRect(); - } } PD_API void Label::Update() { diff --git a/source/ui7/layout.cpp b/source/ui7/layout.cpp index 6391fc1..874252c 100644 --- a/source/ui7/layout.cpp +++ b/source/ui7/layout.cpp @@ -125,7 +125,12 @@ PD_API void Layout::Update() { it->SetPos(it->GetPos() + Pos); it->HandleInput(); it->UnlockInput(); + if (Flags & UI7LayoutFlags_UseClipRect) { + it->SetClipRect(fvec4(Pos, Size)); + } + it->PreDraw(); it->Draw(); + it->PostDraw(); } } diff --git a/source/ui7/menu.cpp b/source/ui7/menu.cpp index 75e6195..0c6ba15 100755 --- a/source/ui7/menu.cpp +++ b/source/ui7/menu.cpp @@ -30,14 +30,13 @@ Menu::Menu(const ID& id, IO::Ref io) : pIO(io), pID(id) { pLayout = Layout::New(id, io); TitleBarHeight = pIO->FontScale * pIO->Font->PixelHeight + pIO->MenuPadding.y; pLayout->WorkRect.y += TitleBarHeight; + pLayout->Flags |= UI7LayoutFlags_UseClipRect; pLayout->CursorInit(); } PD_API void Menu::Label(const std::string& label) { // Layout API auto r = Label::New(label, pIO); - r->SetClipRect(fvec4(pLayout->GetPosition(), - pLayout->GetPosition() + pLayout->GetSize())); pLayout->AddObject(r); }