metaforce/specter/lib/TextField.cpp

945 lines
32 KiB
C++
Raw Normal View History

2016-03-04 23:03:47 +00:00
#include "specter/TextField.hpp"
#include "specter/RootView.hpp"
#include "specter/ViewResources.hpp"
2016-03-04 23:03:47 +00:00
namespace specter
{
2015-12-18 03:26:10 +00:00
TextField::TextField(ViewResources& res, View& parentView, IStringBinding* strBind)
2015-12-27 22:42:45 +00:00
: ITextInputView(res, parentView, strBind)
{
2016-03-30 19:15:32 +00:00
commitResources(res, [&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
buildResources(ctx, res);
2017-01-29 03:57:48 +00:00
m_vertsBinding.init(ctx, res, 41, m_viewVertBlockBuf);
2016-03-30 19:15:32 +00:00
return true;
});
2015-12-17 21:28:37 +00:00
2015-12-20 04:39:09 +00:00
for (int i=28 ; i<32 ; ++i)
m_verts[i].m_color = res.themeData().textfieldSelection();
setInactive();
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2015-12-17 21:28:37 +00:00
m_text.reset(new TextView(res, *this, res.m_mainFont, TextView::Alignment::Left, 1024));
if (strBind)
setText(strBind->getDefault(this));
2015-12-17 21:28:37 +00:00
}
2015-12-27 22:42:45 +00:00
void TextField::_setText()
{
if (m_hasTextSet)
{
_clearSelectionRange();
m_textStr = m_deferredTextStr;
m_text->typesetGlyphs(m_textStr, m_error ? rootView().themeData().uiText() :
rootView().themeData().fieldText());
2018-10-07 02:58:28 +00:00
if (m_controlBinding)
if (IStringBinding* strBind = IStringBinding::castTo(m_controlBinding))
strBind->changed(this, m_textStr);
2015-12-27 22:42:45 +00:00
m_hasTextSet = false;
if (m_deferredMarkStr.size())
m_hasMarkSet = true;
}
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::_setMarkedText()
{
if (m_hasMarkSet)
{
m_markReplStart = m_deferredMarkReplStart;
m_markReplCount = m_deferredMarkReplCount;
m_markSelStart = m_deferredMarkSelStart;
m_markSelCount = m_deferredMarkSelCount;
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
size_t repPoint;
size_t repEnd;
if (m_selectionCount)
{
repPoint = m_selectionStart;
repEnd = m_selectionStart;
}
else
{
repPoint = m_cursorPos;
repEnd = m_cursorPos;
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
if (m_markReplStart != SIZE_MAX)
{
repPoint += m_markReplStart;
repEnd += m_markReplStart + m_markReplCount;
}
2016-09-11 01:24:40 +00:00
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
size_t len = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-27 22:42:45 +00:00
repPoint = std::min(repPoint, len);
repEnd = std::min(repEnd, len);
2017-11-13 06:14:52 +00:00
std::string compStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + repPoint).iter());
2015-12-27 22:42:45 +00:00
compStr += m_deferredMarkStr;
2017-11-13 06:14:52 +00:00
compStr += std::string((UTF8Iterator(textStr.cbegin()) + repEnd).iter(), textStr.cend());
m_text->typesetGlyphs(compStr, m_error ? rootView().themeData().uiText() :
rootView().themeData().fieldText());
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
size_t pos = m_cursorPos;
if (m_deferredMarkStr.size())
pos += m_markSelStart;
if (m_markSelCount)
_reallySetMarkRange(pos, m_markSelCount);
else
_reallySetCursorPos(pos);
2016-09-11 01:24:40 +00:00
2017-11-13 06:14:52 +00:00
std::string_view deferredMarkStr(m_deferredMarkStr);
2015-12-27 22:42:45 +00:00
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
2017-11-13 06:14:52 +00:00
size_t defLen = UTF8Iterator(deferredMarkStr.cbegin()).countTo(deferredMarkStr.cend());
2015-12-27 22:42:45 +00:00
for (auto it=glyphs.begin()+repPoint ; it<glyphs.begin()+repPoint+defLen ; ++it)
it->m_color = rootView().themeData().fieldMarkedText();
m_text->invalidateGlyphs();
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
m_hasMarkSet = false;
}
}
2016-09-11 01:24:40 +00:00
2017-11-13 06:14:52 +00:00
void TextField::setText(std::string_view str)
2015-12-17 21:28:37 +00:00
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2015-12-20 21:59:23 +00:00
UTF8Iterator it(str.cbegin());
for (; it.iter() != str.cend() ; ++it)
if (*it.iter() == '\n')
2015-12-20 04:39:09 +00:00
break;
2015-12-27 22:42:45 +00:00
m_deferredTextStr.assign(str.cbegin(), it.iter());
m_hasTextSet = true;
2015-12-17 21:28:37 +00:00
}
void TextField::setInactive()
{
const IThemeData& theme = rootView().themeData();
if (m_error)
{
2016-03-04 23:03:47 +00:00
m_verts[0].m_color = theme.textfield1Inactive() * zeus::CColor::skRed;
m_verts[1].m_color = theme.textfield2Inactive() * zeus::CColor::skRed;
m_verts[2].m_color = theme.textfield1Inactive() * zeus::CColor::skRed;
m_verts[3].m_color = theme.textfield2Inactive() * zeus::CColor::skRed;
m_verts[4].m_color = theme.textfield2Inactive() * zeus::CColor::skRed;
for (int i=5 ; i<28 ; ++i)
2016-03-04 23:03:47 +00:00
m_verts[i].m_color = theme.textfield2Inactive() * zeus::CColor::skRed;
}
else
{
m_verts[0].m_color = theme.textfield1Inactive();
m_verts[1].m_color = theme.textfield2Inactive();
m_verts[2].m_color = theme.textfield1Inactive();
m_verts[3].m_color = theme.textfield2Inactive();
m_verts[4].m_color = theme.textfield2Inactive();
for (int i=5 ; i<28 ; ++i)
m_verts[i].m_color = theme.textfield2Inactive();
}
m_vertsBinding.load<decltype(m_verts)>(m_verts);
m_bgState = BGState::Inactive;
2015-12-17 21:28:37 +00:00
}
void TextField::setHover()
{
const IThemeData& theme = rootView().themeData();
if (m_error)
{
2016-03-04 23:03:47 +00:00
m_verts[0].m_color = theme.textfield1Hover() * zeus::CColor::skRed;
m_verts[1].m_color = theme.textfield2Hover() * zeus::CColor::skRed;
m_verts[2].m_color = theme.textfield1Hover() * zeus::CColor::skRed;
m_verts[3].m_color = theme.textfield2Hover() * zeus::CColor::skRed;
m_verts[4].m_color = theme.textfield2Hover() * zeus::CColor::skRed;
for (int i=5 ; i<28 ; ++i)
2016-03-04 23:03:47 +00:00
m_verts[i].m_color = theme.textfield2Inactive() * zeus::CColor::skRed;
}
else
{
m_verts[0].m_color = theme.textfield1Hover();
m_verts[1].m_color = theme.textfield2Hover();
m_verts[2].m_color = theme.textfield1Hover();
m_verts[3].m_color = theme.textfield2Hover();
m_verts[4].m_color = theme.textfield2Hover();
for (int i=5 ; i<28 ; ++i)
m_verts[i].m_color = theme.textfield2Inactive();
}
m_vertsBinding.load<decltype(m_verts)>(m_verts);
m_bgState = BGState::Hover;
2015-12-17 21:28:37 +00:00
}
void TextField::setDisabled()
{
const IThemeData& theme = rootView().themeData();
if (m_error)
{
2016-03-04 23:03:47 +00:00
m_verts[0].m_color = theme.textfield1Disabled() * zeus::CColor::skRed;
m_verts[1].m_color = theme.textfield2Disabled() * zeus::CColor::skRed;
m_verts[2].m_color = theme.textfield1Disabled() * zeus::CColor::skRed;
m_verts[3].m_color = theme.textfield2Disabled() * zeus::CColor::skRed;
m_verts[4].m_color = theme.textfield2Disabled() * zeus::CColor::skRed;
for (int i=5 ; i<28 ; ++i)
2016-03-04 23:03:47 +00:00
m_verts[i].m_color = theme.textfield2Disabled() * zeus::CColor::skRed;
}
else
{
m_verts[0].m_color = theme.textfield1Disabled();
m_verts[1].m_color = theme.textfield2Disabled();
m_verts[2].m_color = theme.textfield1Disabled();
m_verts[3].m_color = theme.textfield2Disabled();
m_verts[4].m_color = theme.textfield2Disabled();
for (int i=5 ; i<28 ; ++i)
m_verts[i].m_color = theme.textfield2Disabled();
}
m_vertsBinding.load<decltype(m_verts)>(m_verts);
m_bgState = BGState::Disabled;
}
void TextField::refreshBg()
{
switch (m_bgState)
{
case BGState::Inactive:
setInactive(); break;
case BGState::Hover:
setHover(); break;
case BGState::Disabled:
setDisabled(); break;
}
2015-12-17 21:28:37 +00:00
}
void TextField::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2015-12-20 04:39:09 +00:00
if (!m_active)
{
rootView().setActiveTextView(this);
2015-12-20 21:59:23 +00:00
}
else if (m_clickFrames2 < 15)
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
size_t len = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-27 22:42:45 +00:00
setSelectionRange(0, len);
2015-12-20 21:59:23 +00:00
}
else if (m_clickFrames < 15)
{
size_t startPos = m_text->reverseSelectGlyph(coord.pixel[0] - m_text->subRect().location[0]);
std::pair<size_t,size_t> range = m_text->queryWholeWordRange(startPos);
setSelectionRange(range.first, range.second);
m_clickFrames2 = 0;
2015-12-20 04:39:09 +00:00
}
else
2015-12-20 21:59:23 +00:00
{
size_t startPos = m_text->reverseSelectGlyph(coord.pixel[0] - m_text->subRect().location[0]);
setCursorPos(startPos);
2016-09-11 01:24:40 +00:00
m_dragging |= size_t(1) << size_t(button);
2015-12-20 21:59:23 +00:00
m_dragStart = startPos;
rootView().setActiveDragView(this);
}
m_clickFrames = 0;
2015-12-17 21:28:37 +00:00
}
void TextField::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
2016-09-11 01:24:40 +00:00
m_dragging &= ~(size_t(1) << size_t(button));
2015-12-20 21:59:23 +00:00
if (m_dragging == 0)
rootView().setActiveDragView(nullptr);
2015-12-17 21:28:37 +00:00
}
void TextField::mouseMove(const boo::SWindowCoord& coord)
{
2015-12-20 21:59:23 +00:00
if (m_dragging != 0)
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2015-12-20 21:59:23 +00:00
size_t thisPos = m_text->reverseSelectGlyph(coord.pixel[0] - m_text->subRect().location[0]);
size_t minPos = std::min(m_dragStart, thisPos);
size_t maxPos = std::max(m_dragStart, thisPos);
if (minPos != maxPos)
setSelectionRange(minPos, maxPos-minPos);
else
setCursorPos(minPos);
}
2015-12-17 21:28:37 +00:00
}
void TextField::mouseEnter(const boo::SWindowCoord& coord)
{
2015-12-18 03:26:10 +00:00
setHover();
2015-12-22 01:33:27 +00:00
rootView().setTextFieldHover(true);
2015-12-17 21:28:37 +00:00
}
void TextField::mouseLeave(const boo::SWindowCoord& coord)
{
2015-12-18 03:26:10 +00:00
setInactive();
2015-12-22 01:33:27 +00:00
rootView().setTextFieldHover(false);
2015-12-20 04:39:09 +00:00
}
2015-12-24 03:32:00 +00:00
void TextField::clipboardCopy()
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2016-09-11 01:24:40 +00:00
2015-12-24 03:32:00 +00:00
if (!m_selectionCount)
return;
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
UTF8Iterator begin(textStr.cbegin());
2015-12-24 03:32:00 +00:00
begin += m_selectionStart;
UTF8Iterator end(begin.iter());
end += m_selectionCount;
rootView().window()->clipboardCopy(boo::EClipboardType::UTF8String,
(uint8_t*)&*begin.iter(), end.iter() - begin.iter());
}
2016-09-11 01:24:40 +00:00
2015-12-28 00:24:29 +00:00
void TextField::clipboardCut()
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2016-09-11 01:24:40 +00:00
2015-12-28 00:24:29 +00:00
if (!m_selectionCount)
return;
2016-09-11 01:24:40 +00:00
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
UTF8Iterator begin(textStr.cbegin());
2015-12-28 00:24:29 +00:00
begin += m_selectionStart;
UTF8Iterator end(begin.iter());
end += m_selectionCount;
2016-09-11 01:24:40 +00:00
2015-12-28 00:24:29 +00:00
rootView().window()->clipboardCopy(boo::EClipboardType::UTF8String,
(uint8_t*)&*begin.iter(), end.iter() - begin.iter());
2016-09-11 01:24:40 +00:00
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + m_selectionStart).iter());
newStr.append((UTF8Iterator(textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), textStr.cend());
2015-12-28 00:24:29 +00:00
size_t selStart = m_selectionStart;
setText(newStr);
setCursorPos(selStart);
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
static std::string SanitizeUTF8TextLine(const char* string, size_t len)
{
const char* it = string;
utf8proc_int32_t ch;
utf8proc_ssize_t sz;
std::string ret;
ret.reserve(len);
for (sz = utf8proc_iterate((utf8proc_uint8_t*)it, -1, &ch);
it < string+len;
it += sz, sz = utf8proc_iterate((utf8proc_uint8_t*)it, -1, &ch))
{
if (sz <= 0)
break;
if (ch >= 0x20)
ret.append(it, sz);
}
return ret;
}
2015-12-24 03:32:00 +00:00
void TextField::clipboardPaste()
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2016-09-11 01:24:40 +00:00
2015-12-24 03:32:00 +00:00
size_t retSz;
std::unique_ptr<uint8_t[]> retData =
rootView().window()->clipboardPaste(boo::EClipboardType::UTF8String, retSz);
2015-12-27 22:42:45 +00:00
std::string saniData = SanitizeUTF8TextLine((char*)retData.get(), retSz);
2015-12-24 03:32:00 +00:00
2015-12-27 22:42:45 +00:00
if (retData && saniData.size())
2015-12-24 03:32:00 +00:00
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
2015-12-24 03:32:00 +00:00
if (m_selectionCount)
{
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + m_selectionStart).iter());
2015-12-27 22:42:45 +00:00
newStr.append(saniData);
2017-11-13 06:14:52 +00:00
std::string_view newStrView(newStr);
size_t newSel = UTF8Iterator(newStrView.cbegin()).countTo(newStrView.cend());
newStr.append((UTF8Iterator(textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), textStr.cend());
2015-12-24 03:32:00 +00:00
setText(newStr);
setCursorPos(newSel);
}
else
{
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + m_cursorPos).iter());
2015-12-27 22:42:45 +00:00
newStr.append(saniData);
2017-11-13 06:14:52 +00:00
std::string_view newStrView(newStr);
size_t newSel = UTF8Iterator(newStrView.cbegin()).countTo(newStrView.cend());
newStr.append((UTF8Iterator(textStr.cbegin()) + m_cursorPos).iter(), textStr.cend());
2015-12-24 03:32:00 +00:00
setText(newStr);
setCursorPos(newSel);
}
}
}
2015-12-20 04:39:09 +00:00
void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat)
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (m_deferredMarkStr.size())
return;
2016-09-11 01:24:40 +00:00
2015-12-20 04:39:09 +00:00
if (key == boo::ESpecialKey::Left)
{
2015-12-24 03:32:00 +00:00
if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None)
{
if (m_cursorPos)
{
size_t origPos = m_cursorPos;
if (m_selectionCount)
{
if (m_cursorPos == m_selectionStart)
setSelectionRange(m_cursorPos-1, m_selectionCount+1);
else
setSelectionRange(m_selectionStart, m_selectionCount-1);
}
else
setSelectionRange(m_cursorPos-1, 1);
m_cursorPos = origPos - 1;
}
}
else
{
if (m_selectionCount)
m_cursorPos = m_selectionStart;
setCursorPos(m_cursorPos==0 ? 0 : (m_cursorPos-1));
}
2015-12-20 04:39:09 +00:00
}
else if (key == boo::ESpecialKey::Right)
{
2015-12-24 03:32:00 +00:00
if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None)
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
size_t len = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-27 22:42:45 +00:00
if (m_cursorPos < len)
2015-12-24 03:32:00 +00:00
{
size_t origPos = m_cursorPos;
if (m_selectionCount)
{
if (m_cursorPos == m_selectionStart)
setSelectionRange(m_cursorPos+1, m_selectionCount-1);
else
setSelectionRange(m_selectionStart, m_selectionCount+1);
}
else
setSelectionRange(m_cursorPos, 1);
m_cursorPos = origPos + 1;
}
}
else
{
if (m_selectionCount)
m_cursorPos = m_selectionStart + m_selectionCount - 1;
setCursorPos(m_cursorPos+1);
}
2015-12-20 04:39:09 +00:00
}
2015-12-20 21:59:23 +00:00
else if (key == boo::ESpecialKey::Backspace)
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
2015-12-20 21:59:23 +00:00
if (m_selectionCount)
{
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + m_selectionStart).iter());
newStr.append((UTF8Iterator(textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), textStr.cend());
2015-12-20 21:59:23 +00:00
size_t selStart = m_selectionStart;
setText(newStr);
setCursorPos(selStart);
}
else if (m_cursorPos > 0)
{
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + (m_cursorPos-1)).iter());
newStr.append((UTF8Iterator(textStr.cbegin()) + m_cursorPos).iter(), textStr.cend());
2015-12-20 21:59:23 +00:00
setText(newStr);
setCursorPos(m_cursorPos-1);
}
}
else if (key == boo::ESpecialKey::Delete)
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
size_t len = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-20 21:59:23 +00:00
if (m_selectionCount)
{
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + m_selectionStart).iter());
newStr.append((UTF8Iterator(textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), textStr.cend());
2015-12-20 21:59:23 +00:00
size_t selStart = m_selectionStart;
setText(newStr);
setCursorPos(selStart);
}
2015-12-27 22:42:45 +00:00
else if (m_cursorPos < len)
2015-12-20 21:59:23 +00:00
{
2017-11-13 06:14:52 +00:00
std::string newStr(textStr.cbegin(), (UTF8Iterator(textStr.cbegin()) + m_cursorPos).iter());
newStr.append((UTF8Iterator(textStr.cbegin()) + (m_cursorPos+1)).iter(), textStr.cend());
2015-12-20 21:59:23 +00:00
setText(newStr);
setCursorPos(m_cursorPos);
}
}
2015-12-20 04:39:09 +00:00
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
bool TextField::hasMarkedText() const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
return m_deferredMarkStr.size() != 0;
}
std::pair<int,int> TextField::markedRange() const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (m_deferredMarkStr.empty())
return {-1, 0};
2017-11-13 06:14:52 +00:00
std::string_view deferredMarkStr(m_deferredMarkStr);
return {m_cursorPos, UTF8Iterator(deferredMarkStr.cbegin()).countTo(deferredMarkStr.cend())};
2015-12-27 22:42:45 +00:00
}
std::pair<int,int> TextField::selectedRange() const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (!m_deferredSelectionCount)
return {-1, 0};
return {m_deferredSelectionStart, m_deferredSelectionCount};
}
2017-11-13 06:14:52 +00:00
void TextField::setMarkedText(std::string_view str,
2015-12-27 22:42:45 +00:00
const std::pair<int,int>& selectedRange,
const std::pair<int,int>& replacementRange)
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredMarkStr = SanitizeUTF8TextLine(str.data(), str.size());
m_deferredMarkSelStart = selectedRange.first;
m_deferredMarkSelCount = selectedRange.second;
m_deferredMarkReplStart = replacementRange.first;
m_deferredMarkReplCount = replacementRange.second;
m_hasMarkSet = true;
}
void TextField::unmarkText()
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredMarkStr.clear();
m_deferredMarkReplStart = 0;
m_deferredMarkReplCount = 0;
m_deferredMarkSelStart = 0;
m_deferredMarkSelCount = 0;
m_hasMarkSet = true;
}
std::string TextField::substringForRange(const std::pair<int,int>& range,
std::pair<int,int>& actualRange) const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2017-11-13 06:14:52 +00:00
std::string_view deferredTextStr(m_deferredTextStr);
UTF8Iterator begin(deferredTextStr.cbegin());
size_t curLen = UTF8Iterator(deferredTextStr.cbegin()).countTo(deferredTextStr.cend());
2015-12-27 22:42:45 +00:00
if (range.first >= curLen)
return std::string();
begin += range.first;
size_t endIdx = std::min(size_t(range.first + range.second), curLen);
2017-11-13 06:14:52 +00:00
UTF8Iterator end(deferredTextStr.cbegin());
2015-12-27 22:42:45 +00:00
end += endIdx;
actualRange.first = range.first;
actualRange.second = endIdx;
return std::string(begin.iter(), end.iter());
}
2017-11-13 06:14:52 +00:00
void TextField::insertText(std::string_view str, const std::pair<int,int>& range)
2015-12-27 22:42:45 +00:00
{
std::string saniStr = SanitizeUTF8TextLine(str.data(), str.size());
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2017-11-13 06:14:52 +00:00
std::string_view deferredTextStr(m_deferredTextStr);
size_t curLen = UTF8Iterator(deferredTextStr.cbegin()).countTo(deferredTextStr.cend());
2015-12-27 22:42:45 +00:00
if (range.first < 0 || range.first >= curLen)
{
size_t beginPos = m_deferredCursorPos;
if (m_selectionCount)
beginPos = m_selectionCount;
beginPos = std::min(beginPos, curLen);
2017-11-13 06:14:52 +00:00
std::string newStr(deferredTextStr.cbegin(), (UTF8Iterator(deferredTextStr.cbegin())+beginPos).iter());
2015-12-27 22:42:45 +00:00
newStr += saniStr;
2017-11-13 06:14:52 +00:00
std::string_view newStrView(newStr);
size_t newPos = UTF8Iterator(newStrView.cbegin()).countTo(newStrView.cend());
newStr += std::string((UTF8Iterator(deferredTextStr.cbegin())+beginPos).iter(), deferredTextStr.cend());
2015-12-27 22:42:45 +00:00
setText(newStr);
setCursorPos(newPos);
unmarkText();
return;
}
2016-09-11 01:24:40 +00:00
2017-11-13 06:14:52 +00:00
std::string newStr(deferredTextStr.cbegin(), (UTF8Iterator(deferredTextStr.cbegin()) + range.first).iter());
2015-12-27 22:42:45 +00:00
newStr += saniStr;
2017-11-13 06:14:52 +00:00
std::string_view newStrView(newStr);
size_t newSel = UTF8Iterator(newStrView.cbegin()).countTo(newStrView.cend());
2015-12-27 22:42:45 +00:00
size_t endIdx = range.first + range.second;
if (endIdx >= newSel)
endIdx = newSel - 1;
2017-11-13 06:14:52 +00:00
newStr.append((UTF8Iterator(deferredTextStr.cbegin()) + endIdx).iter(), deferredTextStr.cend());
2015-12-27 22:42:45 +00:00
setText(newStr);
setCursorPos(newSel);
unmarkText();
}
int TextField::characterIndexAtPoint(const boo::SWindowCoord& point) const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
return m_text->reverseSelectGlyph(point.pixel[0]);
}
boo::SWindowRect TextField::rectForCharacterRange(const std::pair<int,int>& range,
std::pair<int,int>& actualRange) const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
UTF8Iterator begin(textStr.cbegin());
size_t curLen = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-27 22:42:45 +00:00
if (range.first >= curLen)
{
const std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
const TextView::RenderGlyph& g = glyphs.back();
return {subRect().location[0] + int(g.m_pos[3][0]), subRect().location[1] + int(g.m_pos[3][1]), 0, 0};
2015-12-27 22:42:45 +00:00
}
begin += range.first;
size_t endIdx = std::min(size_t(range.first + range.second), curLen);
2017-11-13 06:14:52 +00:00
UTF8Iterator end(textStr.cbegin());
2015-12-27 22:42:45 +00:00
end += endIdx;
actualRange.first = range.first;
actualRange.second = endIdx;
const std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
const TextView::RenderGlyph& g1 = glyphs[range.first];
const TextView::RenderGlyph& g2 = glyphs[endIdx];
return {subRect().location[0] + int(g1.m_pos[1][0]), subRect().location[1] + int(g1.m_pos[1][1]),
int(g2.m_pos[3][0]-g1.m_pos[1][0]), int(g2.m_pos[0][1]-g1.m_pos[1][1])};
2015-12-27 22:42:45 +00:00
}
2015-12-24 20:57:38 +00:00
2015-12-20 04:39:09 +00:00
void TextField::think()
{
++m_cursorFrames;
2015-12-20 21:59:23 +00:00
++m_clickFrames;
++m_clickFrames2;
++m_errorFrames;
if (m_error && m_errorFrames <= 360)
{
2016-03-04 23:03:47 +00:00
zeus::CColor errMult;
zeus::CColor errBg;
if (m_errorFrames < 300)
{
errMult = m_viewVertBlock.m_color;
errBg = rootView().themeData().tooltipBackground() * m_viewVertBlock.m_color;
}
else if (m_errorFrames >= 360)
{
2016-03-04 23:03:47 +00:00
errMult = zeus::CColor::skClear;
errBg = rootView().themeData().tooltipBackground();
errBg[3] = 0.0;
}
else
{
float t = (m_errorFrames - 300) / 60.0;
2016-03-04 23:03:47 +00:00
errMult = zeus::CColor::lerp(m_viewVertBlock.m_color, zeus::CColor::skClear, t);
errBg = zeus::CColor::lerp(rootView().themeData().tooltipBackground() * m_viewVertBlock.m_color,
zeus::CColor::skClear, t);
}
for (size_t i=32 ; i<41 ; ++i)
m_verts[i].m_color = errBg;
m_vertsBinding.load<decltype(m_verts)>(m_verts);
m_errText->setMultiplyColor(errMult);
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
_setText();
_setSelectionRange();
_clearSelectionRange();
_setCursorPos();
_setMarkedText();
2015-12-20 04:39:09 +00:00
}
void TextField::setActive(bool active)
{
2015-12-27 22:42:45 +00:00
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2015-12-20 04:39:09 +00:00
m_active = active;
if (!active)
2015-12-24 20:57:38 +00:00
{
2015-12-20 04:39:09 +00:00
clearSelectionRange();
2015-12-24 20:57:38 +00:00
rootView().window()->claimKeyboardFocus(nullptr);
}
else if (!m_selectionCount)
2015-12-27 22:42:45 +00:00
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
size_t len = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-27 22:42:45 +00:00
setSelectionRange(0, len);
}
2015-12-20 04:39:09 +00:00
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::_reallySetCursorPos(size_t pos)
2015-12-20 04:39:09 +00:00
{
float pf = rootView().viewRes().pixelFactor();
2015-12-27 22:42:45 +00:00
int offset1 = 4 * pf + m_text->queryReverseAdvance(pos);
2015-12-20 04:39:09 +00:00
int offset2 = offset1 + 2 * pf;
2016-03-04 23:03:47 +00:00
const zeus::CColor& selColor = rootView().viewRes().themeData().textfieldSelection();
2015-12-20 04:39:09 +00:00
m_verts[28].m_pos.assign(offset1, 18 * pf, 0);
2015-12-27 22:42:45 +00:00
m_verts[28].m_color = selColor;
2015-12-20 04:39:09 +00:00
m_verts[29].m_pos.assign(offset1, 4 * pf, 0);
2015-12-27 22:42:45 +00:00
m_verts[29].m_color = selColor;
2015-12-20 04:39:09 +00:00
m_verts[30].m_pos.assign(offset2, 18 * pf, 0);
2015-12-27 22:42:45 +00:00
m_verts[30].m_color = selColor;
2015-12-20 04:39:09 +00:00
m_verts[31].m_pos.assign(offset2, 4 * pf, 0);
2015-12-27 22:42:45 +00:00
m_verts[31].m_color = selColor;
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2016-09-11 01:24:40 +00:00
2015-12-24 20:57:38 +00:00
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
rootView().window()->claimKeyboardFocus(focusRect);
2015-12-20 04:39:09 +00:00
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::_setCursorPos()
2015-12-20 04:39:09 +00:00
{
2015-12-27 22:42:45 +00:00
if (m_hasCursorSet)
2015-12-24 03:32:00 +00:00
{
2015-12-27 22:42:45 +00:00
m_hasSelectionClear = true;
_clearSelectionRange();
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
m_cursorPos = std::min(m_deferredCursorPos, UTF8Iterator(textStr.cbegin()).countTo(textStr.cend()));
2015-12-27 22:42:45 +00:00
m_deferredCursorPos = m_cursorPos;
m_cursorFrames = 0;
_reallySetCursorPos(m_cursorPos);
m_hasCursorSet = false;
2015-12-24 03:32:00 +00:00
}
2015-12-27 22:42:45 +00:00
}
2015-12-24 03:32:00 +00:00
2015-12-27 22:42:45 +00:00
void TextField::setCursorPos(size_t pos)
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredCursorPos = pos;
m_hasCursorSet = true;
}
2017-11-13 06:14:52 +00:00
void TextField::setErrorState(std::string_view message)
{
m_error = true;
if (m_selectionCount)
_reallySetSelectionRange(m_selectionStart, m_selectionCount);
else
clearSelectionRange();
refreshBg();
m_errText.reset(new TextView(rootView().viewRes(), *this, rootView().viewRes().m_mainFont));
m_errText->typesetGlyphs(message, rootView().themeData().uiText());
updateSize();
m_errorFrames = 0;
}
void TextField::clearErrorState()
{
m_error = false;
if (m_selectionCount)
_reallySetSelectionRange(m_selectionStart, m_selectionCount);
else
clearSelectionRange();
refreshBg();
m_errText.reset();
m_errorFrames = 360;
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::_reallySetSelectionRange(size_t start, size_t len)
{
2015-12-20 04:39:09 +00:00
ViewResources& res = rootView().viewRes();
float pf = res.pixelFactor();
int offset1 = 5 * pf;
int offset2 = offset1;
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
2015-12-27 22:42:45 +00:00
offset1 += glyphs[start].m_pos[0][0];
offset2 += glyphs[start+len-1].m_pos[2][0];
2016-03-04 23:03:47 +00:00
const zeus::CColor& selColor = rootView().themeData().selectedFieldText();
const zeus::CColor& deselColor = m_error ? rootView().themeData().uiText() :
rootView().themeData().fieldText();
2015-12-20 04:39:09 +00:00
for (size_t i=0 ; i<glyphs.size() ; ++i)
{
2015-12-27 22:42:45 +00:00
if (i >= start && i < start + len)
glyphs[i].m_color = selColor;
2015-12-20 04:39:09 +00:00
else
glyphs[i].m_color = deselColor;
2015-12-20 04:39:09 +00:00
}
m_text->invalidateGlyphs();
2016-09-11 01:24:40 +00:00
2015-12-20 04:39:09 +00:00
m_verts[28].m_pos.assign(offset1, 18 * pf, 0);
m_verts[29].m_pos.assign(offset1, 4 * pf, 0);
m_verts[30].m_pos.assign(offset2, 18 * pf, 0);
m_verts[31].m_pos.assign(offset2, 4 * pf, 0);
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
rootView().window()->claimKeyboardFocus(focusRect);
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::_reallySetMarkRange(size_t start, size_t len)
{
ViewResources& res = rootView().viewRes();
float pf = res.pixelFactor();
int offset1 = 5 * pf;
int offset2 = offset1;
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
offset1 += glyphs[start].m_pos[0][0];
offset2 += glyphs[start+len-1].m_pos[2][0];
2016-09-11 01:24:40 +00:00
2016-03-04 23:03:47 +00:00
const zeus::CColor& selColor = rootView().themeData().textfieldMarkSelection();
2015-12-27 22:42:45 +00:00
m_verts[28].m_pos.assign(offset1, 18 * pf, 0);
m_verts[28].m_color = selColor;
m_verts[29].m_pos.assign(offset1, 4 * pf, 0);
m_verts[29].m_color = selColor;
m_verts[30].m_pos.assign(offset2, 18 * pf, 0);
m_verts[30].m_color = selColor;
m_verts[31].m_pos.assign(offset2, 4 * pf, 0);
m_verts[31].m_color = selColor;
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2016-09-11 01:24:40 +00:00
2015-12-24 20:57:38 +00:00
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
rootView().window()->claimKeyboardFocus(focusRect);
2015-12-20 04:39:09 +00:00
}
2015-12-27 22:42:45 +00:00
void TextField::_setSelectionRange()
2015-12-20 04:39:09 +00:00
{
2015-12-27 22:42:45 +00:00
if (m_hasSelectionSet)
{
2017-11-13 06:14:52 +00:00
std::string_view textStr(m_textStr);
size_t len = UTF8Iterator(textStr.cbegin()).countTo(textStr.cend());
2015-12-27 22:42:45 +00:00
m_selectionStart = std::min(m_deferredSelectionStart, len-1);
m_deferredSelectionStart = m_selectionStart;
2015-12-27 22:42:45 +00:00
m_selectionCount = std::min(m_deferredSelectionCount, len-m_selectionStart);
m_deferredSelectionCount = m_selectionCount;
_reallySetSelectionRange(m_selectionStart, m_selectionCount);
m_hasSelectionSet = false;
}
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::setSelectionRange(size_t start, size_t count)
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
2015-12-20 04:39:09 +00:00
2015-12-27 22:42:45 +00:00
if (!count)
{
setCursorPos(start);
return;
}
m_deferredSelectionStart = start;
m_deferredSelectionCount = count;
m_hasSelectionSet = true;
m_hasSelectionClear = false;
}
void TextField::_clearSelectionRange()
{
if (m_hasSelectionClear)
{
m_selectionStart = 0;
m_selectionCount = 0;
2016-09-11 01:24:40 +00:00
2016-03-04 23:03:47 +00:00
const zeus::CColor& deselColor = m_error ? rootView().themeData().uiText() :
rootView().themeData().fieldText();
2015-12-27 22:42:45 +00:00
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
for (size_t i=0 ; i<glyphs.size() ; ++i)
glyphs[i].m_color = deselColor;
m_text->invalidateGlyphs();
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
m_hasSelectionClear = false;
}
}
2016-09-11 01:24:40 +00:00
2015-12-27 22:42:45 +00:00
void TextField::clearSelectionRange()
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredSelectionStart = 0;
m_deferredSelectionCount = 0;
m_hasSelectionClear = true;
m_hasSelectionSet = false;
2015-12-17 21:28:37 +00:00
}
void TextField::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
{
float pf = rootView().viewRes().pixelFactor();
int width = sub.size[0];
int height = 20 * pf;
2015-12-18 03:26:10 +00:00
boo::SWindowRect newRect = sub;
newRect.size[1] = height;
View::resized(root, newRect);
2015-12-17 21:28:37 +00:00
m_verts[0].m_pos.assign(1, height+1, 0);
m_verts[1].m_pos.assign(1, 1, 0);
m_verts[2].m_pos.assign(width+1, height+1, 0);
m_verts[3].m_pos.assign(width+1, 1, 0);
m_verts[4].m_pos.assign(width+1, 1, 0);
m_verts[5].m_pos.assign(1, height+1, 0);
m_verts[6].m_pos.assign(1, height+1, 0);
m_verts[7].m_pos.assign(0, height+1, 0);
m_verts[8].m_pos.assign(1, 1, 0);
m_verts[9].m_pos.assign(0, 1, 0);
m_verts[10].m_pos.assign(0, 1, 0);
m_verts[11].m_pos.assign(width+2, height+1, 0);
m_verts[12].m_pos.assign(width+2, height+1, 0);
m_verts[13].m_pos.assign(width+1, height+1, 0);
m_verts[14].m_pos.assign(width+2, 1, 0);
m_verts[15].m_pos.assign(width+1, 1, 0);
m_verts[16].m_pos.assign(width+1, 1, 0);
m_verts[17].m_pos.assign(1, height+2, 0);
m_verts[18].m_pos.assign(1, height+2, 0);
m_verts[19].m_pos.assign(1, height+1, 0);
m_verts[20].m_pos.assign(width+1, height+2, 0);
m_verts[21].m_pos.assign(width+1, height+1, 0);
m_verts[22].m_pos.assign(width+1, height+1, 0);
m_verts[23].m_pos.assign(1, 1, 0);
m_verts[24].m_pos.assign(1, 1, 0);
m_verts[25].m_pos.assign(1, 0, 0);
m_verts[26].m_pos.assign(width+1, 1, 0);
m_verts[27].m_pos.assign(width+1, 0, 0);
if (m_error)
{
boo::SWindowRect errRect = sub;
errRect.location[1] -= 16 * pf;
errRect.location[0] += 5 * pf;
m_errText->resized(root, errRect);
int eX = 0;
int eY = -22 * pf;
int eWidth = m_errText->nominalWidth() + 10 * pf;
int eHeight = 20 * pf;
m_verts[32].m_pos.assign(eX, eY + eHeight, 0);
m_verts[33].m_pos.assign(eX, eY, 0);
m_verts[34].m_pos.assign(eX + eWidth, eY + eHeight, 0);
m_verts[35].m_pos.assign(eX + eWidth, eY, 0);
m_verts[36] = m_verts[35];
m_verts[37].m_pos.assign(eX + 7 * pf, eY + eHeight + 7 * pf, 0);
m_verts[38] = m_verts[37];
m_verts[39].m_pos.assign(eX, eY + eHeight, 0);
m_verts[40].m_pos.assign(eX + 14 * pf, eY + eHeight, 0);
for (size_t i=32 ; i<41 ; ++i)
2016-03-04 23:03:47 +00:00
m_verts[i].m_color = zeus::CColor::skClear;
}
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2015-12-17 21:28:37 +00:00
m_nomWidth = width;
m_nomHeight = height;
boo::SWindowRect textRect = sub;
textRect.location[0] += 5 * pf;
2015-12-18 03:26:10 +00:00
textRect.location[1] += 7 * pf;
2015-12-17 21:28:37 +00:00
m_text->resized(root, textRect);
}
void TextField::draw(boo::IGraphicsCommandQueue* gfxQ)
{
View::draw(gfxQ);
gfxQ->setShaderDataBinding(m_vertsBinding);
2015-12-17 21:28:37 +00:00
gfxQ->draw(0, 28);
2015-12-20 04:39:09 +00:00
if (m_active)
{
2015-12-27 22:42:45 +00:00
if (!m_selectionCount && !m_markSelCount)
2015-12-20 04:39:09 +00:00
{
if (m_cursorFrames % 60 < 30)
gfxQ->draw(28, 4);
}
else
gfxQ->draw(28, 4);
}
if (m_error)
{
gfxQ->draw(32, 9);
m_errText->draw(gfxQ);
}
2015-12-17 21:28:37 +00:00
m_text->draw(gfxQ);
}
}