Text3D.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include "Drawable.h"
  24. #include "Matrix3x4.h"
  25. #include "Text.h"
  26. #include "VertexBuffer.h"
  27. namespace Urho3D
  28. {
  29. class Text;
  30. /// 3D text component.
  31. class URHO3D_API Text3D : public Drawable
  32. {
  33. OBJECT(Text3D);
  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 and font size. Return true if successful.
  50. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  51. /// Set font and font size. Return true if successful.
  52. bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
  53. /// Set material.
  54. void SetMaterial(Material* material);
  55. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  56. void SetText(const String& text);
  57. /// Set horizontal and vertical alignment.
  58. void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign);
  59. /// Set horizontal alignment.
  60. void SetHorizontalAlignment(HorizontalAlignment align);
  61. /// Set vertical alignment.
  62. void SetVerticalAlignment(VerticalAlignment align);
  63. /// Set row alignment.
  64. void SetTextAlignment(HorizontalAlignment align);
  65. /// Set row spacing, 1.0 for original font spacing.
  66. void SetRowSpacing(float spacing);
  67. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  68. void SetWordwrap(bool enable);
  69. /// Set text effect.
  70. void SetTextEffect(TextEffect textEffect);
  71. /// Set effect color.
  72. void SetEffectColor(const Color& effectColor);
  73. /// Set effect Z bias.
  74. void SetEffectDepthBias(float bias);
  75. /// Set text width. Only has effect in word wrap mode.
  76. void SetWidth(int width);
  77. /// Set color on all corners.
  78. void SetColor(const Color& color);
  79. /// Set color on one corner.
  80. void SetColor(Corner corner, const Color& color);
  81. /// Set opacity.
  82. void SetOpacity(float opacity);
  83. /// Set whether to face the camera automatically.
  84. void SetFaceCamera(bool enable);
  85. /// Set on which axes the text faces the camera. Default all axes (1,1,1). Negative axes imply facing away.
  86. void SetFaceCameraAxes(const Vector3& axes);
  87. /// Return font.
  88. Font* GetFont() const;
  89. /// Return material.
  90. Material* GetMaterial() const;
  91. /// Return font size.
  92. int GetFontSize() const;
  93. /// Return text.
  94. const String& GetText() const;
  95. /// Return row alignment.
  96. HorizontalAlignment GetTextAlignment() const;
  97. /// Return horizontal alignment.
  98. HorizontalAlignment GetHorizontalAlignment() const;
  99. /// Return vertical alignment.
  100. VerticalAlignment GetVerticalAlignment() const;
  101. /// Return row spacing.
  102. float GetRowSpacing() const;
  103. /// Return wordwrap mode.
  104. bool GetWordwrap() const;
  105. /// Return text effect.
  106. TextEffect GetTextEffect() const;
  107. /// Return effect color.
  108. const Color& GetEffectColor() const;
  109. /// Return effect depth bias.
  110. float GetEffectDepthBias() const;
  111. /// Return text width.
  112. int GetWidth() const;
  113. /// Return row height.
  114. int GetRowHeight() const;
  115. /// Return number of rows.
  116. unsigned GetNumRows() const;
  117. /// Return number of characters.
  118. unsigned GetNumChars() const;
  119. /// Return width of row by index.
  120. int GetRowWidth(unsigned index) const;
  121. /// Return position of character by index relative to the text element origin.
  122. IntVector2 GetCharPosition(unsigned index);
  123. /// Return size of character by index.
  124. IntVector2 GetCharSize(unsigned index);
  125. /// Return corner color.
  126. const Color& GetColor(Corner corner) const;
  127. /// Return opacity.
  128. float GetOpacity() const;
  129. /// Return whether faces the camera automatically.
  130. bool GetFaceCamera() const { return faceCamera_; }
  131. /// Return on which coordinate axes camera facing is done.
  132. const Vector3& GetFaceCameraAxes() const { return faceCameraAxes_; }
  133. /// Set font attribute.
  134. void SetFontAttr(ResourceRef value);
  135. /// Return font attribute.
  136. ResourceRef GetFontAttr() const;
  137. /// Set material attribute.
  138. void SetMaterialAttr(ResourceRef value);
  139. /// Return material attribute.
  140. ResourceRef GetMaterialAttr() const;
  141. /// Get color attribute. Uses just the top-left color.
  142. const Color& GetColorAttr() const { return text_.color_[0]; }
  143. protected:
  144. /// Handle node being assigned.
  145. virtual void OnNodeSet(Node* node);
  146. /// Recalculate the world-space bounding box.
  147. virtual void OnWorldBoundingBoxUpdate();
  148. private:
  149. /// Mark text & geometry dirty.
  150. void MarkTextDirty();
  151. /// Update text and font.
  152. void UpdateText();
  153. /// Update text %UI batches.
  154. void UpdateTextBatches();
  155. /// Create materials for text rendering. May only be called from the main thread. Text %UI batches must be up-to-date.
  156. void UpdateTextMaterials(bool forceUpdate = false);
  157. /// Internally used text element.
  158. Text text_;
  159. /// Geometries.
  160. Vector<SharedPtr<Geometry> > geometries_;
  161. /// Vertex buffer.
  162. SharedPtr<VertexBuffer> vertexBuffer_;
  163. /// Material to use as a base for the text material(s).
  164. SharedPtr<Material> material_;
  165. /// Text UI batches.
  166. PODVector<UIBatch> uiBatches_;
  167. /// Text vertex data.
  168. PODVector<float> uiVertexData_;
  169. /// Custom world transform for facing the camera automatically.
  170. Matrix3x4 customWorldTransform_;
  171. /// Coordinate axes on which camera facing is done.
  172. Vector3 faceCameraAxes_;
  173. /// Face camera flag.
  174. bool faceCamera_;
  175. /// Text needs update flag.
  176. bool textDirty_;
  177. /// Geometry dirty flag.
  178. bool geometryDirty_;
  179. };
  180. }