Update DoEditBox logic to be the same as chat

This commit is contained in:
TsFreddie 2020-10-26 01:44:10 +08:00
parent f7ac41adeb
commit cc8fe178b5
5 changed files with 40 additions and 59 deletions

View file

@ -248,7 +248,7 @@ int CInput::Update()
break; break;
} }
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
m_EditingTextLen = 0; m_EditingTextLen = -1;
AddEvent(Event.text.text, 0, IInput::FLAG_TEXT); AddEvent(Event.text.text, 0, IInput::FLAG_TEXT);
break; break;
// handle keys // handle keys

View file

@ -62,6 +62,7 @@ public:
virtual bool GetIMEState(); virtual bool GetIMEState();
virtual void SetIMEState(bool Activate); virtual void SetIMEState(bool Activate);
int GetIMEEditingTextLength() const { return m_EditingTextLen; }
virtual const char *GetIMEEditingText(); virtual const char *GetIMEEditingText();
virtual int GetEditingCursor(); virtual int GetEditingCursor();
virtual void SetEditingPosition(float X, float Y); virtual void SetEditingPosition(float X, float Y);

View file

@ -75,6 +75,7 @@ public:
virtual bool GetIMEState() = 0; virtual bool GetIMEState() = 0;
virtual void SetIMEState(bool Activate) = 0; virtual void SetIMEState(bool Activate) = 0;
virtual int GetIMEEditingTextLength() const = 0;
virtual const char *GetIMEEditingText() = 0; virtual const char *GetIMEEditingText() = 0;
virtual int GetEditingCursor() = 0; virtual int GetEditingCursor() = 0;
virtual void SetEditingPosition(float X, float Y) = 0; virtual void SetEditingPosition(float X, float Y) = 0;

View file

