162 lines
2.9 KiB
C++
162 lines
2.9 KiB
C++
#include "dl_ui_edit_box.h"
|
|
|
|
#include "ui/dl_ui_manager.h"
|
|
|
|
namespace dl::UI
|
|
{
|
|
|
|
void EditBox::_init()
|
|
{
|
|
_colTip = ColorDef::GRAY;
|
|
_strTip = "在这里输入内容……";
|
|
|
|
_colCursor = ColorDef::RED;
|
|
_colSelect = ColorDef::BLUE;
|
|
|
|
_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->SetCoor(_panel->GetCoor());
|
|
|
|
|
|
SetOrder(Z_DEFAULT_UI);
|
|
}
|
|
|
|
void EditBox::_on_char(char32_t ch)
|
|
{
|
|
log_test0(CodeCvt::Utf32ToUtf8(std::u32string{ ch }));
|
|
_str.push_back(ch);
|
|
_refresh_txt();
|
|
}
|
|
|
|
void EditBox::SetFocus(bool focus)
|
|
{
|
|
if (focus)
|
|
{
|
|
if (g_ui->GetFocusEditBox() != this)
|
|
{
|
|
g_ui->SetFocusEditBox(this);
|
|
_panel->SetSelectAll(true);
|
|
_refresh_txt();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (g_ui->GetFocusEditBox() == this)
|
|
{
|
|
g_ui->SetFocusEditBox(nullptr);
|
|
_panel->SetSelectAll(false);
|
|
//_bLoseFocus = true;
|
|
_refresh_txt();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool EditBox::IsFocus()
|
|
{
|
|
return g_ui->GetFocusEditBox() == this;
|
|
}
|
|
|
|
void EditBox::_on_backspace()
|
|
{
|
|
if (_str.empty())
|
|
return;
|
|
_str.pop_back();
|
|
_refresh_txt();
|
|
}
|
|
|
|
void EditBox::SetSingleLine(bool single_line)
|
|
{
|
|
if (_bSingleLine == single_line)
|
|
return;
|
|
if (single_line)
|
|
{
|
|
if (Any::Equal(_str, '\n'))
|
|
{
|
|
log_warn("多行输入框变为单行,存在换行符!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
void EditBox::_refresh_txt()
|
|
{
|
|
bool focus = IsFocus();
|
|
if (_str.empty() && !focus)
|
|
{
|
|
_txt->SetColor(_colTip);
|
|
_txt->SetString(_strTip);
|
|
return;
|
|
}
|
|
_txt->SetColor(focus ? _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();
|
|
}
|
|
|
|
}
|
|
|