MyGUI 3.4.2
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"
9#include "MyGUI_RenderItem.h"
10#include "MyGUI_FontManager.h"
11#include "MyGUI_RenderManager.h"
13#include "MyGUI_TextIterator.h"
14#include "MyGUI_IRenderTarget.h"
15#include "MyGUI_FontData.h"
17
18namespace MyGUI
19{
20
22
25 mEmptyView(false),
26 mCurrentColourNative(0xFFFFFFFF),
27 mInverseColourNative(0xFF000000),
28 mShadowColourNative(0x00000000),
29 mTextOutDate(false),
30 mTextAlign(Align::Default),
31 mColour(Colour::White),
32 mShadowColour(Colour::Black),
33 mAlpha(ALPHA_MAX),
34 mFont(nullptr),
35 mTexture(nullptr),
36 mFontHeight(0),
37 mBackgroundNormal(true),
38 mStartSelect(0),
39 mEndSelect(0),
40 mCursorPosition(0),
41 mVisibleCursor(false),
42 mInvertSelect(true),
43 mShadow(false),
44 mNode(nullptr),
45 mRenderItem(nullptr),
46 mCountVertex(SIMPLETEXT_COUNT_VERTEX),
47 mIsAddCursorWidth(true),
48 mShiftText(false),
49 mWordWrap(false),
50 mManualColour(false),
51 mOldWidth(0)
52 {
53 mVertexFormat = RenderManager::getInstance().getVertexFormat();
54 }
55
57 {
58 if (mVisible == _visible)
59 return;
61
62 if (nullptr != mNode)
64 }
65
67 {
68 if (nullptr != mNode)
70 }
71
73 {
74 if (mWordWrap)
75 {
76 // передается старая координата всегда
77 int width = mCroppedParent->getWidth();
78 if (mOldWidth != width)
79 {
80 mOldWidth = width;
81 mTextOutDate = true;
82 }
83 }
84
85 // первоначальное выравнивание
86 if (mAlign.isHStretch())
87 {
88 // растягиваем
90 mIsMargin = true; // при изменении размеров все пересчитывать
91 }
92 else if (mAlign.isRight())
93 {
94 // двигаем по правому краю
96 }
97 else if (mAlign.isHCenter())
98 {
99 // выравнивание по горизонтали без растяжения
101 }
102
103 if (mAlign.isVStretch())
104 {
105 // растягиваем
107 mIsMargin = true; // при изменении размеров все пересчитывать
108 }
109 else if (mAlign.isBottom())
110 {
111 // двигаем по нижнему краю
113 }
114 else if (mAlign.isVCenter())
115 {
116 // выравнивание по вертикали без растяжения
118 }
119
121 _updateView();
122 }
123
125 {
126 bool margin = _checkMargin();
127
128 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
129
132
133 // вьюпорт стал битым
134 if (margin)
135 {
136 // проверка на полный выход за границу
137 if (_checkOutside())
138 {
139 // запоминаем текущее состояние
141
142 // обновить перед выходом
143 if (nullptr != mNode)
145 return;
146 }
147 }
148
149 // мы обрезаны или были обрезаны
150 if (mIsMargin || margin)
151 {
154 }
155
156 // запоминаем текущее состояние
158
159 if (nullptr != mNode)
161 }
162
164 {
167 mTextOutDate = true;
168
169 checkVertexSize();
170
171 if (nullptr != mNode)
173 }
174
175 void EditText::checkVertexSize()
176 {
177 // reallocate if we need more vertices (extra vertices for selection * 2 and cursor)
178 size_t need = (mUtf32Caption.size() * (mShadow ? 3 : 2) + 2) * VertexQuad::VertexCount;
179 if (mCountVertex < need)
180 {
182 if (nullptr != mRenderItem)
184 }
185 }
186
187 unsigned int EditText::getMixedNativeAlpha(float secondAlpha) const
188 {
189 return (uint8)(mAlpha * secondAlpha * 255) << 24;
190 }
191
193 {
194 return mCaption;
195 }
196
198 {
199 mManualColour = true;
200 _setTextColour(_value);
201 }
202
203 void EditText::_setTextColour(const Colour& _value)
204 {
205 if (mColour == _value)
206 return;
207
208 mColour = _value;
210
211 mCurrentColourNative = (mCurrentColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mColour.alpha);
213
214 if (nullptr != mNode)
216 }
217
219 {
220 return mColour;
221 }
222
224 {
225 if (mAlpha == _value)
226 return;
227 mAlpha = _value;
228
229 mCurrentColourNative = (mCurrentColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mColour.alpha);
230 mShadowColourNative = (mShadowColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mShadowColour.alpha);
232
233 if (nullptr != mNode)
235 }
236
237 void EditText::setFontName(const std::string& _value)
238 {
239 mTexture = nullptr;
241 if (mFont != nullptr)
242 {
244
245 // если надо, устанавливаем дефолтный размер шрифта
246 if (mFont->getDefaultHeight() != 0)
247 {
249 }
250 }
251
252 mTextOutDate = true;
253
254 // если мы были приаттаченны, то удаляем себя
255 if (nullptr != mRenderItem)
256 {
258 mRenderItem = nullptr;
259 }
260
261 // если есть текстура, то приаттачиваемся
262 if (nullptr != mTexture && nullptr != mNode)
263 {
264 mRenderItem = mNode->addToRenderItem(mTexture, false, false);
266 }
267
268 if (nullptr != mNode)
270 }
271
272 const std::string& EditText::getFontName() const
273 {
274 return mFont->getResourceName();
275 }
276
278 {
280 mTextOutDate = true;
281
282 if (nullptr != mNode)
284 }
285
287 {
288 return mFontHeight;
289 }
290
292 {
293 mNode = _node;
294 // если уже есть текстура, то атачимся, актуально для смены леера
295 if (nullptr != mTexture)
296 {
297 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
298
299 mRenderItem = mNode->addToRenderItem(mTexture, false, false);
301 }
302 }
303
305 {
306 if (nullptr != mRenderItem)
307 {
309 mRenderItem = nullptr;
310 }
311 mNode = nullptr;
312 }
313
315 {
316 return mStartSelect;
317 }
318
320 {
321 return mEndSelect;
322 }
323
325 {
328
329 if (nullptr != mNode)
331 }
332
334 {
335 return mBackgroundNormal;
336 }
337
339 {
341 return;
343
344 if (nullptr != mNode)
346 }
347
349 {
350 return mVisibleCursor;
351 }
352
354 {
355 if (mVisibleCursor == _value)
356 return;
358
359 if (nullptr != mNode)
361 }
362
364 {
365 return mCursorPosition;
366 }
367
369 {
370 if (mCursorPosition == _index)
371 return;
373
374 if (nullptr != mNode)
376 }
377
379 {
381
382 if (nullptr != mNode)
384 }
385
387 {
388 return mTextAlign;
389 }
390
392 {
393 // если нуно обновить, или изменились пропорции экрана
394 if (mTextOutDate)
396
398 // плюс размер курсора
400 size.width += 2;
401
402 if (mShadow)
403 {
405 size.width ++;
406 size.height ++;
407 }
408
409 return size;
410 }
411
413 {
414 return mTextView.getData();
415 }
416
418 {
420
421 if (nullptr != mNode)
423 }
424
426 {
427 return mViewOffset;
428 }
429
431 {
432 if (nullptr == mFont)
433 return 0;
434
435 if (mTextOutDate)
437
438 IntPoint point = _point;
440 point += mViewOffset;
441 point -= mCoord.point();
442
443 return mTextView.getCursorPosition(point);
444 }
445
447 {
448 if (nullptr == mFont)
449 return IntCoord();
450
451 if (mTextOutDate)
453
456 point -= mViewOffset;
457 point += mCoord.point();
458
459 return IntCoord(point.left, point.top, 2, mFontHeight);
460 }
461
463 {
464 if (mShiftText == _value)
465 return;
467
468 if (nullptr != mNode)
470 }
471
473 {
475 mTextOutDate = true;
476
477 if (nullptr != mNode)
479 }
480
482 {
483 if (nullptr == mFont)
484 return;
485 // сбрасывам флаги
486 mTextOutDate = false;
487
488 int width = -1;
489 if (mWordWrap)
490 {
491 width = mCoord.width;
492 // обрезать слова нужно по шарине, которую мы реально используем
494 width -= 2;
495 }
496
498 }
499
501 {
502 EditTextStateInfo* data = _data->castType<EditTextStateInfo>();
503 if (!mManualColour && data->getColour() != Colour::Zero)
504 _setTextColour(data->getColour());
505 setShiftText(data->getShift());
506 }
507
509 {
510 if (nullptr == mFont || !mVisible || mEmptyView)
511 return;
512
515
517
519
520 // колличество отрисованных вершин
521 size_t vertexCount = 0;
522
523 // текущие цвета
526 uint32 selectedColour = mInvertSelect ? inverseColour : colour | 0x00FFFFFF;
527
529
530 float top = (float)(-mViewOffset.top + mCoord.top);
531
533
535
536 size_t index = 0;
537
538 for (VectorLineInfo::const_iterator line = textViewData.begin(); line != textViewData.end(); ++line)
539 {
540 float left = (float)(line->offset - mViewOffset.left + mCoord.left);
541
542 for (VectorCharInfo::const_iterator sim = line->symbols.begin(); sim != line->symbols.end(); ++sim)
543 {
544 if (sim->isColour())
545 {
546 colour = sim->getColour() | (colour & 0xFF000000);
547 inverseColour = colour ^ 0x00FFFFFF;
548 selectedColour = mInvertSelect ? inverseColour : colour | 0x00FFFFFF;
549 continue;
550 }
551
552 // смещение текстуры для фона
553 bool select = index >= mStartSelect && index < mEndSelect;
554
555 float fullAdvance = sim->getBearingX() + sim->getAdvance();
556
557 // Render the selection, if any, first.
558 if (select)
559 {
560 vertexRect.set(left, top, left + fullAdvance, top + (float)mFontHeight);
561
563 }
564
565 // Render the glyph shadow, if any.
566 if (mShadow)
567 {
568 vertexRect.left = left + sim->getBearingX() + 1.0f;
569 vertexRect.top = top + sim->getBearingY() + 1.0f;
570 vertexRect.right = vertexRect.left + sim->getWidth();
571 vertexRect.bottom = vertexRect.top + sim->getHeight();
572
573 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, sim->getUVRect(), mShadowColourNative);
574 }
575
576 // Render the glyph itself.
577 vertexRect.left = left + sim->getBearingX();
578 vertexRect.top = top + sim->getBearingY();
579 vertexRect.right = vertexRect.left + sim->getWidth();
580 vertexRect.bottom = vertexRect.top + sim->getHeight();
581
582 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, sim->getUVRect(), (!select || !mInvertSelect) ? colour : inverseColour);
583
584 left += fullAdvance;
585 ++index;
586 }
587
588 top += mFontHeight;
589 ++index;
590 }
591
592 // Render the cursor, if any, last.
593 if (mVisibleCursor)
594 {
597 vertexRect.set((float)point.left, (float)point.top, (float)point.left + cursorGlyph->width, (float)(point.top + mFontHeight));
598
599 drawGlyph(renderTargetInfo, vertex, vertexCount, vertexRect, cursorGlyph->uvRect, mCurrentColourNative | 0x00FFFFFF);
600 }
601
602 // колличество реально отрисованных вершин
604 }
605
607 {
608 if (mInvertSelect == _value)
609 return;
611
612 if (nullptr != mNode)
614 }
615
617 {
618 return mInvertSelect;
619 }
620
622 {
623 return mShadow;
624 }
625
627 {
628 mShadow = _value;
629 mTextOutDate = true;
630
631 checkVertexSize();
632
633 if (nullptr != mNode)
635 }
636
638 {
641
642 mShadowColourNative = (mShadowColourNative & 0x00FFFFFF) | getMixedNativeAlpha(mShadowColour.alpha);
643
644 if (nullptr != mNode)
646 }
647
649 {
650 return mShadowColour;
651 }
652
653 void EditText::drawQuad(
654 Vertex*& _vertex,
655 size_t& _vertexCount,
656 const FloatRect& _vertexRect,
657 float _vertexZ,
658 const FloatRect& _textureRect,
659 uint32 _colour) const
660 {
661 _vertex[0].x = _vertexRect.left;
662 _vertex[0].y = _vertexRect.top;
663 _vertex[0].z = _vertexZ;
664 _vertex[0].colour = _colour;
665 _vertex[0].u = _textureRect.left;
666 _vertex[0].v = _textureRect.top;
667
668 _vertex[2].x = _vertexRect.left;
669 _vertex[2].y = _vertexRect.bottom;
670 _vertex[2].z = _vertexZ;
671 _vertex[2].colour = _colour;
672 _vertex[2].u = _textureRect.left;
673 _vertex[2].v = _textureRect.bottom;
674
675 _vertex[1].x = _vertexRect.right;
676 _vertex[1].y = _vertexRect.top;
677 _vertex[1].z = _vertexZ;
678 _vertex[1].colour = _colour;
679 _vertex[1].u = _textureRect.right;
680 _vertex[1].v = _textureRect.top;
681
682 _vertex[3].x = _vertexRect.right;
683 _vertex[3].y = _vertexRect.top;
684 _vertex[3].z = _vertexZ;
685 _vertex[3].colour = _colour;
686 _vertex[3].u = _textureRect.right;
687 _vertex[3].v = _textureRect.top;
688
689 _vertex[5].x = _vertexRect.left;
690 _vertex[5].y = _vertexRect.bottom;
691 _vertex[5].z = _vertexZ;
692 _vertex[5].colour = _colour;
693 _vertex[5].u = _textureRect.left;
694 _vertex[5].v = _textureRect.bottom;
695
696 _vertex[4].x = _vertexRect.right;
697 _vertex[4].y = _vertexRect.bottom;
698 _vertex[4].z = _vertexZ;
699 _vertex[4].colour = _colour;
700 _vertex[4].u = _textureRect.right;
701 _vertex[4].v = _textureRect.bottom;
702
705 }
706
707 void EditText::drawGlyph(
708 const RenderTargetInfo& _renderTargetInfo,
709 Vertex*& _vertex,
710 size_t& _vertexCount,
711 FloatRect _vertexRect,
712 FloatRect _textureRect,
713 uint32 _colour) const
714 {
715 // символ залазиет влево
716 float leftClip = (float)mCurrentCoord.left - _vertexRect.left;
717 if (leftClip > 0.0f)
718 {
719 if ((float)mCurrentCoord.left < _vertexRect.right)
720 {
721 _textureRect.left += _textureRect.width() * leftClip / _vertexRect.width();
722 _vertexRect.left += leftClip;
723 }
724 else
725 {
726 return;
727 }
728 }
729
730 // символ залазиет вправо
731 float rightClip = _vertexRect.right - (float)mCurrentCoord.right();
732 if (rightClip > 0.0f)
733 {
734 if (_vertexRect.left < (float)mCurrentCoord.right())
735 {
736 _textureRect.right -= _textureRect.width() * rightClip / _vertexRect.width();
737 _vertexRect.right -= rightClip;
738 }
739 else
740 {
741 return;
742 }
743 }
744
745 // символ залазиет вверх
746 float topClip = (float)mCurrentCoord.top - _vertexRect.top;
747 if (topClip > 0.0f)
748 {
749 if ((float)mCurrentCoord.top < _vertexRect.bottom)
750 {
751 _textureRect.top += _textureRect.height() * topClip / _vertexRect.height();
752 _vertexRect.top += topClip;
753 }
754 else
755 {
756 return;
757 }
758 }
759
760 // символ залазиет вниз
761 float bottomClip = _vertexRect.bottom - (float)mCurrentCoord.bottom();
762 if (bottomClip > 0.0f)
763 {
764 if (_vertexRect.top < (float)mCurrentCoord.bottom())
765 {
766 _textureRect.bottom -= _textureRect.height() * bottomClip / _vertexRect.height();
767 _vertexRect.bottom -= bottomClip;
768 }
769 else
770 {
771 return;
772 }
773 }
774
775 float pix_left = mCroppedParent->getAbsoluteLeft() - _renderTargetInfo.leftOffset + _vertexRect.left;
776 float pix_top = mCroppedParent->getAbsoluteTop() - _renderTargetInfo.topOffset + (mShiftText ? 1.0f : 0.0f) + _vertexRect.top;
777
778 FloatRect vertexRect(
779 ((_renderTargetInfo.pixScaleX * pix_left + _renderTargetInfo.hOffset) * 2.0f) - 1.0f,
780 -(((_renderTargetInfo.pixScaleY * pix_top + _renderTargetInfo.vOffset) * 2.0f) - 1.0f),
781 ((_renderTargetInfo.pixScaleX * (pix_left + _vertexRect.width()) + _renderTargetInfo.hOffset) * 2.0f) - 1.0f,
782 -(((_renderTargetInfo.pixScaleY * (pix_top + _vertexRect.height()) + _renderTargetInfo.vOffset) * 2.0f) - 1.0f));
783
784 drawQuad(_vertex, _vertexCount, vertexRect, mNode->getNodeDepth(), _textureRect, _colour);
785 }
786
787} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
void setTextColour(const Colour &_value) override
VertexColourType mVertexFormat
size_t getTextSelectionEnd() const override
void setFontName(const std::string &_value) override
void setAlpha(float _value) override
uint32 mCurrentColourNative
void setSelectBackground(bool _normal) override
void setTextSelection(size_t _start, size_t _end) override
void setWordWrap(bool _value) override
void _setAlign(const IntSize &_oldsize) override
bool isVisibleCursor() const override
bool getShadow() const override
IntCoord getCursorCoord(size_t _position) const override
void _correctView() override
virtual const VectorLineInfo & getLineInfo() const
void setInvertSelected(bool _value) override
const Colour & getTextColour() const override
void setFontHeight(int _value) override
void setVisible(bool _value) override
uint32 mInverseColourNative
const Colour & getShadowColour() const override
ITexture * mTexture
void setShadowColour(const Colour &_value) override
ILayerNode * mNode
const UString & getCaption() const override
bool getInvertSelected() const override
void setCaption(const UString &_value) override
void setTextAlign(Align _value) override
void setStateData(IStateInfo *_data) override
int getFontHeight() const override
size_t getTextSelectionStart() const override
void doRender() override
IntPoint getViewOffset() const override
void setVisibleCursor(bool _value) override
bool getSelectBackground() const override
IntSize getTextSize() const override
const std::string & getFontName() const override
size_t getCursorPosition() const override
void setViewOffset(const IntPoint &_point) override
Align getTextAlign() const override
void _updateView() override
RenderItem * mRenderItem
UString::utf32string mUtf32Caption
void createDrawItem(ITexture *_texture, ILayerNode *_node) override
void setCursorPosition(size_t _index) override
void setShiftText(bool _shift) override
void setShadow(bool _value) override
void updateRawData() const
void destroyDrawItem() override
const Colour & getColour() const
static FontManager & getInstance()
const IntPoint & getAbsolutePosition() const
virtual ITexture * getTextureFont() const =0
virtual int getDefaultHeight() const =0
virtual const GlyphInfo * getGlyphInfo(Char _id) const =0
virtual float getNodeDepth() const =0
virtual void outOfDate(RenderItem *_item)=0
virtual RenderItem * addToRenderItem(ITexture *_texture, bool _firstQueue, bool _separate)=0
virtual const RenderTargetInfo & getInfo() const =0
const std::string & getResourceName() const
void addDrawItem(ISubWidget *_item, size_t _count)
bool getCurrentUpdate() const
IRenderTarget * getRenderTarget()
void reallockDrawItem(ISubWidget *_item, size_t _count)
void removeDrawItem(ISubWidget *_item)
Vertex * getCurrentVertexBuffer() const
void setLastVertexCount(size_t _count)
static RenderManager & getInstance()
size_t getCursorPosition(const IntPoint &_value) const
IntPoint getCursorPoint(size_t _position) const
void update(const UString::utf32string &_text, IFont *_font, int _height, Align _align, VertexColourType _format, int _maxWidth=-1)
const VectorLineInfo & getData() const
const IntSize & getViewSize() const
A UTF-16 string with implicit conversion to/from std::string and std::wstring.
const utf32string & asUTF32() const
returns the current string in UTF-32 form within a utf32string
uint32 toNativeColour(const Colour &_colour, VertexColourType _format)
Convert Colour to 32-bit representation.
uint8_t uint8
Definition MyGUI_Types.h:46
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
const size_t SIMPLETEXT_COUNT_VERTEX
std::vector< LineInfo > VectorLineInfo
const float ALPHA_MAX
types::TRect< float > FloatRect
Definition MyGUI_Types.h:34
uint32_t uint32
Definition MyGUI_Types.h:48
bool isHStretch() const
Definition MyGUI_Align.h:69
bool isVCenter() const
Definition MyGUI_Align.h:49
bool isVStretch() const
Definition MyGUI_Align.h:84
bool isRight() const
Definition MyGUI_Align.h:64
bool isHCenter() const
Definition MyGUI_Align.h:44
bool isBottom() const
Definition MyGUI_Align.h:79
static const Colour Zero
TPoint< T > point() const