diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index bc09d25f1..e0d302ae5 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -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 diff --git a/src/engine/client/input.h b/src/engine/client/input.h index d3f16bbe5..61cd9ed6f 100644 --- a/src/engine/client/input.h +++ b/src/engine/client/input.h @@ -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; } diff --git a/src/engine/input.h b/src/engine/input.h index f712434f0..999ccc4e4 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -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 diff --git a/src/game/client/lineinput.cpp b/src/game/client/lineinput.cpp index 38a5f6fb9..fea6ad250 100644 --- a/src/game/client/lineinput.cpp +++ b/src/game/client/lineinput.cpp @@ -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; }