mirror of
https://github.com/raysan5/raygui.git
synced 2026-02-04 21:29:18 -05:00
GuiTextBox(): Support Unicode inputs #99
This commit is contained in:
53
src/raygui.h
53
src/raygui.h
@ -1414,7 +1414,7 @@ bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
state = GUI_STATE_PRESSED;
|
state = GUI_STATE_PRESSED;
|
||||||
framesCounter++;
|
framesCounter++;
|
||||||
|
|
||||||
int key = GetKeyPressed();
|
int key = GetKeyPressed(); // Returns codepoint as Unicode
|
||||||
int keyCount = strlen(text);
|
int keyCount = strlen(text);
|
||||||
|
|
||||||
// Only allow keys in range [32..125]
|
// Only allow keys in range [32..125]
|
||||||
@ -1422,15 +1422,18 @@ bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
{
|
{
|
||||||
int maxWidth = (bounds.width - (GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)*2));
|
int maxWidth = (bounds.width - (GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)*2));
|
||||||
|
|
||||||
if (GetTextWidth(text) < (maxWidth - GuiGetStyle(DEFAULT, TEXT_SIZE)))
|
if ((GetTextWidth(text) < (maxWidth - GuiGetStyle(DEFAULT, TEXT_SIZE))) && (key >= 32))
|
||||||
{
|
{
|
||||||
if (((key >= 32) && (key <= 125)) ||
|
int byteLength = 0;
|
||||||
((key >= 128) && (key < 255)))
|
const char *textUtf8 = CodepointToUtf8(key, &byteLength);
|
||||||
|
|
||||||
|
for (int i = 0; i < byteLength; i++)
|
||||||
{
|
{
|
||||||
text[keyCount] = (char)key;
|
text[keyCount] = textUtf8[i];
|
||||||
keyCount++;
|
keyCount++;
|
||||||
text[keyCount] = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
text[keyCount] = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3672,6 +3675,44 @@ static int TextToInteger(const char *text)
|
|||||||
|
|
||||||
return value*sign;
|
return value*sign;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode codepoint into utf8 text (char array length returned as parameter)
|
||||||
|
static const char *CodepointToUtf8(int codepoint, int *byteLength)
|
||||||
|
{
|
||||||
|
static char utf8[6] = { 0 };
|
||||||
|
int length = 0;
|
||||||
|
|
||||||
|
if (codepoint <= 0x7f)
|
||||||
|
{
|
||||||
|
utf8[0] = (char)codepoint;
|
||||||
|
length = 1;
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0x7ff)
|
||||||
|
{
|
||||||
|
utf8[0] = (char)(((codepoint >> 6) & 0x1f) | 0xc0);
|
||||||
|
utf8[1] = (char)((codepoint & 0x3f) | 0x80);
|
||||||
|
length = 2;
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0xffff)
|
||||||
|
{
|
||||||
|
utf8[0] = (char)(((codepoint >> 12) & 0x0f) | 0xe0);
|
||||||
|
utf8[1] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
|
||||||
|
utf8[2] = (char)((codepoint & 0x3f) | 0x80);
|
||||||
|
length = 3;
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0x10ffff)
|
||||||
|
{
|
||||||
|
utf8[0] = (char)(((codepoint >> 18) & 0x07) | 0xf0);
|
||||||
|
utf8[1] = (char)(((codepoint >> 12) & 0x3f) | 0x80);
|
||||||
|
utf8[2] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
|
||||||
|
utf8[3] = (char)((codepoint & 0x3f) | 0x80);
|
||||||
|
length = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
*byteLength = length;
|
||||||
|
|
||||||
|
return utf8;
|
||||||
|
}
|
||||||
#endif // RAYGUI_STANDALONE
|
#endif // RAYGUI_STANDALONE
|
||||||
|
|
||||||
#endif // RAYGUI_IMPLEMENTATION
|
#endif // RAYGUI_IMPLEMENTATION
|
||||||
|
|||||||
Reference in New Issue
Block a user