ddnet/src/game/client/lineinput.cpp

109 lines
2.7 KiB
C++
Raw Normal View History

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"
2010-05-29 07:25:38 +00:00
CLineInput::CLineInput()
{
2010-05-29 07:25:38 +00:00
Clear();
}
2010-05-29 07:25:38 +00:00
void CLineInput::Clear()
{
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;
}
2010-05-29 07:25:38 +00:00
void CLineInput::Set(const char *pString)
{
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;
}
}
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)
{
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;
2010-05-29 07:25:38 +00:00
if(CursorPos > Len)
CursorPos = Len;
2010-05-29 07:25:38 +00:00
int Code = e.m_Unicode;
int k = e.m_Key;
// 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)
{
2010-05-29 07:25:38 +00:00
char Tmp[8];
int CharSize = str_utf8_encode(Tmp, Code);
2014-03-28 19:04:35 +00:00
if (Len < StrMaxChars - CharSize && CursorPos < StrMaxSize - CharSize - Len && NumChars < StrMaxChars)
{
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;
}
}
2010-05-29 07:25:38 +00:00
if(e.m_Flags&IInput::FLAG_PRESS)
{
2010-05-29 07:25:38 +00:00
if (k == KEY_BACKSPACE && CursorPos > 0)
{
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;
}
2010-05-29 07:25:38 +00:00
else if (k == KEY_DELETE && CursorPos < Len)
{
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;
}
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);
else if (k == KEY_HOME)
2010-05-29 07:25:38 +00:00
CursorPos = 0;
else if (k == KEY_END)
2010-05-29 07:25:38 +00:00
CursorPos = Len;
}
2013-04-01 18:30:58 +00:00
*pNumCharsPtr = NumChars;
2010-05-29 07:25:38 +00:00
*pCursorPosPtr = CursorPos;
*pStrLenPtr = Len;
return Changes;
}
2010-05-29 07:25:38 +00:00
void CLineInput::ProcessInput(IInput::CEvent e)
{
2013-04-01 18:30:58 +00:00
Manipulate(e, m_Str, MAX_SIZE, MAX_CHARS, &m_Len, &m_CursorPos, &m_NumChars);
}