AdventureMapWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 "../GameEngine.h"
  19. #include "../GameInstance.h"
  20. #include "../gui/Shortcut.h"
  21. #include "../mapView/MapView.h"
  22. #include "../render/IImage.h"
  23. #include "../render/IRenderHandler.h"
  24. #include "../widgets/Buttons.h"
  25. #include "../widgets/Images.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../CPlayerInterface.h"
  28. #include "../PlayerLocalState.h"
  29. #include "../../lib/callback/CCallback.h"
  30. #include "../../lib/constants/StringConstants.h"
  31. #include "../../lib/mapping/CMapHeader.h"
  32. #include "../../lib/filesystem/ResourcePath.h"
  33. AdventureMapWidget::AdventureMapWidget( std::shared_ptr<AdventureMapShortcuts> shortcuts )
  34. : shortcuts(shortcuts)
  35. , mapLevel(0)
  36. {
  37. pos.x = pos.y = 0;
  38. pos.w = ENGINE->screenDimensions().x;
  39. pos.h = ENGINE->screenDimensions().y;
  40. REGISTER_BUILDER("adventureInfobar", &AdventureMapWidget::buildInfobox );
  41. REGISTER_BUILDER("adventureMapImage", &AdventureMapWidget::buildMapImage );
  42. REGISTER_BUILDER("adventureMapButton", &AdventureMapWidget::buildMapButton );
  43. REGISTER_BUILDER("adventureMapContainer", &AdventureMapWidget::buildMapContainer );
  44. REGISTER_BUILDER("adventureMapGameArea", &AdventureMapWidget::buildMapGameArea );
  45. REGISTER_BUILDER("adventureMapHeroList", &AdventureMapWidget::buildMapHeroList );
  46. REGISTER_BUILDER("adventureMapIcon", &AdventureMapWidget::buildMapIcon );
  47. REGISTER_BUILDER("adventureMapTownList", &AdventureMapWidget::buildMapTownList );
  48. REGISTER_BUILDER("adventureMinimap", &AdventureMapWidget::buildMinimap );
  49. REGISTER_BUILDER("adventureResourceDateBar", &AdventureMapWidget::buildResourceDateBar );
  50. REGISTER_BUILDER("adventureStatusBar", &AdventureMapWidget::buildStatusBar );
  51. REGISTER_BUILDER("adventurePlayerTexture", &AdventureMapWidget::buildTexturePlayerColored);
  52. for (const auto & entry : shortcuts->getShortcuts())
  53. addShortcut(entry.shortcut, entry.callback);
  54. const JsonNode config(JsonPath::builtin("config/widgets/adventureMap.json"));
  55. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  56. playerColoredImages.push_back(ImagePath::fromJson(entry));
  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<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)
  104. {
  105. Rect area = readTargetArea(input["area"]);
  106. infoBar = std::make_shared<CInfoBar>(area);
  107. return infoBar;
  108. }
  109. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapImage(const JsonNode & input)
  110. {
  111. Rect targetArea = readTargetArea(input["area"]);
  112. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  113. ImagePath path = ImagePath::fromJson(input["image"]);
  114. if (vstd::contains(playerColoredImages, path))
  115. return std::make_shared<FilledTexturePlayerIndexed>(path, targetArea, sourceArea);
  116. else
  117. return std::make_shared<CFilledTexture>(path, targetArea, sourceArea);
  118. }
  119. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapButton(const JsonNode & input)
  120. {
  121. auto position = readTargetArea(input["area"]);
  122. auto image = AnimationPath::fromJson(input["image"]);
  123. auto help = readHintText(input["help"]);
  124. bool playerColored = input["playerColored"].Bool();
  125. auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
  126. loadButtonBorderColor(button, input["borderColor"]);
  127. loadButtonHotkey(button, input["hotkey"]);
  128. return button;
  129. }
  130. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapContainer(const JsonNode & input)
  131. {
  132. auto position = readTargetArea(input["area"]);
  133. std::shared_ptr<CAdventureMapContainerWidget> result;
  134. if (!input["exists"].isNull())
  135. {
  136. if (!input["exists"]["heightMin"].isNull() && input["exists"]["heightMin"].Integer() >= pos.h)
  137. return nullptr;
  138. if (!input["exists"]["heightMax"].isNull() && input["exists"]["heightMax"].Integer() < pos.h)
  139. return nullptr;
  140. if (!input["exists"]["widthMin"].isNull() && input["exists"]["widthMin"].Integer() >= pos.w)
  141. return nullptr;
  142. if (!input["exists"]["widthMax"].isNull() && input["exists"]["widthMax"].Integer() < pos.w)
  143. return nullptr;
  144. }
  145. if (input["overlay"].Bool())
  146. result = std::make_shared<CAdventureMapOverlayWidget>();
  147. else
  148. result = std::make_shared<CAdventureMapContainerWidget>();
  149. result->disableCondition = input["hideWhen"].String();
  150. result->moveBy(position.topLeft());
  151. subwidgetSizes.push_back(position);
  152. for(const auto & entry : input["items"].Vector())
  153. {
  154. auto widget = buildWidget(entry);
  155. addWidget(entry["name"].String(), widget);
  156. result->ownedChildren.push_back(widget);
  157. // FIXME: remove cast and replace it with better check
  158. if (std::dynamic_pointer_cast<CLabel>(widget) || std::dynamic_pointer_cast<CLabelGroup>(widget))
  159. result->addChild(widget.get(), true);
  160. else
  161. result->addChild(widget.get(), false);
  162. }
  163. subwidgetSizes.pop_back();
  164. return result;
  165. }
  166. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapGameArea(const JsonNode & input)
  167. {
  168. Rect area = readTargetArea(input["area"]);
  169. mapView = std::make_shared<MapView>(area.topLeft(), area.dimensions());
  170. return mapView;
  171. }
  172. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapHeroList(const JsonNode & input)
  173. {
  174. Rect area = readTargetArea(input["area"]);
  175. subwidgetSizes.push_back(area);
  176. Rect item = readTargetArea(input["item"]);
  177. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  178. int itemsCount = input["itemsCount"].Integer();
  179. auto result = std::make_shared<CHeroList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, GAME->interface()->localState->getWanderingHeroes().size());
  180. if(!input["scrollUp"].isNull())
  181. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  182. if(!input["scrollDown"].isNull())
  183. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  184. subwidgetSizes.pop_back();
  185. heroList = result;
  186. return result;
  187. }
  188. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapIcon(const JsonNode & input)
  189. {
  190. Rect area = readTargetArea(input["area"]);
  191. size_t index = input["index"].Integer();
  192. size_t perPlayer = input["perPlayer"].Integer();
  193. return std::make_shared<CAdventureMapIcon>(area.topLeft(), AnimationPath::fromJson(input["image"]), index, perPlayer);
  194. }
  195. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapTownList(const JsonNode & input)
  196. {
  197. Rect area = readTargetArea(input["area"]);
  198. subwidgetSizes.push_back(area);
  199. Rect item = readTargetArea(input["item"]);
  200. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  201. int itemsCount = input["itemsCount"].Integer();
  202. auto result = std::make_shared<CTownList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, GAME->interface()->localState->getOwnedTowns().size());
  203. if(!input["scrollUp"].isNull())
  204. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  205. if(!input["scrollDown"].isNull())
  206. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  207. subwidgetSizes.pop_back();
  208. townList = result;
  209. return result;
  210. }
  211. std::shared_ptr<CIntObject> AdventureMapWidget::buildMinimap(const JsonNode & input)
  212. {
  213. Rect area = readTargetArea(input["area"]);
  214. minimap = std::make_shared<CMinimap>(area);
  215. return minimap;
  216. }
  217. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  218. {
  219. Rect area = readTargetArea(input["area"]);
  220. auto image = ImagePath::fromJson(input["image"]);
  221. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  222. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  223. {
  224. const auto & node = input[GameConstants::RESOURCE_NAMES[i]];
  225. if(node.isNull())
  226. continue;
  227. result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
  228. }
  229. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  230. return result;
  231. }
  232. std::shared_ptr<CIntObject> AdventureMapWidget::buildStatusBar(const JsonNode & input)
  233. {
  234. Rect area = readTargetArea(input["area"]);
  235. auto image = ImagePath::fromJson(input["image"]);
  236. auto background = std::make_shared<CFilledTexture>(image, area);
  237. return CGStatusBar::create(background);
  238. }
  239. std::shared_ptr<CIntObject> AdventureMapWidget::buildTexturePlayerColored(const JsonNode & input)
  240. {
  241. logGlobal->debug("Building widget CFilledTexture");
  242. Rect area = readTargetArea(input["area"]);
  243. return std::make_shared<FilledTexturePlayerColored>(area);
  244. }
  245. std::shared_ptr<CHeroList> AdventureMapWidget::getHeroList()
  246. {
  247. return heroList;
  248. }
  249. std::shared_ptr<CTownList> AdventureMapWidget::getTownList()
  250. {
  251. return townList;
  252. }
  253. std::shared_ptr<CMinimap> AdventureMapWidget::getMinimap()
  254. {
  255. return minimap;
  256. }
  257. std::shared_ptr<MapView> AdventureMapWidget::getMapView()
  258. {
  259. return mapView;
  260. }
  261. std::shared_ptr<CInfoBar> AdventureMapWidget::getInfoBar()
  262. {
  263. return infoBar;
  264. }
  265. void AdventureMapWidget::setPlayerColor(const PlayerColor & player)
  266. {
  267. setPlayerChildren(this, player);
  268. }
  269. void AdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  270. {
  271. for(auto & entry : widget->children)
  272. {
  273. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  274. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  275. auto button = dynamic_cast<CButton *>(entry);
  276. auto resDataBar = dynamic_cast<CResDataBar *>(entry);
  277. auto textureColored = dynamic_cast<FilledTexturePlayerColored *>(entry);
  278. auto textureIndexed = dynamic_cast<FilledTexturePlayerIndexed *>(entry);
  279. if(button)
  280. button->setPlayerColor(player);
  281. if(resDataBar)
  282. resDataBar->setPlayerColor(player);
  283. if(icon)
  284. icon->setPlayerColor(player);
  285. if(container)
  286. setPlayerChildren(container, player);
  287. if (textureColored)
  288. textureColored->setPlayerColor(player);
  289. if (textureIndexed)
  290. textureIndexed->setPlayerColor(player);
  291. }
  292. redraw();
  293. }
  294. CAdventureMapIcon::CAdventureMapIcon(const Point & position, const AnimationPath & animation, size_t index, size_t iconsPerPlayer)
  295. : index(index)
  296. , iconsPerPlayer(iconsPerPlayer)
  297. {
  298. OBJECT_CONSTRUCTION;
  299. pos += position;
  300. image = std::make_shared<CAnimImage>(animation, index);
  301. }
  302. void CAdventureMapIcon::setPlayerColor(const PlayerColor & player)
  303. {
  304. image->setFrame(index + player.getNum() * iconsPerPlayer);
  305. }
  306. void CAdventureMapOverlayWidget::show(Canvas & to)
  307. {
  308. CIntObject::showAll(to);
  309. }
  310. void AdventureMapWidget::updateActiveStateChildren(CIntObject * widget)
  311. {
  312. for(auto & entry : widget->children)
  313. {
  314. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  315. int mapLevels = GAME->interface()->cb->getMapHeader()->mapLevels;
  316. if (container)
  317. {
  318. if (container->disableCondition == "heroAwake")
  319. container->setEnabled(!shortcuts->optionHeroSleeping());
  320. if (container->disableCondition == "heroSleeping")
  321. container->setEnabled(shortcuts->optionHeroSleeping());
  322. if (container->disableCondition == "heroGround")
  323. container->setEnabled(shortcuts->optionHeroGround());
  324. if (container->disableCondition == "heroBoat")
  325. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::SAIL));
  326. if (container->disableCondition == "heroAirship")
  327. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  328. if (container->disableCondition == "mapLayerSurface")
  329. container->setEnabled(shortcuts->optionMapLevel() == 0);
  330. if (container->disableCondition == "mapLayerUnderground")
  331. container->setEnabled(shortcuts->optionMapLevel() == mapLevels - 1);
  332. if (container->disableCondition == "mapLayerOther")
  333. container->setEnabled(shortcuts->optionMapLevel() > 0 && shortcuts->optionMapLevel() < mapLevels - 1);
  334. if (container->disableCondition == "mapViewMode")
  335. container->setEnabled(shortcuts->optionInWorldView());
  336. if (container->disableCondition == "worldViewMode")
  337. container->setEnabled(!shortcuts->optionInWorldView());
  338. updateActiveStateChildren(container);
  339. }
  340. }
  341. }
  342. void AdventureMapWidget::updateActiveState()
  343. {
  344. updateActiveStateChildren(this);
  345. for (auto entry: shortcuts->getShortcuts())
  346. setShortcutBlocked(entry.shortcut, !entry.isEnabled);
  347. }