Text3D.h 6.4 KB

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