From 913b8f882031893f926b6aba89ec4456cb39a2c0 Mon Sep 17 00:00:00 2001 From: jussi Date: Wed, 14 May 2025 11:20:00 +0300 Subject: DrawTextBoxed Color change escape sequence. --- API.md | 2 ++ ReiLua_API.lua | 2 ++ changelog | 1 + examples/textBoxed/main.lua | 10 +++++++-- src/text.c | 51 +++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index 5dc55cc..989289a 100644 --- a/API.md +++ b/API.md @@ -8026,6 +8026,7 @@ Draw multiple character (codepoint) > mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, int|nil tabSize ) Draw text using font inside rectangle limits. +Support for tint change with "\a#FFFFFFFF" - Success return int, Vector2 @@ -8036,6 +8037,7 @@ Draw text using font inside rectangle limits. Draw text using font inside rectangle limits. Return character id from mouse position (default 0). textOffset can be used to set start position inside rectangle. Usefull to pass from previous DrawTextBoxedEx for continuous text. +Support for tint change with "\a#FFFFFFFF" - Success return int, Vector2 diff --git a/ReiLua_API.lua b/ReiLua_API.lua index 8086204..406c038 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -4529,6 +4529,7 @@ function RL.DrawTextCodepoint( font, codepoint, position, fontSize, tint ) end function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, tint ) end ---Draw text using font inside rectangle limits. +---Support for tint change with "\a#FFFFFFFF" ---- Success return int, Vector2 ---@param font any ---@param text string @@ -4546,6 +4547,7 @@ function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, l ---Draw text using font inside rectangle limits. Return character id from mouse position (default 0). ---textOffset can be used to set start position inside rectangle. Usefull to pass from previous ---DrawTextBoxedEx for continuous text. +---Support for tint change with "\a#FFFFFFFF" ---- Success return int, Vector2 ---@param font any ---@param text string diff --git a/changelog b/changelog index ad01bdc..bc5d720 100644 --- a/changelog +++ b/changelog @@ -48,6 +48,7 @@ DETAILED CHANGES: - ADDED: SetImageData. - FIXED: DrawTextBoxed Could draw text outside rect when using special characters. Also accepts optional tabSize argument. + - ADDED: DrawTextBoxed Color change escape sequence. ------------------------------------------------------------------------ Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0 diff --git a/examples/textBoxed/main.lua b/examples/textBoxed/main.lua index df731c3..c6b497f 100644 --- a/examples/textBoxed/main.lua +++ b/examples/textBoxed/main.lua @@ -33,7 +33,7 @@ function RL.draw() RL.ClearBackground( RL.RAYWHITE ) RL.DrawRectangleLines( rect, RL.GREEN ) - mouseCharId, textOffset = RL.DrawTextBoxedEx( + _, textOffset = RL.DrawTextBoxedEx( RL.GetFontDefault(), "\tYou can change the size of the box by pressing right mouse and toggle the wordwrap by pressing space. First we will write some text before the hyperlink to show that it is indeed is as powerful feature as adverticed.", rect, @@ -45,12 +45,18 @@ function RL.draw() rect, textSize, spacing, wordwrap, linkColor, limitHeight, textOffset ) - RL.DrawTextBoxedEx( + _, textOffset = RL.DrawTextBoxedEx( RL.GetFontDefault(), " Then we demonstrate this further by writin more text after the link. Isn't this just amazing! Don't forget to press left mouse to print text to your console when hovering mouse over the hyperlink.", rect, textSize, spacing, wordwrap, RL.RED, limitHeight, textOffset ) + RL.DrawTextBoxedEx( + RL.GetFontDefault(), + " \a#D28484FF Also supports \a#0000FFFF changing text \a#CCD834FF color in the \a#00FFFFFF middle of \a#B623A3FF the \a#00FF00FF text draw.", + rect, + textSize, spacing, wordwrap, RL.WHITE, limitHeight, textOffset + ) if 0 < mouseCharId then linkColor = RL.GREEN diff --git a/src/text.c b/src/text.c index 4456d5e..22af36d 100644 --- a/src/text.c +++ b/src/text.c @@ -24,6 +24,38 @@ static float measureWord( Font font, char* text, float fontSize, float spacing ) return size.x; } +static int getDigit( const char input ) { + switch ( input ) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'a': + case 'A': return 10; + case 'b': + case 'B': return 11; + case 'c': + case 'C': return 12; + case 'd': + case 'D': return 13; + case 'e': + case 'E': return 14; + case 'f': + case 'F': return 15; + } + return 0; +} + +static unsigned char getHexValue( const char* ptr, size_t offset ) { + return getDigit( ptr[ offset ] ) * 16 + getDigit( ptr[ offset + 1 ] ); +} + static int DrawTextBoxed( Font font, char* text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize ) { int lineSpacing = state->lineSpacing; @@ -53,8 +85,20 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, measure = true; } else if ( codepoint == '\t' ) { + textOffset->x += (float)font.recs[ GetGlyphIndex( font, ' ' ) ].width * tabSize * scaleFactor + spacing; measure = true; } + else if ( codepoint == '\a' ) { + i++; /* Jump over escape '\a'. */ + if ( text[i] == '#' ) { + i++; + tint.r = getHexValue( &text[i], 0 ); + tint.g = getHexValue( &text[i], 2 ); + tint.b = getHexValue( &text[i], 4 ); + tint.a = getHexValue( &text[i], 6 ); + i += 8; + } + } else if ( wordWrap && ( wordWidth < rec.width ) ) { if ( measure && codepoint != ' ' ) { wordWidth = measureWord( font, &text[i], fontSize, spacing ); @@ -80,7 +124,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, break; } - if ( codepoint != '\n' && codepoint != '\t' && !( textOffset->x == 0 && codepoint == ' ' ) && codepointWidth < rec.width ) { + if ( codepoint != '\a' && codepoint != '\n' && codepoint != '\t' && !( textOffset->x == 0 && codepoint == ' ' ) && codepointWidth < rec.width ) { DrawTextCodepoint( font, codepoint, (Vector2){ round( rec.x + textOffset->x ), round( rec.y + textOffset->y ) }, fontSize, tint ); if ( CheckCollisionPointRec( mousePos, (Rectangle){ rec.x + textOffset->x - 1, rec.y + textOffset->y, codepointWidth, (float)font.baseSize * scaleFactor } ) ) { @@ -88,9 +132,6 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, } textOffset->x += codepointWidth; } - else if ( codepoint == '\t' ) { - textOffset->x += (float)font.recs[ GetGlyphIndex( font, ' ' ) ].width * tabSize * scaleFactor + spacing; - } i += codepointByteCount; } return mouseChar; @@ -538,6 +579,7 @@ int ltextDrawTextCodepoints( lua_State* L ) { > mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, int|nil tabSize ) Draw text using font inside rectangle limits. +Support for tint change with "\a#FFFFFFFF" - Success return int, Vector2 */ @@ -568,6 +610,7 @@ int ltextDrawTextBoxed( lua_State* L ) { Draw text using font inside rectangle limits. Return character id from mouse position (default 0). textOffset can be used to set start position inside rectangle. Usefull to pass from previous DrawTextBoxedEx for continuous text. +Support for tint change with "\a#FFFFFFFF" - Success return int, Vector2 */ -- cgit v1.2.3