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;
|
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;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
bool CLineInput::Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int *pStrLenPtr, int *pCursorPosPtr)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
int CursorPos = *pCursorPosPtr;
|
|
|
|
int Len = *pStrLenPtr;
|
|
|
|
bool Changes = false;
|
2009-06-13 08:22:37 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(CursorPos > Len)
|
|
|
|
CursorPos = Len;
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Code = e.m_Unicode;
|
|
|
|
int k = e.m_Key;
|
2008-08-27 15:48:50 +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);
|
2009-06-13 08:22:37 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if (Len < StrMaxSize - CharSize && CursorPos < StrMaxSize - CharSize)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_move(pStr + CursorPos + CharSize, pStr + CursorPos, Len - CursorPos + CharSize);
|
|
|
|
for(int i = 0; i < CharSize; i++)
|
|
|
|
pStr[CursorPos+i] = Tmp[i];
|
|
|
|
CursorPos += CharSize;
|
|
|
|
Len += CharSize;
|
|
|
|
Changes = true;
|
2008-08-27 15:48:50 +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;
|
|
|
|
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;
|
|
|
|
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
|
|
|
}
|
2009-06-13 08:22:37 +00:00
|
|
|
|
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
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
Manipulate(e, m_Str, sizeof(m_Str), &m_Len, &m_CursorPos);
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|