DrawTextBoxed Color change escape sequence.

This commit is contained in:
jussi
2025-05-14 11:20:00 +03:00
parent 067e4091ba
commit 913b8f8820
5 changed files with 60 additions and 6 deletions

2
API.md
View File

@@ -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 ) > 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. Draw text using font inside rectangle limits.
Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2 - 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). 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 textOffset can be used to set start position inside rectangle. Usefull to pass from previous
DrawTextBoxedEx for continuous text. DrawTextBoxedEx for continuous text.
Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2 - Success return int, Vector2

View File

@@ -4529,6 +4529,7 @@ function RL.DrawTextCodepoint( font, codepoint, position, fontSize, tint ) end
function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, tint ) end function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, tint ) end
---Draw text using font inside rectangle limits. ---Draw text using font inside rectangle limits.
---Support for tint change with "\a#FFFFFFFF"
---- Success return int, Vector2 ---- Success return int, Vector2
---@param font any ---@param font any
---@param text string ---@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). ---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 ---textOffset can be used to set start position inside rectangle. Usefull to pass from previous
---DrawTextBoxedEx for continuous text. ---DrawTextBoxedEx for continuous text.
---Support for tint change with "\a#FFFFFFFF"
---- Success return int, Vector2 ---- Success return int, Vector2
---@param font any ---@param font any
---@param text string ---@param text string

View File

@@ -48,6 +48,7 @@ DETAILED CHANGES:
- ADDED: SetImageData. - ADDED: SetImageData.
- FIXED: DrawTextBoxed Could draw text outside rect when using special characters. - FIXED: DrawTextBoxed Could draw text outside rect when using special characters.
Also accepts optional tabSize argument. 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 Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0

View File

@@ -33,7 +33,7 @@ function RL.draw()
RL.ClearBackground( RL.RAYWHITE ) RL.ClearBackground( RL.RAYWHITE )
RL.DrawRectangleLines( rect, RL.GREEN ) RL.DrawRectangleLines( rect, RL.GREEN )
mouseCharId, textOffset = RL.DrawTextBoxedEx( _, textOffset = RL.DrawTextBoxedEx(
RL.GetFontDefault(), 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.", "\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, rect,
@@ -45,12 +45,18 @@ function RL.draw()
rect, rect,
textSize, spacing, wordwrap, linkColor, limitHeight, textOffset textSize, spacing, wordwrap, linkColor, limitHeight, textOffset
) )
RL.DrawTextBoxedEx( _, textOffset = RL.DrawTextBoxedEx(
RL.GetFontDefault(), 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.", " 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, rect,
textSize, spacing, wordwrap, RL.RED, limitHeight, textOffset 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 if 0 < mouseCharId then
linkColor = RL.GREEN linkColor = RL.GREEN

View File

@@ -24,6 +24,38 @@ static float measureWord( Font font, char* text, float fontSize, float spacing )
return size.x; 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, static int DrawTextBoxed( Font font, char* text, Rectangle rec, float fontSize,
float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize ) { float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize ) {
int lineSpacing = state->lineSpacing; int lineSpacing = state->lineSpacing;
@@ -53,8 +85,20 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
measure = true; measure = true;
} }
else if ( codepoint == '\t' ) { else if ( codepoint == '\t' ) {
textOffset->x += (float)font.recs[ GetGlyphIndex( font, ' ' ) ].width * tabSize * scaleFactor + spacing;
measure = true; 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 ) ) { else if ( wordWrap && ( wordWidth < rec.width ) ) {
if ( measure && codepoint != ' ' ) { if ( measure && codepoint != ' ' ) {
wordWidth = measureWord( font, &text[i], fontSize, spacing ); wordWidth = measureWord( font, &text[i], fontSize, spacing );
@@ -80,7 +124,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
break; 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 ); 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 } ) ) { 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; textOffset->x += codepointWidth;
} }
else if ( codepoint == '\t' ) {
textOffset->x += (float)font.recs[ GetGlyphIndex( font, ' ' ) ].width * tabSize * scaleFactor + spacing;
}
i += codepointByteCount; i += codepointByteCount;
} }
return mouseChar; 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 ) > 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. Draw text using font inside rectangle limits.
Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2 - 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). 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 textOffset can be used to set start position inside rectangle. Usefull to pass from previous
DrawTextBoxedEx for continuous text. DrawTextBoxedEx for continuous text.
Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2 - Success return int, Vector2
*/ */