XmlFontFile.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "core/Logger.h"
  2. #include "core/UiManager.h"
  3. #include "core/UiFont.h"
  4. #include "core/XmlFontFile.h"
  5. XmlFontFile::XmlFontFile(const Ogre::String& file):
  6. XmlFile(file)
  7. {
  8. }
  9. XmlFontFile::~XmlFontFile()
  10. {
  11. }
  12. void
  13. XmlFontFile::LoadFont()
  14. {
  15. TiXmlNode* node = m_File.RootElement();
  16. if(node == nullptr || node->ValueStr() != "font")
  17. {
  18. LOG_ERROR(m_File.ValueStr() + " is not a valid font file! No <font> in root.");
  19. return;
  20. }
  21. Ogre::String name = GetString(node, "name", "");
  22. Ogre::String image = GetString(node, "image", "");
  23. Ogre::Vector2 size = GetVector2(node, "image_size", Ogre::Vector2::ZERO);
  24. int height = GetInt(node, "height", 0);
  25. if(name != "" && image != "" && size.x != 0 && size.y != 0)
  26. {
  27. UiFont* font = new UiFont(name);
  28. font->SetImage(image, (int)size.x, (int)size.y);
  29. font->SetHeight(height);
  30. node = node->FirstChild();
  31. while(node != nullptr)
  32. {
  33. if(node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "char")
  34. {
  35. UiCharData data;
  36. Ogre::UTFString name = GetUTFString(node, "name", "");
  37. if(name != "")
  38. {
  39. data.char_code = *(name.c_str());
  40. }
  41. data.x = GetInt(node, "x", 0);
  42. data.y = GetInt(node, "y", 0);
  43. data.width = GetInt(node, "width", 0);
  44. data.height = GetInt(node, "height", 0);
  45. data.pre = GetInt(node, "pre", 0);
  46. data.post = GetInt(node, "post", 0);
  47. font->AddCharData(data);
  48. }
  49. node = node->NextSibling();
  50. }
  51. UiManager::getSingleton().AddFont(font);
  52. }
  53. }