ImageFont.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 _BITMAP_FONT_OBJECT_H_
  23. #define _BITMAP_FONT_OBJECT_H_
  24. #ifndef _STRINGBUFFER_H_
  25. #include "string/stringBuffer.h"
  26. #endif
  27. #ifndef _IMAGE_ASSET_H_
  28. #include "2d/assets/ImageAsset.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. //-----------------------------------------------------------------------------
  40. class ImageFont : public SceneObject
  41. {
  42. typedef SceneObject Parent;
  43. public:
  44. enum TextAlignment
  45. {
  46. INVALID_ALIGN,
  47. ALIGN_LEFT,
  48. ALIGN_CENTER,
  49. ALIGN_RIGHT
  50. };
  51. private:
  52. struct TextCell
  53. {
  54. char CharValue;
  55. U32 FrameCell;
  56. TextCell(char charValue, U32 frameCell)
  57. {
  58. CharValue = charValue;
  59. FrameCell = frameCell;
  60. }
  61. };
  62. private:
  63. AssetPtr<ImageAsset> mImageAsset;
  64. StringBuffer mText;
  65. F32 mFontPadding;
  66. Vector2 mFontSize;
  67. TextAlignment mTextAlignment;
  68. private:
  69. void calculateSpatials( void );
  70. public:
  71. ImageFont();
  72. ~ImageFont();
  73. static void initPersistFields();
  74. bool onAdd();
  75. void onRemove();
  76. void copyTo(SimObject* object);
  77. virtual bool canPrepareRender( void ) const { return true; }
  78. virtual bool validRender( void ) const { return mImageAsset.notNull() && mText.length() > 0; }
  79. virtual bool shouldRender( void ) const { return true; }
  80. virtual void scenePrepareRender( const SceneRenderState* pSceneRenderState, SceneRenderQueue* pSceneRenderQueue );
  81. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  82. bool setImage( const char* pImageAssetId );
  83. const char* getImage( void ) const { return mImageAsset.getAssetId(); };
  84. void setText( const StringBuffer& text );
  85. inline StringBuffer& getText( void ) { return mText; }
  86. void setTextAlignment( const TextAlignment alignment );
  87. inline TextAlignment getTextAlignment( void ) const { return mTextAlignment; }
  88. void setFontSize( const Vector2& size );
  89. inline Vector2 getFontSize( void ) const { return mFontSize; }
  90. void setFontPadding( const F32 padding );
  91. inline F32 getFontPadding( void ) const { return mFontPadding; }
  92. static TextAlignment getTextAlignmentEnum(const char* label);
  93. static const char* getTextAlignmentDescription(const TextAlignment alignment);
  94. // Declare Console Object.
  95. DECLARE_CONOBJECT(ImageFont);
  96. protected:
  97. static bool setImage(void* obj, const char* data) { static_cast<ImageFont*>(obj)->setImage( data ); return false; }
  98. static const char* getImage(void* obj, const char* data) { return static_cast<ImageFont*>(obj)->getImage(); }
  99. static bool writeImage( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageFont*>(obj)->mImageAsset.notNull(); }
  100. static bool setText( void* obj, const char* data ) { static_cast<ImageFont*>( obj )->setText( data ); return false; }
  101. static const char* getText( void* obj, const char* data ) { return static_cast<ImageFont*>( obj )->getText().getPtr8(); }
  102. static bool writeText( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageFont*>(obj)->mText.length() != 0; }
  103. static bool setTextAlignment( void* obj, const char* data );
  104. static bool writeTextAlignment( void* obj, StringTableEntry pFieldName ){return static_cast<ImageFont*>(obj)->getTextAlignment() != ImageFont::ALIGN_CENTER; }
  105. static bool setFontSize( void* obj, const char* data ) { static_cast<ImageFont*>( obj )->setFontSize( Utility::mGetStringElementVector(data) ); return false; }
  106. static bool writeFontSize( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageFont*>(obj)->getFontSize().notEqual(Vector2::getOne()); }
  107. static bool setFontPadding( void* obj, const char* data ) { static_cast<ImageFont*>( obj )->setFontPadding( dAtof(data) ); return false; }
  108. static bool writeFontPadding( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageFont*>(obj)->getFontPadding() != 0; }
  109. };
  110. #endif // _BITMAP_FONT_OBJECT_H_