AdventureMapWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 "AdventureMapWidget.h"
  12. #include "AdventureMapShortcuts.h"
  13. #include "CInfoBar.h"
  14. #include "CList.h"
  15. #include "CMinimap.h"
  16. #include "CResDataBar.h"
  17. #include "AdventureState.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../mapView/MapView.h"
  21. #include "../render/CAnimation.h"
  22. #include "../render/IImage.h"
  23. #include "../widgets/Buttons.h"
  24. #include "../widgets/Images.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../CPlayerInterface.h"
  27. #include "../PlayerLocalState.h"
  28. #include "../../lib/constants/StringConstants.h"
  29. #include "../../lib/filesystem/ResourceID.h"
  30. AdventureMapWidget::AdventureMapWidget( std::shared_ptr<AdventureMapShortcuts> shortcuts )
  31. : shortcuts(shortcuts)
  32. , mapLevel(0)
  33. {
  34. pos.x = pos.y = 0;
  35. pos.w = GH.screenDimensions().x;
  36. pos.h = GH.screenDimensions().y;
  37. REGISTER_BUILDER("adventureInfobar", &AdventureMapWidget::buildInfobox );
  38. REGISTER_BUILDER("adventureMapImage", &AdventureMapWidget::buildMapImage );
  39. REGISTER_BUILDER("adventureMapButton", &AdventureMapWidget::buildMapButton );
  40. REGISTER_BUILDER("adventureMapContainer", &AdventureMapWidget::buildMapContainer );
  41. REGISTER_BUILDER("adventureMapGameArea", &AdventureMapWidget::buildMapGameArea );
  42. REGISTER_BUILDER("adventureMapHeroList", &AdventureMapWidget::buildMapHeroList );
  43. REGISTER_BUILDER("adventureMapIcon", &AdventureMapWidget::buildMapIcon );
  44. REGISTER_BUILDER("adventureMapTownList", &AdventureMapWidget::buildMapTownList );
  45. REGISTER_BUILDER("adventureMinimap", &AdventureMapWidget::buildMinimap );
  46. REGISTER_BUILDER("adventureResourceDateBar", &AdventureMapWidget::buildResourceDateBar );
  47. REGISTER_BUILDER("adventureStatusBar", &AdventureMapWidget::buildStatusBar );
  48. REGISTER_BUILDER("adventurePlayerTexture", &AdventureMapWidget::buildTexturePlayerColored);
  49. for (const auto & entry : shortcuts->getShortcuts())
  50. addShortcut(entry.shortcut, entry.callback);
  51. const JsonNode config(ResourceID("config/widgets/adventureMap.json"));
  52. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  53. {
  54. ResourceID resourceName(entry.String(), EResType::IMAGE);
  55. playerColorerImages.push_back(resourceName.getName());
  56. }
  57. build(config);
  58. addUsedEvents(KEYBOARD);
  59. }
  60. void AdventureMapWidget::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  61. {
  62. if(mapLevel == newMapLevel)
  63. return;
  64. mapLevel = newMapLevel;
  65. updateActiveState();
  66. }
  67. Rect AdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
  68. {
  69. const auto & input = source.isNull() ? sourceCommon : source;
  70. return readArea(input, Rect(Point(0, 0), Point(800, 600)));
  71. }
  72. Rect AdventureMapWidget::readTargetArea(const JsonNode & source)
  73. {
  74. if(subwidgetSizes.empty())
  75. return readArea(source, pos);
  76. return readArea(source, subwidgetSizes.back());
  77. }
  78. Rect AdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
  79. {
  80. const auto & object = source.Struct();
  81. if(object.count("left") + object.count("width") + object.count("right") != 2)
  82. logGlobal->error("Invalid area definition in widget! Unable to load width!");
  83. if(object.count("top") + object.count("height") + object.count("bottom") != 2)
  84. logGlobal->error("Invalid area definition in widget! Unable to load height!");
  85. int left = source["left"].Integer();
  86. int width = source["width"].Integer();
  87. int right = source["right"].Integer();
  88. int top = source["top"].Integer();
  89. int height = source["height"].Integer();
  90. int bottom = source["bottom"].Integer();
  91. Point topLeft(left, top);
  92. Point dimensions(width, height);
  93. if(source["left"].isNull())
  94. topLeft.x = boundingBox.w - right - width;
  95. if(source["width"].isNull())
  96. dimensions.x = boundingBox.w - right - left;
  97. if(source["top"].isNull())
  98. topLeft.y = boundingBox.h - bottom - height;
  99. if(source["height"].isNull())
  100. dimensions.y = boundingBox.h - bottom - top;
  101. return Rect(topLeft + boundingBox.topLeft(), dimensions);
  102. }
  103. std::shared_ptr<IImage> AdventureMapWidget::loadImage(const std::string & name)
  104. {
  105. ResourceID resource(name, EResType::IMAGE);
  106. if(images.count(resource.getName()) == 0)
  107. images[resource.getName()] = IImage::createFromFile(resource.getName());
  108. return images[resource.getName()];
  109. }
  110. std::shared_ptr<CAnimation> AdventureMapWidget::loadAnimation(const std::string & name)
  111. {
  112. ResourceID resource(name, EResType::ANIMATION);
  113. if(animations.count(resource.getName()) == 0)
  114. animations[resource.getName()] = std::make_shared<CAnimation>(resource.getName());
  115. return animations[resource.getName()];
  116. }
  117. std::shared_ptr<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)
  118. {
  119. Rect area = readTargetArea(input["area"]);
  120. infoBar = std::make_shared<CInfoBar>(area);
  121. return infoBar;
  122. }
  123. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapImage(const JsonNode & input)
  124. {
  125. Rect targetArea = readTargetArea(input["area"]);
  126. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  127. std::string image = input["image"].String();
  128. return std::make_shared<CFilledTexture>(loadImage(image), targetArea, sourceArea);
  129. }
  130. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapButton(const JsonNode & input)
  131. {
  132. auto position = readTargetArea(input["area"]);
  133. auto image = input["image"].String();
  134. auto help = readHintText(input["help"]);
  135. bool playerColored = input["playerColored"].Bool();
  136. auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
  137. loadButtonBorderColor(button, input["borderColor"]);
  138. loadButtonHotkey(button, input["hotkey"]);
  139. return button;
  140. }
  141. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapContainer(const JsonNode & input)
  142. {
  143. auto position = readTargetArea(input["area"]);
  144. std::shared_ptr<CAdventureMapContainerWidget> result;
  145. if (!input["exists"].isNull())
  146. {
  147. if (!input["exists"]["heightMin"].isNull() && input["exists"]["heightMin"].Integer() >= pos.h)
  148. return nullptr;
  149. if (!input["exists"]["heightMax"].isNull() && input["exists"]["heightMax"].Integer() < pos.h)
  150. return nullptr;
  151. if (!input["exists"]["widthMin"].isNull() && input["exists"]["widthMin"].Integer() >= pos.w)
  152. return nullptr;
  153. if (!input["exists"]["widthMax"].isNull() && input["exists"]["widthMax"].Integer() < pos.w)
  154. return nullptr;
  155. }
  156. if (input["overlay"].Bool())
  157. result = std::make_shared<CAdventureMapOverlayWidget>();
  158. else
  159. result = std::make_shared<CAdventureMapContainerWidget>();
  160. result->disableCondition = input["hideWhen"].String();
  161. result->moveBy(position.topLeft());
  162. subwidgetSizes.push_back(position);
  163. for(const auto & entry : input["items"].Vector())
  164. {
  165. auto widget = buildWidget(entry);
  166. addWidget(entry["name"].String(), widget);
  167. result->ownedChildren.push_back(widget);
  168. // FIXME: remove cast and replace it with better check
  169. if (std::dynamic_pointer_cast<CLabel>(widget) || std::dynamic_pointer_cast<CLabelGroup>(widget))
  170. result->addChild(widget.get(), true);
  171. else
  172. result->addChild(widget.get(), false);
  173. }
  174. subwidgetSizes.pop_back();
  175. return result;
  176. }
  177. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapGameArea(const JsonNode & input)
  178. {
  179. Rect area = readTargetArea(input["area"]);
  180. mapView = std::make_shared<MapView>(area.topLeft(), area.dimensions());
  181. return mapView;
  182. }
  183. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapHeroList(const JsonNode & input)
  184. {
  185. Rect area = readTargetArea(input["area"]);
  186. subwidgetSizes.push_back(area);
  187. Rect item = readTargetArea(input["item"]);
  188. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  189. int itemsCount = input["itemsCount"].Integer();
  190. auto result = std::make_shared<CHeroList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, LOCPLINT->localState->getWanderingHeroes().size());
  191. if(!input["scrollUp"].isNull())
  192. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  193. if(!input["scrollDown"].isNull())
  194. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  195. subwidgetSizes.pop_back();
  196. heroList = result;
  197. return result;
  198. }
  199. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapIcon(const JsonNode & input)
  200. {
  201. Rect area = readTargetArea(input["area"]);
  202. size_t index = input["index"].Integer();
  203. size_t perPlayer = input["perPlayer"].Integer();
  204. std::string image = input["image"].String();
  205. return std::make_shared<CAdventureMapIcon>(area.topLeft(), loadAnimation(image), index, perPlayer);
  206. }
  207. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapTownList(const JsonNode & input)
  208. {
  209. Rect area = readTargetArea(input["area"]);
  210. subwidgetSizes.push_back(area);
  211. Rect item = readTargetArea(input["item"]);
  212. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  213. int itemsCount = input["itemsCount"].Integer();
  214. auto result = std::make_shared<CTownList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, LOCPLINT->localState->getOwnedTowns().size());
  215. if(!input["scrollUp"].isNull())
  216. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  217. if(!input["scrollDown"].isNull())
  218. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  219. subwidgetSizes.pop_back();
  220. townList = result;
  221. return result;
  222. }
  223. std::shared_ptr<CIntObject> AdventureMapWidget::buildMinimap(const JsonNode & input)
  224. {
  225. Rect area = readTargetArea(input["area"]);
  226. minimap = std::make_shared<CMinimap>(area);
  227. return minimap;
  228. }
  229. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  230. {
  231. Rect area = readTargetArea(input["area"]);
  232. std::string image = input["image"].String();
  233. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  234. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  235. {
  236. const auto & node = input[GameConstants::RESOURCE_NAMES[i]];
  237. if(node.isNull())
  238. continue;
  239. result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
  240. }
  241. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  242. return result;
  243. }
  244. std::shared_ptr<CIntObject> AdventureMapWidget::buildStatusBar(const JsonNode & input)
  245. {
  246. Rect area = readTargetArea(input["area"]);
  247. std::string image = input["image"].String();
  248. auto background = std::make_shared<CFilledTexture>(image, area);
  249. return CGStatusBar::create(background);
  250. }
  251. std::shared_ptr<CIntObject> AdventureMapWidget::buildTexturePlayerColored(const JsonNode & input)
  252. {
  253. logGlobal->debug("Building widget CFilledTexture");
  254. auto image = input["image"].String();
  255. Rect area = readTargetArea(input["area"]);
  256. return std::make_shared<FilledTexturePlayerColored>(image, area);
  257. }
  258. std::shared_ptr<CHeroList> AdventureMapWidget::getHeroList()
  259. {
  260. return heroList;
  261. }
  262. std::shared_ptr<CTownList> AdventureMapWidget::getTownList()
  263. {
  264. return townList;
  265. }
  266. std::shared_ptr<CMinimap> AdventureMapWidget::getMinimap()
  267. {
  268. return minimap;
  269. }
  270. std::shared_ptr<MapView> AdventureMapWidget::getMapView()
  271. {
  272. return mapView;
  273. }
  274. std::shared_ptr<CInfoBar> AdventureMapWidget::getInfoBar()
  275. {
  276. return infoBar;
  277. }
  278. void AdventureMapWidget::setPlayer(const PlayerColor & player)
  279. {
  280. setPlayerChildren(this, player);
  281. }
  282. void AdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  283. {
  284. for(auto & entry : widget->children)
  285. {
  286. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  287. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  288. auto button = dynamic_cast<CButton *>(entry);
  289. auto resDataBar = dynamic_cast<CResDataBar *>(entry);
  290. auto texture = dynamic_cast<FilledTexturePlayerColored *>(entry);
  291. if(button)
  292. button->setPlayerColor(player);
  293. if(resDataBar)
  294. resDataBar->colorize(player);
  295. if(icon)
  296. icon->setPlayer(player);
  297. if(container)
  298. setPlayerChildren(container, player);
  299. if (texture)
  300. texture->playerColored(player);
  301. }
  302. for(const auto & entry : playerColorerImages)
  303. {
  304. if(images.count(entry))
  305. images[entry]->playerColored(player);
  306. }
  307. redraw();
  308. }
  309. CAdventureMapIcon::CAdventureMapIcon(const Point & position, std::shared_ptr<CAnimation> animation, size_t index, size_t iconsPerPlayer)
  310. : index(index)
  311. , iconsPerPlayer(iconsPerPlayer)
  312. {
  313. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  314. pos += position;
  315. image = std::make_shared<CAnimImage>(animation, index);
  316. }
  317. void CAdventureMapIcon::setPlayer(const PlayerColor & player)
  318. {
  319. image->setFrame(index + player.getNum() * iconsPerPlayer);
  320. }
  321. void CAdventureMapOverlayWidget::show(Canvas & to)
  322. {
  323. CIntObject::showAll(to);
  324. }
  325. void AdventureMapWidget::updateActiveStateChildden(CIntObject * widget)
  326. {
  327. for(auto & entry : widget->children)
  328. {
  329. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  330. if (container)
  331. {
  332. if (container->disableCondition == "heroAwake")
  333. container->setEnabled(!shortcuts->optionHeroSleeping());
  334. if (container->disableCondition == "heroSleeping")
  335. container->setEnabled(shortcuts->optionHeroSleeping());
  336. if (container->disableCondition == "mapLayerSurface")
  337. container->setEnabled(shortcuts->optionMapLevelSurface());
  338. if (container->disableCondition == "mapLayerUnderground")
  339. container->setEnabled(!shortcuts->optionMapLevelSurface());
  340. if (container->disableCondition == "mapViewMode")
  341. container->setEnabled(shortcuts->optionInWorldView());
  342. if (container->disableCondition == "worldViewMode")
  343. container->setEnabled(!shortcuts->optionInWorldView());
  344. updateActiveStateChildden(container);
  345. }
  346. }
  347. }
  348. void AdventureMapWidget::updateActiveState()
  349. {
  350. updateActiveStateChildden(this);
  351. for (auto entry: shortcuts->getShortcuts())
  352. setShortcutBlocked(entry.shortcut, !entry.isEnabled);
  353. }