#ifndef GUI_TEXTBOX_EXTENDED_H
#define GUI_TEXTBOX_EXTENDED_H
typedef struct GuiTextBoxState {
int cursor; int start; int index; int select; } GuiTextBoxState;
#ifdef __cplusplus
extern "C" { #endif
RAYGUIDEF void GuiTextBoxSetActive(Rectangle bounds); RAYGUIDEF Rectangle GuiTextBoxGetActive(void);
RAYGUIDEF void GuiTextBoxSetCursor(int cursor); RAYGUIDEF int GuiTextBoxGetCursor(void);
RAYGUIDEF void GuiTextBoxSetSelection(int start, int length); RAYGUIDEF Vector2 GuiTextBoxGetSelection(void);
RAYGUIDEF bool GuiTextBoxIsActive(Rectangle bounds); RAYGUIDEF GuiTextBoxState GuiTextBoxGetState(void); RAYGUIDEF void GuiTextBoxSetState(GuiTextBoxState state);
RAYGUIDEF void GuiTextBoxSelectAll(const char *text); RAYGUIDEF void GuiTextBoxCopy(const char *text); RAYGUIDEF void GuiTextBoxPaste(char *text, int textSize); RAYGUIDEF void GuiTextBoxCut(char *text); RAYGUIDEF int GuiTextBoxDelete(char *text, int length, bool before); RAYGUIDEF int GuiTextBoxGetByteIndex(const char *text, int start, int from, int to);
RAYGUIDEF bool GuiTextBoxEx(Rectangle bounds, char *text, int textSize, bool editMode);
#ifdef __cplusplus
}
#endif
#endif
#if defined(GUI_TEXTBOX_EXTENDED_IMPLEMENTATION)
#ifndef RAYGUI_H
#include "raygui.h"
#endif
typedef enum {
GUI_MEASURE_MODE_CURSOR_END = 0xA,
GUI_MEASURE_MODE_CURSOR_POS,
GUI_MEASURE_MODE_CURSOR_COORDS
} GuiMeasureMode;
static Rectangle guiTextBoxActive = { 0 };
static GuiTextBoxState guiTextBoxState = { .cursor = -1,
.start = 0,
.index = 0,
.select = -1
};
static int GetPrevCodepoint(const char *text, const char *start, int *prev);
static int GuiMeasureTextBox(const char *text, int length, Rectangle rec, int *pos, int mode);
static int GuiMeasureTextBoxRev(const char *text, int length, Rectangle rec, int *pos);
static inline int GuiTextBoxGetCursorCoordinates(const char *text, int length, Rectangle rec, int pos); static inline int GuiTextBoxGetCursorFromMouse(const char *text, int length, Rectangle rec, int *pos); static inline int GuiTextBoxMaxCharacters(const char *text, int length, Rectangle rec); static inline unsigned int GuiCountCodepointsUntilNewline(const char *text);
static inline void MoveTextBoxCursorRight(const char *text, int length, Rectangle textRec);
static inline void MoveTextBoxCursorLeft(const char *text);
static int EncodeCodepoint(unsigned int c, char out[5]);
RAYGUIDEF void GuiTextBoxSetActive(Rectangle bounds)
{
guiTextBoxActive = bounds;
guiTextBoxState = (GuiTextBoxState){ .cursor = -1, .start = 0, .index = 0, .select = -1 };
}
RAYGUIDEF Rectangle GuiTextBoxGetActive(void) { return guiTextBoxActive; }
RAYGUIDEF void GuiTextBoxSetCursor(int cursor)
{
guiTextBoxState.cursor = (cursor < 0) ? -1 : cursor;
guiTextBoxState.start = -1; }
RAYGUIDEF int GuiTextBoxGetCursor(void) { return guiTextBoxState.cursor; }
RAYGUIDEF void GuiTextBoxSetSelection(int start, int length)
{
if (start < 0) start = 0;
if (length < 0) length = 0;
GuiTextBoxSetCursor(start + length);
guiTextBoxState.select = start;
}
RAYGUIDEF Vector2 GuiTextBoxGetSelection(void)
{
if (guiTextBoxState.select == -1 || guiTextBoxState.select == guiTextBoxState.cursor) return RAYGUI_CLITERAL(Vector2){ 0 };
else if (guiTextBoxState.cursor > guiTextBoxState.select) return RAYGUI_CLITERAL(Vector2){ guiTextBoxState.select, guiTextBoxState.cursor - guiTextBoxState.select };
return RAYGUI_CLITERAL(Vector2){ guiTextBoxState.cursor, guiTextBoxState.select - guiTextBoxState.cursor };
}
RAYGUIDEF bool GuiTextBoxIsActive(Rectangle bounds)
{
return (bounds.x == guiTextBoxActive.x && bounds.y == guiTextBoxActive.y &&
bounds.width == guiTextBoxActive.width && bounds.height == guiTextBoxActive.height);
}
RAYGUIDEF GuiTextBoxState GuiTextBoxGetState(void) { return guiTextBoxState; }
RAYGUIDEF void GuiTextBoxSetState(GuiTextBoxState state)
{
guiTextBoxState = state;
}
RAYGUIDEF int GuiTextBoxGetByteIndex(const char *text, int start, int from, int to)
{
int i = start, k = from;
while ((text[i] != '\0') && (k < to))
{
int j = 0;
int letter = GetNextCodepoint(&text[i], &j);
if (letter == 0x3f) j = 1;
i += j;
++k;
}
return i;
}
RAYGUIDEF int GuiTextBoxDelete(char *text, int length, bool before)
{
if ((guiTextBoxState.cursor != -1) && (text != NULL))
{
int startIdx = 0, endIdx = 0;
if ((guiTextBoxState.select != -1) && (guiTextBoxState.select != guiTextBoxState.cursor))
{
int start = guiTextBoxState.cursor;
int end = guiTextBoxState.select;
if (guiTextBoxState.cursor > guiTextBoxState.select)
{
start = guiTextBoxState.select;
end = guiTextBoxState.cursor;
}
startIdx = GuiTextBoxGetByteIndex(text, 0, 0, start);
endIdx = GuiTextBoxGetByteIndex(text, 0, 0, end);
guiTextBoxState.cursor = start; if (guiTextBoxState.select < guiTextBoxState.start) guiTextBoxState.start = -1; }
else
{
if (before)
{
if (guiTextBoxState.cursor != 0)
{
endIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
guiTextBoxState.cursor--;
startIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
if (guiTextBoxState.cursor < guiTextBoxState.start) guiTextBoxState.start = -1; }
}
else
{
if (guiTextBoxState.cursor + 1 <= GuiCountCodepointsUntilNewline(text))
{
startIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
endIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor+1);
}
}
}
memmove(&text[startIdx], &text[endIdx], length - endIdx);
text[length - (endIdx - startIdx)] = '\0';
guiTextBoxState.select = -1;
return (endIdx - startIdx);
}
return 0;
}
RAYGUIDEF void GuiTextBoxSelectAll(const char *text)
{
guiTextBoxState.cursor = GuiCountCodepointsUntilNewline(text);
if (guiTextBoxState.cursor > 0)
{
guiTextBoxState.select = 0;
guiTextBoxState.start = -1; }
else guiTextBoxState.select = -1;
}
RAYGUIDEF void GuiTextBoxCopy(const char *text)
{
if ((text != NULL) &&
(guiTextBoxState.select != -1) &&
(guiTextBoxState.cursor != -1) &&
(guiTextBoxState.select != guiTextBoxState.cursor))
{
int start = guiTextBoxState.cursor;
int end = guiTextBoxState.select;
if (guiTextBoxState.cursor > guiTextBoxState.select)
{
start = guiTextBoxState.select;
end = guiTextBoxState.cursor;
}
start = GuiTextBoxGetByteIndex(text, 0, 0, start);
end = GuiTextBoxGetByteIndex(text, 0, 0, end);
const char *clipText = TextSubtext(text, start, end - start);
SetClipboardText(clipText);
}
}
RAYGUIDEF void GuiTextBoxPaste(char *text, int textSize)
{
const char *clipText = GetClipboardText(); int length = strlen(text);
if ((text != NULL) && (clipText != NULL) && (guiTextBoxState.cursor != -1))
{
if ((guiTextBoxState.select != -1) && (guiTextBoxState.select != guiTextBoxState.cursor))
{
length -= GuiTextBoxDelete(text, length, true);
}
int clipLen = strlen(clipText);
int size = ((length + clipLen) <= textSize) ? clipLen : textSize - length;
int startIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
int endIdx = startIdx + size;
memmove(&text[endIdx], &text[startIdx], length - startIdx);
text[length + size] = '\0';
memcpy(&text[startIdx], clipText, size);
guiTextBoxState.cursor = 0;
for (int i = 0; i < (startIdx + size); guiTextBoxState.cursor++)
{
int next = 0;
int letter = GetNextCodepoint(&text[i], &next);
if (letter != 0x3f) i += next;
else i += 1;
}
guiTextBoxState.start = -1; }
}
RAYGUIDEF void GuiTextBoxCut(char* text)
{
if ((text != NULL) &&
(guiTextBoxState.select != -1) &&
(guiTextBoxState.cursor != -1) &&
(guiTextBoxState.select != guiTextBoxState.cursor))
{
int start = guiTextBoxState.cursor, end = guiTextBoxState.select;
if (guiTextBoxState.cursor > guiTextBoxState.select)
{
start = guiTextBoxState.select;
end = guiTextBoxState.cursor;
}
int startIdx = GuiTextBoxGetByteIndex(text, 0, 0, start);
int endIdx = GuiTextBoxGetByteIndex(text, 0, 0, end);
const char *clipText = TextSubtext(text, startIdx, endIdx - startIdx);
SetClipboardText(clipText);
int len = strlen(text);
memmove(&text[startIdx], &text[endIdx], len - endIdx);
text[len - (endIdx - startIdx)] = '\0';
guiTextBoxState.cursor = start; if (guiTextBoxState.select < guiTextBoxState.start) guiTextBoxState.start = -1; guiTextBoxState.select = -1; }
}
RAYGUIDEF bool GuiTextBoxEx(Rectangle bounds, char *text, int textSize, bool editMode)
{
#define TEXTBOX_CURSOR_COOLDOWN 5
static int framesCounter = 0;
GuiControlState state = guiState;
bool pressed = false;
int length = strlen(text);
if (length > textSize)
{
text[textSize] = '\0';
length = textSize;
}
if ((bounds.width - 2*GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)) < GuiGetStyle(DEFAULT, TEXT_SIZE))
{
bounds.width = GuiGetStyle(DEFAULT, TEXT_SIZE) + 2*GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING);
}
int verticalPadding = (bounds.height - 2*GuiGetStyle(TEXTBOX, BORDER_WIDTH) - GuiGetStyle(DEFAULT, TEXT_SIZE))/2;
if (verticalPadding < 0)
{
bounds.height = 2*GuiGetStyle(TEXTBOX, BORDER_WIDTH) + GuiGetStyle(DEFAULT, TEXT_SIZE);
verticalPadding = 0;
}
Rectangle textRec = { bounds.x + GuiGetStyle(TEXTBOX, BORDER_WIDTH) + GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING),
bounds.y + verticalPadding + GuiGetStyle(TEXTBOX, BORDER_WIDTH),
bounds.width - 2*(GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING) + GuiGetStyle(TEXTBOX, BORDER_WIDTH)),
GuiGetStyle(DEFAULT, TEXT_SIZE) };
Vector2 cursorPos = { textRec.x, textRec.y }; bool active = GuiTextBoxIsActive(bounds);
int selStart = 0, selLength = 0, textStartIndex = 0;
if ((state != GUI_STATE_DISABLED) && !guiLocked)
{
Vector2 mousePoint = GetMousePosition();
if (editMode)
{
if (!active)
{
if (CheckCollisionPointRec(mousePoint, bounds) && (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)))
{
active = true;
GuiTextBoxSetActive(bounds);
}
}
else if (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
{
GuiTextBoxSetActive(RAYGUI_CLITERAL(Rectangle){ 0, 0, -1, -1 });
active = false;
}
if (active)
{
state = GUI_STATE_PRESSED;
framesCounter++;
if (guiTextBoxState.cursor > length) guiTextBoxState.cursor = -1;
if (guiTextBoxState.select > length) guiTextBoxState.select = -1;
if (guiTextBoxState.start > length) guiTextBoxState.start = -1;
if (guiTextBoxState.cursor == -1)
{
guiTextBoxState.cursor = GuiTextBoxMaxCharacters(text, length, textRec);
}
if (guiTextBoxState.start == -1)
{
if (guiTextBoxState.cursor == 0)
{
guiTextBoxState.start = guiTextBoxState.index = 0; }
else
{
int pos = 0;
int len = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
guiTextBoxState.index = GuiMeasureTextBoxRev(text, len, textRec, &pos);
guiTextBoxState.start = guiTextBoxState.cursor - pos + 1;
}
}
if (IsKeyPressed(KEY_RIGHT) || (IsKeyDown(KEY_RIGHT) && (framesCounter%TEXTBOX_CURSOR_COOLDOWN == 0)))
{
if (IsKeyDown(KEY_LEFT_SHIFT))
{
if (guiTextBoxState.select == -1) guiTextBoxState.select = guiTextBoxState.cursor;
MoveTextBoxCursorRight(text, length, textRec);
}
else
{
if (guiTextBoxState.select != -1 && guiTextBoxState.select != guiTextBoxState.cursor)
{
if (guiTextBoxState.cursor < guiTextBoxState.select)
{
guiTextBoxState.cursor = guiTextBoxState.select - 1;
MoveTextBoxCursorRight(text, length, textRec);
}
}
else
{
MoveTextBoxCursorRight(text, length, textRec);
}
guiTextBoxState.select = -1;
}
framesCounter = 0;
}
else if (IsKeyPressed(KEY_LEFT) || (IsKeyDown(KEY_LEFT) && (framesCounter%TEXTBOX_CURSOR_COOLDOWN == 0)))
{
if (IsKeyDown(KEY_LEFT_SHIFT))
{
if (guiTextBoxState.select == -1) guiTextBoxState.select = guiTextBoxState.cursor;
MoveTextBoxCursorLeft(text);
}
else
{
if ((guiTextBoxState.select != -1) && (guiTextBoxState.select != guiTextBoxState.cursor))
{
if (guiTextBoxState.cursor > guiTextBoxState.select)
{
guiTextBoxState.cursor = guiTextBoxState.select;
if (guiTextBoxState.start > guiTextBoxState.cursor)
{
guiTextBoxState.start = guiTextBoxState.cursor;
guiTextBoxState.index = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.start); }
}
}
else
{
MoveTextBoxCursorLeft(text);
}
guiTextBoxState.select = -1;
}
framesCounter = 0;
}
else if (IsKeyPressed(KEY_BACKSPACE) || (IsKeyDown(KEY_BACKSPACE) && (framesCounter%TEXTBOX_CURSOR_COOLDOWN) == 0))
{
GuiTextBoxDelete(text, length, true);
}
else if (IsKeyPressed(KEY_DELETE) || (IsKeyDown(KEY_DELETE) && (framesCounter%TEXTBOX_CURSOR_COOLDOWN) == 0))
{
GuiTextBoxDelete(text, length, false);
}
else if (IsKeyPressed(KEY_HOME))
{
if (IsKeyDown(KEY_LEFT_SHIFT))
{
if ((guiTextBoxState.select > guiTextBoxState.cursor) || ((guiTextBoxState.select == -1) && (guiTextBoxState.cursor != 0)))
{
guiTextBoxState.select = guiTextBoxState.cursor;
}
}
else guiTextBoxState.select = -1;
guiTextBoxState.cursor = guiTextBoxState.start = guiTextBoxState.index = 0;
framesCounter = 0;
}
else if (IsKeyPressed(KEY_END))
{
int max = GuiCountCodepointsUntilNewline(text);
if (IsKeyDown(KEY_LEFT_SHIFT))
{
if ((guiTextBoxState.select == -1) && (guiTextBoxState.cursor != max))
{
guiTextBoxState.select = guiTextBoxState.cursor;
}
}
else guiTextBoxState.select = -1;
int pos = 0;
guiTextBoxState.cursor = max;
int len = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
guiTextBoxState.index = GuiMeasureTextBoxRev(text, len, textRec, &pos);
guiTextBoxState.start = guiTextBoxState.cursor - pos + 1;
}
else if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_A)) GuiTextBoxSelectAll(text); else if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_C)) GuiTextBoxCopy(text); else if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_X)) GuiTextBoxCut(text); else if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V)) GuiTextBoxPaste(text, textSize); else if (IsKeyPressed(KEY_ENTER)) pressed = true;
else
{
int key = GetKeyPressed();
if ((key >= 32) && ((guiTextBoxState.cursor + 1) < textSize))
{
if ((guiTextBoxState.select != -1) && (guiTextBoxState.select != guiTextBoxState.cursor))
{
GuiTextBoxDelete(text, length, true);
}
char out[5] = {0};
int sz = EncodeCodepoint(key, &out[0]);
if (sz != 0)
{
int startIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
int endIdx = startIdx + sz;
if (endIdx <= textSize && length < textSize - 1)
{
guiTextBoxState.cursor++;
guiTextBoxState.select = -1;
memmove(&text[endIdx], &text[startIdx], length - startIdx);
memcpy(&text[startIdx], &out[0], sz);
length += sz;
text[length] = '\0';
if (guiTextBoxState.start != -1)
{
const int max = GuiTextBoxMaxCharacters(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec);
if ((guiTextBoxState.cursor - guiTextBoxState.start) > max) guiTextBoxState.start = -1;
}
}
}
}
}
if (CheckCollisionPointRec(mousePoint, bounds))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
if (CheckCollisionPointRec(mousePoint, textRec))
{
GuiTextBoxGetCursorFromMouse(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec, &guiTextBoxState.cursor);
guiTextBoxState.cursor += guiTextBoxState.start;
guiTextBoxState.select = -1;
}
else
{
if (mousePoint.x <= bounds.x+bounds.width/2) guiTextBoxState.cursor = 0 + guiTextBoxState.start;
else guiTextBoxState.cursor = guiTextBoxState.start + GuiTextBoxMaxCharacters(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec);
guiTextBoxState.select = -1;
}
}
else if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
int cursor = guiTextBoxState.cursor - guiTextBoxState.start;
bool move = false;
if (CheckCollisionPointRec(mousePoint, textRec))
{
GuiTextBoxGetCursorFromMouse(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec, &cursor);
}
else
{
move = true;
if (mousePoint.x > bounds.x+bounds.width/2)
{
cursor = GuiTextBoxMaxCharacters(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec);
}
}
guiTextBoxState.cursor = cursor + guiTextBoxState.start;
if (guiTextBoxState.select == -1)
{
guiTextBoxState.select = guiTextBoxState.cursor;
}
if ((framesCounter%TEXTBOX_CURSOR_COOLDOWN) == 0 && move)
{
if (cursor == 0) MoveTextBoxCursorLeft(text);
else if (cursor == GuiTextBoxMaxCharacters(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec))
{
MoveTextBoxCursorRight(text, length, textRec);
}
}
}
}
cursorPos.x = GuiTextBoxGetCursorCoordinates(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec, guiTextBoxState.cursor - guiTextBoxState.start);
textStartIndex = guiTextBoxState.index;
if (guiTextBoxState.select == -1)
{
selStart = guiTextBoxState.cursor;
selLength = 0;
}
else if (guiTextBoxState.cursor > guiTextBoxState.select)
{
selStart = guiTextBoxState.select;
selLength = guiTextBoxState.cursor - guiTextBoxState.select;
}
else
{
selStart = guiTextBoxState.cursor;
selLength = guiTextBoxState.select - guiTextBoxState.cursor;
}
if (guiTextBoxState.start > selStart)
{
selLength -= guiTextBoxState.start - selStart;
selStart = 0;
}
else selStart = selStart - guiTextBoxState.start;
}
else state = GUI_STATE_FOCUSED;
if (IsKeyPressed(KEY_ENTER) || (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(0))) pressed = true;
}
else
{
if (active && IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_C))
{
int select = guiTextBoxState.select;
int cursor = guiTextBoxState.cursor;
int start = guiTextBoxState.start;
if ((guiTextBoxState.select == -1) || (guiTextBoxState.select == guiTextBoxState.cursor))
{
GuiTextBoxSelectAll(text);
}
GuiTextBoxCopy(text);
guiTextBoxState.select = select;
guiTextBoxState.cursor = cursor;
guiTextBoxState.start = start;
}
if (CheckCollisionPointRec(mousePoint, bounds))
{
state = GUI_STATE_FOCUSED;
if (IsMouseButtonPressed(0)) pressed = true;
}
}
if (pressed) framesCounter = 0;
}
DrawRectangleLinesEx(bounds, GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha));
if (state == GUI_STATE_PRESSED)
{
DrawRectangle(bounds.x + GuiGetStyle(TEXTBOX, BORDER_WIDTH), bounds.y + GuiGetStyle(TEXTBOX, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(TEXTBOX, BORDER_WIDTH), bounds.height - 2*GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BASE_COLOR_FOCUSED)), guiAlpha));
if (editMode && active && ((framesCounter/TEXTEDIT_CURSOR_BLINK_FRAMES)%2 == 0) && selLength == 0)
{
DrawRectangle(cursorPos.x, cursorPos.y, 1, GuiGetStyle(DEFAULT, TEXT_SIZE)*2, Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER_COLOR_PRESSED)), guiAlpha));
}
}
else if (state == GUI_STATE_DISABLED)
{
DrawRectangle(bounds.x + GuiGetStyle(TEXTBOX, BORDER_WIDTH), bounds.y + GuiGetStyle(TEXTBOX, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(TEXTBOX, BORDER_WIDTH), bounds.height - 2*GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BASE_COLOR_DISABLED)), guiAlpha));
}
DrawTextRecEx(guiFont, &text[textStartIndex], textRec, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING), false, Fade(GetColor(GuiGetStyle(TEXTBOX, TEXT + (state*3))), guiAlpha), selStart, selLength, GetColor(GuiGetStyle(TEXTBOX, COLOR_SELECTED_FG)), GetColor(GuiGetStyle(TEXTBOX, COLOR_SELECTED_BG)));
return pressed;
}
static int GetPrevCodepoint(const char *text, const char *start, int *prev)
{
int c = 0x3f;
char *p = (char *)text;
*prev = 1;
for (int i = 0; (p >= start) && (i < 4); p--, i++)
{
if ((((unsigned char)*p) >> 6) != 2)
{
c = GetNextCodepoint(p, prev);
break;
}
}
return c;
}
static inline unsigned int GuiCountCodepointsUntilNewline(const char *text)
{
unsigned int len = 0;
char *ptr = (char*)&text[0];
while ((*ptr != '\0') && (*ptr != '\n'))
{
int next = 0;
int letter = GetNextCodepoint(ptr, &next);
if (letter == 0x3f) ptr += 1;
else ptr += next;
++len;
}
return len;
}
static int GuiMeasureTextBox(const char *text, int length, Rectangle rec, int *pos, int mode)
{
const Font font = guiFont;
const float fontSize = GuiGetStyle(DEFAULT, TEXT_SIZE);
const float spacing = GuiGetStyle(DEFAULT, TEXT_SPACING);
int textOffsetX = 0; float scaleFactor = 0.0f;
int letter = 0; int index = 0;
scaleFactor = fontSize/font.baseSize;
int i = 0, k = 0;
int glyphWidth = 0;
for (i = 0; i < length; i++, k++)
{
glyphWidth = 0;
int next = 1;
letter = GetNextCodepoint(&text[i], &next);
if (letter == 0x3f) next = 1;
index = GetGlyphIndex(font, letter);
i += next - 1;
if (letter != '\n')
{
glyphWidth = (font.chars[index].advanceX == 0)?
(int)(font.recs[index].width*scaleFactor + spacing):
(int)(font.chars[index].advanceX*scaleFactor + spacing);
if ((textOffsetX + glyphWidth + 1) >= rec.width) break;
if ((mode == GUI_MEASURE_MODE_CURSOR_POS) && (*pos == k)) break;
else if (mode == GUI_MEASURE_MODE_CURSOR_COORDS)
{
Rectangle grec = {rec.x + textOffsetX - 1, rec.y, glyphWidth, (font.baseSize + font.baseSize/2)*scaleFactor - 1 };
Vector2 mouse = GetMousePosition();
if (CheckCollisionPointRec(mouse, grec))
{
if (mouse.x > (grec.x + glyphWidth/2))
{
textOffsetX += glyphWidth;
k++;
}
break;
}
}
}
else break;
textOffsetX += glyphWidth;
}
*pos = k;
return (rec.x + textOffsetX - 1);
}
static int GuiMeasureTextBoxRev(const char *text, int length, Rectangle rec, int *pos)
{
const Font font = guiFont;
const float fontSize = GuiGetStyle(DEFAULT, TEXT_SIZE);
const float spacing = GuiGetStyle(DEFAULT, TEXT_SPACING);
int textOffsetX = 0; float scaleFactor = 0.0f;
int letter = 0; int index = 0;
scaleFactor = fontSize/font.baseSize;
int i = 0, k = 0;
int glyphWidth = 0, prev = 1;
for (i = length; i >= 0; i--, k++)
{
glyphWidth = 0;
letter = GetPrevCodepoint(&text[i], &text[0], &prev);
if (letter == 0x3f) prev = 1;
index = GetGlyphIndex(font, letter);
i -= prev - 1;
if (letter != '\n')
{
glyphWidth = (font.chars[index].advanceX == 0)?
(int)(font.recs[index].width*scaleFactor + spacing):
(int)(font.chars[index].advanceX*scaleFactor + spacing);
if ((textOffsetX + glyphWidth + 1) >= rec.width) break;
}
else break;
textOffsetX += glyphWidth;
}
*pos = k;
return (i + prev);
}
static inline int GuiTextBoxGetCursorCoordinates(const char *text, int length, Rectangle rec, int pos)
{
return GuiMeasureTextBox(text, length, rec, &pos, GUI_MEASURE_MODE_CURSOR_POS);
}
static inline int GuiTextBoxGetCursorFromMouse(const char *text, int length, Rectangle rec, int* pos)
{
return GuiMeasureTextBox(text, length, rec, pos, GUI_MEASURE_MODE_CURSOR_COORDS);
}
static inline int GuiTextBoxMaxCharacters(const char *text, int length, Rectangle rec)
{
int pos = -1;
GuiMeasureTextBox(text, length, rec, &pos, GUI_MEASURE_MODE_CURSOR_END);
return pos;
}
static inline void MoveTextBoxCursorRight(const char* text, int length, Rectangle textRec)
{
int count = GuiCountCodepointsUntilNewline(text);
if (guiTextBoxState.cursor < count ) guiTextBoxState.cursor++;
const int max = GuiTextBoxMaxCharacters(&text[guiTextBoxState.index], length - guiTextBoxState.index, textRec);
if ((guiTextBoxState.cursor - guiTextBoxState.start) > max)
{
const int cidx = GuiTextBoxGetByteIndex(text, guiTextBoxState.index, guiTextBoxState.start, guiTextBoxState.cursor);
int pos = 0;
guiTextBoxState.index = GuiMeasureTextBoxRev(text, cidx - 1, textRec, &pos);
guiTextBoxState.start = guiTextBoxState.cursor - pos;
}
}
static inline void MoveTextBoxCursorLeft(const char* text)
{
if (guiTextBoxState.cursor > 0) guiTextBoxState.cursor--;
if (guiTextBoxState.cursor < guiTextBoxState.start)
{
int prev = 0;
int letter = GetPrevCodepoint(&text[guiTextBoxState.index - 1], text, &prev);
if (letter == 0x3f) prev = 1;
guiTextBoxState.start--;
guiTextBoxState.index -= prev;
}
}
static int EncodeCodepoint(unsigned int c, char out[5])
{
int len = 0;
if (c <= 0x7f)
{
out[0] = (char)c;
len = 1;
}
else if (c <= 0x7ff)
{
out[0] = (char)(((c >> 6) & 0x1f) | 0xc0);
out[1] = (char)((c & 0x3f) | 0x80);
len = 2;
}
else if (c <= 0xffff)
{
out[0] = (char)(((c >> 12) & 0x0f) | 0xe0);
out[1] = (char)(((c >> 6) & 0x3f) | 0x80);
out[2] = (char)((c & 0x3f) | 0x80);
len = 3;
}
else if (c <= 0x10ffff)
{
out[0] = (char)(((c >> 18) & 0x07) | 0xf0);
out[1] = (char)(((c >> 12) & 0x3f) | 0x80);
out[2] = (char)(((c >> 6) & 0x3f) | 0x80);
out[3] = (char)((c & 0x3f) | 0x80);
len = 4;
}
out[len] = 0;
return len;
}
#endif