BsFontRTTI.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsRTTIType.h"
  4. #include "BsFont.h"
  5. #include "BsFontManager.h"
  6. #include "BsTexture.h"
  7. namespace BansheeEngine
  8. {
  9. /** @cond RTTI */
  10. /** @addtogroup RTTI-Impl-Core
  11. * @{
  12. */
  13. class BS_CORE_EXPORT FontBitmapRTTI : public RTTIType<FontBitmap, IReflectable, FontBitmapRTTI>
  14. {
  15. private:
  16. UINT32& getSize(FontBitmap* obj) { return obj->size; }
  17. void setSize(FontBitmap* obj, UINT32& size) { obj->size = size; }
  18. FONT_DESC& getFontDesc(FontBitmap* obj) { return obj->fontDesc; }
  19. void setFontDesc(FontBitmap* obj, FONT_DESC& val) { obj->fontDesc = val; }
  20. HTexture& getTexture(FontBitmap* obj, UINT32 idx) { return obj->texturePages.at(idx); }
  21. void setTexture(FontBitmap* obj, UINT32 idx, HTexture& value) { obj->texturePages[idx] = value; }
  22. UINT32 getTextureArraySize(FontBitmap* obj) { return (UINT32)obj->texturePages.size(); }
  23. void setTextureArraySize(FontBitmap* obj, UINT32 size) { obj->texturePages.resize(size); }
  24. public:
  25. FontBitmapRTTI()
  26. {
  27. addPlainField("size", 0, &FontBitmapRTTI::getSize, &FontBitmapRTTI::setSize);
  28. addPlainField("fontDesc", 1, &FontBitmapRTTI::getFontDesc, &FontBitmapRTTI::setFontDesc);
  29. addReflectableArrayField("texturePages", 2, &FontBitmapRTTI::getTexture, &FontBitmapRTTI::getTextureArraySize, &FontBitmapRTTI::setTexture, &FontBitmapRTTI::setTextureArraySize);
  30. }
  31. const String& getRTTIName() override
  32. {
  33. static String name = "FontData";
  34. return name;
  35. }
  36. UINT32 getRTTIId() override
  37. {
  38. return TID_FontBitmap;
  39. }
  40. std::shared_ptr<IReflectable> newRTTIObject() override
  41. {
  42. return bs_shared_ptr_new<FontBitmap>();
  43. }
  44. };
  45. class BS_CORE_EXPORT FontRTTI : public RTTIType<Font, Resource, FontRTTI>
  46. {
  47. struct FontInitData
  48. {
  49. Vector<SPtr<FontBitmap>> fontDataPerSize;
  50. };
  51. private:
  52. FontBitmap& getBitmap(Font* obj, UINT32 idx)
  53. {
  54. if(idx >= obj->mFontDataPerSize.size())
  55. BS_EXCEPT(InternalErrorException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((int)obj->mFontDataPerSize.size()));
  56. auto iter = obj->mFontDataPerSize.begin();
  57. for(UINT32 i = 0; i < idx; i++, ++iter)
  58. { }
  59. return *iter->second;
  60. }
  61. void setBitmap(Font* obj, UINT32 idx, FontBitmap& value)
  62. {
  63. FontInitData* initData = any_cast<FontInitData*>(obj->mRTTIData);
  64. initData->fontDataPerSize[idx] = bs_shared_ptr_new<FontBitmap>();
  65. *initData->fontDataPerSize[idx] = value;
  66. }
  67. UINT32 getNumBitmaps(Font* obj)
  68. {
  69. return (UINT32)obj->mFontDataPerSize.size();
  70. }
  71. void setNumBitmaps(Font* obj, UINT32 size)
  72. {
  73. FontInitData* initData = any_cast<FontInitData*>(obj->mRTTIData);
  74. initData->fontDataPerSize.resize(size);
  75. }
  76. public:
  77. FontRTTI()
  78. {
  79. addReflectableArrayField("mBitmaps", 0, &FontRTTI::getBitmap, &FontRTTI::getNumBitmaps, &FontRTTI::setBitmap, &FontRTTI::setNumBitmaps);
  80. }
  81. const String& getRTTIName() override
  82. {
  83. static String name = "Font";
  84. return name;
  85. }
  86. UINT32 getRTTIId() override
  87. {
  88. return TID_Font;
  89. }
  90. std::shared_ptr<IReflectable> newRTTIObject() override
  91. {
  92. return FontManager::instance()._createEmpty();
  93. }
  94. protected:
  95. void onDeserializationStarted(IReflectable* obj) override
  96. {
  97. FontInitData* initData = bs_new<FontInitData>();
  98. Font* font = static_cast<Font*>(obj);
  99. font->mRTTIData = initData;
  100. }
  101. void onDeserializationEnded(IReflectable* obj) override
  102. {
  103. Font* font = static_cast<Font*>(obj);
  104. FontInitData* initData = any_cast<FontInitData*>(font->mRTTIData);
  105. font->initialize(initData->fontDataPerSize);
  106. bs_delete(initData);
  107. }
  108. };
  109. /** @} */
  110. /** @endcond */
  111. }