Text3D.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (c) 2008-2014 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. #ifdef ATOMIC_3D
  24. #include "../Graphics/Drawable.h"
  25. #include "../Math/Matrix3x4.h"
  26. #include "../UI/Text.h"
  27. #include "../Graphics/VertexBuffer.h"
  28. namespace Atomic
  29. {
  30. class Text;
  31. /// 3D text component.
  32. class ATOMIC_API Text3D : public Drawable
  33. {
  34. OBJECT(Text3D);
  35. public:
  36. /// Construct.
  37. Text3D(Context* context);
  38. /// Destruct.
  39. ~Text3D();
  40. /// Register object factory. Drawable must be registered first.
  41. static void RegisterObject(Context* context);
  42. /// Apply attribute changes that can not be applied immediately.
  43. virtual void ApplyAttributes();
  44. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  45. virtual void UpdateBatches(const FrameInfo& frame);
  46. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  47. virtual void UpdateGeometry(const FrameInfo& frame);
  48. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  49. virtual UpdateGeometryType GetUpdateGeometryType();
  50. /// Set font and font size and use signed distance field font. Return true if successful.
  51. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  52. /// Set font and font size and use signed distance field font. Return true if successful.
  53. bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
  54. /// Set material.
  55. void SetMaterial(Material* material);
  56. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  57. void SetText(const String& text);
  58. /// Set horizontal and vertical alignment.
  59. void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign);
  60. /// Set horizontal alignment.
  61. void SetHorizontalAlignment(HorizontalAlignment align);
  62. /// Set vertical alignment.
  63. void SetVerticalAlignment(VerticalAlignment align);
  64. /// Set row alignment.
  65. void SetTextAlignment(HorizontalAlignment align);
  66. /// Set row spacing, 1.0 for original font spacing.
  67. void SetRowSpacing(float spacing);
  68. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  69. void SetWordwrap(bool enable);
  70. /// Set text effect.
  71. void SetTextEffect(TextEffect textEffect);
  72. /// Set effect color.
  73. void SetEffectColor(const Color& effectColor);
  74. /// Set effect Z bias.
  75. void SetEffectDepthBias(float bias);
  76. /// Set text width. Only has effect in word wrap mode.
  77. void SetWidth(int width);
  78. /// Set color on all corners.
  79. void SetColor(const Color& color);
  80. /// Set color on one corner.
  81. void SetColor(Corner corner, const Color& color);
  82. /// Set opacity.
  83. void SetOpacity(float opacity);
  84. /// Set how the text should rotate in relation to the camera. Default is to not rotate (FC_NONE.)
  85. void SetFaceCameraMode(FaceCameraMode mode);
  86. /// Return font.
  87. Font* GetFont() const;
  88. /// Return material.
  89. Material* GetMaterial() const;
  90. /// Return font size.
  91. int GetFontSize() const;
  92. /// Return text.
  93. const String& GetText() const;
  94. /// Return row alignment.
  95. HorizontalAlignment GetTextAlignment() const;
  96. /// Return horizontal alignment.
  97. HorizontalAlignment GetHorizontalAlignment() const;
  98. /// Return vertical alignment.
  99. VerticalAlignment GetVerticalAlignment() const;
  100. /// Return row spacing.
  101. float GetRowSpacing() const;
  102. /// Return wordwrap mode.
  103. bool GetWordwrap() const;
  104. /// Return text effect.
  105. TextEffect GetTextEffect() const;
  106. /// Return effect color.
  107. const Color& GetEffectColor() const;
  108. /// Return effect depth bias.
  109. float GetEffectDepthBias() const;
  110. /// Return text width.
  111. int GetWidth() const;
  112. /// Return row height.
  113. int GetRowHeight() const;
  114. /// Return number of rows.
  115. unsigned GetNumRows() const;
  116. /// Return number of characters.
  117. unsigned GetNumChars() const;
  118. /// Return width of row by index.
  119. int GetRowWidth(unsigned index) const;
  120. /// Return position of character by index relative to the text element origin.
  121. IntVector2 GetCharPosition(unsigned index);
  122. /// Return size of character by index.
  123. IntVector2 GetCharSize(unsigned index);
  124. /// Return corner color.
  125. const Color& GetColor(Corner corner) const;
  126. /// Return opacity.
  127. float GetOpacity() const;
  128. /// Return how the text rotates in relation to the camera.
  129. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; }
  130. /// Set font attribute.
  131. void SetFontAttr(const ResourceRef& value);
  132. /// Return font attribute.
  133. ResourceRef GetFontAttr() const;
  134. /// Set material attribute.
  135. void SetMaterialAttr(const ResourceRef& value);
  136. /// Return material attribute.
  137. ResourceRef GetMaterialAttr() const;
  138. /// Get color attribute. Uses just the top-left color.
  139. const Color& GetColorAttr() const { return text_.color_[0]; }
  140. protected:
  141. /// Handle node being assigned.
  142. virtual void OnNodeSet(Node* node);
  143. /// Recalculate the world-space bounding box.
  144. virtual void OnWorldBoundingBoxUpdate();
  145. private:
  146. /// Mark text & geometry dirty.
  147. void MarkTextDirty();
  148. /// Update text and font.
  149. void UpdateText();
  150. /// Update text %UI batches.
  151. void UpdateTextBatches();
  152. /// Create materials for text rendering. May only be called from the main thread. Text %UI batches must be up-to-date.
  153. void UpdateTextMaterials(bool forceUpdate = false);
  154. /// Internally used text element.
  155. Text text_;
  156. /// Geometries.
  157. Vector<SharedPtr<Geometry> > geometries_;
  158. /// Vertex buffer.
  159. SharedPtr<VertexBuffer> vertexBuffer_;
  160. /// Material to use as a base for the text material(s).
  161. SharedPtr<Material> material_;
  162. /// Text UI batches.
  163. PODVector<UIBatch> uiBatches_;
  164. /// Text vertex data.
  165. PODVector<float> uiVertexData_;
  166. /// Custom world transform for facing the camera automatically.
  167. Matrix3x4 customWorldTransform_;
  168. /// Text rotation mode in relation to the camera.
  169. FaceCameraMode faceCameraMode_;
  170. /// Text needs update flag.
  171. bool textDirty_;
  172. /// Geometry dirty flag.
  173. bool geometryDirty_;
  174. };
  175. }
  176. #endif