BsFontRTTI.h 3.9 KB

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