CAdventureMapWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * CAdventureMapWidget.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 "CAdventureMapWidget.h"
  12. #include "CInfoBar.h"
  13. #include "CList.h"
  14. #include "CMinimap.h"
  15. #include "CResDataBar.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../mapView/MapView.h"
  18. #include "../render/CAnimation.h"
  19. #include "../render/IImage.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/Images.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../PlayerLocalState.h"
  25. #include "../../lib/StringConstants.h"
  26. #include "../../lib/filesystem/ResourceID.h"
  27. CAdventureMapWidget::CAdventureMapWidget()
  28. : state(EGameState::NOT_INITIALIZED)
  29. {
  30. pos.w = 800;
  31. pos.h = 600;
  32. REGISTER_BUILDER("adventureInfobar", &CAdventureMapWidget::buildInfobox );
  33. REGISTER_BUILDER("adventureMapImage", &CAdventureMapWidget::buildMapImage );
  34. REGISTER_BUILDER("adventureMapButton", &CAdventureMapWidget::buildMapButton );
  35. REGISTER_BUILDER("adventureMapContainer", &CAdventureMapWidget::buildMapContainer );
  36. REGISTER_BUILDER("adventureMapGameArea", &CAdventureMapWidget::buildMapGameArea );
  37. REGISTER_BUILDER("adventureMapHeroList", &CAdventureMapWidget::buildMapHeroList );
  38. REGISTER_BUILDER("adventureMapIcon", &CAdventureMapWidget::buildMapIcon );
  39. REGISTER_BUILDER("adventureMapTownList", &CAdventureMapWidget::buildMapTownList );
  40. REGISTER_BUILDER("adventureMinimap", &CAdventureMapWidget::buildMinimap );
  41. REGISTER_BUILDER("adventureResourceDateBar", &CAdventureMapWidget::buildResourceDateBar );
  42. REGISTER_BUILDER("adventureStatusBar", &CAdventureMapWidget::buildStatusBar );
  43. const JsonNode config(ResourceID("config/widgets/adventureMap.json"));
  44. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  45. {
  46. ResourceID resourceName(entry.String(), EResType::IMAGE);
  47. playerColorerImages.push_back(resourceName.getName());
  48. }
  49. build(config);
  50. }
  51. Rect CAdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
  52. {
  53. const auto & input = source.isNull() ? sourceCommon : source;
  54. return readArea(input, Rect(Point(0, 0), Point(800, 600)));
  55. }
  56. Rect CAdventureMapWidget::readTargetArea(const JsonNode & source)
  57. {
  58. if(subwidgetSizes.empty())
  59. return readArea(source, pos);
  60. return readArea(source, subwidgetSizes.back());
  61. }
  62. Rect CAdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
  63. {
  64. const auto & object = source.Struct();
  65. if(object.count("left") + object.count("width") + object.count("right") != 2)
  66. logGlobal->error("Invalid area definition in widget! Unable to load width!");
  67. if(object.count("top") + object.count("height") + object.count("bottom") != 2)
  68. logGlobal->error("Invalid area definition in widget! Unable to load height!");
  69. int left = source["left"].Integer();
  70. int width = source["width"].Integer();
  71. int right = source["right"].Integer();
  72. int top = source["top"].Integer();
  73. int height = source["height"].Integer();
  74. int bottom = source["bottom"].Integer();
  75. Point topLeft(left, top);
  76. Point dimensions(width, height);
  77. if(source["left"].isNull())
  78. topLeft.x = boundingBox.w - right - width;
  79. if(source["width"].isNull())
  80. dimensions.x = boundingBox.w - right - left;
  81. if(source["top"].isNull())
  82. topLeft.y = boundingBox.h - bottom - height;
  83. if(source["height"].isNull())
  84. dimensions.y = boundingBox.h - bottom - top;
  85. return Rect(topLeft + boundingBox.topLeft(), dimensions);
  86. }
  87. std::shared_ptr<IImage> CAdventureMapWidget::loadImage(const std::string & name)
  88. {
  89. ResourceID resource(name, EResType::IMAGE);
  90. if(images.count(resource.getName()) == 0)
  91. images[resource.getName()] = IImage::createFromFile(resource.getName());
  92. ;
  93. return images[resource.getName()];
  94. }
  95. std::shared_ptr<CAnimation> CAdventureMapWidget::loadAnimation(const std::string & name)
  96. {
  97. ResourceID resource(name, EResType::ANIMATION);
  98. if(animations.count(resource.getName()) == 0)
  99. animations[resource.getName()] = std::make_shared<CAnimation>(resource.getName());
  100. return animations[resource.getName()];
  101. }
  102. std::shared_ptr<CIntObject> CAdventureMapWidget::buildInfobox(const JsonNode & input)
  103. {
  104. Rect area = readTargetArea(input["area"]);
  105. return std::make_shared<CInfoBar>(area);
  106. }
  107. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapImage(const JsonNode & input)
  108. {
  109. Rect targetArea = readTargetArea(input["area"]);
  110. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  111. std::string image = input["image"].String();
  112. return std::make_shared<CFilledTexture>(loadImage(image), targetArea, sourceArea);
  113. }
  114. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapButton(const JsonNode & input)
  115. {
  116. auto position = readTargetArea(input["area"]);
  117. auto image = input["image"].String();
  118. auto help = readHintText(input["help"]);
  119. return std::make_shared<CButton>(position.topLeft(), image, help);
  120. }
  121. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapContainer(const JsonNode & input)
  122. {
  123. auto position = readTargetArea(input["area"]);
  124. auto result = std::make_shared<CAdventureMapContainerWidget>();
  125. result->moveBy(position.topLeft());
  126. subwidgetSizes.push_back(position);
  127. for(const auto & entry : input["items"].Vector())
  128. {
  129. result->ownedChildren.push_back(buildWidget(entry));
  130. result->addChild(result->ownedChildren.back().get(), true);
  131. }
  132. subwidgetSizes.pop_back();
  133. return result;
  134. }
  135. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapGameArea(const JsonNode & input)
  136. {
  137. Rect area = readTargetArea(input["area"]);
  138. return std::make_shared<MapView>(area.topLeft(), area.dimensions());
  139. }
  140. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapHeroList(const JsonNode & input)
  141. {
  142. Rect area = readTargetArea(input["area"]);
  143. Rect item = readTargetArea(input["item"]);
  144. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  145. int itemsCount = input["itemsCount"].Integer();
  146. auto result = std::make_shared<CHeroList>(itemsCount, area.topLeft() + item.topLeft(), itemOffset, LOCPLINT->localState->getWanderingHeroes().size());
  147. subwidgetSizes.push_back(area);
  148. if(!input["scrollUp"].isNull())
  149. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  150. if(!input["scrollDown"].isNull())
  151. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  152. subwidgetSizes.pop_back();
  153. return result;
  154. }
  155. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapIcon(const JsonNode & input)
  156. {
  157. Rect area = readTargetArea(input["area"]);
  158. size_t index = input["index"].Integer();
  159. size_t perPlayer = input["perPlayer"].Integer();
  160. std::string image = input["image"].String();
  161. return std::make_shared<CAdventureMapIcon>(area.topLeft(), loadAnimation(image), index, perPlayer);
  162. }
  163. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapTownList(const JsonNode & input)
  164. {
  165. Rect area = readTargetArea(input["area"]);
  166. Rect item = readTargetArea(input["item"]);
  167. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  168. int itemsCount = input["itemsCount"].Integer();
  169. auto result = std::make_shared<CTownList>(itemsCount, area.topLeft() + item.topLeft(), itemOffset, LOCPLINT->localState->getOwnedTowns().size());
  170. subwidgetSizes.push_back(area);
  171. if(!input["scrollUp"].isNull())
  172. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  173. if(!input["scrollDown"].isNull())
  174. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  175. subwidgetSizes.pop_back();
  176. return result;
  177. }
  178. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMinimap(const JsonNode & input)
  179. {
  180. Rect area = readTargetArea(input["area"]);
  181. return std::make_shared<CMinimap>(area);
  182. }
  183. std::shared_ptr<CIntObject> CAdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  184. {
  185. Rect area = readTargetArea(input["area"]);
  186. std::string image = input["image"].String();
  187. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  188. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  189. {
  190. const auto & node = input[GameConstants::RESOURCE_NAMES[i]];
  191. if(node.isNull())
  192. continue;
  193. result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
  194. }
  195. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  196. return result;
  197. }
  198. std::shared_ptr<CIntObject> CAdventureMapWidget::buildStatusBar(const JsonNode & input)
  199. {
  200. Rect area = readTargetArea(input["area"]);
  201. std::string image = input["image"].String();
  202. auto background = std::make_shared<CFilledTexture>(image, area);
  203. return CGStatusBar::create(background);
  204. }
  205. std::shared_ptr<CHeroList> CAdventureMapWidget::getHeroList()
  206. {
  207. return widget<CHeroList>("heroList");
  208. }
  209. std::shared_ptr<CTownList> CAdventureMapWidget::getTownList()
  210. {
  211. return widget<CTownList>("townList");
  212. }
  213. std::shared_ptr<CMinimap> CAdventureMapWidget::getMinimap()
  214. {
  215. return widget<CMinimap>("minimap");
  216. }
  217. std::shared_ptr<MapView> CAdventureMapWidget::getMapView()
  218. {
  219. return widget<MapView>("mapView");
  220. }
  221. std::shared_ptr<CInfoBar> CAdventureMapWidget::getInfoBar()
  222. {
  223. return widget<CInfoBar>("infoBar");
  224. }
  225. void CAdventureMapWidget::setPlayer(const PlayerColor & player)
  226. {
  227. setPlayerChildren(this, player);
  228. }
  229. void CAdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  230. {
  231. for(auto & entry : widget->children)
  232. {
  233. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  234. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  235. auto button = dynamic_cast<CButton *>(entry);
  236. if(button)
  237. button->setPlayerColor(player);
  238. if(icon)
  239. icon->setPlayer(player);
  240. if(container)
  241. setPlayerChildren(container, player);
  242. }
  243. for(const auto & entry : playerColorerImages)
  244. {
  245. if(images.count(entry))
  246. images[entry]->playerColored(player);
  247. }
  248. redraw();
  249. }
  250. void CAdventureMapWidget::setState(EGameState newState)
  251. {
  252. state = newState;
  253. if(newState == EGameState::WORLD_VIEW)
  254. widget<CIntObject>("worldViewContainer")->enable();
  255. else
  256. widget<CIntObject>("worldViewContainer")->disable();
  257. }
  258. EGameState CAdventureMapWidget::getState()
  259. {
  260. return state;
  261. }
  262. void CAdventureMapWidget::setOptionHasQuests(bool on)
  263. {
  264. }
  265. void CAdventureMapWidget::setOptionHasUnderground(bool on)
  266. {
  267. }
  268. void CAdventureMapWidget::setOptionUndergroundLevel(bool on)
  269. {
  270. }
  271. void CAdventureMapWidget::setOptionHeroSleeping(bool on)
  272. {
  273. }
  274. void CAdventureMapWidget::setOptionHeroSelected(bool on)
  275. {
  276. }
  277. void CAdventureMapWidget::setOptionHeroCanMove(bool on)
  278. {
  279. }
  280. void CAdventureMapWidget::setOptionHasNextHero(bool on)
  281. {
  282. }
  283. CAdventureMapIcon::CAdventureMapIcon(const Point & position, std::shared_ptr<CAnimation> animation, size_t index, size_t iconsPerPlayer)
  284. : index(index)
  285. , iconsPerPlayer(iconsPerPlayer)
  286. {
  287. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  288. pos += position;
  289. image = std::make_shared<CAnimImage>(animation, index);
  290. }
  291. void CAdventureMapIcon::setPlayer(const PlayerColor & player)
  292. {
  293. image->setFrame(index + player.getNum() * iconsPerPlayer);
  294. }