25 EditText::EditText() :
28 mVertexFormat = RenderManager::getInstance().getVertexFormat();
31 void EditText::setVisible(
bool _visible)
33 if (mVisible == _visible)
38 mNode->outOfDate(mRenderItem);
41 void EditText::_correctView()
44 mNode->outOfDate(mRenderItem);
47 void EditText::_setAlign(
const IntSize& _oldsize)
52 int width = mCroppedParent->getWidth();
53 if (mOldWidth != width)
60 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
65 mCurrentCoord = mCoord;
69 void EditText::_updateView()
71 bool margin = _checkMargin();
73 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
75 mCurrentCoord.left = mCoord.left + mMargin.left;
76 mCurrentCoord.top = mCoord.top + mMargin.top;
81 if (_checkOutsideUnrotated())
88 mNode->outOfDate(mRenderItem);
94 if (mIsMargin || margin)
96 mCurrentCoord.width = _getViewWidth();
97 mCurrentCoord.height = _getViewHeight();
103 if (
nullptr != mNode)
104 mNode->outOfDate(mRenderItem);
107 void EditText::setCaption(
const UString& _value)
110 if (!mTextOutDate && mCaption == _value)
118 if (
nullptr != mNode)
119 mNode->outOfDate(mRenderItem);
122 void EditText::checkVertexSize()
125 size_t need = (mCaption.size() * (mShadow ? 3 : 2) + 2) * VertexQuad::VertexCount;
126 if (mCountVertex < need)
129 if (
nullptr != mRenderItem)
130 mRenderItem->reallockDrawItem(
this, mCountVertex);
134 unsigned int EditText::getMixedNativeAlpha(
float secondAlpha)
const
136 return (uint8)(mAlpha * secondAlpha * 255) << 24;
139 const UString& EditText::getCaption()
const
144 void EditText::setTextColour(
const Colour& _value)
146 mManualColour =
true;
147 _setTextColour(_value);
150 void EditText::_setTextColour(
const Colour& _value)
152 if (mColour == _value)
156 mCurrentColourNative = texture_utility::toNativeColour(mColour, mVertexFormat);
158 mCurrentColourNative = (mCurrentColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mColour.alpha);
159 mInverseColourNative = mCurrentColourNative ^ 0x00FFFFFF;
161 if (
nullptr != mNode)
162 mNode->outOfDate(mRenderItem);
165 const Colour& EditText::getTextColour()
const
170 void EditText::setAlpha(
float _value)
172 if (mAlpha == _value)
176 mCurrentColourNative = (mCurrentColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mColour.alpha);
177 mShadowColourNative = (mShadowColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mShadowColour.alpha);
178 mInverseColourNative = mCurrentColourNative ^ 0x00FFFFFF;
180 if (
nullptr != mNode)
181 mNode->outOfDate(mRenderItem);
184 void EditText::setFontName(std::string_view _value)
187 mFont = FontManager::getInstance().getByName(_value);
188 if (mFont !=
nullptr)
190 mTexture = mFont->getTextureFont();
193 if (mFont->getDefaultHeight() != 0 && !mCustomFontHeight)
195 mFontHeight = mFont->getDefaultHeight();
202 if (
nullptr != mRenderItem)
204 mRenderItem->removeDrawItem(
this);
205 mRenderItem =
nullptr;
209 if (
nullptr != mTexture &&
nullptr != mNode)
211 mRenderItem = mNode->addToRenderItem(mTexture,
false);
212 mRenderItem->addDrawItem(
this, mCountVertex);
215 if (
nullptr != mNode)
216 mNode->outOfDate(mRenderItem);
219 std::string_view EditText::getFontName()
const
221 return mFont->getResourceName();
224 void EditText::setFontHeight(
int _value)
226 mCustomFontHeight = _value;
227 if (!mCustomFontHeight && mFont)
228 mFontHeight = mFont->getDefaultHeight();
230 mFontHeight = _value;
232 _invalidateTextLayout();
235 int EditText::getFontHeight()
const
240 void EditText::createDrawItem(ITexture* _texture, ILayerNode* _node)
244 if (
nullptr != mTexture)
246 MYGUI_ASSERT(!mRenderItem,
"mRenderItem must be nullptr");
248 mRenderItem = mNode->addToRenderItem(mTexture,
false);
249 mRenderItem->addDrawItem(
this, mCountVertex);
253 void EditText::destroyDrawItem()
255 if (
nullptr != mRenderItem)
257 mRenderItem->removeDrawItem(
this);
258 mRenderItem =
nullptr;
263 size_t EditText::getTextSelectionStart()
const
268 size_t EditText::getTextSelectionEnd()
const
273 void EditText::setTextSelection(
size_t _start,
size_t _end)
275 if (mStartSelect == _start && mEndSelect == _end)
277 mStartSelect = _start;
280 if (
nullptr != mNode)
281 mNode->outOfDate(mRenderItem);
284 bool EditText::getSelectBackground()
const
286 return mBackgroundNormal;
289 void EditText::setSelectBackground(
bool _normal)
291 if (mBackgroundNormal == _normal)
293 mBackgroundNormal = _normal;
295 if (
nullptr != mNode)
296 mNode->outOfDate(mRenderItem);
299 bool EditText::isVisibleCursor()
const
301 return mVisibleCursor;
304 void EditText::setVisibleCursor(
bool _value)
306 if (mVisibleCursor == _value)
308 mVisibleCursor = _value;
310 if (
nullptr != mNode)
311 mNode->outOfDate(mRenderItem);
314 size_t EditText::getCursorPosition()
const
316 return mCursorPosition;
319 void EditText::setCursorPosition(
size_t _index)
321 if (mCursorPosition == _index)
323 mCursorPosition = _index;
325 if (
nullptr != mNode)
326 mNode->outOfDate(mRenderItem);
329 void EditText::setTextAlign(Align _value)
331 if (mTextAlign == _value)
335 _invalidateTextLayout();
338 Align EditText::getTextAlign()
const
343 IntSize EditText::getTextSize()
const
349 IntSize size = mTextView.getViewSize();
351 if (mIsAddCursorWidth)
356 if (!mIsAddCursorWidth)
366 return mTextView.getData();
369 void EditText::setViewOffset(
const IntPoint& _point)
371 if (mViewOffset == _point)
373 mViewOffset = _point;
375 if (
nullptr != mNode)
376 mNode->outOfDate(mRenderItem);
379 IntPoint EditText::getViewOffset()
const
384 size_t EditText::getCursorPosition(
const IntPoint& _point)
const
386 if (
nullptr == mFont)
393 point -= mCroppedParent->getAbsolutePosition();
394 point += mViewOffset;
395 point -= mCoord.point();
397 return mTextView.getCursorPosition(point);
400 IntCoord EditText::getCursorCoord(
size_t _position)
const
402 if (
nullptr == mFont)
408 IntPoint point = mTextView.getCursorPoint(_position);
409 point += mCroppedParent->getAbsolutePosition();
410 point -= mViewOffset;
411 point += mCoord.point();
413 return {point.left, point.top, 2, mFontHeight};
416 void EditText::setShiftText(
bool _value)
418 if (mShiftText == _value)
422 if (
nullptr != mNode)
423 mNode->outOfDate(mRenderItem);
426 void EditText::setWordWrap(
bool _value)
428 if (mWordWrap == _value)
431 _invalidateTextLayout();
434 void EditText::_invalidateTextLayout()
438 if (
nullptr != mNode)
439 mNode->outOfDate(mRenderItem);
442 void EditText::updateRawData()
const
444 if (
nullptr == mFont)
447 mTextOutDate =
false;
452 width = mCoord.width;
454 if (mIsAddCursorWidth)
458 mTextView.update(mCaption, mFont, mFontHeight, mTextAlign, mVertexFormat, width);
461 void EditText::setStateData(IStateInfo* _data)
463 EditTextStateInfo* data = _data->castType<EditTextStateInfo>();
464 if (!mManualColour && data->getColour() != Colour::Zero)
465 _setTextColour(data->getColour());
466 setShiftText(data->getShift());
469 void EditText::doRender()
471 if (
nullptr == mFont || !mVisible || mEmptyView)
474 if (mRenderItem->getCurrentUpdate() || mTextOutDate)
477 Vertex* vertex = mRenderItem->getCurrentVertexBuffer();
479 const RenderTargetInfo& renderTargetInfo = mRenderItem->getRenderTarget()->getInfo();
482 size_t vertexCount = 0;
485 uint32 colour = mCurrentColourNative;
486 uint32 inverseColour = mInverseColourNative;
487 uint32 selectedColour = mInvertSelect ? inverseColour : colour | 0x00FFFFFF;
491 float top = (float)(-mViewOffset.top + mCoord.top);
496 mFont->getGlyphInfo(mBackgroundNormal ? FontCodeType::Selected : FontCodeType::SelectedBack)->uvRect;
500 for (
const auto& line : textViewData)
502 float left = (float)(line.offset - mViewOffset.left + mCoord.left);
505 float selLeft = 0, selRight = 0;
506 bool hasSelection =
false;
507 if (mStartSelect < mEndSelect)
510 size_t charIndex = index;
511 for (
const auto& sym : line.symbols)
516 pos += sym.getKerning();
518 if (charIndex >= mStartSelect && charIndex < mEndSelect)
522 selRight = pos + sym.getAdvance();
525 pos += sym.getAdvance();
533 vertexRect.set(selLeft, top, selRight, top + (
float)mFontHeight);
534 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, selectedUVRect, selectedColour);
538 for (
const auto& sym : line.symbols)
542 colour = sym.getColour() | (colour & 0xFF000000);
543 inverseColour = colour ^ 0x00FFFFFF;
547 bool select = index >= mStartSelect && index < mEndSelect;
549 left += sym.getKerning();
554 vertexRect.left = left + sym.getBearingX() + 1.0f;
555 vertexRect.top = top + sym.getBearingY() + 1.0f;
556 vertexRect.right = vertexRect.left + sym.getWidth();
557 vertexRect.bottom = vertexRect.top + sym.getHeight();
559 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, sym.getUVRect(), mShadowColourNative);
563 vertexRect.left = left + sym.getBearingX();
564 vertexRect.top = top + sym.getBearingY();
565 vertexRect.right = vertexRect.left + sym.getWidth();
566 vertexRect.bottom = vertexRect.top + sym.getHeight();
574 (!select || !mInvertSelect) ? colour : inverseColour);
576 left += sym.getAdvance();
587 IntPoint point = mTextView.getCursorPoint(mCursorPosition) - mViewOffset + mCoord.point();
588 const GlyphInfo* cursorGlyph = mFont->getGlyphInfo(
static_cast<Char>(FontCodeType::Cursor));
592 (
float)point.left + cursorGlyph->width,
593 (
float)(point.top + mFontHeight));
601 mCurrentColourNative | 0x00FFFFFF);
605 mRenderItem->setLastVertexCount(vertexCount);
608 void EditText::setInvertSelected(
bool _value)
610 if (mInvertSelect == _value)
612 mInvertSelect = _value;
614 if (
nullptr != mNode)
615 mNode->outOfDate(mRenderItem);
618 bool EditText::getInvertSelected()
const
620 return mInvertSelect;
623 bool EditText::getShadow()
const
628 void EditText::setShadow(
bool _value)
630 if (mShadow == _value)
637 if (
nullptr != mNode)
638 mNode->outOfDate(mRenderItem);
641 void EditText::setShadowColour(
const Colour& _value)
643 if (mShadowColour == _value)
645 mShadowColour = _value;
646 mShadowColourNative = texture_utility::toNativeColour(mShadowColour, mVertexFormat);
648 mShadowColourNative = (mShadowColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mShadowColour.alpha);
650 if (
nullptr != mNode)
651 mNode->outOfDate(mRenderItem);
654 const Colour& EditText::getShadowColour()
const
656 return mShadowColour;
659 void EditText::drawQuad(
661 size_t& _vertexCount,
662 const FloatRect& _vertexRect,
664 const FloatRect& _textureRect,
665 uint32 _colour)
const
667 _vertex[0].x = _vertexRect.left;
668 _vertex[0].y = _vertexRect.top;
669 _vertex[0].z = _vertexZ;
670 _vertex[0].colour = _colour;
671 _vertex[0].u = _textureRect.left;
672 _vertex[0].v = _textureRect.top;
674 _vertex[2].x = _vertexRect.left;
675 _vertex[2].y = _vertexRect.bottom;
676 _vertex[2].z = _vertexZ;
677 _vertex[2].colour = _colour;
678 _vertex[2].u = _textureRect.left;
679 _vertex[2].v = _textureRect.bottom;
681 _vertex[1].x = _vertexRect.right;
682 _vertex[1].y = _vertexRect.top;
683 _vertex[1].z = _vertexZ;
684 _vertex[1].colour = _colour;
685 _vertex[1].u = _textureRect.right;
686 _vertex[1].v = _textureRect.top;
688 _vertex[3].x = _vertexRect.right;
689 _vertex[3].y = _vertexRect.top;
690 _vertex[3].z = _vertexZ;
691 _vertex[3].colour = _colour;
692 _vertex[3].u = _textureRect.right;
693 _vertex[3].v = _textureRect.top;
695 _vertex[5].x = _vertexRect.left;
696 _vertex[5].y = _vertexRect.bottom;
697 _vertex[5].z = _vertexZ;
698 _vertex[5].colour = _colour;
699 _vertex[5].u = _textureRect.left;
700 _vertex[5].v = _textureRect.bottom;
702 _vertex[4].x = _vertexRect.right;
703 _vertex[4].y = _vertexRect.bottom;
704 _vertex[4].z = _vertexZ;
705 _vertex[4].colour = _colour;
706 _vertex[4].u = _textureRect.right;
707 _vertex[4].v = _textureRect.bottom;
709 _vertex += VertexQuad::VertexCount;
710 _vertexCount += VertexQuad::VertexCount;
713 void EditText::drawGlyph(
714 const RenderTargetInfo& _renderTargetInfo,
716 size_t& _vertexCount,
717 FloatRect _vertexRect,
718 FloatRect _textureRect,
719 uint32 _colour)
const
722 float leftClip = (float)mCurrentCoord.left - _vertexRect.left;
725 if ((
float)mCurrentCoord.left < _vertexRect.right)
727 _textureRect.left += _textureRect.width() * leftClip / _vertexRect.width();
728 _vertexRect.left += leftClip;
737 float rightClip = _vertexRect.right - (float)mCurrentCoord.right();
738 if (rightClip > 0.0f)
740 if (_vertexRect.left < (
float)mCurrentCoord.right())
742 _textureRect.right -= _textureRect.width() * rightClip / _vertexRect.width();
743 _vertexRect.right -= rightClip;
752 float topClip = (float)mCurrentCoord.top - _vertexRect.top;
755 if ((
float)mCurrentCoord.top < _vertexRect.bottom)
757 _textureRect.top += _textureRect.height() * topClip / _vertexRect.height();
758 _vertexRect.top += topClip;
767 float bottomClip = _vertexRect.bottom - (float)mCurrentCoord.bottom();
768 if (bottomClip > 0.0f)
770 if (_vertexRect.top < (
float)mCurrentCoord.bottom())
772 _textureRect.bottom -= _textureRect.height() * bottomClip / _vertexRect.height();
773 _vertexRect.bottom -= bottomClip;
781 const float shift = mShiftText ? 1.0f : 0.0f;
782 const FloatRect vertexRect = geometry_utility::toRenderTarget(
783 FloatRect(_vertexRect.left, _vertexRect.top + shift, _vertexRect.right, _vertexRect.bottom + shift),
785 mCroppedParent->getAbsolutePosition());
787 drawQuad(_vertex, _vertexCount, vertexRect, mNode->getNodeDepth(), _textureRect, _colour);
#define MYGUI_ASSERT(exp, dest)
std::vector< LineInfo > VectorLineInfo
types::TPoint< int > IntPoint
types::TRect< float > FloatRect
const size_t SIMPLETEXT_COUNT_VERTEX
types::TCoord< int > IntCoord
types::TSize< int > IntSize
void set(T const &_left, T const &_top)