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"
#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)
{
m_aInputEvents[m_NumEvents].m_Unicode = Unicode;
m_aInputEvents[m_NumEvents].m_Key = Key;
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++;
}
}
@ -171,16 +174,7 @@ int CInput::Update()
{
case SDL_TEXTINPUT:
{
const char *text = Event.text.text;
while(*text)
{
int Code = str_utf8_decode(&text);
if(Code == -1)
break;
AddEvent(Code, 0, 0);
}
AddEvent(Event.text.text, 0, IInput::FLAG_TEXT);
break;
}
// handle keys

View file

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

View file

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

View file

@ -39,7 +39,7 @@ void CLineInput::Add(const char *pString)
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 CursorPos = *pCursorPosPtr;
@ -49,31 +49,41 @@ bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int St
if(CursorPos > Len)
CursorPos = Len;
int Code = e.m_Unicode;
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)
if(Event.m_Flags&IInput::FLAG_TEXT)
{
char Tmp[8];
int CharSize = str_utf8_encode(Tmp, Code);
if (Len < StrMaxSize - CharSize && CursorPos < StrMaxSize - CharSize && NumChars < StrMaxChars)
// gather string stats
int CharCount = 0;
int CharSize = 0;
while(Event.m_aText[CharSize])
{
mem_move(pStr + CursorPos + CharSize, pStr + CursorPos, Len-CursorPos+1); // +1 == null term
for(int i = 0; i < CharSize; i++)
pStr[CursorPos+i] = Tmp[i];
CursorPos += CharSize;
Len += CharSize;
if(CharSize > 0)
++NumChars;
Changes = true;
int NewCharSize = str_utf8_forward(Event.m_aText, CharSize);
if(NewCharSize != CharSize)
{
++CharCount;
CharSize = NewCharSize;
}
}
// 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 CharSize = CursorPos-NewCursorPos;
@ -84,7 +94,7 @@ bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int St
--NumChars;
Changes = true;
}
else if (k == KEY_DELETE && CursorPos < Len)
else if(Key == KEY_DELETE && CursorPos < Len)
{
int p = str_utf8_forward(pStr, CursorPos);
int CharSize = p-CursorPos;
@ -94,13 +104,13 @@ bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int St
--NumChars;
Changes = true;
}
else if (k == KEY_LEFT && CursorPos > 0)
else if(Key == KEY_LEFT && CursorPos > 0)
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);
else if (k == KEY_HOME)
else if(Key == KEY_HOME)
CursorPos = 0;
else if (k == KEY_END)
else if(Key == KEY_END)
CursorPos = Len;
}