@ -422,12 +422,6 @@ int CMenus::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrS
const char *pDisplayStr = pStr; const char *pDisplayStr = pStr;
char aStars[128]; char aStars[128];
if(pDisplayStr[0] == '\0')
{
pDisplayStr = pEmptyText;
TextRender()->TextColor(1, 1, 1, 0.75f);
}
if(Hidden) if(Hidden)
{ {
unsigned s = str_length(pDisplayStr); unsigned s = str_length(pDisplayStr);
@ -439,39 +433,48 @@ int CMenus::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrS
pDisplayStr = aStars; pDisplayStr = aStars;
} }
char aInputing[IInput::INPUT_TEXT_SIZE] = {0}; char aDispEditingText[128 + IInput::INPUT_TEXT_SIZE + 2] = {0};
if(UI()->HotItem() == pID && Input()->GetIMEState()) int DispCursorPos = s_AtIndex;
if(UI()->LastActiveItem() == pID && Input()->GetIMEEditingTextLength() > -1)
{ {
str_copy(aInputing, pStr, sizeof(aInputing)); int EditingTextCursor = Input()->GetEditingCursor();
const char *Text = Input()->GetIMEEditingText(); str_copy(aDispEditingText, pDisplayStr, sizeof(aDispEditingText));
if(str_length(Text)) char aEditingText[IInput::INPUT_TEXT_SIZE + 2];
if(Hidden)
{ {
int NewTextLen = str_length(Text); // Do not show editing text in password field
int CharsLeft = StrSize - str_length(aInputing) - 1; str_copy(aEditingText, "[*]", sizeof(aEditingText));
int FillCharLen = minimum(NewTextLen, CharsLeft); EditingTextCursor = 1;
//Push Char Backward
for(int i = str_length(aInputing); i >= s_AtIndex; i--)
aInputing[i + FillCharLen] = aInputing[i];
for(int i = 0; i < FillCharLen; i++)
{
if(Text[i] == '\n')
aInputing[s_AtIndex + i] = ' ';
else
aInputing[s_AtIndex + i] = Text[i];
}
//s_AtIndex = s_AtIndex+FillCharLen;
pDisplayStr = aInputing;
} }
else
{
str_format(aEditingText, sizeof(aEditingText), "[%s]", Input()->GetIMEEditingText());
}
int NewTextLen = str_length(aEditingText);
int CharsLeft = (int)sizeof(aDispEditingText) - str_length(aDispEditingText) - 1;
int FillCharLen = minimum(NewTextLen, CharsLeft);
for(int i = str_length(aDispEditingText) - 1; i >= s_AtIndex; i--)
aDispEditingText[i + FillCharLen] = aDispEditingText[i];
for(int i = 0; i < FillCharLen; i++)
aDispEditingText[s_AtIndex + i] = aEditingText[i];
DispCursorPos = s_AtIndex + EditingTextCursor + 1;
pDisplayStr = aDispEditingText;
}
if(pDisplayStr[0] == '\0')
{
pDisplayStr = pEmptyText;
TextRender()->TextColor(1, 1, 1, 0.75f);
} }
// check if the text has to be moved // check if the text has to be moved
if(UI()->LastActiveItem() == pID && !JustGotActive && (UpdateOffset || m_NumInputEvents)) if(UI()->LastActiveItem() == pID && !JustGotActive && (UpdateOffset || m_NumInputEvents))
{ {
float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, s_AtIndex, -1.0f); float w = TextRender()->TextWidth(0, FontSize, pStr, s_AtIndex, -1.0f);
if(w - *Offset > Textbox.w) if(w - *Offset > Textbox.w)
{ {
// move to the left // move to the left
float wt = TextRender()->TextWidth(0, FontSize, pDisplayStr, -1, -1.0f); float wt = TextRender()->TextWidth(0, FontSize, pStr, -1, -1.0f);
do do
{ {
*Offset += minimum(wt - *Offset - Textbox.w, Textbox.w / 3); *Offset += minimum(wt - *Offset - Textbox.w, Textbox.w / 3);
@ -502,29 +505,10 @@ int CMenus::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrS
if(UI()->LastActiveItem() == pID && !JustGotActive) if(UI()->LastActiveItem() == pID && !JustGotActive)
{ {
float OffsetGlyph = TextRender()->GetGlyphOffsetX(FontSize, '|'); float OffsetGlyph = TextRender()->GetGlyphOffsetX(FontSize, '|');
CUIRect TmpTextBox = Textbox;
if(str_length(aInputing))
{
float OffsetGlyphThis = OffsetGlyph;
if(StrLenDispl >= 1 && StrLenDispl == s_AtIndex + Input()->GetEditingCursor() && pDisplayStr[StrLenDispl - 1] != ' ')
OffsetGlyphThis = 0;
float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, s_AtIndex + Input()->GetEditingCursor(), -1.0f);
Textbox.x += w + OffsetGlyphThis;
Graphics()->TextureClear(); if(StrLenDispl >= 1 && StrLenDispl == DispCursorPos && pDisplayStr[StrLenDispl - 1] != ' ')
Graphics()->QuadsBegin();
Graphics()->SetColor(0, 0, 0, 0.3f);
IGraphics::CQuadItem CursorTBack(Textbox.x - (OnePixelWidth * 2.0f) / 2.0f, Textbox.y, OnePixelWidth * 2 * 2.0f, Textbox.h);
Graphics()->QuadsDrawTL(&CursorTBack, 1);
Graphics()->SetColor(1, 1, 1, 1);
IGraphics::CQuadItem CursorT(Textbox.x, Textbox.y + OnePixelWidth * 1.5f, OnePixelWidth * 2.0f, Textbox.h - OnePixelWidth * 1.5f * 2);
Graphics()->QuadsDrawTL(&CursorT, 1);
Graphics()->QuadsEnd();
}
if(StrLenDispl >= 1 && StrLenDispl == s_AtIndex && pDisplayStr[StrLenDispl - 1] != ' ')
OffsetGlyph = 0; OffsetGlyph = 0;
float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, s_AtIndex, -1.0f); float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, DispCursorPos, -1.0f);
Textbox = TmpTextBox;
Textbox.x += w + OffsetGlyph; Textbox.x += w + OffsetGlyph;
if((2 * time_get() / time_freq()) % 2) if((2 * time_get() / time_freq()) % 2)

View file

@ -33,20 +33,15 @@ void CLineInput::Set(const char *pString)
void CLineInput::Editing(const char *pString, int Cursor) void CLineInput::Editing(const char *pString, int Cursor)
{ {
str_copy(m_DisplayStr, m_Str, sizeof(m_DisplayStr)); str_copy(m_DisplayStr, m_Str, sizeof(m_DisplayStr));
char EditingText[IInput::INPUT_TEXT_SIZE + 2]; char aEditingText[IInput::INPUT_TEXT_SIZE + 2];
str_format(EditingText, sizeof(EditingText), "[%s]", pString); str_format(aEditingText, sizeof(aEditingText), "[%s]", pString);
int NewTextLen = str_length(EditingText); int NewTextLen = str_length(aEditingText);
int CharsLeft = (int)sizeof(m_DisplayStr) - str_length(m_DisplayStr) - 1; int CharsLeft = (int)sizeof(m_DisplayStr) - str_length(m_DisplayStr) - 1;
int FillCharLen = NewTextLen < CharsLeft ? NewTextLen : CharsLeft; int FillCharLen = NewTextLen < CharsLeft ? NewTextLen : CharsLeft;
for(int i = str_length(m_DisplayStr) - 1; i >= m_CursorPos; i--) for(int i = str_length(m_DisplayStr) - 1; i >= m_CursorPos; i--)
m_DisplayStr[i + FillCharLen] = m_DisplayStr[i]; m_DisplayStr[i + FillCharLen] = m_DisplayStr[i];
for(int i = 0; i < FillCharLen; i++) for(int i = 0; i < FillCharLen; i++)
{ m_DisplayStr[m_CursorPos + i] = aEditingText[i];
if(EditingText[i] == 28)
m_DisplayStr[m_CursorPos + i] = ' ';
else
m_DisplayStr[m_CursorPos + i] = EditingText[i];
}
m_FakeLen = str_length(m_DisplayStr); m_FakeLen = str_length(m_DisplayStr);
m_FakeCursorPos = m_CursorPos + Cursor + 1; m_FakeCursorPos = m_CursorPos + Cursor + 1;
} }