From 6c38aa6f21aa67096d47c9fc881824a1412a8742 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Mon, 15 Dec 2025 22:16:19 +0100 Subject: [PATCH] Adding and fixing stuff - Fix HexChar2Int - Remove some devisions in tween engine - Add PathAdd function for x, y seperated instead of fvec2 - Readd text wrapping (rd7-palladium maybe) --- include/pd/core/strings.hpp | 4 ++-- include/pd/core/tween.hpp | 22 +++++++++++----------- include/pd/lithium/drawlist.hpp | 10 +++++++++- include/pd/lithium/font.hpp | 2 ++ pd/lithium/source/drawlist.cpp | 2 +- pd/lithium/source/font.cpp | 33 +++++++++++++++++++++++++++++++-- 6 files changed, 56 insertions(+), 17 deletions(-) diff --git a/include/pd/core/strings.hpp b/include/pd/core/strings.hpp index c5cf895..552b421 100755 --- a/include/pd/core/strings.hpp +++ b/include/pd/core/strings.hpp @@ -31,10 +31,10 @@ namespace PD { */ namespace Strings { constexpr int HexChar2Int(char c) { - /** Imagine mat hat ne lookup table dafür verwendet :/ */ + /** Imagine man hat ne lookup table dafür verwendet :/ */ if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); - if (c >= 'A' && c <= 'F') return 10 + (c - 'a'); + if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); return -1; // Error } /** diff --git a/include/pd/core/tween.hpp b/include/pd/core/tween.hpp index b838eac..f9421a7 100755 --- a/include/pd/core/tween.hpp +++ b/include/pd/core/tween.hpp @@ -60,7 +60,7 @@ class Tween { * @param delta deltatime */ void Update(float delta) { - time += delta / 1000.f; + time += delta * 0.001f; if (time > tend) { finished = true; time = tend; @@ -162,10 +162,10 @@ class Tween { return -(end - start) * t * (t - 2) + start; break; case EaseInOutQuad: - t = time / (tend / 2); - if (t < 1) return (end - start) / 2 * t * t + start; + t = time / (tend * 0.5f); + if (t < 1) return (end - start) * 0.5f * t * t + start; t--; - return -(end - start) / 2 * (t * (t - 2) - 1) + start; + return -(end - start) * 0.5f * (t * (t - 2) - 1) + start; break; case EaseInCubic: t = time / tend; @@ -177,20 +177,20 @@ class Tween { return (end - start) * (t * t * t + 1) + start; break; // case EaseInOutCubic: - // t = time / (tend / 2); - // if (t < 1) return (end - start) / 2 * t * t * t + start; + // t = time / (tend *0.5f); + // if (t < 1) return (end - start) *0.5f * t * t * t + start; // t--; - // return (end - start) / 2 * (t * t * t * 2) + start; + // return (end - start) *0.5f * (t * t * t * 2) + start; // break; case EaseInSine: - return -(end - start) * cos(time / tend * (M_PI / 2)) + (end - start) + - start; + return -(end - start) * cos(time / tend * (M_PI * 0.5f)) + + (end - start) + start; break; case EaseOutSine: - return (end - start) * sin(time / tend * (M_PI / 2)) + start; + return (end - start) * sin(time / tend * (M_PI * 0.5f)) + start; break; case EaseInOutSine: - return -(end - start) / 2 * (cos(M_PI * time / tend) - 1) + start; + return -(end - start) * 0.5f * (cos(M_PI * time / tend) - 1) + start; break; default: // Linear diff --git a/include/pd/lithium/drawlist.hpp b/include/pd/lithium/drawlist.hpp index 625baa0..e733165 100755 --- a/include/pd/lithium/drawlist.hpp +++ b/include/pd/lithium/drawlist.hpp @@ -97,7 +97,7 @@ class PD_LITHIUM_API DrawList { * Extended Draw Text Function */ void DrawTextEx(const fvec2& p, const std::string& text, u32 color, - LiTextFlags flags, fvec2 box = fvec2(0.f)); + LiTextFlags flags, const fvec2& box = fvec2(0.f)); void DrawLine(const fvec2& a, const fvec2& b, u32 color, int t = 1); /** * Take list of points and display it as a line on screen @@ -138,6 +138,14 @@ class PD_LITHIUM_API DrawList { * @param v Position to add */ void PathAdd(const fvec2& v) { pPath.Add(v); } + /** + * Add a Point to the Path + * @note Keep in mind that this function is used for + * setting the starting point + * @param x X Position to add + * @param y Y Position to add + */ + void PathAdd(float x, float y) { pPath.Add(fvec2(x, y)); } /** * Path Stroke Create Line from point to point * @note For Primitives like Rect or Triangle mak sure to use diff --git a/include/pd/lithium/font.hpp b/include/pd/lithium/font.hpp index 072d1f4..efdbee8 100755 --- a/include/pd/lithium/font.hpp +++ b/include/pd/lithium/font.hpp @@ -101,6 +101,8 @@ class PD_LITHIUM_API Font { */ void pMakeAtlas(bool final, std::vector& font_tex, int texszs, PD::Li::Texture::Ref tex); + std::string pWrapText(const std::string& txt, float scale, + const PD::fvec2& max, PD::fvec2& dim); /** Data Section */ int PixelHeight; diff --git a/pd/lithium/source/drawlist.cpp b/pd/lithium/source/drawlist.cpp index 67d47f7..15feef4 100755 --- a/pd/lithium/source/drawlist.cpp +++ b/pd/lithium/source/drawlist.cpp @@ -281,7 +281,7 @@ PD_LITHIUM_API void DrawList::DrawText(const fvec2 &pos, PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2 &p, const std::string &text, u32 color, - LiTextFlags flags, fvec2 box) { + LiTextFlags flags, const fvec2 &box) { if (!pCurrentFont) { return; } diff --git a/pd/lithium/source/font.cpp b/pd/lithium/source/font.cpp index 2cfb8f5..fcba9e2 100755 --- a/pd/lithium/source/font.cpp +++ b/pd/lithium/source/font.cpp @@ -250,6 +250,10 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector &cmds, fvec2 td; fvec2 rpos = pos; fvec2 rbox = box; + std::string txt = text; + if (flags & LiTextFlags_Wrap) { + txt = pWrapText(txt, scale, box, td); + } if (flags & (LiTextFlags_AlignMid | LiTextFlags_AlignRight)) { td = GetTextBounds(text, scale); } @@ -261,7 +265,7 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector &cmds, } std::vector lines; - std::istringstream iss(text); + std::istringstream iss(txt); std::string tmp; while (std::getline(iss, tmp)) { lines.push_back(tmp); @@ -315,5 +319,30 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector &cmds, } } +PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale, + const PD::fvec2 &max, + PD::fvec2 &dim) { + std::string ret; + std::string line; + int lx = 0; + std::stringstream s(txt); + std::string tmp; + // Simply go over every word + while (s >> tmp) { + auto d = GetTextBounds(tmp, scale); + if (lx + d.x <= max.x) { + line += tmp + ' '; + lx += d.x; + } else { + ret += line + '\n'; + line = tmp + ' '; + lx = GetTextBounds(line, scale).x; + } + } + ret += line; + dim = GetTextBounds(ret, scale); + return ret; +} + } // namespace Li -} // namespace PD +} // namespace PD \ No newline at end of file