BsFontRTTI.h 3.6 KB

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