Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_RENDERER_BASIC2D_XL2DLABEL_H_
24 : #define XENOLITH_RENDERER_BASIC2D_XL2DLABEL_H_
25 :
26 : #include "XL2dSprite.h"
27 : #include "XLCoreInput.h"
28 : #include "XLFontLabelBase.h"
29 : #include <future>
30 :
31 : namespace STAPPLER_VERSIONIZED stappler::xenolith {
32 :
33 : class EventListener;
34 :
35 : }
36 :
37 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
38 :
39 : struct LabelResult : Ref {
40 : TransformVertexData data;
41 : Vector<ColorMask> colorMap;
42 : };
43 :
44 : class LabelDeferredResult : public DeferredVertexResult {
45 : public:
46 : virtual ~LabelDeferredResult();
47 :
48 : bool init(std::future<Rc<LabelResult>> &&);
49 :
50 : virtual SpanView<TransformVertexData> getData() override;
51 :
52 : virtual void handleReady(Rc<LabelResult> &&);
53 :
54 : void updateColor(const Color4F &);
55 :
56 : Rc<VertexData> getResult() const;
57 :
58 : protected:
59 : mutable Mutex _mutex;
60 : Rc<LabelResult> _result;
61 : std::future<Rc<LabelResult>> *_future = nullptr;
62 : };
63 :
64 : class Label : public Sprite, public font::LabelBase {
65 : public:
66 : using TextLayout = font::TextLayout;
67 : using LineLayout = font::LineLayoutData;
68 : using TextAlign = font::TextAlign;
69 :
70 : using ColorMapVec = Vector<Vector<bool>>;
71 :
72 : class Selection : public Sprite {
73 : public:
74 : virtual ~Selection();
75 : virtual bool init() override;
76 : virtual void clear();
77 : virtual void emplaceRect(const Rect &);
78 : virtual void updateColor() override;
79 :
80 13566 : virtual core::TextCursor getTextCursor() const { return _cursor; }
81 13906 : virtual void setTextCursor(core::TextCursor c) { _cursor = c; }
82 :
83 : protected:
84 : virtual void updateVertexes() override;
85 :
86 : core::TextCursor _cursor = core::TextCursor::InvalidCursor;
87 : };
88 :
89 : static void writeQuads(VertexArray &vertexes, const font::TextLayoutData<memory::StandartInterface> *format, Vector<ColorMask> &colorMap);
90 : static void writeQuads(VertexArray &vertexes, const font::TextLayoutData<memory::PoolInterface> *format, Vector<ColorMask> &colorMap);
91 : static Rc<LabelResult> writeResult(TextLayout *format, const Color4F &);
92 :
93 : virtual ~Label();
94 :
95 : virtual bool init() override;
96 : virtual bool init(StringView) override;
97 : virtual bool init(StringView, float w, TextAlign = TextAlign::Left);
98 : virtual bool init(font::FontController *, const DescriptionStyle & = DescriptionStyle(),
99 : StringView = StringView(), float w = 0.0f, TextAlign = TextAlign::Left);
100 : virtual bool init(const DescriptionStyle &, StringView = StringView(),
101 : float w = 0.0f, TextAlign = TextAlign::Left);
102 :
103 : virtual void tryUpdateLabel();
104 :
105 : virtual void setStyle(const DescriptionStyle &);
106 : virtual const DescriptionStyle &getStyle() const;
107 :
108 : virtual void onContentSizeDirty() override;
109 : virtual void onTransformDirty(const Mat4 &) override;
110 : virtual void onGlobalTransformDirty(const Mat4 &) override;
111 :
112 : virtual void setAdjustValue(uint8_t);
113 : virtual uint8_t getAdjustValue() const;
114 :
115 : virtual bool isOverflow() const;
116 :
117 : virtual size_t getCharsCount() const;
118 : virtual size_t getLinesCount() const;
119 : virtual LineLayout getLine(uint32_t num) const;
120 :
121 : virtual uint16_t getFontHeight() const;
122 :
123 : virtual Vec2 getCursorPosition(uint32_t charIndex, bool prefix = true) const;
124 : virtual Vec2 getCursorOrigin() const;
125 :
126 : // returns character index in FormatSpec for position in label or maxOf<uint32_t>()
127 : // pair.second - true if index match suffix or false if index match prefix
128 : // use convertToNodeSpace to get position
129 : virtual Pair<uint32_t, bool> getCharIndex(const Vec2 &, font::CharSelectMode = font::CharSelectMode::Best) const;
130 :
131 : virtual core::TextCursor selectWord(uint32_t) const;
132 :
133 : virtual float getMaxLineX() const;
134 :
135 : virtual void setDeferred(bool);
136 0 : virtual bool isDeferred() const { return _deferred; }
137 :
138 : virtual void setSelectionCursor(core::TextCursor);
139 : virtual core::TextCursor getSelectionCursor() const;
140 :
141 : virtual void setSelectionColor(const Color4F &);
142 : virtual Color4F getSelectionColor() const;
143 :
144 : virtual void setMarkedCursor(core::TextCursor);
145 : virtual core::TextCursor getMarkedCursor() const;
146 :
147 : virtual void setMarkedColor(const Color4F &);
148 : virtual Color4F getMarkedColor() const;
149 :
150 : protected:
151 : using Sprite::init;
152 :
153 : virtual Rc<LabelDeferredResult> runDeferred(thread::TaskQueue &, TextLayout *format, const Color4F &color);
154 :
155 : virtual void applyLayout(TextLayout *);
156 :
157 : virtual void updateLabel();
158 : virtual void onFontSourceUpdated();
159 : virtual void onFontSourceLoaded();
160 : virtual void onLayoutUpdated();
161 : virtual void updateColor() override;
162 : virtual void updateVertexes() override;
163 : virtual void updateVertexesColor() override;
164 :
165 : virtual void updateQuadsForeground(font::FontController *, TextLayout *, Vector<ColorMask> &);
166 :
167 : virtual bool checkVertexDirty() const override;
168 :
169 : virtual NodeFlags processParentFlags(FrameInfo &info, NodeFlags parentFlags) override;
170 :
171 : virtual void pushCommands(FrameInfo &, NodeFlags flags) override;
172 :
173 : void updateLabelScale(const Mat4 &parent);
174 :
175 : EventListener *_listener = nullptr;
176 : Time _quadRequestTime;
177 : Rc<font::FontController> _source;
178 : Rc<TextLayout> _format;
179 : Vector<ColorMask> _colorMap;
180 :
181 : bool _deferred = true;
182 :
183 : uint8_t _adjustValue = 0;
184 : size_t _updateCount = 0;
185 :
186 : Selection *_selection = nullptr;
187 : Selection *_marked = nullptr;
188 :
189 : Rc<LabelDeferredResult> _deferredResult;
190 : };
191 :
192 : }
193 :
194 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DLABEL_H_ */
|