add CLineInput::SetRange

This commit is contained in:
Robert Müller 2021-11-25 00:00:20 +01:00
parent fa8f540108
commit 35a3d8c604
2 changed files with 39 additions and 0 deletions

View file

@ -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));

View file

@ -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; }