ddnet/src/game/client/lineinput.cpp

149 lines
3.8 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;
}
}
void CLineInput::Editing(const char *pString, int Cursor)
{
str_copy(m_DisplayStr, m_Str, sizeof(m_DisplayStr));
char Texting[34];
str_format(Texting, sizeof(Texting), "[%s]", pString);
int NewTextLen = str_length(Texting);
int CharsLeft = (int)sizeof(m_DisplayStr) - str_length(m_DisplayStr) - 1;
int FillCharLen = NewTextLen < CharsLeft ? NewTextLen : CharsLeft;
for(int i = str_length(m_DisplayStr) - 1; i >= m_CursorPos ; i--)
m_DisplayStr[i+FillCharLen] = m_DisplayStr[i];
for(int i = 0; i < FillCharLen; i++)
{
if(Texting[i] == 28)
m_DisplayStr[m_CursorPos + i] = ' ';
else
m_DisplayStr[m_CursorPos + i] = Texting[i];
}
m_FakeLen = str_length(m_DisplayStr);
2017-02-21 16:10:08 +00:00
m_FakeCursorPos = m_CursorPos + Cursor + 1;
}
void CLineInput::Add(const char *pString)
{
if((int)sizeof(m_Str) - m_Len <= (int)str_length(pString))
return;
str_copy(m_Str + m_Len, pString, sizeof(m_Str) - m_Len);
m_Len = str_length(m_Str);
m_CursorPos = m_Len;
}
2016-04-30 02:02:32 +00:00
bool CLineInput::Manipulate(IInput::CEvent Event, 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;
2016-04-30 02:02:32 +00:00
if(Event.m_Flags&IInput::FLAG_TEXT)
{
2016-04-30 02:02:32 +00:00
// gather string stats
int CharCount = 0;
int CharSize = 0;
while(Event.m_aText[CharSize])
{
int NewCharSize = str_utf8_forward(Event.m_aText, CharSize);
if(NewCharSize != CharSize)
{
++CharCount;
CharSize = NewCharSize;
}
}
2016-04-30 02:02:32 +00:00
// add new string
if(CharCount)
{
2016-04-30 02:02:32 +00:00
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;
}
}
}
2016-04-30 02:02:32 +00:00
if(Event.m_Flags&IInput::FLAG_PRESS)
{
2016-04-30 02:02:32 +00:00
int Key = Event.m_Key;
if(Key == 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;
}
2016-04-30 02:02:32 +00:00
else if(Key == 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;
}
2016-04-30 02:02:32 +00:00
else if(Key == KEY_LEFT && CursorPos > 0)
2010-05-29 07:25:38 +00:00
CursorPos = str_utf8_rewind(pStr, CursorPos);
2016-04-30 02:02:32 +00:00
else if(Key == KEY_RIGHT && CursorPos < Len)
2010-05-29 07:25:38 +00:00
CursorPos = str_utf8_forward(pStr, CursorPos);
2016-04-30 02:02:32 +00:00
else if(Key == KEY_HOME)
2010-05-29 07:25:38 +00:00
CursorPos = 0;
2016-04-30 02:02:32 +00:00
else if(Key == 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);
}