Replaced DrawTextBoxedSelectable with DrawTextBoxedTinted.

This commit is contained in:
jussi
2023-11-07 01:37:21 +02:00
parent 7b8ef2cd90
commit 28ac27fae1
6 changed files with 99 additions and 55 deletions

12
API.md
View File

@@ -6058,15 +6058,19 @@ Draw multiple character (codepoint)
--- ---
> RL.DrawTextBoxed(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint ) > mouseCharId = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint )
Draw text using font inside rectangle limits. Function from raylib [text] example - Rectangle bounds Draw text using font inside rectangle limits. Return character from mouse position. Function from raylib [text] example - Rectangle bounds.
- Success return int
--- ---
> RL.DrawTextBoxedSelectable( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, Color selectTint, Color selectBackTint ) > mouseCharId = RL.DrawTextBoxedTinted( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tints, Color backTints )
Draw text using font inside rectangle limits with support for text selection. Function from raylib [text] example - Rectangle bounds Draw text using font inside rectangle limits with support for tint and background tint for each character. Return character from mouse position
- Success return int
--- ---

View File

@@ -3434,18 +3434,8 @@ function RL.DrawTextCodepoint( font, codepoint, position, fontSize, tint ) end
---@return any RL.DrawTextCodepoints ---@return any RL.DrawTextCodepoints
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. Function from raylib [text] example - Rectangle bounds ---Draw text using font inside rectangle limits. Return character from mouse position. Function from raylib [text] example - Rectangle bounds.
---@param font any ---- Success return int
---@param char any
---@param rec table
---@param fontSize number
---@param spacing number
---@param wordWrap boolean
---@param tint table
---@return any RL.DrawTextBoxed
function RL.DrawTextBoxed( font, char, rec, fontSize, spacing, wordWrap, tint ) end
---Draw text using font inside rectangle limits with support for text selection. Function from raylib [text] example - Rectangle bounds
---@param font any ---@param font any
---@param text string ---@param text string
---@param rec table ---@param rec table
@@ -3453,12 +3443,21 @@ function RL.DrawTextBoxed( font, char, rec, fontSize, spacing, wordWrap, tint )
---@param spacing number ---@param spacing number
---@param wordWrap boolean ---@param wordWrap boolean
---@param tint table ---@param tint table
---@param selectStart integer ---@return any mouseCharId
---@param selectLength integer function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint ) end
---@param selectTint table
---@param selectBackTint table ---Draw text using font inside rectangle limits with support for tint and background tint for each character. Return character from mouse position
---@return any RL.DrawTextBoxedSelectable ---- Success return int
function RL.DrawTextBoxedSelectable( font, text, rec, fontSize, spacing, wordWrap, tint, selectStart, selectLength, selectTint, selectBackTint ) end ---@param font any
---@param text string
---@param rec table
---@param fontSize number
---@param spacing number
---@param wordWrap boolean
---@param tints table
---@param backTints table
---@return any mouseCharId
function RL.DrawTextBoxedTinted( font, text, rec, fontSize, spacing, wordWrap, tints, backTints ) end
-- Text - Font info functions -- Text - Font info functions

View File

@@ -40,6 +40,8 @@ DETAILED CHANGES:
- ADDED: GetBufferType and GetBufferSize. - ADDED: GetBufferType and GetBufferSize.
- ADDED: Compress data example. - ADDED: Compress data example.
- ADDED: ExportBuffer and LoadBufferFromFile. - ADDED: ExportBuffer and LoadBufferFromFile.
- REMOVED: DrawTextBoxedSelectable.
- ADDED: DrawTextBoxedTinted.
------------------------------------------------------------------------ ------------------------------------------------------------------------
Release: ReiLua version 0.5.0 Using Raylib 4.5 Release: ReiLua version 0.5.0 Using Raylib 4.5

View File

@@ -15,7 +15,7 @@ int ltextDrawTextPro( lua_State *L );
int ltextDrawTextCodepoint( lua_State *L ); int ltextDrawTextCodepoint( lua_State *L );
int ltextDrawTextCodepoints( lua_State *L ); int ltextDrawTextCodepoints( lua_State *L );
int ltextDrawTextBoxed( lua_State *L ); int ltextDrawTextBoxed( lua_State *L );
int ltextDrawTextBoxedSelectable( lua_State *L ); int ltextDrawTextBoxedTinted( lua_State *L );
/* Font info functions. */ /* Font info functions. */
int ltextMeasureText( lua_State *L ); int ltextMeasureText( lua_State *L );
int ltextGetGlyphIndex( lua_State *L ); int ltextGetGlyphIndex( lua_State *L );

