TextSprite.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TEXT_SPRITE_H_
  23. #define _TEXT_SPRITE_H_
  24. #ifndef _STRINGBUFFER_H_
  25. #include "string/stringBuffer.h"
  26. #endif
  27. #ifndef _FONT_ASSET_H_
  28. #include "2d/assets/FontAsset.h"
  29. #endif
  30. #ifndef _SCENE_OBJECT_H_
  31. #include "2d/sceneobject/SceneObject.h"
  32. #endif
  33. #ifndef _ASSET_PTR_H_
  34. #include "assets/assetPtr.h"
  35. #endif
  36. #ifndef _UTILITY_H_
  37. #include "2d/core/utility.h"
  38. #endif
  39. #ifndef _BITMAP_FONT_LINE_INFO_H_
  40. #include "bitmapFont/BitmapFontLineInfo.h"
  41. #endif
  42. #ifndef _BITMAP_FONT_CHARACTER_INFO_H_
  43. #include "bitmapFont/BitmapFontCharacterInfo.h"
  44. #endif
  45. using CharInfoMap = std::map<U32, BitmapFontCharacterInfo>;
  46. //-----------------------------------------------------------------------------
  47. class TextSprite : public SceneObject
  48. {
  49. typedef SceneObject Parent;
  50. public:
  51. enum TextAlign
  52. {
  53. INVALID_ALIGN,
  54. ALIGN_LEFT,
  55. ALIGN_CENTER,
  56. ALIGN_RIGHT,
  57. ALIGN_JUSTIFY,
  58. };
  59. enum TextVAlign
  60. {
  61. INVALID_VALIGN,
  62. VALIGN_TOP,
  63. VALIGN_MIDDLE,
  64. VALIGN_BOTTOM,
  65. };
  66. enum OverflowModeX
  67. {
  68. INVALID_OVERFLOW_X,
  69. OVERFLOW_X_WRAP,
  70. OVERFLOW_X_VISIBLE,
  71. OVERFLOW_X_HIDDEN,
  72. OVERFLOW_X_SHRINK,
  73. };
  74. enum OverflowModeY
  75. {
  76. INVALID_OVERFLOW_Y,
  77. OVERFLOW_Y_VISIBLE,
  78. OVERFLOW_Y_HIDDEN,
  79. OVERFLOW_Y_SHRINK,
  80. };
  81. private:
  82. AssetPtr<FontAsset> mFontAsset;
  83. StringBuffer mText;
  84. F32 mFontSize;
  85. F32 mFontScaleX;
  86. F32 mFontScaleY;
  87. TextAlign mTextAlign;
  88. TextVAlign mTextVAlign;
  89. OverflowModeX mOverflowX;
  90. OverflowModeY mOverflowY;
  91. bool mAutoLineHeight;
  92. F32 mCustomLineHeight;
  93. F32 mKerning;
  94. std::vector<BitmapFontLineInfo> mLine;
  95. bool mFontSpatialsDirty;
  96. Vector2 mCalculatedSize;
  97. CharInfoMap mCharInfo;
  98. public:
  99. TextSprite();
  100. ~TextSprite();
  101. static void initPersistFields();
  102. bool onAdd();
  103. void onRemove();
  104. void copyTo(SimObject* object);
  105. virtual bool canPrepareRender( void ) const { return true; }
  106. virtual bool validRender( void ) const { return mFontAsset.notNull() && mText.length() > 0; }
  107. virtual bool shouldRender( void ) const { return true; }
  108. virtual void scenePrepareRender( const SceneRenderState* pSceneRenderState, SceneRenderQueue* pSceneRenderQueue );
  109. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  110. bool setFont( const char* pFontAssetId );
  111. const char* getFont(void) const { return mFontAsset.getAssetId(); }
  112. inline void setText(const StringBuffer& text) { mText.set(&text); mFontSpatialsDirty = true; mCharInfo.clear(); }
  113. inline StringBuffer& getText(void) { return mText; }
  114. inline void setFontSize(const F32 size) { mFontSize = size; mFontSpatialsDirty = true; }
  115. inline F32 getFontSize(void) const { return mFontSize; }
  116. inline void setFontScaleX(const F32 scale) { mFontScaleX = scale; mFontSpatialsDirty = true; }
  117. inline F32 getFontScaleX(void) const { return mFontScaleX; }
  118. inline void setFontScaleY(const F32 scale) { mFontScaleY = scale; mFontSpatialsDirty = true; }
  119. inline F32 getFontScaleY(void) const { return mFontScaleY; }
  120. inline void setTextAlignment(const TextAlign alignment) { mTextAlign = alignment; mFontSpatialsDirty = true; }
  121. inline TextAlign getTextAlignment(void) const { return mTextAlign; }
  122. static TextAlign getTextAlignmentEnum(const char* label);
  123. static const char* getTextAlignmentDescription(const TextAlign alignment);
  124. inline void setTextVAlignment(const TextVAlign alignment) { mTextVAlign = alignment; mFontSpatialsDirty = true; }
  125. inline TextVAlign getTextVAlignment(void) const { return mTextVAlign; }
  126. static TextVAlign getTextVAlignmentEnum(const char* label);
  127. static const char* getTextVAlignmentDescription(const TextVAlign alignment);
  128. inline void setOverflowModeX(const OverflowModeX mode) { mOverflowX = mode; mFontSpatialsDirty = true; }
  129. inline OverflowModeX getOverflowModeX(void) const { return mOverflowX; }
  130. static OverflowModeX getOverflowModeXEnum(const char* label);
  131. static const char* getOverflowModeXDescription(const OverflowModeX mode);
  132. inline void setOverflowModeY(const OverflowModeY mode) { mOverflowY = mode; mFontSpatialsDirty = true; }
  133. inline OverflowModeY getOverflowModeY(void) const { return mOverflowY; }
  134. static OverflowModeY getOverflowModeYEnum(const char* label);
  135. static const char* getOverflowModeYDescription(const OverflowModeY mode);
  136. inline void setAutoLineHeight(const bool isAuto) { mAutoLineHeight = isAuto; mFontSpatialsDirty = true; }
  137. inline bool getAutoLineHeight(void) const { return mAutoLineHeight; }
  138. inline void setCustomLineHeight(const F32 lineHeight) { mCustomLineHeight = lineHeight; mFontSpatialsDirty = true; }
  139. inline F32 getCustomLineHeight(void) const { return mCustomLineHeight; }
  140. inline void setKerning(const F32 kern) { mKerning = kern; mFontSpatialsDirty = true; }
  141. inline F32 getKerning(void) const { return mKerning; }
  142. void resetCharacterSettings(void) { mCharInfo.clear(); }
  143. void setCharacterBlendColor(const U32 charNum, const ColorF color);
  144. ColorF getCharacterBlendColor(const U32 charNum);
  145. bool getCharacterHasBlendColor(const U32 charNum);
  146. void resetCharacterBlendColor(const U32 charNum);
  147. void setCharacterScale(const U32 charNum, const F32 scaleX, const F32 scaleY);
  148. Vector2 getCharacterScale(const U32 charNum);
  149. void resetCharacterScale(const U32 charNum);
  150. void setCharacterOffset(const U32 charNum, const F32 offsetX, const F32 offsetY);
  151. Vector2 getCharacterOffset(const U32 charNum);
  152. void resetCharacterOffset(const U32 charNum);
  153. // Declare Console Object.
  154. DECLARE_CONOBJECT(TextSprite);
  155. protected:
  156. static bool setFont(void* obj, const char* data) { static_cast<TextSprite*>(obj)->setFont( data ); return false; }
  157. static const char* getFont(void* obj, const char* data) { return static_cast<TextSprite*>(obj)->getFont(); }
  158. static bool writeFont( void* obj, StringTableEntry pFieldName ) { return static_cast<TextSprite*>(obj)->mFontAsset.notNull(); }
  159. static bool setText( void* obj, const char* data ) { static_cast<TextSprite*>( obj )->setText( data ); return false; }
  160. static const char* getText( void* obj, const char* data ) { return static_cast<TextSprite*>( obj )->getText().getPtr8(); }
  161. static bool writeText(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->mText.length() != 0; }
  162. static bool setFontSize(void* obj, const char* data) { static_cast<TextSprite*>(obj)->setFontSize(dAtof(data)); return false; }
  163. static bool writeFontSize(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->getFontSize() != 1.0f; }
  164. static bool setFontScaleX(void* obj, const char* data) { static_cast<TextSprite*>(obj)->setFontScaleX(dAtof(data)); return false; }
  165. static bool writeFontScaleX(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->getFontScaleX() != 1.0f; }
  166. static bool setFontScaleY(void* obj, const char* data) { static_cast<TextSprite*>(obj)->setFontScaleY(dAtof(data)); return false; }
  167. static bool writeFontScaleY(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->getFontScaleY() != 1.0f; }
  168. static bool setTextAlignment(void* obj, const char* data);
  169. static bool writeTextAlignment(void* obj, StringTableEntry pFieldName){ return static_cast<TextSprite*>(obj)->getTextAlignment() != TextSprite::ALIGN_LEFT; }
  170. static bool setTextVAlignment(void* obj, const char* data);
  171. static bool writeTextVAlignment(void* obj, StringTableEntry pFieldName){ return static_cast<TextSprite*>(obj)->getTextVAlignment() != TextSprite::VALIGN_TOP; }
  172. static bool setOverflowModeX(void* obj, const char* data);
  173. static bool writeOverflowModeX(void* obj, StringTableEntry pFieldName){ return static_cast<TextSprite*>(obj)->getOverflowModeX() != TextSprite::OVERFLOW_X_WRAP; }
  174. static bool setOverflowModeY(void* obj, const char* data);
  175. static bool writeOverflowModeY(void* obj, StringTableEntry pFieldName){ return static_cast<TextSprite*>(obj)->getOverflowModeY() != TextSprite::OVERFLOW_Y_HIDDEN; }
  176. static bool writeAutoLineHeight(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->getAutoLineHeight() != true; }
  177. static bool setCustomLineHeight(void* obj, const char* data) { static_cast<TextSprite*>(obj)->setCustomLineHeight(dAtof(data)); return false; }
  178. static bool writeCustomLineHeight(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->getCustomLineHeight() != 1.0f; }
  179. static bool setKerning(void* obj, const char* data) { static_cast<TextSprite*>(obj)->setKerning(dAtof(data)); return false; }
  180. static bool writeKerning(void* obj, StringTableEntry pFieldName) { return static_cast<TextSprite*>(obj)->getKerning() != 0.0f; }
  181. private:
  182. void RenderLetter(BatchRender* pBatchRenderer, Vector2& cursor, U32 charID, F32 ratio, U32 charNum);
  183. void ApplyAlignment(Vector2& cursor, U32 totalRows, U32 row, F32 length, U32 charCount, F32 ratio);
  184. F32 getCursorAdvance(U32 charID, S32 prevCharID, F32 ratio);
  185. F32 getCursorAdvance(const BitmapFontCharacter& bmChar, S32 prevCharID, F32 ratio);
  186. F32 GetLineHeight() { return (mAutoLineHeight) ? mFontSize : mCustomLineHeight; }
  187. void CalculateSpatials(F32 ratio);
  188. };
  189. #endif // _TEXT_SPRITE_H_