Text3DText.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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. #pragma once
  23. #include "../../Scene/Animatable.h"
  24. #include "Text3DBatch.h"
  25. namespace Atomic
  26. {
  27. static const float TEXT3D_DEFAULT_FONT_SIZE = 12;
  28. class Text3DFont;
  29. class Text3DFontFace;
  30. struct Text3DFontGlyph;
  31. /// Text effect.
  32. enum Text3DTextEffect
  33. {
  34. TE_NONE = 0,
  35. TE_SHADOW,
  36. TE_STROKE
  37. };
  38. /// Cached character location and size within text. Used for queries related to text editing.
  39. struct Text3DCharLocation
  40. {
  41. /// Position.
  42. Vector2 position_;
  43. /// Size.
  44. Vector2 size_;
  45. };
  46. /// Glyph and its location within the text. Used when preparing text rendering.
  47. struct Text3DGlyphLocation
  48. {
  49. /// Construct.
  50. Text3DGlyphLocation(float x, float y, const Text3DFontGlyph* glyph) :
  51. x_(x),
  52. y_(y),
  53. glyph_(glyph)
  54. {
  55. }
  56. /// X coordinate.
  57. float x_;
  58. /// Y coordinate.
  59. float y_;
  60. /// Glyph.
  61. const Text3DFontGlyph* glyph_;
  62. };
  63. enum Text3DHorizontalAlignment
  64. {
  65. HA_LEFT = 0,
  66. HA_CENTER,
  67. HA_RIGHT,
  68. HA_CUSTOM
  69. };
  70. enum Text3DVerticalAlignment
  71. {
  72. VA_TOP = 0,
  73. VA_CENTER,
  74. VA_BOTTOM,
  75. VA_CUSTOM
  76. };
  77. enum Text3DCorner
  78. {
  79. C_TOPLEFT = 0,
  80. C_TOPRIGHT,
  81. C_BOTTOMLEFT,
  82. C_BOTTOMRIGHT,
  83. MAX_TEXT_CORNERS
  84. };
  85. class ATOMIC_API Text3DText : public Animatable
  86. {
  87. ATOMIC_OBJECT(Text3DText, Animatable)
  88. friend class Text3D;
  89. public:
  90. /// Construct.
  91. Text3DText(Context* context);
  92. /// Destruct.
  93. virtual ~Text3DText();
  94. /// Register object factory.
  95. static void RegisterObject(Context* context);
  96. /// Apply attribute changes that can not be applied immediately.
  97. virtual void ApplyAttributes();
  98. /// Return UI rendering batches.
  99. virtual void GetBatches(PODVector<Text3DBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  100. /// React to resize.
  101. virtual void OnResize(const IntVector2& newSize, const IntVector2& delta);
  102. /// React to indent change.
  103. virtual void OnIndentSet();
  104. /// Set font by looking from resource cache by name and font size. Return true if successful.
  105. bool SetFont(const String& fontName, float size = TEXT3D_DEFAULT_FONT_SIZE);
  106. /// Set font and font size. Return true if successful.
  107. bool SetFont(Text3DFont* font, float size = TEXT3D_DEFAULT_FONT_SIZE);
  108. /// Set font size only while retaining the existing font. Return true if successful.
  109. bool SetFontSize(float size);
  110. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  111. void SetText(const String& text);
  112. /// Set row alignment.
  113. void SetTextAlignment(Text3DHorizontalAlignment align);
  114. /// Set row spacing, 1.0 for original font spacing.
  115. void SetRowSpacing(float spacing);
  116. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  117. void SetWordwrap(bool enable);
  118. /// The text will be automatically translated. The text value used as string identifier.
  119. void SetAutoLocalizable(bool enable);
  120. /// Set selection. When length is not provided, select until the text ends.
  121. void SetSelection(unsigned start, unsigned length = M_MAX_UNSIGNED);
  122. /// Clear selection.
  123. void ClearSelection();
  124. /// Set selection background color. Color with 0 alpha (default) disables.
  125. void SetSelectionColor(const Color& color);
  126. /// Set hover background color. Color with 0 alpha (default) disables.
  127. void SetHoverColor(const Color& color);
  128. /// Set text effect.
  129. void SetTextEffect(Text3DTextEffect textEffect);
  130. /// Set shadow offset.
  131. void SetEffectShadowOffset(const IntVector2& offset);
  132. /// Set stroke thickness.
  133. void SetEffectStrokeThickness(int thickness);
  134. /// Set stroke rounding. Corners of the font will be rounded off in the stroke so the stroke won't have corners.
  135. void SetEffectRoundStroke(bool roundStroke);
  136. /// Set effect color.
  137. void SetEffectColor(const Color& effectColor);
  138. /// Return font.
  139. Text3DFont* GetFont() const { return font_; }
  140. /// Return font size.
  141. float GetFontSize() const { return fontSize_; }
  142. /// Return text.
  143. const String& GetText() const { return text_; }
  144. /// Return row alignment.
  145. Text3DHorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  146. /// Return row spacing.
  147. float GetRowSpacing() const { return rowSpacing_; }
  148. /// Return wordwrap mode.
  149. bool GetWordwrap() const { return wordWrap_; }
  150. /// Return auto localizable mode.
  151. bool GetAutoLocalizable() const { return autoLocalizable_; }
  152. /// Return selection start.
  153. unsigned GetSelectionStart() const { return selectionStart_; }
  154. /// Return selection length.
  155. unsigned GetSelectionLength() const { return selectionLength_; }
  156. /// Return selection background color.
  157. const Color& GetSelectionColor() const { return selectionColor_; }
  158. /// Return hover background color.
  159. const Color& GetHoverColor() const { return hoverColor_; }
  160. /// Return text effect.
  161. Text3DTextEffect GetTextEffect() const { return textEffect_; }
  162. /// Return effect shadow offset.
  163. const IntVector2& GetEffectShadowOffset() const { return shadowOffset_; }
  164. /// Return effect stroke thickness.
  165. int GetEffectStrokeThickness() const { return strokeThickness_; }
  166. /// Return effect round stroke.
  167. bool GetEffectRoundStroke() const { return roundStroke_; }
  168. /// Return effect color.
  169. const Color& GetEffectColor() const { return effectColor_; }
  170. /// Return row height.
  171. float GetRowHeight() const { return rowHeight_; }
  172. /// Return number of rows.
  173. unsigned GetNumRows() const { return rowWidths_.Size(); }
  174. /// Return number of characters.
  175. unsigned GetNumChars() const { return unicodeText_.Size(); }
  176. /// Return width of row by index.
  177. float GetRowWidth(unsigned index) const;
  178. /// Return position of character by index relative to the text element origin.
  179. Vector2 GetCharPosition(unsigned index);
  180. /// Return size of character by index.
  181. Vector2 GetCharSize(unsigned index);
  182. /// Set text effect Z bias. Zero by default, adjusted only in 3D mode.
  183. void SetEffectDepthBias(float bias);
  184. /// Return effect Z bias.
  185. float GetEffectDepthBias() const { return effectDepthBias_; }
  186. /// Set size.
  187. void SetSize(const IntVector2& size);
  188. /// Set size.
  189. void SetSize(int width, int height);
  190. /// Set width only.
  191. void SetWidth(int width);
  192. /// Set height only.
  193. void SetHeight(int height);
  194. /// Set minimum size.
  195. void SetMinSize(const IntVector2& minSize);
  196. /// Set minimum size.
  197. void SetMinSize(int width, int height);
  198. /// Set minimum width.
  199. void SetMinWidth(int width);
  200. /// Set minimum height.
  201. void SetMinHeight(int height);
  202. /// Set maximum size.
  203. void SetMaxSize(const IntVector2& maxSize);
  204. /// Set maximum size.
  205. void SetMaxSize(int width, int height);
  206. /// Set maximum width.
  207. void SetMaxWidth(int width);
  208. /// Set maximum height.
  209. void SetMaxHeight(int height);
  210. /// Set fixed size.
  211. void SetFixedSize(const IntVector2& size);
  212. /// Set fixed size.
  213. void SetFixedSize(int width, int height);
  214. /// Set fixed width.
  215. void SetFixedWidth(int width);
  216. /// Set fixed height.
  217. void SetFixedHeight(int height);
  218. /// Return size.
  219. const IntVector2& GetSize() const { return size_; }
  220. /// Return width.
  221. int GetWidth() const { return size_.x_; }
  222. /// Return height.
  223. int GetHeight() const { return size_.y_; }
  224. /// Return minimum size.
  225. const IntVector2& GetMinSize() const { return minSize_; }
  226. /// Return minimum width.
  227. int GetMinWidth() const { return minSize_.x_; }
  228. /// Return minimum height.
  229. int GetMinHeight() const { return minSize_.y_; }
  230. /// Return maximum size.
  231. const IntVector2& GetMaxSize() const { return maxSize_; }
  232. /// Return minimum width.
  233. int GetMaxWidth() const { return maxSize_.x_; }
  234. /// Return minimum height.
  235. int GetMaxHeight() const { return maxSize_.y_; }
  236. /// Return true if size is fixed.
  237. bool IsFixedSize() const { return minSize_ == maxSize_; }
  238. /// Return true if width is fixed.
  239. bool IsFixedWidth() const { return minSize_.x_ == maxSize_.x_; }
  240. /// Return true if height is fixed.
  241. bool IsFixedHeight() const { return minSize_.y_ == maxSize_.y_; }
  242. /// Set horizontal indentation.
  243. void SetIndent(int indent);
  244. /// Set indent spacing (number of pixels per indentation level).
  245. void SetIndentSpacing(int indentSpacing);
  246. /// Return horizontal indentation.
  247. int GetIndent() const { return indent_; }
  248. /// Return indent spacing (number of pixels per indentation level).
  249. int GetIndentSpacing() const { return indentSpacing_; }
  250. /// Return indent width in pixels.
  251. int GetIndentWidth() const { return indent_ * indentSpacing_; }
  252. /// Return corner color.
  253. const Color& GetColor(Text3DCorner corner) const { return color_[corner]; }
  254. const Color& GetColor() const { return color_[C_TOPLEFT]; }
  255. /// Set color on all corners.
  256. void SetColor(const Color& color);
  257. /// Set color on one corner.
  258. void SetColor(Text3DCorner corner, const Color& color);
  259. /// Return whether has different color in at least one corner.
  260. bool HasColorGradient() const { return colorGradient_; }
  261. /// Set opacity.
  262. void SetOpacity(float opacity);
  263. /// Return opacity.
  264. float GetOpacity() const { return opacity_; }
  265. /// Return whether is selected.
  266. bool IsSelected() const { return selected_; }
  267. /// Set selected mode.
  268. void SetSelected(bool enable);
  269. /// Return whether the cursor is hovering on this element.
  270. bool IsHovering() const { return hovering_; }
  271. /// Set hovering state.
  272. void SetHovering(bool enable);
  273. /// Set font attribute.
  274. void SetFontAttr(const ResourceRef& value);
  275. /// Return font attribute.
  276. ResourceRef GetFontAttr() const;
  277. /// Set text attribute.
  278. void SetTextAttr(const String& value);
  279. /// Return text attribute.
  280. String GetTextAttr() const;
  281. protected:
  282. /// Handle attribute animation added.
  283. virtual void OnAttributeAnimationAdded();
  284. /// Handle attribute animation removed.
  285. virtual void OnAttributeAnimationRemoved();
  286. virtual void MarkDirty();
  287. /// Update text when text, font or spacing changed.
  288. void UpdateText(bool onResize = false);
  289. /// Update cached character locations after text update, or when text alignment or indent has changed.
  290. void UpdateCharLocations();
  291. /// Validate text selection to be within the text.
  292. void ValidateSelection();
  293. /// Return row start X position.
  294. int GetRowStartPosition(unsigned rowIndex) const;
  295. /// Contruct batch.
  296. void ConstructBatch
  297. (Text3DBatch& pageBatch, const PODVector<Text3DGlyphLocation>& pageGlyphLocation, float dx = 0, float dy = 0, Color* color = 0,
  298. float depthBias = 0.0f);
  299. /// Font.
  300. SharedPtr<Text3DFont> font_;
  301. /// Current face.
  302. WeakPtr<Text3DFontFace> fontFace_;
  303. /// Font size.
  304. float fontSize_;
  305. /// UTF-8 encoded text.
  306. String text_;
  307. /// Row alignment.
  308. Text3DHorizontalAlignment textAlignment_;
  309. /// Row spacing.
  310. float rowSpacing_;
  311. /// Wordwrap mode.
  312. bool wordWrap_;
  313. /// Char positions dirty flag.
  314. bool charLocationsDirty_;
  315. /// Selection start.
  316. unsigned selectionStart_;
  317. /// Selection length.
  318. unsigned selectionLength_;
  319. /// Selection background color.
  320. Color selectionColor_;
  321. /// Hover background color.
  322. Color hoverColor_;
  323. /// Text effect.
  324. Text3DTextEffect textEffect_;
  325. /// Text effect shadow offset.
  326. IntVector2 shadowOffset_;
  327. /// Text effect stroke thickness.
  328. int strokeThickness_;
  329. /// Text effect stroke rounding flag.
  330. bool roundStroke_;
  331. /// Effect color.
  332. Color effectColor_;
  333. /// Text effect Z bias.
  334. float effectDepthBias_;
  335. /// Row height.
  336. float rowHeight_;
  337. /// Text as Unicode characters.
  338. PODVector<unsigned> unicodeText_;
  339. /// Text modified into printed form.
  340. PODVector<unsigned> printText_;
  341. /// Mapping of printed form back to original char indices.
  342. PODVector<unsigned> printToText_;
  343. /// Row widths.
  344. PODVector<float> rowWidths_;
  345. /// Glyph locations per each texture in the font.
  346. Vector<PODVector<Text3DGlyphLocation> > pageGlyphLocations_;
  347. /// Cached locations of each character in the text.
  348. PODVector<Text3DCharLocation> charLocations_;
  349. /// The text will be automatically translated.
  350. bool autoLocalizable_;
  351. /// Localization string id storage. Used when autoLocalizable flag is set.
  352. String stringId_;
  353. /// Handle change Language.
  354. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData);
  355. /// UTF8 to Unicode.
  356. void DecodeToUnicode();
  357. /// Size.
  358. IntVector2 size_;
  359. /// Minimum size.
  360. IntVector2 minSize_;
  361. /// Maximum size.
  362. IntVector2 maxSize_;
  363. /// Horizontal indentation.
  364. int indent_;
  365. /// Indent spacing (number of pixels per indentation level).
  366. int indentSpacing_;
  367. /// Colors.
  368. Color color_[MAX_TEXT_CORNERS];
  369. /// Has color gradient flag.
  370. bool colorGradient_;
  371. /// Opacity.
  372. float opacity_;
  373. /// Selected flag.
  374. bool selected_;
  375. /// Hovering flag.
  376. bool hovering_;
  377. private:
  378. /// Handle logic post-update event.
  379. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  380. };
  381. }