2
0

CampaignRegions.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * CampaignRegions.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CampaignRegions.h"
  12. #include "../json/JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. CampaignRegions::RegionDescription::RegionDescription(const JsonNode & node)
  15. {
  16. infix = node["infix"].String();
  17. pos = Point(static_cast<int>(node["x"].Float()), static_cast<int>(node["y"].Float()));
  18. if(!node["labelPos"].isNull())
  19. labelPos = Point(static_cast<int>(node["labelPos"]["x"].Float()), static_cast<int>(node["labelPos"]["y"].Float()));
  20. else
  21. labelPos = std::nullopt;
  22. }
  23. JsonNode CampaignRegions::RegionDescription::toJson() const
  24. {
  25. JsonNode node;
  26. node["infix"].String() = infix;
  27. node["x"].Float() = pos.x;
  28. node["y"].Float() = pos.y;
  29. if(labelPos != std::nullopt)
  30. {
  31. node["labelPos"]["x"].Float() = (*labelPos).x;
  32. node["labelPos"]["y"].Float() = (*labelPos).y;
  33. }
  34. else
  35. node["labelPos"].clear();
  36. return node;
  37. }
  38. CampaignRegions::CampaignRegions(const JsonNode & node)
  39. {
  40. campPrefix = node["prefix"].String();
  41. colorSuffixLength = static_cast<int>(node["colorSuffixLength"].Float());
  42. campSuffix = node["suffix"].isNull() ? std::vector<std::string>() : std::vector<std::string>{node["suffix"].Vector()[0].String(), node["suffix"].Vector()[1].String(), node["suffix"].Vector()[2].String()};
  43. campBackground = node["background"].isNull() ? "" : node["background"].String();
  44. for(const JsonNode & desc : node["desc"].Vector())
  45. regions.push_back(CampaignRegions::RegionDescription(desc));
  46. }
  47. JsonNode CampaignRegions::toJson() const
  48. {
  49. JsonNode node;
  50. node["prefix"].String() = campPrefix;
  51. node["colorSuffixLength"].Float() = colorSuffixLength;
  52. if(campSuffix.empty())
  53. node["suffix"].clear();
  54. else
  55. node["suffix"].Vector() = JsonVector{ JsonNode(campSuffix[0]), JsonNode(campSuffix[1]), JsonNode(campSuffix[2]) };
  56. if(campBackground.empty())
  57. node["background"].clear();
  58. else
  59. node["background"].String() = campBackground;
  60. node["desc"].Vector() = JsonVector();
  61. for(const auto & region : regions)
  62. node["desc"].Vector().push_back(region.toJson());
  63. return node;
  64. }
  65. ImagePath CampaignRegions::getBackgroundName() const
  66. {
  67. if(campBackground.empty())
  68. return ImagePath::builtin(campPrefix + "_BG.BMP");
  69. else
  70. return ImagePath::builtin(campBackground);
  71. }
  72. Point CampaignRegions::getPosition(CampaignScenarioID which) const
  73. {
  74. const auto & region = regions[which.getNum()];
  75. return region.pos;
  76. }
  77. std::optional<Point> CampaignRegions::getLabelPosition(CampaignScenarioID which) const
  78. {
  79. const auto & region = regions[which.getNum()];
  80. return region.labelPos;
  81. }
  82. ImagePath CampaignRegions::getNameFor(CampaignScenarioID which, int colorIndex, const std::string & type) const
  83. {
  84. const auto & region = regions[which.getNum()];
  85. static const std::array<std::array<std::string, 8>, 3> colors = {{
  86. { "", "", "", "", "", "", "", "" },
  87. { "R", "B", "N", "G", "O", "V", "T", "P" },
  88. { "Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi" }
  89. }};
  90. std::string color = colors[colorSuffixLength][colorIndex];
  91. return ImagePath::builtin(campPrefix + region.infix + "_" + type + color + ".BMP");
  92. }
  93. ImagePath CampaignRegions::getAvailableName(CampaignScenarioID which, int color) const
  94. {
  95. if(campSuffix.empty())
  96. return getNameFor(which, color, "En");
  97. else
  98. return getNameFor(which, color, campSuffix[0]);
  99. }
  100. ImagePath CampaignRegions::getSelectedName(CampaignScenarioID which, int color) const
  101. {
  102. if(campSuffix.empty())
  103. return getNameFor(which, color, "Se");
  104. else
  105. return getNameFor(which, color, campSuffix[1]);
  106. }
  107. ImagePath CampaignRegions::getConqueredName(CampaignScenarioID which, int color) const
  108. {
  109. if(campSuffix.empty())
  110. return getNameFor(which, color, "Co");
  111. else
  112. return getNameFor(which, color, campSuffix[2]);
  113. }
  114. int CampaignRegions::regionsCount() const
  115. {
  116. return regions.size();
  117. }
  118. VCMI_LIB_NAMESPACE_END