AdventureMapWidget.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. #include "../../lib/MapLayerHandler.h"
  37. AdventureMapWidget::AdventureMapWidget( std::shared_ptr<AdventureMapShortcuts> shortcuts )
  38. : shortcuts(shortcuts)
  39. , mapLevel(0)
  40. {
  41. pos.x = pos.y = 0;
  42. pos.w = ENGINE->screenDimensions().x;
  43. pos.h = ENGINE->screenDimensions().y;
  44. REGISTER_BUILDER("adventureInfobar", &AdventureMapWidget::buildInfobox );
  45. REGISTER_BUILDER("adventureMapImage", &AdventureMapWidget::buildMapImage );
  46. REGISTER_BUILDER("adventureMapButton", &AdventureMapWidget::buildMapButton );
  47. REGISTER_BUILDER("adventureMapContainer", &AdventureMapWidget::buildMapContainer );
  48. REGISTER_BUILDER("adventureMapGameArea", &AdventureMapWidget::buildMapGameArea );
  49. REGISTER_BUILDER("adventureMapHeroList", &AdventureMapWidget::buildMapHeroList );
  50. REGISTER_BUILDER("adventureMapIcon", &AdventureMapWidget::buildMapIcon );
  51. REGISTER_BUILDER("adventureMapTownList", &AdventureMapWidget::buildMapTownList );
  52. REGISTER_BUILDER("adventureMinimap", &AdventureMapWidget::buildMinimap );
  53. REGISTER_BUILDER("adventureResourceDateBar", &AdventureMapWidget::buildResourceDateBar );
  54. REGISTER_BUILDER("adventureStatusBar", &AdventureMapWidget::buildStatusBar );
  55. REGISTER_BUILDER("adventurePlayerTexture", &AdventureMapWidget::buildTexturePlayerColored);
  56. REGISTER_BUILDER("adventureResourceAdditional", &AdventureMapWidget::buildResourceAdditional );
  57. for (const auto & entry : shortcuts->getShortcuts())
  58. addShortcut(entry.shortcut, entry.callback);
  59. const JsonNode config(JsonPath::builtin(pos.w < pos.h && settings["video"]["allowPortrait"].Bool() ? "config/widgets/adventureMapPortrait.json" : "config/widgets/adventureMap.json"));
  60. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  61. playerColoredImages.push_back(ImagePath::fromJson(entry));
  62. build(config);
  63. addUsedEvents(KEYBOARD);
  64. updateMapLayerButtonsHelp();
  65. }
  66. void AdventureMapWidget::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  67. {
  68. if(mapLevel == newMapLevel)
  69. return;
  70. mapLevel = newMapLevel;
  71. updateActiveState();
  72. }
  73. Rect AdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
  74. {
  75. const auto & input = source.isNull() ? sourceCommon : source;
  76. return readArea(input, Rect(Point(0, 0), Point(800, 600)));
  77. }
  78. Rect AdventureMapWidget::readTargetArea(const JsonNode & source)
  79. {
  80. if(subwidgetSizes.empty())
  81. return readArea(source, pos);
  82. return readArea(source, subwidgetSizes.back());
  83. }
  84. Rect AdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
  85. {
  86. const auto & object = source.Struct();
  87. if(object.count("left") + object.count("width") + object.count("right") != 2)
  88. logGlobal->error("Invalid area definition in widget! Unable to load width!");
  89. if(object.count("top") + object.count("height") + object.count("bottom") != 2)
  90. logGlobal->error("Invalid area definition in widget! Unable to load height!");
  91. int left = source["left"].Integer();
  92. int width = source["width"].Integer();
  93. int right = source["right"].Integer();
  94. int top = source["top"].Integer();
  95. int height = source["height"].Integer();
  96. int bottom = source["bottom"].Integer();
  97. Point topLeft(left, top);
  98. Point dimensions(width, height);
  99. if(source["left"].isNull())
  100. topLeft.x = boundingBox.w - right - width;
  101. if(source["width"].isNull())
  102. dimensions.x = boundingBox.w - right - left;
  103. if(source["top"].isNull())
  104. topLeft.y = boundingBox.h - bottom - height;
  105. if(source["height"].isNull())
  106. dimensions.y = boundingBox.h - bottom - top;
  107. return Rect(topLeft + boundingBox.topLeft(), dimensions);
  108. }
  109. std::shared_ptr<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)
  110. {
  111. Rect area = readTargetArea(input["area"]);
  112. infoBar = std::make_shared<CInfoBar>(area);
  113. return infoBar;
  114. }
  115. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapImage(const JsonNode & input)
  116. {
  117. Rect targetArea = readTargetArea(input["area"]);
  118. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  119. ImagePath path = ImagePath::fromJson(input["image"]);
  120. if (vstd::contains(playerColoredImages, path))
  121. return std::make_shared<FilledTexturePlayerIndexed>(path, targetArea, sourceArea);
  122. else
  123. return std::make_shared<CFilledTexture>(path, targetArea, sourceArea);
  124. }
  125. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapButton(const JsonNode & input)
  126. {
  127. auto position = readTargetArea(input["area"]);
  128. auto image = AnimationPath::fromJson(input["image"]);
  129. auto help = readHintText(input["help"]);
  130. bool playerColored = input["playerColored"].Bool();
  131. if(!input["generateFromBaseImage"].isNull())
  132. {
  133. bool small = input["generateSmall"].Bool();
  134. auto assetGenerator = ENGINE->renderHandler().getAssetGenerator();
  135. auto layout = assetGenerator->createAdventureMapButton(ImagePath::fromJson(input["generateFromBaseImage"]), small);
  136. assetGenerator->addAnimationFile(AnimationPath::builtin("SPRITES/" + input["image"].String()), layout);
  137. ENGINE->renderHandler().updateGeneratedAssets();
  138. }
  139. auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
  140. loadButtonBorderColor(button, input["borderColor"]);
  141. loadButtonHotkey(button, input["hotkey"]);
  142. return button;
  143. }
  144. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapContainer(const JsonNode & input)
  145. {
  146. auto position = readTargetArea(input["area"]);
  147. std::shared_ptr<CAdventureMapContainerWidget> result;
  148. if (!input["exists"].isNull())
  149. {
  150. if (!input["exists"]["heightMin"].isNull() && input["exists"]["heightMin"].Integer() >= pos.h)
  151. return nullptr;
  152. if (!input["exists"]["heightMax"].isNull() && input["exists"]["heightMax"].Integer() < pos.h)
  153. return nullptr;
  154. if (!input["exists"]["widthMin"].isNull() && input["exists"]["widthMin"].Integer() >= pos.w)
  155. return nullptr;
  156. if (!input["exists"]["widthMax"].isNull() && input["exists"]["widthMax"].Integer() < pos.w)
  157. return nullptr;
  158. }
  159. if (input["overlay"].Bool())
  160. result = std::make_shared<CAdventureMapOverlayWidget>();
  161. else
  162. result = std::make_shared<CAdventureMapContainerWidget>();
  163. result->disableCondition = input["hideWhen"].String();
  164. result->moveBy(position.topLeft());
  165. subwidgetSizes.push_back(position);
  166. for(const auto & entry : input["items"].Vector())
  167. {
  168. auto widget = buildWidget(entry);
  169. addWidget(entry["name"].String(), widget);
  170. result->ownedChildren.push_back(widget);
  171. // FIXME: remove cast and replace it with better check
  172. if (std::dynamic_pointer_cast<CLabel>(widget) || std::dynamic_pointer_cast<CLabelGroup>(widget))
  173. result->addChild(widget.get(), true);
  174. else
  175. result->addChild(widget.get(), false);
  176. }
  177. subwidgetSizes.pop_back();
  178. return result;
  179. }
  180. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapGameArea(const JsonNode & input)
  181. {
  182. Rect area = readTargetArea(input["area"]);
  183. mapView = std::make_shared<MapView>(area.topLeft(), area.dimensions());
  184. return mapView;
  185. }
  186. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapHeroList(const JsonNode & input)
  187. {
  188. Rect area = readTargetArea(input["area"]);
  189. subwidgetSizes.push_back(area);
  190. Rect item = readTargetArea(input["item"]);
  191. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  192. int itemsCount = input["itemsCount"].Integer();
  193. auto result = std::make_shared<CHeroList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, GAME->interface()->localState->getWanderingHeroes().size());
  194. if(!input["scrollUp"].isNull())
  195. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  196. if(!input["scrollDown"].isNull())
  197. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  198. subwidgetSizes.pop_back();
  199. heroList = result;
  200. return result;
  201. }
  202. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapIcon(const JsonNode & input)
  203. {
  204. Rect area = readTargetArea(input["area"]);
  205. size_t index = input["index"].Integer();
  206. size_t perPlayer = input["perPlayer"].Integer();
  207. return std::make_shared<CAdventureMapIcon>(area.topLeft(), AnimationPath::fromJson(input["image"]), index, perPlayer);
  208. }
  209. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapTownList(const JsonNode & input)
  210. {
  211. Rect area = readTargetArea(input["area"]);
  212. subwidgetSizes.push_back(area);
  213. Rect item = readTargetArea(input["item"]);
  214. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  215. int itemsCount = input["itemsCount"].Integer();
  216. auto result = std::make_shared<CTownList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, GAME->interface()->localState->getOwnedTowns().size());
  217. if(!input["scrollUp"].isNull())
  218. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  219. if(!input["scrollDown"].isNull())
  220. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  221. subwidgetSizes.pop_back();
  222. townList = result;
  223. return result;
  224. }
  225. std::shared_ptr<CIntObject> AdventureMapWidget::buildMinimap(const JsonNode & input)
  226. {
  227. Rect area = readTargetArea(input["area"]);
  228. minimap = std::make_shared<CMinimap>(area);
  229. return minimap;
  230. }
  231. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  232. {
  233. Rect area = readTargetArea(input["area"]);
  234. auto image = ImagePath::fromJson(input["image"]);
  235. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  236. for (const auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  237. {
  238. const auto & node = input[i.toResource()->getJsonKey()];
  239. if(node.isNull())
  240. continue;
  241. result->setResourcePosition(i, Point(node["x"].Integer(), node["y"].Integer()));
  242. }
  243. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  244. return result;
  245. }
  246. std::shared_ptr<CIntObject> AdventureMapWidget::buildStatusBar(const JsonNode & input)
  247. {
  248. Rect area = readTargetArea(input["area"]);
  249. auto image = ImagePath::fromJson(input["image"]);
  250. auto background = std::make_shared<CFilledTexture>(image, area);
  251. return CGStatusBar::create(background);
  252. }
  253. std::shared_ptr<CIntObject> AdventureMapWidget::buildTexturePlayerColored(const JsonNode & input)
  254. {
  255. logGlobal->debug("Building widget CFilledTexture");
  256. Rect area = readTargetArea(input["area"]);
  257. return std::make_shared<FilledTexturePlayerColored>(area);
  258. }
  259. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceAdditional(const JsonNode & input)
  260. {
  261. OBJECT_CONSTRUCTION;
  262. logGlobal->debug("Building widget ResourceAdditional");
  263. Rect area = readTargetArea(input["area"]);
  264. auto obj = std::make_shared<CIntObject>();
  265. int remainingSpace = area.w;
  266. int resElementSize = 84;
  267. int fitOffset = 2;
  268. for(const auto & resource : LIBRARY->resourceTypeHandler->getAllObjects())
  269. {
  270. if(resource.getNum() < GameConstants::RESOURCE_QUANTITY)
  271. continue;
  272. if(remainingSpace < resElementSize)
  273. break;
  274. auto res = std::make_shared<CResDataBar>(ImagePath::builtin("ResBarElement"), area.topRight() + Point(remainingSpace - area.w - resElementSize + fitOffset, 0));
  275. res->setResourcePosition(resource, Point(35, 3));
  276. addWidget("", res);
  277. obj->addChild(res.get());
  278. auto resIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(resource), 0, res->pos.x + 4, res->pos.y + 2);
  279. addWidget("", resIcon);
  280. obj->addChild(resIcon.get());
  281. remainingSpace -= resElementSize;
  282. }
  283. area.w = remainingSpace + fitOffset;
  284. auto texture = std::make_shared<FilledTexturePlayerColored>(area);
  285. addWidget("", texture);
  286. obj->addChild(texture.get());
  287. return obj;
  288. }
  289. std::shared_ptr<CHeroList> AdventureMapWidget::getHeroList()
  290. {
  291. return heroList;
  292. }
  293. std::shared_ptr<CTownList> AdventureMapWidget::getTownList()
  294. {
  295. return townList;
  296. }
  297. std::shared_ptr<CMinimap> AdventureMapWidget::getMinimap()
  298. {
  299. return minimap;
  300. }
  301. std::shared_ptr<MapView> AdventureMapWidget::getMapView()
  302. {
  303. return mapView;
  304. }
  305. std::shared_ptr<CInfoBar> AdventureMapWidget::getInfoBar()
  306. {
  307. return infoBar;
  308. }
  309. void AdventureMapWidget::setPlayerColor(const PlayerColor & player)
  310. {
  311. setPlayerChildren(this, player);
  312. }
  313. void AdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  314. {
  315. for(auto & entry : widget->children)
  316. {
  317. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  318. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  319. auto button = dynamic_cast<CButton *>(entry);
  320. auto resDataBar = dynamic_cast<CResDataBar *>(entry);
  321. auto textureColored = dynamic_cast<FilledTexturePlayerColored *>(entry);
  322. auto textureIndexed = dynamic_cast<FilledTexturePlayerIndexed *>(entry);
  323. if(button)
  324. button->setPlayerColor(player);
  325. if(resDataBar)
  326. resDataBar->setPlayerColor(player);
  327. if(icon)
  328. icon->setPlayerColor(player);
  329. if(container)
  330. setPlayerChildren(container, player);
  331. if(textureColored)
  332. textureColored->setPlayerColor(player);
  333. if(textureIndexed)
  334. textureIndexed->setPlayerColor(player);
  335. if(entry)
  336. setPlayerChildren(entry, player);
  337. }
  338. redraw();
  339. }
  340. CAdventureMapIcon::CAdventureMapIcon(const Point & position, const AnimationPath & animation, size_t index, size_t iconsPerPlayer)
  341. : index(index)
  342. , iconsPerPlayer(iconsPerPlayer)
  343. {
  344. OBJECT_CONSTRUCTION;
  345. pos += position;
  346. image = std::make_shared<CAnimImage>(animation, index);
  347. }
  348. void CAdventureMapIcon::setPlayerColor(const PlayerColor & player)
  349. {
  350. image->setFrame(index + player.getNum() * iconsPerPlayer);
  351. }
  352. void CAdventureMapOverlayWidget::show(Canvas & to)
  353. {
  354. CIntObject::showAll(to);
  355. }
  356. void AdventureMapWidget::updateActiveStateChildren(CIntObject * widget)
  357. {
  358. for(auto & entry : widget->children)
  359. {
  360. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  361. int mapLevels = GAME->interface()->cb->getMapHeader()->levels();
  362. if (container)
  363. {
  364. if (container->disableCondition == "heroAwake")
  365. container->setEnabled(!shortcuts->optionHeroSleeping());
  366. if (container->disableCondition == "heroSleeping")
  367. container->setEnabled(shortcuts->optionHeroSleeping());
  368. if (container->disableCondition == "heroGround")
  369. container->setEnabled(!shortcuts->optionHeroBoat(EPathfindingLayer::SAIL) && !shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  370. if (container->disableCondition == "heroBoat")
  371. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::SAIL));
  372. if (container->disableCondition == "heroAirship")
  373. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  374. if (container->disableCondition == "mapLayerSurface")
  375. container->setEnabled(shortcuts->optionMapLevel() == 0);
  376. if (container->disableCondition == "mapLayerUnderground")
  377. container->setEnabled(shortcuts->optionMapLevel() == mapLevels - 1);
  378. if (container->disableCondition == "mapLayerOther")
  379. container->setEnabled(shortcuts->optionMapLevel() > 0 && shortcuts->optionMapLevel() < mapLevels - 1);
  380. if (container->disableCondition == "mapViewMode")
  381. container->setEnabled(shortcuts->optionInWorldView());
  382. if (container->disableCondition == "worldViewMode")
  383. container->setEnabled(!shortcuts->optionInWorldView());
  384. updateActiveStateChildren(container);
  385. }
  386. }
  387. }
  388. void AdventureMapWidget::updateActiveState()
  389. {
  390. updateActiveStateChildren(this);
  391. for (auto entry: shortcuts->getShortcuts())
  392. setShortcutBlocked(entry.shortcut, !entry.isEnabled);
  393. updateMapLayerButtonsHelp();
  394. }
  395. void AdventureMapWidget::updateMapLayerButtonsHelp()
  396. {
  397. if(!settings["general"]["enableUiEnhancements"].Bool())
  398. return;
  399. int mapLevelNext = (mapLevel + 1) % GAME->interface()->cb->getMapHeader()->levels();
  400. auto currentLevel = GAME->interface()->cb->getMapHeader()->mapLayers.at(mapLevel);
  401. auto nextLevel = GAME->interface()->cb->getMapHeader()->mapLayers.at(mapLevelNext);
  402. for (auto & widgetname : { "buttonUnderground", "buttonSurface", "buttonLayerOther" })
  403. {
  404. if (auto w = widget<CButton>(widgetname))
  405. {
  406. auto replaceText = [&](auto & text){
  407. text.replaceTextID(currentLevel.toEntity(LIBRARY)->getNameTextID());
  408. text.replaceNumber(mapLevel);
  409. text.replaceTextID(nextLevel.toEntity(LIBRARY)->getNameTextID());
  410. text.replaceNumber(mapLevelNext);
  411. };
  412. auto hoverText = MetaString::createFromTextID("vcmi.adventureMap.layer.hover");
  413. replaceText(hoverText);
  414. auto helpText = MetaString::createFromTextID("vcmi.adventureMap.layer.help");
  415. replaceText(helpText);
  416. w->setHelp({hoverText.toString(), helpText.toString()});
  417. // Refresh hover state using current cursor position so statusbar updates immediately
  418. Point cursor = ENGINE->getCursorPosition();
  419. bool inside = w->pos.isInside(cursor);
  420. w->hover(inside);
  421. }
  422. }
  423. }