MyGUI 3.5.3
MyGUI_EditText.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_EditText.h"
10#include "MyGUI_RenderItem.h"
11#include "MyGUI_FontManager.h"
12#include "MyGUI_RenderManager.h"
14#include "MyGUI_TextIterator.h"
15#include "MyGUI_IRenderTarget.h"
16#include "MyGUI_FontData.h"
19
20namespace MyGUI
21{
22
24
25 EditText::EditText() :
26 mCountVertex(SIMPLETEXT_COUNT_VERTEX)
27 {
28 mVertexFormat = RenderManager::getInstance().getVertexFormat();
29 }
30
31 void EditText::setVisible(bool _visible)
32 {
33 if (mVisible == _visible)
34 return;
35 mVisible = _visible;
36
37 if (nullptr != mNode)
38 mNode->outOfDate(mRenderItem);
39 }
40
41 void EditText::_correctView()
42 {
43 if (nullptr != mNode)
44 mNode->outOfDate(mRenderItem);
45 }
46
47 void EditText::_setAlign(const IntSize& _oldsize)
48 {
49 if (mWordWrap)
50 {
51 // old coord is passed
52 int width = mCroppedParent->getWidth();
53 if (mOldWidth != width)
54 {
55 mOldWidth = width;
56 mTextOutDate = true;
57 }
58 }
59
60 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
61 mCoord = coord;
62 if (resize)
63 mIsMargin = true;
64
65 mCurrentCoord = mCoord;
66 _updateView();
67 }
68
69 void EditText::_updateView()
70 {
71 bool margin = _checkMargin();
72
73 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
74
75 mCurrentCoord.left = mCoord.left + mMargin.left;
76 mCurrentCoord.top = mCoord.top + mMargin.top;
77
78 // viewport became invalid
79 if (margin)
80 {
81 if (_checkOutsideUnrotated())
82 {
83 // remember current state
84 mIsMargin = margin;
85
86 // update before exit
87 if (nullptr != mNode)
88 mNode->outOfDate(mRenderItem);
89 return;
90 }
91 }
92
93 // we are clipped or were clipped
94 if (mIsMargin || margin)
95 {
96 mCurrentCoord.width = _getViewWidth();
97 mCurrentCoord.height = _getViewHeight();
98 }
99
100 // remember current state
101 mIsMargin = margin;
102
103 if (nullptr != mNode)
104 mNode->outOfDate(mRenderItem);
105 }
106
107 void EditText::setCaption(const UString& _value)
108 {
109 // Compare only when unchanged text can preserve the current layout.
110 if (!mTextOutDate && mCaption == _value)
111 return;
112
113 mCaption = _value;
114 mTextOutDate = true;
115
116 checkVertexSize();
117
118 if (nullptr != mNode)
119 mNode->outOfDate(mRenderItem);
120 }
121
122 void EditText::checkVertexSize()
123 {
124 // reallocate if we need more vertices (extra vertices for selection * 2 and cursor)
125 size_t need = (mCaption.size() * (mShadow ? 3 : 2) + 2) * VertexQuad::VertexCount;
126 if (mCountVertex < need)
127 {
128 mCountVertex = need + SIMPLETEXT_COUNT_VERTEX;
129 if (nullptr != mRenderItem)
130 mRenderItem->reallockDrawItem(this, mCountVertex);
131 }
132 }
133
134 unsigned int EditText::getMixedNativeAlpha(float secondAlpha) const
135 {
136 return (uint8)(mAlpha * secondAlpha * 255) << 24;
137 }
138
139 const UString& EditText::getCaption() const
140 {
141 return mCaption;
142 }
143
144 void EditText::setTextColour(const Colour& _value)
145 {
146 mManualColour = true;
147 _setTextColour(_value);
148 }
149
150 void EditText::_setTextColour(const Colour& _value)
151 {
152 if (mColour == _value)
153 return;
154
155 mColour = _value;
156 mCurrentColourNative = texture_utility::toNativeColour(mColour, mVertexFormat);
157
158 mCurrentColourNative = (mCurrentColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mColour.alpha);
159 mInverseColourNative = mCurrentColourNative ^ 0x00FFFFFF;
160
161 if (nullptr != mNode)
162 mNode->outOfDate(mRenderItem);
163 }
164
165 const Colour& EditText::getTextColour() const
166 {
167 return mColour;
168 }
169
170 void EditText::setAlpha(float _value)
171 {
172 if (mAlpha == _value)
173 return;
174 mAlpha = _value;
175
176 mCurrentColourNative = (mCurrentColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mColour.alpha);
177 mShadowColourNative = (mShadowColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mShadowColour.alpha);
178 mInverseColourNative = mCurrentColourNative ^ 0x00FFFFFF;
179
180 if (nullptr != mNode)
181 mNode->outOfDate(mRenderItem);
182 }
183
184 void EditText::setFontName(std::string_view _value)
185 {
186 mTexture = nullptr;
187 mFont = FontManager::getInstance().getByName(_value);
188 if (mFont != nullptr)
189 {
190 mTexture = mFont->getTextureFont();
191
192 // set default font height
193 if (mFont->getDefaultHeight() != 0 && !mCustomFontHeight)
194 {
195 mFontHeight = mFont->getDefaultHeight();
196 }
197 }
198
199 mTextOutDate = true;
200
201 // if we were attached, remove ourselves
202 if (nullptr != mRenderItem)
203 {
204 mRenderItem->removeDrawItem(this);
205 mRenderItem = nullptr;
206 }
207
208 // if there is a texture, attach
209 if (nullptr != mTexture && nullptr != mNode)
210 {
211 mRenderItem = mNode->addToRenderItem(mTexture, false);
212 mRenderItem->addDrawItem(this, mCountVertex);
213 }
214
215 if (nullptr != mNode)
216 mNode->outOfDate(mRenderItem);
217 }
218
219 std::string_view EditText::getFontName() const
220 {
221 return mFont->getResourceName();
222 }
223
224 void EditText::setFontHeight(int _value)
225 {
226 mCustomFontHeight = _value;
227 if (!mCustomFontHeight && mFont)
228 mFontHeight = mFont->getDefaultHeight();
229 else
230 mFontHeight = _value;
231
232 _invalidateTextLayout();
233 }
234
235 int EditText::getFontHeight() const
236 {
237 return mFontHeight;
238 }
239
240 void EditText::createDrawItem(ITexture* _texture, ILayerNode* _node)
241 {
242 mNode = _node;
243 // if there is already a texture, attach (relevant for layer change)
244 if (nullptr != mTexture)
245 {
246 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
247
248 mRenderItem = mNode->addToRenderItem(mTexture, false);
249 mRenderItem->addDrawItem(this, mCountVertex);
250 }
251 }
252
253 void EditText::destroyDrawItem()
254 {
255 if (nullptr != mRenderItem)
256 {
257 mRenderItem->removeDrawItem(this);
258 mRenderItem = nullptr;
259 }
260 mNode = nullptr;
261 }
262
263 size_t EditText::getTextSelectionStart() const
264 {
265 return mStartSelect;
266 }
267
268 size_t EditText::getTextSelectionEnd() const
269 {
270 return mEndSelect;
271 }
272
273 void EditText::setTextSelection(size_t _start, size_t _end)
274 {
275 if (mStartSelect == _start && mEndSelect == _end)
276 return;
277 mStartSelect = _start;
278 mEndSelect = _end;
279
280 if (nullptr != mNode)
281 mNode->outOfDate(mRenderItem);
282 }
283
284 bool EditText::getSelectBackground() const
285 {
286 return mBackgroundNormal;
287 }
288
289 void EditText::setSelectBackground(bool _normal)
290 {
291 if (mBackgroundNormal == _normal)
292 return;
293 mBackgroundNormal = _normal;
294
295 if (nullptr != mNode)
296 mNode->outOfDate(mRenderItem);
297 }
298
299 bool EditText::isVisibleCursor() const
300 {
301 return mVisibleCursor;
302 }
303
304 void EditText::setVisibleCursor(bool _value)
305 {
306 if (mVisibleCursor == _value)
307 return;
308 mVisibleCursor = _value;
309
310 if (nullptr != mNode)
311 mNode->outOfDate(mRenderItem);
312 }
313
314 size_t EditText::getCursorPosition() const
315 {
316 return mCursorPosition;
317 }
318
319 void EditText::setCursorPosition(size_t _index)
320 {
321 if (mCursorPosition == _index)
322 return;
323 mCursorPosition = _index;
324
325 if (nullptr != mNode)
326 mNode->outOfDate(mRenderItem);
327 }
328
329 void EditText::setTextAlign(Align _value)
330 {
331 if (mTextAlign == _value)
332 return;
333 mTextAlign = _value;
334
335 _invalidateTextLayout();
336 }
337
338 Align EditText::getTextAlign() const
339 {
340 return mTextAlign;
341 }
342
343 IntSize EditText::getTextSize() const
344 {
345 // if need to update, or screen proportions changed
346 if (mTextOutDate)
347 updateRawData();
348
349 IntSize size = mTextView.getViewSize();
350 // plus cursor size
351 if (mIsAddCursorWidth)
352 size.width += 2;
353
354 if (mShadow)
355 {
356 if (!mIsAddCursorWidth)
357 size.width++;
358 size.height++;
359 }
360
361 return size;
362 }
363
364 const VectorLineInfo& EditText::getLineInfo() const
365 {
366 return mTextView.getData();
367 }
368
369 void EditText::setViewOffset(const IntPoint& _point)
370 {
371 if (mViewOffset == _point)
372 return;
373 mViewOffset = _point;
374
375 if (nullptr != mNode)
376 mNode->outOfDate(mRenderItem);
377 }
378
379 IntPoint EditText::getViewOffset() const
380 {
381 return mViewOffset;
382 }
383
384 size_t EditText::getCursorPosition(const IntPoint& _point) const
385 {
386 if (nullptr == mFont)
387 return 0;
388
389 if (mTextOutDate)
390 updateRawData();
391
392 IntPoint point = _point;
393 point -= mCroppedParent->getAbsolutePosition();
394 point += mViewOffset;
395 point -= mCoord.point();
396
397 return mTextView.getCursorPosition(point);
398 }
399
400 IntCoord EditText::getCursorCoord(size_t _position) const
401 {
402 if (nullptr == mFont)
403 return {};
404
405 if (mTextOutDate)
406 updateRawData();
407
408 IntPoint point = mTextView.getCursorPoint(_position);
409 point += mCroppedParent->getAbsolutePosition();
410 point -= mViewOffset;
411 point += mCoord.point();
412
413 return {point.left, point.top, 2, mFontHeight};
414 }
415
416 void EditText::setShiftText(bool _value)
417 {
418 if (mShiftText == _value)
419 return;
420 mShiftText = _value;
421
422 if (nullptr != mNode)
423 mNode->outOfDate(mRenderItem);
424 }
425
426 void EditText::setWordWrap(bool _value)
427 {
428 if (mWordWrap == _value)
429 return;
430 mWordWrap = _value;
431 _invalidateTextLayout();
432 }
433
434 void EditText::_invalidateTextLayout()
435 {
436 mTextOutDate = true;
437
438 if (nullptr != mNode)
439 mNode->outOfDate(mRenderItem);
440 }
441
442 void EditText::updateRawData() const
443 {
444 if (nullptr == mFont)
445 return;
446 // reset flags
447 mTextOutDate = false;
448
449 int width = -1;
450 if (mWordWrap)
451 {
452 width = mCoord.width;
453 // word wrap by the width we actually use
454 if (mIsAddCursorWidth)
455 width -= 2;
456 }
457
458 mTextView.update(mCaption, mFont, mFontHeight, mTextAlign, mVertexFormat, width);
459 }
460
461 void EditText::setStateData(IStateInfo* _data)
462 {
463 EditTextStateInfo* data = _data->castType<EditTextStateInfo>();
464 if (!mManualColour && data->getColour() != Colour::Zero)
465 _setTextColour(data->getColour());
466 setShiftText(data->getShift());
467 }
468
469 void EditText::doRender()
470 {
471 if (nullptr == mFont || !mVisible || mEmptyView)
472 return;
473
474 if (mRenderItem->getCurrentUpdate() || mTextOutDate)
475 updateRawData();
476
477 Vertex* vertex = mRenderItem->getCurrentVertexBuffer();
478
479 const RenderTargetInfo& renderTargetInfo = mRenderItem->getRenderTarget()->getInfo();
480
481 // number of rendered vertices
482 size_t vertexCount = 0;
483
484 // current colours
485 uint32 colour = mCurrentColourNative;
486 uint32 inverseColour = mInverseColourNative;
487 uint32 selectedColour = mInvertSelect ? inverseColour : colour | 0x00FFFFFF;
488
489 const VectorLineInfo& textViewData = mTextView.getData();
490
491 float top = (float)(-mViewOffset.top + mCoord.top);
492
493 FloatRect vertexRect;
494
495 const FloatRect& selectedUVRect =
496 mFont->getGlyphInfo(mBackgroundNormal ? FontCodeType::Selected : FontCodeType::SelectedBack)->uvRect;
497
498 size_t index = 0;
499
500 for (const auto& line : textViewData)
501 {
502 float left = (float)(line.offset - mViewOffset.left + mCoord.left);
503
504 // Pass 1: find selection bounds on this line.
505 float selLeft = 0, selRight = 0;
506 bool hasSelection = false;
507 if (mStartSelect < mEndSelect)
508 {
509 float pos = left;
510 size_t charIndex = index;
511 for (const auto& sym : line.symbols)
512 {
513 if (sym.isColour())
514 continue;
515
516 pos += sym.getKerning();
517
518 if (charIndex >= mStartSelect && charIndex < mEndSelect)
519 {
520 if (!hasSelection)
521 selLeft = pos;
522 selRight = pos + sym.getAdvance();
523 hasSelection = true;
524 }
525 pos += sym.getAdvance();
526 charIndex++;
527 }
528 }
529
530 // Draw one selection rectangle for the entire line.
531 if (hasSelection)
532 {
533 vertexRect.set(selLeft, top, selRight, top + (float)mFontHeight);
534 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, selectedUVRect, selectedColour);
535 }
536
537 // Pass 2: render glyphs.
538 for (const auto& sym : line.symbols)
539 {
540 if (sym.isColour())
541 {
542 colour = sym.getColour() | (colour & 0xFF000000);
543 inverseColour = colour ^ 0x00FFFFFF;
544 continue;
545 }
546
547 bool select = index >= mStartSelect && index < mEndSelect;
548
549 left += sym.getKerning();
550
551 // Render the glyph shadow, if any.
552 if (mShadow)
553 {
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();
558
559 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, sym.getUVRect(), mShadowColourNative);
560 }
561
562 // Render the glyph itself.
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();
567
568 drawGlyph(
569 renderTargetInfo,
570 vertex,
571 vertexCount,
572 vertexRect,
573 sym.getUVRect(),
574 (!select || !mInvertSelect) ? colour : inverseColour);
575
576 left += sym.getAdvance();
577 ++index;
578 }
579
580 top += mFontHeight;
581 ++index;
582 }
583
584 // Render the cursor, if any, last.
585 if (mVisibleCursor)
586 {
587 IntPoint point = mTextView.getCursorPoint(mCursorPosition) - mViewOffset + mCoord.point();
588 const GlyphInfo* cursorGlyph = mFont->getGlyphInfo(static_cast<Char>(FontCodeType::Cursor));
589 vertexRect.set(
590 (float)point.left,
591 (float)point.top,
592 (float)point.left + cursorGlyph->width,
593 (float)(point.top + mFontHeight));
594
595 drawGlyph(
596 renderTargetInfo,
597 vertex,
598 vertexCount,
599 vertexRect,
600 cursorGlyph->uvRect,
601 mCurrentColourNative | 0x00FFFFFF);
602 }
603
604 // number of actually rendered vertices
605 mRenderItem->setLastVertexCount(vertexCount);
606 }
607
608 void EditText::setInvertSelected(bool _value)
609 {
610 if (mInvertSelect == _value)
611 return;
612 mInvertSelect = _value;
613
614 if (nullptr != mNode)
615 mNode->outOfDate(mRenderItem);
616 }
617
618 bool EditText::getInvertSelected() const
619 {
620 return mInvertSelect;
621 }
622
623 bool EditText::getShadow() const
624 {
625 return mShadow;
626 }
627
628 void EditText::setShadow(bool _value)
629 {
630 if (mShadow == _value)
631 return;
632 mShadow = _value;
633 mTextOutDate = true;
634
635 checkVertexSize();
636
637 if (nullptr != mNode)
638 mNode->outOfDate(mRenderItem);
639 }
640
641 void EditText::setShadowColour(const Colour& _value)
642 {
643 if (mShadowColour == _value)
644 return;
645 mShadowColour = _value;
646 mShadowColourNative = texture_utility::toNativeColour(mShadowColour, mVertexFormat);
647
648 mShadowColourNative = (mShadowColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mShadowColour.alpha);
649
650 if (nullptr != mNode)
651 mNode->outOfDate(mRenderItem);
652 }
653
654 const Colour& EditText::getShadowColour() const
655 {
656 return mShadowColour;
657 }
658
659 void EditText::drawQuad(
660 Vertex*& _vertex,
661 size_t& _vertexCount,
662 const FloatRect& _vertexRect,
663 float _vertexZ,
664 const FloatRect& _textureRect,
665 uint32 _colour) const
666 {
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;
673
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;
680
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;
687
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;
694
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;
701
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;
708
709 _vertex += VertexQuad::VertexCount;
710 _vertexCount += VertexQuad::VertexCount;
711 }
712
713 void EditText::drawGlyph(
714 const RenderTargetInfo& _renderTargetInfo,
715 Vertex*& _vertex,
716 size_t& _vertexCount,
717 FloatRect _vertexRect,
718 FloatRect _textureRect,
719 uint32 _colour) const
720 {
721 // character extends left
722 float leftClip = (float)mCurrentCoord.left - _vertexRect.left;
723 if (leftClip > 0.0f)
724 {
725 if ((float)mCurrentCoord.left < _vertexRect.right)
726 {
727 _textureRect.left += _textureRect.width() * leftClip / _vertexRect.width();
728 _vertexRect.left += leftClip;
729 }
730 else
731 {
732 return;
733 }
734 }
735
736 // character extends right
737 float rightClip = _vertexRect.right - (float)mCurrentCoord.right();
738 if (rightClip > 0.0f)
739 {
740 if (_vertexRect.left < (float)mCurrentCoord.right())
741 {
742 _textureRect.right -= _textureRect.width() * rightClip / _vertexRect.width();
743 _vertexRect.right -= rightClip;
744 }
745 else
746 {
747 return;
748 }
749 }
750
751 // character extends up
752 float topClip = (float)mCurrentCoord.top - _vertexRect.top;
753 if (topClip > 0.0f)
754 {
755 if ((float)mCurrentCoord.top < _vertexRect.bottom)
756 {
757 _textureRect.top += _textureRect.height() * topClip / _vertexRect.height();
758 _vertexRect.top += topClip;
759 }
760 else
761 {
762 return;
763 }
764 }
765
766 // character extends down
767 float bottomClip = _vertexRect.bottom - (float)mCurrentCoord.bottom();
768 if (bottomClip > 0.0f)
769 {
770 if (_vertexRect.top < (float)mCurrentCoord.bottom())
771 {
772 _textureRect.bottom -= _textureRect.height() * bottomClip / _vertexRect.height();
773 _vertexRect.bottom -= bottomClip;
774 }
775 else
776 {
777 return;
778 }
779 }
780
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),
784 _renderTargetInfo,
785 mCroppedParent->getAbsolutePosition());
786
787 drawQuad(_vertex, _vertexCount, vertexRect, mNode->getNodeDepth(), _textureRect, _colour);
788 }
789
790} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
uint32_t uint32
Definition MyGUI_Types.h:47
std::vector< LineInfo > VectorLineInfo
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
unsigned int Char
Definition MyGUI_Types.h:49
const size_t SIMPLETEXT_COUNT_VERTEX
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
void set(T const &_left, T const &_top)