Browse Source

Merge pull request #734 from Demizdor/master

Added DrawTextRecEx()
pull/747/head
Ray 6 years ago
committed by GitHub
parent
commit
01ace743d0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 58 deletions
  1. +2
    -0
      src/raylib.h
  2. +76
    -58
      src/text.c

+ 2
- 0
src/raylib.h View File

@ -1134,6 +1134,8 @@ RLAPI void DrawFPS(int posX, int posY);
RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font) RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using font and additional parameters RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using font and additional parameters
RLAPI void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint); // Draw text using font inside rectangle limits RLAPI void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint); // Draw text using font inside rectangle limits
RLAPI void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint,
int selectStart, int selectLength, Color selectText, Color selectBG); // Draw text using font inside rectangle limits with support for text selection
// Text misc. functions // Text misc. functions
RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font

+ 76
- 58
src/text.c View File

@ -781,6 +781,13 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f
// Draw text using font inside rectangle limits // Draw text using font inside rectangle limits
void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint) void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint)
{
DrawTextRecEx(font, text, rec, fontSize, spacing, wordWrap, tint, 0, 0, WHITE, WHITE);
}
// Draw text using font inside rectangle limits with support for text selection
void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint,
int selectStart, int selectLength, Color selectText, Color selectBG)
{ {
int length = strlen(text); int length = strlen(text);
int textOffsetX = 0; // Offset between characters int textOffsetX = 0; // Offset between characters
@ -792,12 +799,10 @@ void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, flo
scaleFactor = fontSize/font.baseSize; scaleFactor = fontSize/font.baseSize;
enum { MEASURE_WORD = 0, DRAW_WORD = 1 }; enum { MEASURE_STATE = 0, DRAW_STATE = 1 };
int state = wordWrap ? MEASURE_WORD : DRAW_WORD; int state = wordWrap?MEASURE_STATE:DRAW_STATE;
int lastTextOffsetX = 0; int startLine = -1; // Index where to begin drawing (where a line begins)
int wordStart = 0; int endLine = -1; // Index where to stop drawing (where a line ends)
bool firstWord = true;
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
@ -827,77 +832,90 @@ void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, flo
(int)(font.chars[index].advanceX*scaleFactor + spacing); (int)(font.chars[index].advanceX*scaleFactor + spacing);
} }
// NOTE: When word wrap is active first we measure a `word`(measure until a ' ','\n','\t' is found) // NOTE: When wordWrap is ON we first measure how much of the text we can draw
// then set all the variables back to what they were before the measurement, change the state to // before going outside of the `rec` container. We store this info inside
// draw that word then change the state again and repeat until the end of the string...when the word // `startLine` and `endLine` then we change states, draw the text between those two
// doesn't fit inside the rect we simple increase `textOffsetY` to draw it on the next line // variables then change states again and again recursively until the end of the text
if (state == MEASURE_WORD) // (or until we get outside of the container).
// When wordWrap is OFF we don't need the measure state so we go to the drawing
// state immediately and begin drawing on the next line before we can get outside
// the container.
if (state == MEASURE_STATE)
{ {
// Measuring state if((letter == ' ') || (letter == '\t') || (letter == '\n')) endLine = i;
if ((letter == ' ') || (letter == '\n') || (letter == '\t') || ((i + 1) == length)) if(textOffsetX + glyphWidth + 1 >= rec.width)
{ {
int t = textOffsetX + glyphWidth; endLine = (endLine < 1) ? i : endLine;
if(i == endLine) endLine -= 1;
if (textOffsetX+1>=rec.width) if(startLine + 1 == endLine ) endLine = i - 1;
{ state = !state;
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); }
lastTextOffsetX = t - lastTextOffsetX; else if(i + 1 == length)
textOffsetX = 0; {
} endLine = i;
else state = !state;
{ }
textOffsetX = lastTextOffsetX; else if(letter == '\n')
lastTextOffsetX = t; {
} state = !state;
}
if(state == DRAW_STATE) {
textOffsetX = 0;
i = startLine;
glyphWidth = 0; glyphWidth = 0;
state = !state; // Change state
t = i;
i = firstWord?-1:wordStart;
wordStart = t;
} }
} }
else else
{ {
// Drawing state if (letter == '\n')
int t = textOffsetX + glyphWidth;
if (letter == '\n')
{ {
n">textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); if(!wordWrap){
lastTextOffsetX = t - lastTextOffsetX; textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
class="k">if class="p">(lastTextOffsetX o"><n> 0) lastTextOffsetX = 0; textOffsetX = 0;
n">textOffsetX = 0; p">}
} }
else if ((letter != ' ') && (letter != '\t')) else
{ {
if ((t + 1) >= rec.width) if(!wordWrap && textOffsetX + glyphWidth + 1 >= rec.width) {
{
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
textOffsetX = 0; textOffsetX = 0;
} }
if ((textOffsetY + (int)((font.baseSize + font.baseSize/2)*scaleFactor)) > rec.height) break; if ((textOffsetY + (int)((font.baseSize + font.baseSize/2)*scaleFactor)) > rec.height) break;
DrawTexturePro(font.texture, font.chars[index].rec, //draw selected
(Rectangle){ rec.x + textOffsetX + font.chars[index].offsetX*scaleFactor, bool isGlyphSelected = false;
rec.y + textOffsetY + font.chars[index].offsetY*scaleFactor, if(selectStart >= 0 && i >= selectStart && i < selectStart + selectLength) {
font.chars[index].rec.width*scaleFactor, Rectangle strec = {rec.x + textOffsetX-1, rec.y + textOffsetY,
font.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint); glyphWidth,(font.baseSize + font.baseSize/4)*scaleFactor};
} DrawRectangleRec(strec, selectBG);
isGlyphSelected = true;
if (wordWrap) }
{ //draw glyph
if ((letter == ' ') || (letter == '\n') || (letter == '\t')) if ((letter != ' ') && (letter != '\t'))
{ {
o">// After drawing a word change the state n">DrawTexturePro(font.texture, font.chars[index].rec,
firstWord = false; (Rectangle){ rec.x + textOffsetX + font.chars[index].offsetX*scaleFactor,
i = wordStart; rec.y + textOffsetY + font.chars[index].offsetY*scaleFactor,
textOffsetX = lastTextOffsetX; font.chars[index].rec.width*scaleFactor,
glyphWidth = 0; font.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f,
state = !state; (!isGlyphSelected) ? tint : selectText);
} }
} }
if (wordWrap && i == endLine)
{
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
textOffsetX = 0;
startLine = endLine;
endLine = -1;
glyphWidth = 0;
state = !state;
}
} }
textOffsetX += glyphWidth; textOffsetX += glyphWidth;

||||||
x
 
000:0
Loading…
Cancel
Save