mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
add CLineInput::SetRange
This commit is contained in:
parent
fa8f540108
commit
35a3d8c604
|
@ -1,5 +1,9 @@
|
|||
/* (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. */
|
||||
#include <base/math.h>
|
||||
#include <base/system.h>
|
||||
#include <base/tl/base.h>
|
||||
|
||||
#include "lineinput.h"
|
||||
#include <engine/keys.h>
|
||||
|
||||
|
@ -20,6 +24,40 @@ void CLineInput::Set(const char *pString)
|
|||
m_CursorPos = m_Len;
|
||||
}
|
||||
|
||||
void CLineInput::SetRange(const char *pString, int Begin, int End)
|
||||
{
|
||||
if(Begin > End)
|
||||
swap(Begin, End);
|
||||
Begin = clamp(Begin, 0, m_Len);
|
||||
End = clamp(End, 0, m_Len);
|
||||
|
||||
int RemovedCharSize, RemovedCharCount;
|
||||
str_utf8_stats(m_aStr + Begin, End - Begin + 1, MAX_CHARS, &RemovedCharSize, &RemovedCharCount);
|
||||
|
||||
int AddedCharSize, AddedCharCount;
|
||||
str_utf8_stats(pString, MAX_SIZE - m_Len + RemovedCharSize, MAX_CHARS - m_NumChars + RemovedCharCount, &AddedCharSize, &AddedCharCount);
|
||||
|
||||
if(RemovedCharSize || AddedCharSize)
|
||||
{
|
||||
if(AddedCharSize < RemovedCharSize)
|
||||
{
|
||||
if(AddedCharSize)
|
||||
mem_copy(m_aStr + Begin, pString, AddedCharSize);
|
||||
mem_move(m_aStr + Begin + AddedCharSize, m_aStr + Begin + RemovedCharSize, m_Len - Begin - AddedCharSize);
|
||||
}
|
||||
else if(AddedCharSize > RemovedCharSize)
|
||||
mem_move(m_aStr + End + AddedCharSize - RemovedCharSize, m_aStr + End, m_Len - End);
|
||||
|
||||
if(AddedCharSize >= RemovedCharSize)
|
||||
mem_copy(m_aStr + Begin, pString, AddedCharSize);
|
||||
|
||||
m_CursorPos = End - RemovedCharSize + AddedCharSize;
|
||||
m_Len += AddedCharSize - RemovedCharSize;
|
||||
m_NumChars += AddedCharCount - RemovedCharCount;
|
||||
m_aStr[m_Len] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void CLineInput::Editing(const char *pString, int Cursor)
|
||||
{
|
||||
str_copy(m_DisplayStr, m_aStr, sizeof(m_DisplayStr));
|
||||
|
|
|
@ -52,6 +52,7 @@ public:
|
|||
void ProcessInput(IInput::CEvent e);
|
||||
void Editing(const char *pString, int Cursor);
|
||||
void Set(const char *pString);
|
||||
void SetRange(const char *pString, int Begin, int End);
|
||||
void Add(const char *pString);
|
||||
const char *GetString(bool Editing = false) const { return Editing ? m_DisplayStr : m_aStr; }
|
||||
int GetLength(bool Editing = false) const { return Editing ? m_FakeLen : m_Len; }
|
||||
|
|
Loading…
Reference in a new issue