// // Copyright (c) 2008-2017 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #pragma once #include "../../Graphics/Drawable.h" #include "../../Graphics/VertexBuffer.h" #include "../../Math/Matrix3x4.h" #include "Text3DText.h" namespace Atomic { class Text3DText; /// 3D text component. class ATOMIC_API Text3D : public Drawable { ATOMIC_OBJECT(Text3D, Drawable) public: /// Construct. Text3D(Context* context); /// Destruct. ~Text3D(); /// Register object factory. Drawable must be registered first. static void RegisterObject(Context* context); /// Apply attribute changes that can not be applied immediately. virtual void ApplyAttributes(); /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly. virtual void UpdateBatches(const FrameInfo& frame); /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.) virtual void UpdateGeometry(const FrameInfo& frame); /// Return whether a geometry update is necessary, and if it can happen in a worker thread. virtual UpdateGeometryType GetUpdateGeometryType(); /// Set font by looking from resource cache by name and font size. Return true if successful. bool SetFont(const String& fontName, float size = TEXT3D_DEFAULT_FONT_SIZE); /// Set font and font size. Return true if successful. bool SetFont(Text3DFont* font, float size = TEXT3D_DEFAULT_FONT_SIZE); /// Set font size only while retaining the existing font. Return true if successful. bool SetFontSize(float size); /// Set material. void SetMaterial(Material* material); /// Set text. Text is assumed to be either ASCII or UTF8-encoded. void SetText(const String& text); /// Set horizontal and vertical alignment. void SetAlignment(Text3DHorizontalAlignment hAlign, Text3DVerticalAlignment vAlign); /// Set horizontal alignment. void SetHorizontalAlignment(Text3DHorizontalAlignment align); /// Set vertical alignment. void SetVerticalAlignment(Text3DVerticalAlignment align); /// Set row alignment. void SetTextAlignment(Text3DHorizontalAlignment align); /// Set row spacing, 1.0 for original font spacing. void SetRowSpacing(float spacing); /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely. void SetWordwrap(bool enable); /// Set text effect. void SetTextEffect(Text3DTextEffect textEffect); /// Set shadow offset. void SetEffectShadowOffset(const IntVector2& offset); /// Set stroke thickness. void SetEffectStrokeThickness(int thickness); /// Set stroke rounding. Corners of the font will be rounded off in the stroke so the stroke won't have corners. void SetEffectRoundStroke(bool roundStroke); /// Set effect color. void SetEffectColor(const Color& effectColor); /// Set effect Z bias. void SetEffectDepthBias(float bias); /// Set text width. Only has effect in word wrap mode. void SetWidth(int width); /// Set color on all corners. void SetColor(const Color& color); /// Set color on one corner. void SetColor(Text3DCorner corner, const Color& color); /// Set opacity. void SetOpacity(float opacity); /// 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. void SetFixedScreenSize(bool enable); /// Set how the text should rotate in relation to the camera. Default is to not rotate (FC_NONE.) void SetFaceCameraMode(FaceCameraMode mode); /// Return font. Text3DFont* GetFont() const; /// Return font size. float GetFontSize() const; /// Return material. Material* GetMaterial() const; /// Return text. const String& GetText() const; /// Return row alignment. Text3DHorizontalAlignment GetTextAlignment() const; /// Return horizontal alignment. Text3DHorizontalAlignment GetHorizontalAlignment() const; /// Return vertical alignment. Text3DVerticalAlignment GetVerticalAlignment() const; /// Return row spacing. float GetRowSpacing() const; /// Return wordwrap mode. bool GetWordwrap() const; /// Return text effect. Text3DTextEffect GetTextEffect() const; /// Return effect shadow offset. const IntVector2& GetEffectShadowOffset() const; /// Return effect stroke thickness. int GetEffectStrokeThickness() const; /// Return effect round stroke. bool GetEffectRoundStroke() const; /// Return effect color. const Color& GetEffectColor() const; /// Return effect depth bias. float GetEffectDepthBias() const; /// Return text width. int GetWidth() const; /// Return text height. int GetHeight() const; /// Return row height. int GetRowHeight() const; /// Return number of rows. unsigned GetNumRows() const; /// Return number of characters. unsigned GetNumChars() const; /// Return width of row by index. int GetRowWidth(unsigned index) const; /// Return position of character by index relative to the text element origin. Vector2 GetCharPosition(unsigned index); /// Return size of character by index. Vector2 GetCharSize(unsigned index); /// Return corner color. const Color& GetColor(Text3DCorner corner) const; /// Return opacity. float GetOpacity() const; /// Return whether text has fixed screen size. bool IsFixedScreenSize() const { return fixedScreenSize_; } /// Return how the text rotates in relation to the camera. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; } /// Set font attribute. void SetFontAttr(const ResourceRef& value); /// Return font attribute. ResourceRef GetFontAttr() const; /// Set material attribute. void SetMaterialAttr(const ResourceRef& value); /// Return material attribute. ResourceRef GetMaterialAttr() const; /// Set text attribute. void SetTextAttr(const String& value); /// Return text attribute. String GetTextAttr() const; /// Get color attribute. Uses just the top-left color. const Color& GetColorAttr() const { return text_.color_[0]; } protected: /// Handle node being assigned. virtual void OnNodeSet(Node* node); /// Recalculate the world-space bounding box. virtual void OnWorldBoundingBoxUpdate(); /// Mark text & geometry dirty. void MarkTextDirty(); /// Update text %UI batches. void UpdateTextBatches(); /// Create materials for text rendering. May only be called from the main thread. Text %UI batches must be up-to-date. void UpdateTextMaterials(bool forceUpdate = false); /// Recalculate camera facing and fixed screen size. void CalculateFixedScreenSize(const FrameInfo& frame); /// Internally used text element. Text3DText text_; /// Geometries. Vector > geometries_; /// Vertex buffer. SharedPtr vertexBuffer_; /// Material to use as a base for the text material(s). SharedPtr material_; /// Text UI batches. PODVector uiBatches_; /// Text vertex data. PODVector uiVertexData_; /// Custom world transform for facing the camera automatically. Matrix3x4 customWorldTransform_; /// Text rotation mode in relation to the camera. FaceCameraMode faceCameraMode_; /// Minimal angle between text normal and look-at direction. float minAngle_; /// Fixed screen size flag. bool fixedScreenSize_; /// Text needs update flag. bool textDirty_; /// Geometry dirty flag. bool geometryDirty_; /// Flag for whether currently using SDF shader defines in the generated material. bool usingSDFShader_; /// Font texture data lost flag. bool fontDataLost_; Text3DHorizontalAlignment horizontalAlignment_; Text3DVerticalAlignment verticalAlignment_; }; }