Text3D.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // Copyright (c) 2008-2020 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 "../Graphics/Drawable.h"
  24. #include "../Graphics/VertexBuffer.h"
  25. #include "../Math/Matrix3x4.h"
  26. #include "../UI/Text.h"
  27. namespace Urho3D
  28. {
  29. class Text;
  30. /// 3D text component.
  31. class URHO3D_API Text3D : public Drawable
  32. {
  33. URHO3D_OBJECT(Text3D, Drawable);
  34. public:
  35. /// Construct.
  36. explicit Text3D(Context* context);
  37. /// Destruct.
  38. ~Text3D() override;
  39. /// Register object factory. Drawable must be registered first.
  40. static void RegisterObject(Context* context);
  41. /// Apply attribute changes that can not be applied immediately.
  42. void ApplyAttributes() override;
  43. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  44. void UpdateBatches(const FrameInfo& frame) override;
  45. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update).
  46. void UpdateGeometry(const FrameInfo& frame) override;
  47. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  48. UpdateGeometryType GetUpdateGeometryType() override;
  49. /// Set font by looking from resource cache by name and font size. Return true if successful.
  50. bool SetFont(const String& fontName, float size = DEFAULT_FONT_SIZE);
  51. /// Set font and font size. Return true if successful.
  52. bool SetFont(Font* font, float size = DEFAULT_FONT_SIZE);
  53. /// Set font size only while retaining the existing font. Return true if successful.
  54. /// @property
  55. bool SetFontSize(float size);
  56. /// Set material.
  57. /// @property
  58. void SetMaterial(Material* material);
  59. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  60. /// @property
  61. void SetText(const String& text);
  62. /// Set horizontal and vertical alignment.
  63. void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign);
  64. /// Set horizontal alignment.
  65. /// @property
  66. void SetHorizontalAlignment(HorizontalAlignment align);
  67. /// Set vertical alignment.
  68. /// @property
  69. void SetVerticalAlignment(VerticalAlignment align);
  70. /// Set row alignment.
  71. /// @property
  72. void SetTextAlignment(HorizontalAlignment align);
  73. /// Set row spacing, 1.0 for original font spacing.
  74. /// @property
  75. void SetRowSpacing(float spacing);
  76. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  77. /// @property
  78. void SetWordwrap(bool enable);
  79. /// Set text effect.
  80. /// @property
  81. void SetTextEffect(TextEffect textEffect);
  82. /// Set shadow offset.
  83. /// @property
  84. void SetEffectShadowOffset(const IntVector2& offset);
  85. /// Set stroke thickness.
  86. /// @property
  87. void SetEffectStrokeThickness(int thickness);
  88. /// Set stroke rounding. Corners of the font will be rounded off in the stroke so the stroke won't have corners.
  89. /// @property
  90. void SetEffectRoundStroke(bool roundStroke);
  91. /// Set effect color.
  92. /// @property
  93. void SetEffectColor(const Color& effectColor);
  94. /// Set effect Z bias.
  95. /// @property
  96. void SetEffectDepthBias(float bias);
  97. /// Set text width. Only has effect in word wrap mode.
  98. /// @property
  99. void SetWidth(int width);
  100. /// Set color on all corners.
  101. /// @property
  102. void SetColor(const Color& color);
  103. /// Set color on one corner.
  104. /// @property{set_colors}
  105. void SetColor(Corner corner, const Color& color);
  106. /// Set opacity.
  107. /// @property
  108. void SetOpacity(float opacity);
  109. /// Set whether text has fixed size on screen (pixel-perfect) regardless of distance to camera. Works best when combined with face camera rotation. Default false.
  110. /// @property
  111. void SetFixedScreenSize(bool enable);
  112. /// Set how the text should rotate in relation to the camera. Default is to not rotate (FC_NONE).
  113. /// @property
  114. void SetFaceCameraMode(FaceCameraMode mode);
  115. /// Return font.
  116. /// @property
  117. Font* GetFont() const;
  118. /// Return font size.
  119. /// @property
  120. float GetFontSize() const;
  121. /// Return material.
  122. /// @property
  123. Material* GetMaterial() const;
  124. /// Return text.
  125. /// @property
  126. const String& GetText() const;
  127. /// Return row alignment.
  128. /// @property
  129. HorizontalAlignment GetTextAlignment() const;
  130. /// Return horizontal alignment.
  131. /// @property
  132. HorizontalAlignment GetHorizontalAlignment() const;
  133. /// Return vertical alignment.
  134. /// @property
  135. VerticalAlignment GetVerticalAlignment() const;
  136. /// Return row spacing.
  137. /// @property
  138. float GetRowSpacing() const;
  139. /// Return wordwrap mode.
  140. /// @property
  141. bool GetWordwrap() const;
  142. /// Return text effect.
  143. /// @property
  144. TextEffect GetTextEffect() const;
  145. /// Return effect shadow offset.
  146. /// @property
  147. const IntVector2& GetEffectShadowOffset() const;
  148. /// Return effect stroke thickness.
  149. /// @property
  150. int GetEffectStrokeThickness() const;
  151. /// Return effect round stroke.
  152. /// @property
  153. bool GetEffectRoundStroke() const;
  154. /// Return effect color.
  155. /// @property
  156. const Color& GetEffectColor() const;
  157. /// Return effect depth bias.
  158. /// @property
  159. float GetEffectDepthBias() const;
  160. /// Return text width.
  161. /// @property
  162. int GetWidth() const;
  163. /// Return text height.
  164. /// @property
  165. int GetHeight() const;
  166. /// Return row height.
  167. /// @property
  168. int GetRowHeight() const;
  169. /// Return number of rows.
  170. /// @property
  171. unsigned GetNumRows() const;
  172. /// Return number of characters.
  173. /// @property
  174. unsigned GetNumChars() const;
  175. /// Return width of row by index.
  176. /// @property{get_rowWidths}
  177. int GetRowWidth(unsigned index) const;
  178. /// Return position of character by index relative to the text element origin.
  179. /// @property{get_charPositions}
  180. Vector2 GetCharPosition(unsigned index);
  181. /// Return size of character by index.
  182. /// @property{get_charSizes}
  183. Vector2 GetCharSize(unsigned index);
  184. /// Return corner color.
  185. /// @property{get_colors}
  186. const Color& GetColor(Corner corner) const;
  187. /// Return opacity.
  188. /// @property
  189. float GetOpacity() const;
  190. /// Return whether text has fixed screen size.
  191. /// @property
  192. bool IsFixedScreenSize() const { return fixedScreenSize_; }
  193. /// Return how the text rotates in relation to the camera.
  194. /// @property
  195. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; }
  196. /// Set font attribute.
  197. void SetFontAttr(const ResourceRef& value);
  198. /// Return font attribute.
  199. ResourceRef GetFontAttr() const;
  200. /// Set material attribute.
  201. void SetMaterialAttr(const ResourceRef& value);
  202. /// Return material attribute.
  203. ResourceRef GetMaterialAttr() const;
  204. /// Set text attribute.
  205. void SetTextAttr(const String& value);
  206. /// Return text attribute.
  207. String GetTextAttr() const;
  208. /// Get color attribute. Uses just the top-left color.
  209. const Color& GetColorAttr() const { return text_.colors_[0]; }
  210. protected:
  211. /// Handle node being assigned.
  212. void OnNodeSet(Node* node) override;
  213. /// Recalculate the world-space bounding box.
  214. void OnWorldBoundingBoxUpdate() override;
  215. /// Mark text & geometry dirty.
  216. void MarkTextDirty();
  217. /// Update text %UI batches.
  218. void UpdateTextBatches();
  219. /// Create materials for text rendering. May only be called from the main thread. Text %UI batches must be up-to-date.
  220. void UpdateTextMaterials(bool forceUpdate = false);
  221. /// Recalculate camera facing and fixed screen size.
  222. void CalculateFixedScreenSize(const FrameInfo& frame);
  223. /// Internally used text element.
  224. Text text_;
  225. /// Geometries.
  226. Vector<SharedPtr<Geometry> > geometries_;
  227. /// Vertex buffer.
  228. SharedPtr<VertexBuffer> vertexBuffer_;
  229. /// Material to use as a base for the text material(s).
  230. SharedPtr<Material> material_;
  231. /// Text UI batches.
  232. PODVector<UIBatch> uiBatches_;
  233. /// Text vertex data.
  234. PODVector<float> uiVertexData_;
  235. /// Custom world transform for facing the camera automatically.
  236. Matrix3x4 customWorldTransform_;
  237. /// Text rotation mode in relation to the camera.
  238. FaceCameraMode faceCameraMode_;
  239. /// Minimal angle between text normal and look-at direction.
  240. float minAngle_;
  241. /// Fixed screen size flag.
  242. bool fixedScreenSize_;
  243. /// Text needs update flag.
  244. bool textDirty_;
  245. /// Geometry dirty flag.
  246. bool geometryDirty_;
  247. /// Flag for whether currently using SDF shader defines in the generated material.
  248. bool usingSDFShader_;
  249. /// Font texture data lost flag.
  250. bool fontDataLost_;
  251. };
  252. }