mirror of https://github.com/AxioDL/metaforce.git
785 lines
27 KiB
C++
785 lines
27 KiB
C++
#include "Specter/TextField.hpp"
|
|
#include "Specter/RootView.hpp"
|
|
#include "Specter/ViewResources.hpp"
|
|
|
|
namespace Specter
|
|
{
|
|
|
|
TextField::TextField(ViewResources& res, View& parentView, IStringBinding* strBind)
|
|
: ITextInputView(res, parentView, strBind)
|
|
{
|
|
m_bVertsBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SolidShaderVert), 32);
|
|
|
|
if (!res.m_viewRes.m_texVtxFmt)
|
|
{
|
|
boo::VertexElementDescriptor vdescs[] =
|
|
{
|
|
{m_bVertsBuf, nullptr, boo::VertexSemantic::Position4},
|
|
{m_bVertsBuf, nullptr, boo::VertexSemantic::Color}
|
|
};
|
|
m_bVtxFmt = res.m_factory->newVertexFormat(2, vdescs);
|
|
boo::IGraphicsBuffer* bufs[] = {m_viewVertBlockBuf};
|
|
m_bShaderBinding = res.m_factory->newShaderDataBinding(res.m_viewRes.m_solidShader,
|
|
m_bVtxFmt, m_bVertsBuf, nullptr,
|
|
nullptr, 1, bufs, 0, nullptr);
|
|
}
|
|
else
|
|
{
|
|
boo::IGraphicsBuffer* bufs[] = {m_viewVertBlockBuf};
|
|
m_bShaderBinding = res.m_factory->newShaderDataBinding(res.m_viewRes.m_solidShader,
|
|
res.m_viewRes.m_texVtxFmt,
|
|
m_bVertsBuf, nullptr,
|
|
nullptr, 1, bufs, 0, nullptr);
|
|
}
|
|
commitResources(res);
|
|
|
|
m_verts[0].m_color = rootView().themeData().textfield1Inactive();
|
|
m_verts[1].m_color = rootView().themeData().textfield2Inactive();
|
|
m_verts[2].m_color = rootView().themeData().textfield1Inactive();
|
|
m_verts[3].m_color = rootView().themeData().textfield2Inactive();
|
|
m_verts[4].m_color = rootView().themeData().textfield2Inactive();
|
|
for (int i=5 ; i<28 ; ++i)
|
|
m_verts[i].m_color = res.themeData().textfield2Inactive();
|
|
for (int i=28 ; i<32 ; ++i)
|
|
m_verts[i].m_color = res.themeData().textfieldSelection();
|
|
m_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
|
|
m_text.reset(new TextView(res, *this, res.m_mainFont, TextView::Alignment::Left, 1024));
|
|
setText("テスト");
|
|
}
|
|
|
|
void TextField::_setText()
|
|
{
|
|
if (m_hasTextSet)
|
|
{
|
|
_clearSelectionRange();
|
|
m_textStr = m_deferredTextStr;
|
|
m_text->typesetGlyphs(m_textStr, rootView().themeData().fieldText());
|
|
m_hasTextSet = false;
|
|
if (m_deferredMarkStr.size())
|
|
m_hasMarkSet = true;
|
|
}
|
|
}
|
|
|
|
void TextField::_setMarkedText()
|
|
{
|
|
if (m_hasMarkSet)
|
|
{
|
|
m_markReplStart = m_deferredMarkReplStart;
|
|
m_markReplCount = m_deferredMarkReplCount;
|
|
m_markSelStart = m_deferredMarkSelStart;
|
|
m_markSelCount = m_deferredMarkSelCount;
|
|
|
|
size_t repPoint;
|
|
size_t repEnd;
|
|
if (m_selectionCount)
|
|
{
|
|
repPoint = m_selectionStart;
|
|
repEnd = m_selectionStart;
|
|
}
|
|
else
|
|
{
|
|
repPoint = m_cursorPos;
|
|
repEnd = m_cursorPos;
|
|
}
|
|
|
|
if (m_markReplStart != SIZE_MAX)
|
|
{
|
|
repPoint += m_markReplStart;
|
|
repEnd += m_markReplStart + m_markReplCount;
|
|
}
|
|
|
|
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
repPoint = std::min(repPoint, len);
|
|
repEnd = std::min(repEnd, len);
|
|
std::string compStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + repPoint).iter());
|
|
compStr += m_deferredMarkStr;
|
|
compStr += std::string((UTF8Iterator(m_textStr.cbegin()) + repEnd).iter(), m_textStr.cend());
|
|
m_text->typesetGlyphs(compStr, rootView().themeData().fieldText());
|
|
|
|
size_t pos = m_cursorPos;
|
|
if (m_deferredMarkStr.size())
|
|
pos += m_markSelStart;
|
|
if (m_markSelCount)
|
|
_reallySetMarkRange(pos, m_markSelCount);
|
|
else
|
|
_reallySetCursorPos(pos);
|
|
|
|
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
|
|
size_t defLen = UTF8Iterator(m_deferredMarkStr.cbegin()).countTo(m_deferredMarkStr.cend());
|
|
for (auto it=glyphs.begin()+repPoint ; it<glyphs.begin()+repPoint+defLen ; ++it)
|
|
it->m_color = rootView().themeData().fieldMarkedText();
|
|
m_text->updateGlyphs();
|
|
|
|
m_hasMarkSet = false;
|
|
|
|
}
|
|
}
|
|
|
|
void TextField::setText(const std::string& str)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
UTF8Iterator it(str.cbegin());
|
|
for (; it.iter() != str.cend() ; ++it)
|
|
if (*it.iter() == '\n')
|
|
break;
|
|
m_deferredTextStr.assign(str.cbegin(), it.iter());
|
|
m_hasTextSet = true;
|
|
}
|
|
|
|
void TextField::setInactive()
|
|
{
|
|
m_verts[0].m_color = rootView().themeData().textfield1Inactive();
|
|
m_verts[1].m_color = rootView().themeData().textfield2Inactive();
|
|
m_verts[2].m_color = rootView().themeData().textfield1Inactive();
|
|
m_verts[3].m_color = rootView().themeData().textfield2Inactive();
|
|
m_verts[4].m_color = rootView().themeData().textfield2Inactive();
|
|
m_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
}
|
|
|
|
void TextField::setHover()
|
|
{
|
|
m_verts[0].m_color = rootView().themeData().textfield1Hover();
|
|
m_verts[1].m_color = rootView().themeData().textfield2Hover();
|
|
m_verts[2].m_color = rootView().themeData().textfield1Hover();
|
|
m_verts[3].m_color = rootView().themeData().textfield2Hover();
|
|
m_verts[4].m_color = rootView().themeData().textfield2Hover();
|
|
m_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
}
|
|
|
|
void TextField::setDisabled()
|
|
{
|
|
m_verts[0].m_color = rootView().themeData().textfield1Disabled();
|
|
m_verts[1].m_color = rootView().themeData().textfield2Disabled();
|
|
m_verts[2].m_color = rootView().themeData().textfield1Disabled();
|
|
m_verts[3].m_color = rootView().themeData().textfield2Disabled();
|
|
m_verts[4].m_color = rootView().themeData().textfield2Disabled();
|
|
m_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
}
|
|
|
|
void TextField::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
if (!m_active)
|
|
{
|
|
rootView().setActiveTextView(this);
|
|
}
|
|
else if (m_clickFrames2 < 15)
|
|
{
|
|
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
setSelectionRange(0, len);
|
|
}
|
|
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;
|
|
}
|
|
else
|
|
{
|
|
size_t startPos = m_text->reverseSelectGlyph(coord.pixel[0] - m_text->subRect().location[0]);
|
|
setCursorPos(startPos);
|
|
m_dragging |= size_t(1 << int(button));
|
|
m_dragStart = startPos;
|
|
rootView().setActiveDragView(this);
|
|
}
|
|
m_clickFrames = 0;
|
|
}
|
|
|
|
void TextField::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
|
|
{
|
|
m_dragging &= ~(1 << int(button));
|
|
if (m_dragging == 0)
|
|
rootView().setActiveDragView(nullptr);
|
|
}
|
|
|
|
void TextField::mouseMove(const boo::SWindowCoord& coord)
|
|
{
|
|
if (m_dragging != 0)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
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);
|
|
}
|
|
}
|
|
|
|
void TextField::mouseEnter(const boo::SWindowCoord& coord)
|
|
{
|
|
setHover();
|
|
rootView().setTextFieldHover(true);
|
|
}
|
|
|
|
void TextField::mouseLeave(const boo::SWindowCoord& coord)
|
|
{
|
|
setInactive();
|
|
rootView().setTextFieldHover(false);
|
|
}
|
|
|
|
void TextField::clipboardCopy()
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
|
|
if (!m_selectionCount)
|
|
return;
|
|
|
|
UTF8Iterator begin(m_textStr.cbegin());
|
|
begin += m_selectionStart;
|
|
UTF8Iterator end(begin.iter());
|
|
end += m_selectionCount;
|
|
|
|
rootView().window()->clipboardCopy(boo::EClipboardType::UTF8String,
|
|
(uint8_t*)&*begin.iter(), end.iter() - begin.iter());
|
|
}
|
|
|
|
void TextField::clipboardCut()
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
|
|
if (!m_selectionCount)
|
|
return;
|
|
|
|
UTF8Iterator begin(m_textStr.cbegin());
|
|
begin += m_selectionStart;
|
|
UTF8Iterator end(begin.iter());
|
|
end += m_selectionCount;
|
|
|
|
rootView().window()->clipboardCopy(boo::EClipboardType::UTF8String,
|
|
(uint8_t*)&*begin.iter(), end.iter() - begin.iter());
|
|
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend());
|
|
size_t selStart = m_selectionStart;
|
|
setText(newStr);
|
|
setCursorPos(selStart);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void TextField::clipboardPaste()
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
|
|
size_t retSz;
|
|
std::unique_ptr<uint8_t[]> retData =
|
|
rootView().window()->clipboardPaste(boo::EClipboardType::UTF8String, retSz);
|
|
std::string saniData = SanitizeUTF8TextLine((char*)retData.get(), retSz);
|
|
|
|
if (retData && saniData.size())
|
|
{
|
|
if (m_selectionCount)
|
|
{
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
|
|
newStr.append(saniData);
|
|
size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend());
|
|
setText(newStr);
|
|
setCursorPos(newSel);
|
|
}
|
|
else
|
|
{
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter());
|
|
newStr.append(saniData);
|
|
size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter(), m_textStr.cend());
|
|
setText(newStr);
|
|
setCursorPos(newSel);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
if (m_deferredMarkStr.size())
|
|
return;
|
|
|
|
if (key == boo::ESpecialKey::Left)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
else if (key == boo::ESpecialKey::Right)
|
|
{
|
|
if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None)
|
|
{
|
|
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
if (m_cursorPos < len)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
else if (key == boo::ESpecialKey::Backspace)
|
|
{
|
|
if (m_selectionCount)
|
|
{
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend());
|
|
size_t selStart = m_selectionStart;
|
|
setText(newStr);
|
|
setCursorPos(selStart);
|
|
}
|
|
else if (m_cursorPos > 0)
|
|
{
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + (m_cursorPos-1)).iter());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter(), m_textStr.cend());
|
|
setText(newStr);
|
|
setCursorPos(m_cursorPos-1);
|
|
}
|
|
}
|
|
else if (key == boo::ESpecialKey::Delete)
|
|
{
|
|
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
if (m_selectionCount)
|
|
{
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend());
|
|
size_t selStart = m_selectionStart;
|
|
setText(newStr);
|
|
setCursorPos(selStart);
|
|
}
|
|
else if (m_cursorPos < len)
|
|
{
|
|
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter());
|
|
newStr.append((UTF8Iterator(m_textStr.cbegin()) + (m_cursorPos+1)).iter(), m_textStr.cend());
|
|
setText(newStr);
|
|
setCursorPos(m_cursorPos);
|
|
}
|
|
}
|
|
}
|
|
|
|
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};
|
|
return {m_cursorPos, UTF8Iterator(m_deferredMarkStr.cbegin()).countTo(m_deferredMarkStr.cend())};
|
|
}
|
|
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};
|
|
}
|
|
void TextField::setMarkedText(const std::string& str,
|
|
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);
|
|
UTF8Iterator begin(m_deferredTextStr.cbegin());
|
|
size_t curLen = UTF8Iterator(m_deferredTextStr.cbegin()).countTo(m_deferredTextStr.cend());
|
|
if (range.first >= curLen)
|
|
return std::string();
|
|
begin += range.first;
|
|
size_t endIdx = std::min(size_t(range.first + range.second), curLen);
|
|
UTF8Iterator end(m_deferredTextStr.cbegin());
|
|
end += endIdx;
|
|
actualRange.first = range.first;
|
|
actualRange.second = endIdx;
|
|
return std::string(begin.iter(), end.iter());
|
|
}
|
|
void TextField::insertText(const std::string& str, const std::pair<int,int>& range)
|
|
{
|
|
std::string saniStr = SanitizeUTF8TextLine(str.data(), str.size());
|
|
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
size_t curLen = UTF8Iterator(m_deferredTextStr.cbegin()).countTo(m_deferredTextStr.cend());
|
|
if (range.first < 0 || range.first >= curLen)
|
|
{
|
|
size_t beginPos = m_deferredCursorPos;
|
|
if (m_selectionCount)
|
|
beginPos = m_selectionCount;
|
|
beginPos = std::min(beginPos, curLen);
|
|
std::string newStr(m_deferredTextStr.cbegin(), (UTF8Iterator(m_deferredTextStr.cbegin())+beginPos).iter());
|
|
newStr += saniStr;
|
|
size_t newPos = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
|
|
newStr += std::string((UTF8Iterator(m_deferredTextStr.cbegin())+beginPos).iter(), m_deferredTextStr.cend());
|
|
setText(newStr);
|
|
setCursorPos(newPos);
|
|
unmarkText();
|
|
return;
|
|
}
|
|
|
|
std::string newStr(m_deferredTextStr.cbegin(), (UTF8Iterator(m_deferredTextStr.cbegin()) + range.first).iter());
|
|
newStr += saniStr;
|
|
size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
|
|
size_t endIdx = range.first + range.second;
|
|
if (endIdx >= newSel)
|
|
endIdx = newSel - 1;
|
|
newStr.append((UTF8Iterator(m_deferredTextStr.cbegin()) + endIdx).iter(), m_deferredTextStr.cend());
|
|
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);
|
|
UTF8Iterator begin(m_textStr.cbegin());
|
|
size_t curLen = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
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])}};
|
|
}
|
|
begin += range.first;
|
|
size_t endIdx = std::min(size_t(range.first + range.second), curLen);
|
|
UTF8Iterator end(m_textStr.cbegin());
|
|
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];
|
|
fprintf(stderr, "Ret %d %d\n", subRect().location[0] + int(g1.m_pos[1][0]), subRect().location[1] + int(g1.m_pos[1][1]));
|
|
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])}};
|
|
}
|
|
|
|
void TextField::think()
|
|
{
|
|
++m_cursorFrames;
|
|
++m_clickFrames;
|
|
++m_clickFrames2;
|
|
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
_setText();
|
|
_setSelectionRange();
|
|
_clearSelectionRange();
|
|
_setCursorPos();
|
|
_setMarkedText();
|
|
}
|
|
|
|
void TextField::setActive(bool active)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
m_active = active;
|
|
if (!active)
|
|
{
|
|
clearSelectionRange();
|
|
rootView().window()->claimKeyboardFocus(nullptr);
|
|
}
|
|
else if (!m_selectionCount)
|
|
{
|
|
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
setSelectionRange(0, len);
|
|
}
|
|
}
|
|
|
|
void TextField::_reallySetCursorPos(size_t pos)
|
|
{
|
|
float pf = rootView().viewRes().pixelFactor();
|
|
int offset1 = 4 * pf + m_text->queryReverseAdvance(pos);
|
|
int offset2 = offset1 + 2 * pf;
|
|
const Zeus::CColor& selColor = rootView().viewRes().themeData().textfieldSelection();
|
|
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_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
|
|
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
|
|
rootView().window()->claimKeyboardFocus(focusRect);
|
|
}
|
|
|
|
void TextField::_setCursorPos()
|
|
{
|
|
if (m_hasCursorSet)
|
|
{
|
|
m_hasSelectionClear = true;
|
|
_clearSelectionRange();
|
|
m_cursorPos = std::min(m_deferredCursorPos, UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend()));
|
|
m_deferredCursorPos = m_cursorPos;
|
|
m_cursorFrames = 0;
|
|
_reallySetCursorPos(m_cursorPos);
|
|
m_hasCursorSet = false;
|
|
}
|
|
}
|
|
|
|
void TextField::setCursorPos(size_t pos)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
m_deferredCursorPos = pos;
|
|
m_hasCursorSet = true;
|
|
}
|
|
|
|
void TextField::_reallySetSelectionRange(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];
|
|
for (size_t i=0 ; i<glyphs.size() ; ++i)
|
|
{
|
|
if (i >= start && i < start + len)
|
|
glyphs[i].m_color = rootView().themeData().selectedFieldText();
|
|
else
|
|
glyphs[i].m_color = rootView().themeData().fieldText();
|
|
}
|
|
m_text->updateGlyphs();
|
|
|
|
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_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
|
|
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
|
|
rootView().window()->claimKeyboardFocus(focusRect);
|
|
}
|
|
|
|
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];
|
|
|
|
const Zeus::CColor& selColor = rootView().themeData().textfieldMarkSelection();
|
|
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_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
|
|
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
|
|
rootView().window()->claimKeyboardFocus(focusRect);
|
|
}
|
|
|
|
void TextField::_setSelectionRange()
|
|
{
|
|
if (m_hasSelectionSet)
|
|
{
|
|
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
|
|
m_selectionStart = std::min(m_deferredSelectionStart, len-1);
|
|
m_deferredSelectionStart = m_selectionStart;
|
|
m_selectionCount = std::min(m_deferredSelectionCount, len-m_selectionStart);
|
|
m_deferredSelectionCount = m_selectionCount;
|
|
_reallySetSelectionRange(m_selectionStart, m_selectionCount);
|
|
m_hasSelectionSet = false;
|
|
}
|
|
}
|
|
|
|
void TextField::setSelectionRange(size_t start, size_t count)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
|
|
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;
|
|
|
|
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
|
|
for (size_t i=0 ; i<glyphs.size() ; ++i)
|
|
glyphs[i].m_color = rootView().themeData().fieldText();
|
|
m_text->updateGlyphs();
|
|
|
|
m_hasSelectionClear = false;
|
|
}
|
|
}
|
|
|
|
void TextField::clearSelectionRange()
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
|
|
m_deferredSelectionStart = 0;
|
|
m_deferredSelectionCount = 0;
|
|
m_hasSelectionClear = true;
|
|
m_hasSelectionSet = false;
|
|
}
|
|
|
|
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;
|
|
boo::SWindowRect newRect = sub;
|
|
newRect.size[1] = height;
|
|
View::resized(root, newRect);
|
|
|
|
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);
|
|
|
|
m_bVertsBuf->load(m_verts, sizeof(m_verts));
|
|
|
|
m_nomWidth = width;
|
|
m_nomHeight = height;
|
|
|
|
boo::SWindowRect textRect = sub;
|
|
textRect.location[0] += 5 * pf;
|
|
textRect.location[1] += 7 * pf;
|
|
m_text->resized(root, textRect);
|
|
}
|
|
|
|
void TextField::draw(boo::IGraphicsCommandQueue* gfxQ)
|
|
{
|
|
View::draw(gfxQ);
|
|
gfxQ->setShaderDataBinding(m_bShaderBinding);
|
|
gfxQ->setDrawPrimitive(boo::Primitive::TriStrips);
|
|
gfxQ->draw(0, 28);
|
|
if (m_active)
|
|
{
|
|
if (!m_selectionCount && !m_markSelCount)
|
|
{
|
|
if (m_cursorFrames % 60 < 30)
|
|
gfxQ->draw(28, 4);
|
|
}
|
|
else
|
|
gfxQ->draw(28, 4);
|
|
}
|
|
m_text->draw(gfxQ);
|
|
}
|
|
|
|
}
|