AdventureMapWidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/AssetGenerator.h"
  23. #include "../render/IImage.h"
  24. #include "../render/IRenderHandler.h"
  25. #include "../widgets/Buttons.h"
  26. #include "../widgets/Images.h"
  27. #include "../widgets/TextControls.h"
  28. #include "../CPlayerInterface.h"
  29. #include "../PlayerLocalState.h"
  30. #include "../../lib/GameLibrary.h"
  31. #include "../../lib/callback/CCallback.h"
  32. #include "../../lib/constants/StringConstants.h"
  33. #include "../../lib/mapping/CMapHeader.h"
  34. #include "../../lib/filesystem/ResourcePath.h"
  35. #include "../../lib/entities/ResourceTypeHandler.h"
  36. AdventureMapWidget::AdventureMapWidget( std::shared_ptr<AdventureMapShortcuts> shortcuts )
  37. : shortcuts(shortcuts)
  38. , mapLevel(0)
  39. {
  40. pos.x = pos.y = 0;
  41. pos.w = ENGINE->screenDimensions().x;
  42. pos.h = ENGINE->screenDimensions().y;
  43. REGISTER_BUILDER("adventureInfobar", &AdventureMapWidget::buildInfobox );
  44. REGISTER_BUILDER("adventureMapImage", &AdventureMapWidget::buildMapImage );
  45. REGISTER_BUILDER("adventureMapButton", &AdventureMapWidget::buildMapButton );
  46. REGISTER_BUILDER("adventureMapContainer", &AdventureMapWidget::buildMapContainer );
  47. REGISTER_BUILDER("adventureMapGameArea", &AdventureMapWidget::buildMapGameArea );
  48. REGISTER_BUILDER("adventureMapHeroList", &AdventureMapWidget::buildMapHeroList );
  49. REGISTER_BUILDER("adventureMapIcon", &AdventureMapWidget::buildMapIcon );
  50. REGISTER_BUILDER("adventureMapTownList", &AdventureMapWidget::buildMapTownList );
  51. REGISTER_BUILDER("adventureMinimap", &AdventureMapWidget::buildMinimap );
  52. REGISTER_BUILDER("adventureResourceDateBar", &AdventureMapWidget::buildResourceDateBar );
  53. REGISTER_BUILDER("adventureStatusBar", &AdventureMapWidget::buildStatusBar );
  54. REGISTER_BUILDER("adventurePlayerTexture", &AdventureMapWidget::buildTexturePlayerColored);
  55. for (const auto & entry : shortcuts->getShortcuts())
  56. addShortcut(entry.shortcut, entry.callback);
  57. const JsonNode config(JsonPath::builtin("config/widgets/adventureMap.json"));
  58. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  59. playerColoredImages.push_back(ImagePath::fromJson(entry));
  60. build(config);
  61. addUsedEvents(KEYBOARD);
  62. }
  63. void AdventureMapWidget::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  64. {
  65. if(mapLevel == newMapLevel)
  66. return;
  67. mapLevel = newMapLevel;
  68. updateActiveState();
  69. }
  70. Rect AdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
  71. {
  72. const auto & input = source.isNull() ? sourceCommon : source;
  73. return readArea(input, Rect(Point(0, 0), Point(800, 600)));
  74. }
  75. Rect AdventureMapWidget::readTargetArea(const JsonNode & source)
  76. {
  77. if(subwidgetSizes.empty())
  78. return readArea(source, pos);
  79. return readArea(source, subwidgetSizes.back());
  80. }
  81. Rect AdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
  82. {
  83. const auto & object = source.Struct();
  84. if(object.count("left") + object.count("width") + object.count("right") != 2)
  85. logGlobal->error("Invalid area definition in widget! Unable to load width!");
  86. if(object.count("top") + object.count("height") + object.count("bottom") != 2)
  87. logGlobal->error("Invalid area definition in widget! Unable to load height!");
  88. int left = source["left"].Integer();
  89. int width = source["width"].Integer();
  90. int right = source["right"].Integer();
  91. int top = source["top"].Integer();
  92. int height = source["height"].Integer();
  93. int bottom = source["bottom"].Integer();
  94. Point topLeft(left, top);
  95. Point dimensions(width, height);
  96. if(source["left"].isNull())
  97. topLeft.x = boundingBox.w - right - width;
  98. if(source["width"].isNull())
  99. dimensions.x = boundingBox.w - right - left;
  100. if(source["top"].isNull())
  101. topLeft.y = boundingBox.h - bottom - height;
  102. if(source["height"].isNull())
  103. dimensions.y = boundingBox.h - bottom - top;
  104. return Rect(topLeft + boundingBox.topLeft(), dimensions);
  105. }
  106. std::shared_ptr<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)
  107. {
  108. Rect area = readTargetArea(input["area"]);
  109. infoBar = std::make_shared<CInfoBar>(area);
  110. return infoBar;
  111. }
  112. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapImage(const JsonNode & input)
  113. {
  114. Rect targetArea = readTargetArea(input["area"]);
  115. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  116. ImagePath path = ImagePath::fromJson(input["image"]);
  117. if (vstd::contains(playerColoredImages, path))
  118. return std::make_shared<FilledTexturePlayerIndexed>(path, targetArea, sourceArea);
  119. else
  120. return std::make_shared<CFilledTexture>(path, targetArea, sourceArea);
  121. }
  122. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapButton(const JsonNode & input)
  123. {
  124. auto position = readTargetArea(input["area"]);
  125. auto image = AnimationPath::fromJson(input["image"]);
  126. auto help = readHintText(input["help"]);
  127. bool playerColored = input["playerColored"].Bool();
  128. if(!input["generateFromBaseImage"].isNull())
  129. {
  130. bool small = input["generateSmall"].Bool();
  131. auto assetGenerator = ENGINE->renderHandler().getAssetGenerator();
  132. auto layout = assetGenerator->createAdventureMapButton(ImagePath::fromJson(input["generateFromBaseImage"]), small);
  133. assetGenerator->addAnimationFile(AnimationPath::builtin("SPRITES/" + input["image"].String()), layout);
  134. ENGINE->renderHandler().updateGeneratedAssets();
  135. }
  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, GAME->interface()->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. return std::make_shared<CAdventureMapIcon>(area.topLeft(), AnimationPath::fromJson(input["image"]), index, perPlayer);
  205. }
  206. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapTownList(const JsonNode & input)
  207. {
  208. Rect area = readTargetArea(input["area"]);
  209. subwidgetSizes.push_back(area);
  210. Rect item = readTargetArea(input["item"]);
  211. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  212. int itemsCount = input["itemsCount"].Integer();
  213. auto result = std::make_shared<CTownList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, GAME->interface()->localState->getOwnedTowns().size());
  214. if(!input["scrollUp"].isNull())
  215. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  216. if(!input["scrollDown"].isNull())
  217. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  218. subwidgetSizes.pop_back();
  219. townList = result;
  220. return result;
  221. }
  222. std::shared_ptr<CIntObject> AdventureMapWidget::buildMinimap(const JsonNode & input)
  223. {
  224. Rect area = readTargetArea(input["area"]);
  225. minimap = std::make_shared<CMinimap>(area);
  226. return minimap;
  227. }
  228. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  229. {
  230. Rect area = readTargetArea(input["area"]);
  231. auto image = ImagePath::fromJson(input["image"]);
  232. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  233. for (auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  234. {
  235. const auto & node = input[i.toResource()->getJsonKey()];
  236. if(node.isNull())
  237. continue;
  238. result->setResourcePosition(i, Point(node["x"].Integer(), node["y"].Integer()));
  239. }
  240. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  241. return result;
  242. }
  243. std::shared_ptr<CIntObject> AdventureMapWidget::buildStatusBar(const JsonNode & input)
  244. {
  245. Rect area = readTargetArea(input["area"]);
  246. auto image = ImagePath::fromJson(input["image"]);
  247. auto background = std::make_shared<CFilledTexture>(image, area);
  248. return CGStatusBar::create(background);
  249. }
  250. std::shared_ptr<CIntObject> AdventureMapWidget::buildTexturePlayerColored(const JsonNode & input)
  251. {
  252. logGlobal->debug("Building widget CFilledTexture");
  253. Rect area = readTargetArea(input["area"]);
  254. return std::make_shared<FilledTexturePlayerColored>(area);
  255. }
  256. std::shared_ptr<CHeroList> AdventureMapWidget::getHeroList()
  257. {
  258. return heroList;
  259. }
  260. std::shared_ptr<CTownList> AdventureMapWidget::getTownList()
  261. {
  262. return townList;
  263. }
  264. std::shared_ptr<CMinimap> AdventureMapWidget::getMinimap()
  265. {
  266. return minimap;
  267. }
  268. std::shared_ptr<MapView> AdventureMapWidget::getMapView()
  269. {
  270. return mapView;
  271. }
  272. std::shared_ptr<CInfoBar> AdventureMapWidget::getInfoBar()
  273. {
  274. return infoBar;
  275. }
  276. void AdventureMapWidget::setPlayerColor(const PlayerColor & player)
  277. {
  278. setPlayerChildren(this, player);
  279. }
  280. void AdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  281. {
  282. for(auto & entry : widget->children)
  283. {
  284. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  285. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  286. auto button = dynamic_cast<CButton *>(entry);
  287. auto resDataBar = dynamic_cast<CResDataBar *>(entry);
  288. auto textureColored = dynamic_cast<FilledTexturePlayerColored *>(entry);
  289. auto textureIndexed = dynamic_cast<FilledTexturePlayerIndexed *>(entry);
  290. if(button)
  291. button->setPlayerColor(player);
  292. if(resDataBar)
  293. resDataBar->setPlayerColor(player);
  294. if(icon)
  295. icon->setPlayerColor(player);
  296. if(container)
  297. setPlayerChildren(container, player);
  298. if (textureColored)
  299. textureColored->setPlayerColor(player);
  300. if (textureIndexed)
  301. textureIndexed->setPlayerColor(player);
  302. }
  303. redraw();
  304. }
  305. CAdventureMapIcon::CAdventureMapIcon(const Point & position, const AnimationPath & animation, size_t index, size_t iconsPerPlayer)
  306. : index(index)
  307. , iconsPerPlayer(iconsPerPlayer)
  308. {
  309. OBJECT_CONSTRUCTION;
  310. pos += position;
  311. image = std::make_shared<CAnimImage>(animation, index);
  312. }
  313. void CAdventureMapIcon::setPlayerColor(const PlayerColor & player)
  314. {
  315. image->setFrame(index + player.getNum() * iconsPerPlayer);
  316. }
  317. void CAdventureMapOverlayWidget::show(Canvas & to)
  318. {
  319. CIntObject::showAll(to);
  320. }
  321. void AdventureMapWidget::updateActiveStateChildren(CIntObject * widget)
  322. {
  323. for(auto & entry : widget->children)
  324. {
  325. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  326. int mapLevels = GAME->interface()->cb->getMapHeader()->mapLevels;
  327. if (container)
  328. {
  329. if (container->disableCondition == "heroAwake")
  330. container->setEnabled(!shortcuts->optionHeroSleeping());
  331. if (container->disableCondition == "heroSleeping")
  332. container->setEnabled(shortcuts->optionHeroSleeping());
  333. if (container->disableCondition == "heroGround")
  334. container->setEnabled(!shortcuts->optionHeroBoat(EPathfindingLayer::SAIL) && !shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  335. if (container->disableCondition == "heroBoat")
  336. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::SAIL));
  337. if (container->disableCondition == "heroAirship")
  338. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  339. if (container->disableCondition == "mapLayerSurface")
  340. container->setEnabled(shortcuts->optionMapLevel() == 0);
  341. if (container->disableCondition == "mapLayerUnderground")
  342. container->setEnabled(shortcuts->optionMapLevel() == mapLevels - 1);
  343. if (container->disableCondition == "mapLayerOther")
  344. container->setEnabled(shortcuts->optionMapLevel() > 0 && shortcuts->optionMapLevel() < mapLevels - 1);
  345. if (container->disableCondition == "mapViewMode")
  346. container->setEnabled(shortcuts->optionInWorldView());
  347. if (container->disableCondition == "worldViewMode")
  348. container->setEnabled(!shortcuts->optionInWorldView());
  349. updateActiveStateChildren(container);
  350. }
  351. }
  352. }
  353. void AdventureMapWidget::updateActiveState()
  354. {
  355. updateActiveStateChildren(this);
  356. for (auto entry: shortcuts->getShortcuts())
  357. setShortcutBlocked(entry.shortcut, !entry.isEnabled);
  358. }