TextHandler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "TextHandler.h"
  16. #include <iostream>
  17. #include "core/TextHandlerCommands.h"
  18. #include "core/XmlTextsFile.h"
  19. #include "core/XmlTextFile.h"
  20. /**
  21. * Text handler singleton.
  22. */
  23. template<>TextHandler* Ogre::Singleton<TextHandler>::msSingleton = NULL;
  24. TextHandler::TextHandler(): language_(""){
  25. for (int i = 0; i < 3; i ++) current_party_[i] = -1;
  26. for (int i = 0; i < 11; i ++) character_names_[i] = std::string("CHAR_") + std::to_string(i);
  27. InitCmd();
  28. }
  29. TextHandler::~TextHandler(){UnloadTexts();}
  30. void TextHandler::LoadFieldText(const std::string file_name){
  31. XmlTextFile texts("./data/fields/" + file_name);
  32. texts.LoadTexts();
  33. }
  34. void TextHandler::SetLanguage(const Ogre::String& language){
  35. language_ = language;
  36. UnloadTexts();
  37. XmlTextsFile texts("./data/texts/_texts.xml");
  38. texts.LoadTexts();
  39. }
  40. const Ogre::String& TextHandler::GetLanguage(){return language_;}
  41. void TextHandler::AddText(const Ogre::String& name, TiXmlNode* node){
  42. Text text;
  43. text.name = name;
  44. text.node = node;
  45. texts_.push_back(text);
  46. }
  47. void TextHandler::AddDialog(
  48. const Ogre::String& name, TiXmlNode* node, const float width, const float height
  49. ){
  50. Dialog dialog;
  51. dialog.name = name;
  52. dialog.node = node;
  53. dialog.width = width;
  54. dialog.height = height;
  55. dialogs_.push_back(dialog);
  56. }
  57. TiXmlNode* TextHandler::GetText(const Ogre::String& name) const{
  58. for (unsigned int i = 0; i < texts_.size(); ++ i)
  59. if (texts_[i].name == name) return texts_[i].node;
  60. LOG_WARNING("Can't find text '" + name + "'.");
  61. return NULL;
  62. }
  63. TiXmlNode* TextHandler::GetDialog(const Ogre::String& name, float &width, float& height) const{
  64. for (unsigned int i = 0; i < dialogs_.size(); ++ i){
  65. if (dialogs_[i].name == name){
  66. width = dialogs_[i].width;
  67. height = dialogs_[i].height;
  68. return dialogs_[i].node;
  69. }
  70. }
  71. return NULL;
  72. }
  73. std::string TextHandler::ExtractText(
  74. const TiXmlNode* node, const std::function<std::string(int)>& character_name
  75. ){
  76. std::string text;
  77. if (node == nullptr) return text;
  78. for (const TiXmlNode* child = node->FirstChild(); child; child = child->NextSibling()){
  79. if (child->ToText() != nullptr){
  80. text += child->ToText()->Value();
  81. }
  82. else if (child->ToElement() != nullptr){
  83. if (child->ValueStr() == "character"){
  84. int id = -1;
  85. if (child->ToElement()->QueryIntAttribute("id", &id) == TIXML_SUCCESS)
  86. text += character_name(id);
  87. }
  88. else text += ExtractText(child, character_name);
  89. }
  90. }
  91. return text;
  92. }
  93. std::string TextHandler::GetDialogText(const std::string name){
  94. for (unsigned int i = 0; i < dialogs_.size(); ++ i){
  95. if (dialogs_[i].name == name){
  96. return ExtractText(dialogs_[i].node, [this](int id){return GetCharacterName(id);});
  97. }
  98. }
  99. return "";
  100. }
  101. void TextHandler::UnloadTexts(){
  102. for (unsigned int i = 0; i < texts_.size(); ++ i) delete texts_[i].node;
  103. texts_.clear();
  104. for (unsigned int i = 0; i < dialogs_.size(); ++ i) delete dialogs_[i].node;
  105. dialogs_.clear();
  106. }
  107. std::string TextHandler::GetCharacterName(int id){
  108. if (id >= 0 && id < 11) return character_names_[id];
  109. else return "UNKNOWN_CHAR_" + std::to_string(id);
  110. }
  111. std::string TextHandler::GetPartyCharacterName(int position){
  112. if (position >= 1 && position <= 3){
  113. if (current_party_[position - 1] < 0) return "";
  114. else return GetCharacterName(current_party_[position - 1]);
  115. }
  116. else return std::string("UNKNOWN_PARTY_POS_" + position);
  117. }
  118. void TextHandler::SetCharacterName(int id, const char* name){
  119. if (id >= 0 && id < 11) character_names_[id] = std::string(name);
  120. else LOG_WARNING("Not setting character name for character ID " + std::to_string(id) + ".");
  121. }
  122. void TextHandler::SetParty(int char_1, int char_2, int char_3){
  123. current_party_[0] = char_1;
  124. current_party_[1] = char_2;
  125. current_party_[2] = char_3;
  126. }
  127. std::vector<int> TextHandler::GetParty(){
  128. std::vector<int> party = {current_party_[0], current_party_[1], current_party_[2]};
  129. return party;
  130. }