Text3D.h 6.4 KB

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