2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/keys.h>
|
|
|
|
#include "lineinput.h"
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CLineInput::CLineInput()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
Clear();
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLineInput::Clear()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_zero(m_Str, sizeof(m_Str));
|
|
|
|
m_Len = 0;
|
|
|
|
m_CursorPos = 0;
|
2013-04-01 18:30:58 +00:00
|
|
|
m_NumChars = 0;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLineInput::Set(const char *pString)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
str_copy(m_Str, pString, sizeof(m_Str));
|
|
|
|
m_Len = str_length(m_Str);
|
|
|
|
m_CursorPos = m_Len;
|
2013-04-01 18:30:58 +00:00
|
|
|
m_NumChars = 0;
|
|
|
|
int Offset = 0;
|
|
|
|
while(pString[Offset])
|
|
|
|
{
|
|
|
|
Offset = str_utf8_forward(pString, Offset);
|
|
|
|
++m_NumChars;
|
|
|
|
}
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 18:30:58 +00:00
|
|
|
bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int StrMaxChars, int *pStrLenPtr, int *pCursorPosPtr, int *pNumCharsPtr)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2013-04-01 18:30:58 +00:00
|
|
|
int NumChars = *pNumCharsPtr;
|
2010-05-29 07:25:38 +00:00
|
|
|
int CursorPos = *pCursorPosPtr;
|
|
|
|
int Len = *pStrLenPtr;
|
|
|
|
bool Changes = false;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(CursorPos > Len)
|
|
|
|
CursorPos = Len;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Code = e.m_Unicode;
|
|
|
|
int k = e.m_Key;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-12-30 15:52:26 +00:00
|
|
|
// 127 is produced on Mac OS X and corresponds to the delete key
|
2010-05-29 07:25:38 +00:00
|
|
|
if (!(Code >= 0 && Code < 32) && Code != 127)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
char Tmp[8];
|
|
|
|
int CharSize = str_utf8_encode(Tmp, Code);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2014-03-28 19:04:35 +00:00
|
|
|
if (Len < StrMaxChars - CharSize && CursorPos < StrMaxSize - CharSize - Len && NumChars < StrMaxChars)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2011-07-28 20:01:14 +00:00
|
|
|
mem_move(pStr + CursorPos + CharSize, pStr + CursorPos, Len-CursorPos+1); // +1 == null term
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < CharSize; i++)
|
|
|
|
pStr[CursorPos+i] = Tmp[i];
|
|
|
|
CursorPos += CharSize;
|
|
|
|
Len += CharSize;
|
2013-04-01 18:30:58 +00:00
|
|
|
if(CharSize > 0)
|
|
|
|
++NumChars;
|
2010-05-29 07:25:38 +00:00
|
|
|
Changes = true;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(e.m_Flags&IInput::FLAG_PRESS)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if (k == KEY_BACKSPACE && CursorPos > 0)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
int NewCursorPos = str_utf8_rewind(pStr, CursorPos);
|
|
|
|
int CharSize = CursorPos-NewCursorPos;
|
|
|
|
mem_move(pStr+NewCursorPos, pStr+CursorPos, Len - NewCursorPos - CharSize + 1); // +1 == null term
|
|
|
|
CursorPos = NewCursorPos;
|
|
|
|
Len -= CharSize;
|
2013-04-01 18:30:58 +00:00
|
|
|
if(CharSize > 0)
|
|
|
|
--NumChars;
|
2010-05-29 07:25:38 +00:00
|
|
|
Changes = true;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
else if (k == KEY_DELETE && CursorPos < Len)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
int p = str_utf8_forward(pStr, CursorPos);
|
|
|
|
int CharSize = p-CursorPos;
|
|
|
|
mem_move(pStr + CursorPos, pStr + CursorPos + CharSize, Len - CursorPos - CharSize + 1); // +1 == null term
|
|
|
|
Len -= CharSize;
|
2013-04-01 18:30:58 +00:00
|
|
|
if(CharSize > 0)
|
|
|
|
--NumChars;
|
2010-05-29 07:25:38 +00:00
|
|
|
Changes = true;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
else if (k == KEY_LEFT && CursorPos > 0)
|
|
|
|
CursorPos = str_utf8_rewind(pStr, CursorPos);
|
|
|
|
else if (k == KEY_RIGHT && CursorPos < Len)
|
|
|
|
CursorPos = str_utf8_forward(pStr, CursorPos);
|
2008-08-27 15:48:50 +00:00
|
|
|
else if (k == KEY_HOME)
|
2010-05-29 07:25:38 +00:00
|
|
|
CursorPos = 0;
|
2008-08-27 15:48:50 +00:00
|
|
|
else if (k == KEY_END)
|
2010-05-29 07:25:38 +00:00
|
|
|
CursorPos = Len;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2013-04-01 18:30:58 +00:00
|
|
|
*pNumCharsPtr = NumChars;
|
2010-05-29 07:25:38 +00:00
|
|
|
*pCursorPosPtr = CursorPos;
|
|
|
|
*pStrLenPtr = Len;
|
|
|
|
|
|
|
|
return Changes;
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLineInput::ProcessInput(IInput::CEvent e)
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2013-04-01 18:30:58 +00:00
|
|
|
Manipulate(e, m_Str, MAX_SIZE, MAX_CHARS, &m_Len, &m_CursorPos, &m_NumChars);
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|