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)
This commit is contained in:
2025-12-15 22:16:19 +01:00
parent f19c947fc3
commit 6c38aa6f21
6 changed files with 56 additions and 17 deletions

View File

@@ -31,10 +31,10 @@ namespace PD {
*/ */
namespace Strings { namespace Strings {
constexpr int HexChar2Int(char c) { 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 >= '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'); if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
return -1; // Error return -1; // Error
} }
/** /**

View File

@@ -60,7 +60,7 @@ class Tween {
* @param delta deltatime * @param delta deltatime
*/ */
void Update(float delta) { void Update(float delta) {
time += delta / 1000.f; time += delta * 0.001f;
if (time > tend) { if (time > tend) {
finished = true; finished = true;
time = tend; time = tend;
@@ -162,10 +162,10 @@ class Tween {
return -(end - start) * t * (t - 2) + start; return -(end - start) * t * (t - 2) + start;
break; break;
case EaseInOutQuad: case EaseInOutQuad:
t = time / (tend / 2); t = time / (tend * 0.5f);
if (t < 1) return (end - start) / 2 * t * t + start; if (t < 1) return (end - start) * 0.5f * t * t + start;
t--; t--;
return -(end - start) / 2 * (t * (t - 2) - 1) + start; return -(end - start) * 0.5f * (t * (t - 2) - 1) + start;
break; break;
case EaseInCubic: case EaseInCubic:
t = time / tend; t = time / tend;
@@ -177,20 +177,20 @@ class Tween {
return (end - start) * (t * t * t + 1) + start; return (end - start) * (t * t * t + 1) + start;
break; break;
// case EaseInOutCubic: // case EaseInOutCubic:
// t = time / (tend / 2); // t = time / (tend *0.5f);
// if (t < 1) return (end - start) / 2 * t * t * t + start; // if (t < 1) return (end - start) *0.5f * t * t * t + start;
// t--; // t--;
// return (end - start) / 2 * (t * t * t * 2) + start; // return (end - start) *0.5f * (t * t * t * 2) + start;
// break; // break;
case EaseInSine: case EaseInSine:
return -(end - start) * cos(time / tend * (M_PI / 2)) + (end - start) + return -(end - start) * cos(time / tend * (M_PI * 0.5f)) +
start; (end - start) + start;
break; break;
case EaseOutSine: case EaseOutSine:
return (end - start) * sin(time / tend * (M_PI / 2)) + start; return (end - start) * sin(time / tend * (M_PI * 0.5f)) + start;
break; break;
case EaseInOutSine: 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; break;
default: // Linear default: // Linear

View File

@@ -97,7 +97,7 @@ class PD_LITHIUM_API DrawList {
* Extended Draw Text Function * Extended Draw Text Function
*/ */
void DrawTextEx(const fvec2& p, const std::string& text, u32 color, 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); 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 * 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 * @param v Position to add
*/ */
void PathAdd(const fvec2& v) { pPath.Add(v); } 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 * Path Stroke Create Line from point to point
* @note For Primitives like Rect or Triangle mak sure to use * @note For Primitives like Rect or Triangle mak sure to use

View File

@@ -101,6 +101,8 @@ class PD_LITHIUM_API Font {
*/ */
void pMakeAtlas(bool final, std::vector<u8>& font_tex, int texszs, void pMakeAtlas(bool final, std::vector<u8>& font_tex, int texszs,
PD::Li::Texture::Ref tex); PD::Li::Texture::Ref tex);
std::string pWrapText(const std::string& txt, float scale,
const PD::fvec2& max, PD::fvec2& dim);
/** Data Section */ /** Data Section */
int PixelHeight; int PixelHeight;

View File

@@ -281,7 +281,7 @@ PD_LITHIUM_API void DrawList::DrawText(const fvec2 &pos,
PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2 &p, PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2 &p,
const std::string &text, u32 color, const std::string &text, u32 color,
LiTextFlags flags, fvec2 box) { LiTextFlags flags, const fvec2 &box) {
if (!pCurrentFont) { if (!pCurrentFont) {
return; return;
} }

View File

@@ -250,6 +250,10 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
fvec2 td; fvec2 td;
fvec2 rpos = pos; fvec2 rpos = pos;
fvec2 rbox = box; fvec2 rbox = box;
std::string txt = text;
if (flags & LiTextFlags_Wrap) {
txt = pWrapText(txt, scale, box, td);
}
if (flags & (LiTextFlags_AlignMid | LiTextFlags_AlignRight)) { if (flags & (LiTextFlags_AlignMid | LiTextFlags_AlignRight)) {
td = GetTextBounds(text, scale); td = GetTextBounds(text, scale);
} }
@@ -261,7 +265,7 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
} }
std::vector<std::string> lines; std::vector<std::string> lines;
std::istringstream iss(text); std::istringstream iss(txt);
std::string tmp; std::string tmp;
while (std::getline(iss, tmp)) { while (std::getline(iss, tmp)) {
lines.push_back(tmp); lines.push_back(tmp);
@@ -315,5 +319,30 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &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 Li
} // namespace PD } // namespace PD