227 lines
4.0 KiB
C++
227 lines
4.0 KiB
C++
#include "dl_ui_edit_box.h"
|
|
|
|
#ifndef DL_DISABLE_RENDER
|
|
#include "ui/dl_ui_manager.h"
|
|
|
|
namespace dl::UI
|
|
{
|
|
|
|
void EditBox::_init()
|
|
{
|
|
_colTip = ColorDef::GRAY;
|
|
_strTip = "在这里输入内容……";
|
|
|
|
_colCursor = ColorDef::GREEN;
|
|
_colSelect = ColorDef::BLUE;
|
|
|
|
_timerCursor.Set(0.5, true);
|
|
|
|
_panel->SetAutoRun(false);
|
|
_panel->SetAutoCoor(false);
|
|
|
|
_txt = g_factory->CreateTextDefault();
|
|
_txt->GetCoor()->SetParent(_panel->GetCoor());
|
|
|
|
_sprCursor = g_factory->CreateSpriteDefault(_colCursor);
|
|
_sprCursor->SetQuad(createQuad(toPosition(1.0f, _txt->GetFontSize()), false));
|
|
_sprCursor->AddCoor()->SetParent(_panel->GetCoor());
|
|
|
|
|
|
SetOrder(Z_DEFAULT_UI);
|
|
}
|
|
|
|
void EditBox::SetFocus(bool focus)
|
|
{
|
|
g_ui->SetFocusEditBox(this, focus);
|
|
}
|
|
|
|
void EditBox::_on_char(char32_t ch)
|
|
{
|
|
log_test0(CodeCvt::Utf32ToUtf8(std::u32string{ ch }));
|
|
|
|
_str.insert(_iCursor, 1, ch);
|
|
++_iCursor;
|
|
_bChange = true;
|
|
|
|
_refresh_txt();
|
|
_refresh_cursor();
|
|
_refresh_select();
|
|
}
|
|
|
|
void EditBox::_on_backspace()
|
|
{
|
|
if (_str.empty())
|
|
return;
|
|
if (_iBeg == _iEnd)
|
|
{
|
|
if (_iCursor > 0)
|
|
{
|
|
_str.erase(_iCursor - 1, 1);
|
|
--_iCursor;
|
|
_bChange = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_delete_select();
|
|
}
|
|
|
|
_refresh_txt();
|
|
_refresh_cursor();
|
|
_refresh_select();
|
|
}
|
|
|
|
void EditBox::_on_string(std::string_view str)
|
|
{
|
|
log_test0(str);
|
|
|
|
std::u32string s = CodeCvt::Utf8ToUtf32(String::CvtLineBreak(str));
|
|
_str.insert(_str.begin() + _iCursor, s.begin(), s.end());
|
|
_iCursor += s.size();
|
|
_bChange = true;
|
|
|
|
_refresh_txt();
|
|
_refresh_cursor();
|
|
_refresh_select();
|
|
}
|
|
|
|
bool EditBox::IsFocus()
|
|
{
|
|
return g_ui->GetFocusEditBox() == this;
|
|
}
|
|
|
|
void EditBox::SetSingleLine(bool single_line)
|
|
{
|
|
if (_bSingleLine == single_line)
|
|
return;
|
|
_bSingleLine = single_line;
|
|
if (single_line)
|
|
{
|
|
if (Any::Equal(_str, '\n'))
|
|
{
|
|
log_warn0("多行输入框变为单行,存在换行符!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_txt->SetMaxW(_panel->GetSize()[0]);
|
|
}
|
|
}
|
|
|
|
void EditBox::_refresh_txt(bool* focus)
|
|
{
|
|
bool f = focus ? *focus : IsFocus();
|
|
if (_str.empty() && !f)
|
|
{
|
|
_txt->SetColor(_colTip);
|
|
_txt->SetString(_strTip);
|
|
return;
|
|
}
|
|
_txt->SetColor(f ? _col[2] : _col[0]);
|
|
_txt->SetString(CodeCvt::Utf32ToUtf8(_str));
|
|
}
|
|
|
|
void EditBox::_refresh_cursor()
|
|
{
|
|
_limit_range();
|
|
|
|
Position pos = Cursor2Pos(_iCursor);
|
|
//RoundVec2(pos);
|
|
_sprCursor->GetCoor()->SetPosition(pos);
|
|
}
|
|
|
|
void EditBox::_refresh_select()
|
|
{
|
|
_limit_range();
|
|
//CCLOG("%d, %d", _iBeg, _iEnd);
|
|
_vecSprSelect.clear();
|
|
size_t i_min = std::min(_iBeg, _iEnd);
|
|
size_t i_max = std::max(_iBeg, _iEnd);
|
|
|
|
Position p0 = Cursor2Pos(i_min);
|
|
for (size_t i = i_min; i < i_max; ++i)
|
|
{
|
|
// 遇到换行
|
|
if (_str[i] == '\n')
|
|
{
|
|
Position p1 = Cursor2Pos(i);
|
|
//RoundVec2(p0);
|
|
//RoundVec2(p1);
|
|
gcSprite spr = g_factory->CreateSpriteDefault(_colSelect);
|
|
spr->SetQuad(createQuad(p0, { p1[0], p1[1] + _txt->GetFontSize() + 2.0f }));
|
|
_vecSprSelect.push_back(spr);
|
|
p0 = Cursor2Pos(i + 1);
|
|
}
|
|
}
|
|
Position p1 = Cursor2Pos(i_max);
|
|
//RoundVec2(p0);
|
|
//RoundVec2(p1);
|
|
gcSprite spr = g_factory->CreateSpriteDefault(_colSelect);
|
|
spr->SetQuad(createQuad(p0, { p1[0], p1[1] + _txt->GetFontSize() + 2.0f }));
|
|
_vecSprSelect.push_back(spr);
|
|
}
|
|
|
|
void EditBox::_limit_range()
|
|
{
|
|
if (_str.empty())
|
|
{
|
|
_iCursor = 0;
|
|
_iBeg = 0;
|
|
_iEnd = 0;
|
|
return;
|
|
}
|
|
if (_iCursor > _str.size())
|
|
_iCursor = _str.size();
|
|
if (_iBeg > _str.size())
|
|
_iBeg = _str.size();
|
|
if (_iEnd > _str.size())
|
|
_iEnd = _str.size();
|
|
}
|
|
|
|
void EditBox::_delete_select()
|
|
{
|
|
if (_iBeg == _iEnd)
|
|
return;
|
|
size_t i_min = std::min(_iBeg, _iEnd);
|
|
size_t i_max = std::max(_iBeg, _iEnd);
|
|
_str.erase(_str.begin() + i_min, _str.begin() + i_max);
|
|
_iCursor = i_min;
|
|
_bChange = true;
|
|
_iBeg = _iCursor;
|
|
_iEnd = _iCursor;
|
|
}
|
|
|
|
void EditBox::_on_focus(bool focus)
|
|
{
|
|
_panel->SetSelectAll(focus);
|
|
_refresh_txt(&focus);
|
|
}
|
|
|
|
void EditBox::_on_left()
|
|
{
|
|
if (_iCursor == 0)
|
|
return;
|
|
--_iCursor;
|
|
_timerCursor.Reset(true);
|
|
_refresh_cursor();
|
|
}
|
|
|
|
void EditBox::_on_right()
|
|
{
|
|
if (_iCursor == _str.size())
|
|
return;
|
|
++_iCursor;
|
|
_timerCursor.Reset(true);
|
|
_refresh_cursor();
|
|
}
|
|
|
|
void EditBox::_on_enter()
|
|
{
|
|
if (_bSingleLine)
|
|
return;
|
|
_on_char('\n');
|
|
}
|
|
|
|
}
|
|
|
|
#endif |