fixed utf8 text input from tw master

This commit is contained in:
def 2016-04-30 04:02:32 +02:00
parent 502c5aa3cb
commit 74f4ad77d6
4 changed files with 45 additions and 40 deletions

View file

@ -17,13 +17,16 @@
#include "keynames.h" #include "keynames.h"
#undef KEYS_INCLUDE #undef KEYS_INCLUDE
void CInput::AddEvent(int Unicode, int Key, int Flags) void CInput::AddEvent(char *pText, int Key, int Flags)
{ {
if(m_NumEvents != INPUT_BUFFER_SIZE) if(m_NumEvents != INPUT_BUFFER_SIZE)
{ {
m_aInputEvents[m_NumEvents].m_Unicode = Unicode;
m_aInputEvents[m_NumEvents].m_Key = Key; m_aInputEvents[m_NumEvents].m_Key = Key;
m_aInputEvents[m_NumEvents].m_Flags = Flags; m_aInputEvents[m_NumEvents].m_Flags = Flags;
if(!pText)
m_aInputEvents[m_NumEvents].m_aText[0] = 0;
else
str_copy(m_aInputEvents[m_NumEvents].m_aText, pText, sizeof(m_aInputEvents[m_NumEvents].m_aText));
m_NumEvents++; m_NumEvents++;
} }
} }
@ -171,16 +174,7 @@ int CInput::Update()
{ {
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
{ {
const char *text = Event.text.text; AddEvent(Event.text.text, 0, IInput::FLAG_TEXT);
while(*text)
{
int Code = str_utf8_decode(&text);
if(Code == -1)
break;
AddEvent(Code, 0, 0);
}
break; break;
} }
// handle keys // handle keys

View file

@ -15,7 +15,7 @@ class CInput : public IEngineInput
int m_VideoRestartNeeded; int m_VideoRestartNeeded;
void AddEvent(int Unicode, int Key, int Flags); void AddEvent(char *pText, int Key, int Flags);
IEngineGraphics *Graphics() { return m_pGraphics; } IEngineGraphics *Graphics() { return m_pGraphics; }

View file

@ -15,8 +15,8 @@ public:
{ {
public: public:
int m_Flags; int m_Flags;
int m_Unicode;
int m_Key; int m_Key;
char m_aText[32];
}; };
protected: protected:
@ -45,7 +45,8 @@ public:
{ {
FLAG_PRESS=1, FLAG_PRESS=1,
FLAG_RELEASE=2, FLAG_RELEASE=2,
FLAG_REPEAT=4 FLAG_REPEAT=4,
FLAG_TEXT=8,
}; };
// events // events

View file

@ -39,7 +39,7 @@ void CLineInput::Add(const char *pString)
m_CursorPos = m_Len; m_CursorPos = m_Len;
} }
bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int StrMaxChars, int *pStrLenPtr, int *pCursorPosPtr, int *pNumCharsPtr) bool CLineInput::Manipulate(IInput::CEvent Event, char *pStr, int StrMaxSize, int StrMaxChars, int *pStrLenPtr, int *pCursorPosPtr, int *pNumCharsPtr)
{ {
int NumChars = *pNumCharsPtr; int NumChars = *pNumCharsPtr;
int CursorPos = *pCursorPosPtr; int CursorPos = *pCursorPosPtr;
@ -49,31 +49,41 @@ bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int St
if(CursorPos > Len) if(CursorPos > Len)
CursorPos = Len; CursorPos = Len;
int Code = e.m_Unicode; if(Event.m_Flags&IInput::FLAG_TEXT)
int k = e.m_Key;
// 127 is produced on Mac OS X and corresponds to the delete key
if (!(Code >= 0 && Code < 32) && Code != 127)
{ {
char Tmp[8]; // gather string stats
int CharSize = str_utf8_encode(Tmp, Code); int CharCount = 0;
int CharSize = 0;
if (Len < StrMaxSize - CharSize && CursorPos < StrMaxSize - CharSize && NumChars < StrMaxChars) while(Event.m_aText[CharSize])
{ {
mem_move(pStr + CursorPos + CharSize, pStr + CursorPos, Len-CursorPos+1); // +1 == null term int NewCharSize = str_utf8_forward(Event.m_aText, CharSize);
for(int i = 0; i < CharSize; i++) if(NewCharSize != CharSize)
pStr[CursorPos+i] = Tmp[i]; {
CursorPos += CharSize; ++CharCount;
Len += CharSize; CharSize = NewCharSize;
if(CharSize > 0) }
++NumChars; }
Changes = true;
// add new string
if(CharCount)
{
if(Len+CharSize < StrMaxSize && CursorPos+CharSize < StrMaxSize && NumChars+CharCount < StrMaxChars)
{
mem_move(pStr + CursorPos + CharSize, pStr + CursorPos, Len-CursorPos+1); // +1 == null term
for(int i = 0; i < CharSize; i++)
pStr[CursorPos+i] = Event.m_aText[i];
CursorPos += CharSize;
Len += CharSize;
NumChars += CharCount;
Changes = true;
}
} }
} }
if(e.m_Flags&IInput::FLAG_PRESS) if(Event.m_Flags&IInput::FLAG_PRESS)
{ {
if (k == KEY_BACKSPACE && CursorPos > 0) int Key = Event.m_Key;
if(Key == KEY_BACKSPACE && CursorPos > 0)
{ {
int NewCursorPos = str_utf8_rewind(pStr, CursorPos); int NewCursorPos = str_utf8_rewind(pStr, CursorPos);
int CharSize = CursorPos-NewCursorPos; int CharSize = CursorPos-NewCursorPos;
@ -84,7 +94,7 @@ bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int St
--NumChars; --NumChars;
Changes = true; Changes = true;
} }
else if (k == KEY_DELETE && CursorPos < Len) else if(Key == KEY_DELETE && CursorPos < Len)
{ {
int p = str_utf8_forward(pStr, CursorPos); int p = str_utf8_forward(pStr, CursorPos);
int CharSize = p-CursorPos; int CharSize = p-CursorPos;
@ -94,13 +104,13 @@ bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int St
--NumChars; --NumChars;
Changes = true; Changes = true;
} }
else if (k == KEY_LEFT && CursorPos > 0) else if(Key == KEY_LEFT && CursorPos > 0)
CursorPos = str_utf8_rewind(pStr, CursorPos); CursorPos = str_utf8_rewind(pStr, CursorPos);
else if (k == KEY_RIGHT && CursorPos < Len) else if(Key == KEY_RIGHT && CursorPos < Len)
CursorPos = str_utf8_forward(pStr, CursorPos); CursorPos = str_utf8_forward(pStr, CursorPos);
else if (k == KEY_HOME) else if(Key == KEY_HOME)
CursorPos = 0; CursorPos = 0;
else if (k == KEY_END) else if(Key == KEY_END)
CursorPos = Len; CursorPos = Len;
} }