Text3D.h 9.1 KB

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