summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjussi2023-11-07 01:37:21 +0200
committerjussi2023-11-07 01:37:21 +0200
commit28ac27fae16d5d2df6174564dde05c6064515a33 (patch)
tree8c338c22b9b8ec220d29d00b9acb0eb83742f588 /src
parent7b8ef2cd90997382dca80f5122908eb6a8e60467 (diff)
downloadreilua-enhanced-28ac27fae16d5d2df6174564dde05c6064515a33.tar.gz
reilua-enhanced-28ac27fae16d5d2df6174564dde05c6064515a33.tar.bz2
reilua-enhanced-28ac27fae16d5d2df6174564dde05c6064515a33.zip
Replaced DrawTextBoxedSelectable with DrawTextBoxedTinted.
Diffstat (limited to 'src')
-rw-r--r--src/lua_core.c2
-rw-r--r--src/text.c101
2 files changed, 71 insertions, 32 deletions
diff --git a/src/lua_core.c b/src/lua_core.c
index 60f7f7b..0ea2f4e 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -1961,7 +1961,7 @@ void luaRegister() {
assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint );
assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints );
assingGlobalFunction( "DrawTextBoxed", ltextDrawTextBoxed );
- assingGlobalFunction( "DrawTextBoxedSelectable", ltextDrawTextBoxedSelectable );
+ assingGlobalFunction( "DrawTextBoxedTinted", ltextDrawTextBoxedTinted );
/* Font info functions. */
assingGlobalFunction( "MeasureText", ltextMeasureText );
assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex );
diff --git a/src/text.c b/src/text.c
index 4184794..205ad30 100644
--- a/src/text.c
+++ b/src/text.c
@@ -4,10 +4,11 @@
#include "textures.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
-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)
+// Draw text using font inside rectangle limits
+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
@@ -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 endLine = -1; // Index where to stop drawing (where a line ends)
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++)
{
+ if ( i < tintCount ) {
+ tint = tints[i];
+ }
+ if ( i < backTintCount ) {
+ backTint = backTints[i];
+ }
// Get next codepoint from byte string and glyph index in font
int codepointByteCount = 0;
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;
// Draw selection background
- bool isGlyphSelected = false;
- if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength)))
- {
- DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint);
- isGlyphSelected = true;
- }
+ // bool isGlyphSelected = false;
+ // if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength)))
+ // {
+ // DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint);
+ // 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
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;
endLine = -1;
glyphWidth = 0;
- selectStart += lastk - k;
+ // selectStart += lastk - k;
k = lastk;
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
}
-}
-
-// 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);
+ return mouseChar;
}
/*
@@ -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 ) {
Font *font = uluaGetFont( L, 1 );
+ const char *text = luaL_checkstring( L, 2 );
Rectangle rec = uluaGetRectangle( L, 3 );
float fontSize = luaL_checknumber( L, 4 );
float spacing = luaL_checknumber( L, 5 );
bool wordWrap = uluaGetBoolean( L, 6 );
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 );
+ const char *text = luaL_checkstring( L, 2 );
Rectangle rec = uluaGetRectangle( L, 3 );
float fontSize = luaL_checknumber( L, 4 );
float spacing = luaL_checknumber( L, 5 );
bool wordWrap = uluaGetBoolean( L, 6 );
- Color tint = uluaGetColor( L, 7 );
- int selectStart = luaL_checkinteger( L, 8 );
- int selectLength = luaL_checkinteger( L, 9 );
- Color selectTint = uluaGetColor( L, 10 );
- Color selectBackTint = uluaGetColor( L, 11 );
+ int tintCount = uluaGetTableLen( L, 7 );
+ int backTintCount = uluaGetTableLen( L, 8 );
- 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;
}
/*