View File

@@ -1961,7 +1961,7 @@ void luaRegister() {
assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint ); assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint );
assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints ); assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints );
assingGlobalFunction( "DrawTextBoxed", ltextDrawTextBoxed ); assingGlobalFunction( "DrawTextBoxed", ltextDrawTextBoxed );
assingGlobalFunction( "DrawTextBoxedSelectable", ltextDrawTextBoxedSelectable ); assingGlobalFunction( "DrawTextBoxedTinted", ltextDrawTextBoxedTinted );
/* Font info functions. */ /* Font info functions. */
assingGlobalFunction( "MeasureText", ltextMeasureText ); assingGlobalFunction( "MeasureText", ltextMeasureText );
assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex ); assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex );

View File

@@ -4,10 +4,11 @@
#include "textures.h" #include "textures.h"
#include "lua_core.h" #include "lua_core.h"
// DrawTextBoxed and DrawTextBoxedSelectable from raylib [text] example - Rectangle bounds // DrawTextBoxed is modified DrawTextBoxedSelectable from raylib [text] example - Rectangle bounds
// Draw text using font inside rectangle limits with support for text selection // Draw text using font inside rectangle limits
static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, Color selectTint, Color selectBackTint) static int DrawTextBoxed( Font font, const char *text, Rectangle rec, float fontSize, float spacing,
bool wordWrap, Color *tints, int tintCount, Color *backTints, int backTintCount )
{ {
int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
@@ -23,9 +24,19 @@ static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec,
int startLine = -1; // Index where to begin drawing (where a line begins) int startLine = -1; // Index where to begin drawing (where a line begins)
int endLine = -1; // Index where to stop drawing (where a line ends) int endLine = -1; // Index where to stop drawing (where a line ends)
int lastk = -1; // Holds last value of the character position int lastk = -1; // Holds last value of the character position
Color tint = BLACK;
Color backTint = BLANK;
Vector2 mousePos = GetMousePosition();
int mouseChar = -1;
for (int i = 0, k = 0; i < length; i++, k++) for (int i = 0, k = 0; i < length; i++, k++)
{ {
if ( i < tintCount ) {
tint = tints[i];
}
if ( i < backTintCount ) {
backTint = backTints[i];
}
// Get next codepoint from byte string and glyph index in font // Get next codepoint from byte string and glyph index in font
int codepointByteCount = 0; int codepointByteCount = 0;
int codepoint = GetCodepoint(&text[i], &codepointByteCount); int codepoint = GetCodepoint(&text[i], &codepointByteCount);
@@ -104,17 +115,26 @@ static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec,
if ((textOffsetY + font.baseSize*scaleFactor) > rec.height) break; if ((textOffsetY + font.baseSize*scaleFactor) > rec.height) break;
// Draw selection background // Draw selection background
bool isGlyphSelected = false; // bool isGlyphSelected = false;
if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength))) // if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength)))
{ // {
DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint); // DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint);
isGlyphSelected = true; // isGlyphSelected = true;
} // }
if ( CheckCollisionPointRec( mousePos, (Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor } ) ) {
mouseChar = i;
}
if ( 0 < backTint.a ) {
DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, backTint);
}
// Draw current character glyph // Draw current character glyph
if ((codepoint != ' ') && (codepoint != '\t')) if ((codepoint != ' ') && (codepoint != '\t'))
{ {
DrawTextCodepoint(font, codepoint, (Vector2){ rec.x + textOffsetX, rec.y + textOffsetY }, fontSize, isGlyphSelected? selectTint : tint); // DrawTextCodepoint(font, codepoint, (Vector2){ rec.x + textOffsetX, rec.y + textOffsetY }, fontSize, isGlyphSelected? selectTint : tint);
DrawTextCodepoint( font, codepoint, (Vector2){ rec.x + textOffsetX, rec.y + textOffsetY }, fontSize, tint );
} }
} }
@@ -125,7 +145,7 @@ static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec,
startLine = endLine; startLine = endLine;
endLine = -1; endLine = -1;
glyphWidth = 0; glyphWidth = 0;
selectStart += lastk - k; // selectStart += lastk - k;
k = lastk; k = lastk;
state = !state; state = !state;
@@ -134,12 +154,7 @@ static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec,
if ((textOffsetX != 0) || (codepoint != ' ')) textOffsetX += glyphWidth; // avoid leading spaces if ((textOffsetX != 0) || (codepoint != ' ')) textOffsetX += glyphWidth; // avoid leading spaces
} }
} return mouseChar;
// Draw text using font inside rectangle limits
static void DrawTextBoxed(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint)
{
DrawTextBoxedSelectable(font, text, rec, fontSize, spacing, wordWrap, tint, 0, 0, WHITE, WHITE);
} }
/* /*
@@ -378,43 +393,67 @@ int ltextDrawTextCodepoints( lua_State *L ) {
} }
/* /*
> RL.DrawTextBoxed(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint ) > mouseCharId = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint )
Draw text using font inside rectangle limits. Function from raylib [text] example - Rectangle bounds Draw text using font inside rectangle limits. Return character from mouse position. Function from raylib [text] example - Rectangle bounds.
- Success return int
*/ */
int ltextDrawTextBoxed( lua_State *L ) { int ltextDrawTextBoxed( lua_State *L ) {
Font *font = uluaGetFont( L, 1 ); Font *font = uluaGetFont( L, 1 );
const char *text = luaL_checkstring( L, 2 );
Rectangle rec = uluaGetRectangle( L, 3 ); Rectangle rec = uluaGetRectangle( L, 3 );
float fontSize = luaL_checknumber( L, 4 ); float fontSize = luaL_checknumber( L, 4 );
float spacing = luaL_checknumber( L, 5 ); float spacing = luaL_checknumber( L, 5 );
bool wordWrap = uluaGetBoolean( L, 6 ); bool wordWrap = uluaGetBoolean( L, 6 );
Color tint = uluaGetColor( L, 7 ); Color tint = uluaGetColor( L, 7 );
DrawTextBoxedSelectable( *font, luaL_checkstring( L, 2 ), rec, fontSize, spacing, wordWrap, tint, 0, 0, WHITE, WHITE ); lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, &tint, 1, NULL, 0 ) );
return 0; return 1;
} }
/* /*
> RL.DrawTextBoxedSelectable( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, Color selectTint, Color selectBackTint ) > mouseCharId = RL.DrawTextBoxedTinted( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tints, Color backTints )
Draw text using font inside rectangle limits with support for text selection. Function from raylib [text] example - Rectangle bounds Draw text using font inside rectangle limits with support for tint and background tint for each character. Return character from mouse position
- Success return int
*/ */
int ltextDrawTextBoxedSelectable( lua_State *L ) { int ltextDrawTextBoxedTinted( lua_State *L ) {
Font *font = uluaGetFont( L, 1 ); Font *font = uluaGetFont( L, 1 );
const char *text = luaL_checkstring( L, 2 );
Rectangle rec = uluaGetRectangle( L, 3 ); Rectangle rec = uluaGetRectangle( L, 3 );
float fontSize = luaL_checknumber( L, 4 ); float fontSize = luaL_checknumber( L, 4 );
float spacing = luaL_checknumber( L, 5 ); float spacing = luaL_checknumber( L, 5 );
bool wordWrap = uluaGetBoolean( L, 6 ); bool wordWrap = uluaGetBoolean( L, 6 );
Color tint = uluaGetColor( L, 7 ); int tintCount = uluaGetTableLen( L, 7 );
int selectStart = luaL_checkinteger( L, 8 ); int backTintCount = uluaGetTableLen( L, 8 );
int selectLength = luaL_checkinteger( L, 9 );
Color selectTint = uluaGetColor( L, 10 );
Color selectBackTint = uluaGetColor( L, 11 );
DrawTextBoxedSelectable( *font, luaL_checkstring( L, 2 ), rec, fontSize, spacing, wordWrap, tint, selectStart, selectLength, selectTint, selectBackTint ); Color tints[ tintCount ];
Color backTints[ backTintCount ];
return 0; /* Tints. */
int t = 7, i = 0;
lua_pushnil( L );
while ( lua_next( L, t ) != 0 ) {
tints[i] = uluaGetColor( L, lua_gettop( L ) );
i++;
lua_pop( L, 1 );
}
/* Back tints. */
t = 8; i = 0;
lua_pushnil( L );
while ( lua_next( L, t ) != 0 ) {
backTints[i] = uluaGetColor( L, lua_gettop( L ) );
i++;
lua_pop( L, 1 );
}
lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, tints, tintCount, backTints, backTintCount ) );
return 1;
} }
/* /*