AdventureMapWidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. REGISTER_BUILDER("adventureResourceAdditional", &AdventureMapWidget::buildResourceAdditional );
  56. for (const auto & entry : shortcuts->getShortcuts())
  57. addShortcut(entry.shortcut, entry.callback);
  58. const JsonNode config(JsonPath::builtin("config/widgets/adventureMap.json"));
  59. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  60. playerColoredImages.push_back(ImagePath::fromJson(entry));
  61. build(config);
  62. addUsedEvents(KEYBOARD);
  63. }
  64. void AdventureMapWidget::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  65. {
  66. if(mapLevel == newMapLevel)
  67. return;
  68. mapLevel = newMapLevel;
  69. updateActiveState();
  70. }
  71. Rect AdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
  72. {
  73. const auto & input = source.isNull() ? sourceCommon : source;
  74. return readArea(input, Rect(Point(0, 0), Point(800, 600)));
  75. }
  76. Rect AdventureMapWidget::readTargetArea(const JsonNode & source)
  77. {
  78. if(subwidgetSizes.empty())
  79. return readArea(source, pos);
  80. return readArea(source, subwidgetSizes.back());
  81. }
  82. Rect AdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
  83. {
  84. const auto & object = source.Struct();
  85. if(object.count("left") + object.count("width") + object.count("right") != 2)
  86. logGlobal->error("Invalid area definition in widget! Unable to load width!");
  87. if(object.count("top") + object.count("height") + object.count("bottom") != 2)
  88. logGlobal->error("Invalid area definition in widget! Unable to load height!");
  89. int left = source["left"].Integer();
  90. int width = source["width"].Integer();
  91. int right = source["right"].Integer();
  92. int top = source["top"].Integer();
  93. int height = source["height"].Integer();
  94. int bottom = source["bottom"].Integer();
  95. Point topLeft(left, top);
  96. Point dimensions(width, height);
  97. if(source["left"].isNull())
  98. topLeft.x = boundingBox.w - right - width;
  99. if(source["width"].isNull())
  100. dimensions.x = boundingBox.w - right - left;
  101. if(source["top"].isNull())
  102. topLeft.y = boundingBox.h - bottom - height;
  103. if(source["height"].isNull())
  104. dimensions.y = boundingBox.h - bottom - top;
  105. return Rect(topLeft + boundingBox.topLeft(), dimensions);
  106. }
  107. std::shared_ptr<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)
  108. {
  109. Rect area = readTargetArea(input["area"]);
  110. infoBar = std::make_shared<CInfoBar>(area);
  111. return infoBar;
  112. }
  113. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapImage(const JsonNode & input)
  114. {
  115. Rect targetArea = readTargetArea(input["area"]);
  116. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  117. ImagePath path = ImagePath::fromJson(input["image"]);
  118. if (vstd::contains(playerColoredImages, path))
  119. return std::make_shared<FilledTexturePlayerIndexed>(path, targetArea, sourceArea);
  120. else
  121. return std::make_shared<CFilledTexture>(path, targetArea, sourceArea);
  122. }
  123. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapButton(const JsonNode & input)
  124. {
  125. auto position = readTargetArea(input["area"]);
  126. auto image = AnimationPath::fromJson(input["image"]);
  127. auto help = readHintText(input["help"]);
  128. bool playerColored = input["playerColored"].Bool();
  129. if(!input["generateFromBaseImage"].isNull())
  130. {
  131. bool small = input["generateSmall"].Bool();
  132. auto assetGenerator = ENGINE->renderHandler().getAssetGenerator();
  133. auto layout = assetGenerator->createAdventureMapButton(ImagePath::fromJson(input["generateFromBaseImage"]), small);
  134. assetGenerator->addAnimationFile(AnimationPath::builtin("SPRITES/" + input["image"].String()), layout);
  135. ENGINE->renderHandler().updateGeneratedAssets();
  136. }
  137. auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
  138. loadButtonBorderColor(button, input["borderColor"]);
  139. loadButtonHotkey(button, input["hotkey"]);
  140. return button;
  141. }
  142. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapContainer(const JsonNode & input)
  143. {
  144. auto position = readTargetArea(input["area"]);
  145. std::shared_ptr<CAdventureMapContainerWidget> result;
  146. if (!input["exists"].isNull())
  147. {
  148. if (!input["exists"]["heightMin"].isNull() && input["exists"]["heightMin"].Integer() >= pos.h)
  149. return nullptr;
  150. if (!input["exists"]["heightMax"].isNull() && input["exists"]["heightMax"].Integer() < pos.h)
  151. return nullptr;
  152. if (!input["exists"]["widthMin"].isNull() && input["exists"]["widthMin"].Integer() >= pos.w)
  153. return nullptr;
  154. if (!input["exists"]["widthMax"].isNull() && input["exists"]["widthMax"].Integer() < pos.w)
  155. return nullptr;
  156. }
  157. if (input["overlay"].Bool())
  158. result = std::make_shared<CAdventureMapOverlayWidget>();
  159. else
  160. result = std::make_shared<CAdventureMapContainerWidget>();
  161. result->disableCondition = input["hideWhen"].String();
  162. result->moveBy(position.topLeft());
  163. subwidgetSizes.push_back(position);
  164. for(const auto & entry : input["items"].Vector())
  165. {
  166. auto widget = buildWidget(entry);
  167. addWidget(entry["name"].String(), widget);
  168. result->ownedChildren.push_back(widget);
  169. // FIXME: remove cast and replace it with better check
  170. if (std::dynamic_pointer_cast<CLabel>(widget) || std::dynamic_pointer_cast<CLabelGroup>(widget))
  171. result->addChild(widget.get(), true);
  172. else
  173. result->addChild(widget.get(), false);
  174. }
  175. subwidgetSizes.pop_back();
  176. return result;
  177. }
  178. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapGameArea(const JsonNode & input)
  179. {
  180. Rect area = readTargetArea(input["area"]);
  181. mapView = std::make_shared<MapView>(area.topLeft(), area.dimensions());
  182. return mapView;
  183. }
  184. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapHeroList(const JsonNode & input)
  185. {
  186. Rect area = readTargetArea(input["area"]);
  187. subwidgetSizes.push_back(area);
  188. Rect item = readTargetArea(input["item"]);
  189. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  190. int itemsCount = input["itemsCount"].Integer();
  191. auto result = std::make_shared<CHeroList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, GAME->interface()->localState->getWanderingHeroes().size());
  192. if(!input["scrollUp"].isNull())
  193. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  194. if(!input["scrollDown"].isNull())
  195. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  196. subwidgetSizes.pop_back();
  197. heroList = result;
  198. return result;
  199. }
  200. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapIcon(const JsonNode & input)
  201. {
  202. Rect area = readTargetArea(input["area"]);
  203. size_t index = input["index"].Integer();
  204. size_t perPlayer = input["perPlayer"].Integer();
  205. return std::make_shared<CAdventureMapIcon>(area.topLeft(), AnimationPath::fromJson(input["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, GAME->interface()->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. auto image = ImagePath::fromJson(input["image"]);
  233. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  234. for (const auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  235. {
  236. const auto & node = input[i.toResource()->getJsonKey()];
  237. if(node.isNull())
  238. continue;
  239. result->setResourcePosition(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. auto image = ImagePath::fromJson(input["image"]);
  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. Rect area = readTargetArea(input["area"]);
  255. return std::make_shared<FilledTexturePlayerColored>(area);
  256. }
  257. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceAdditional(const JsonNode & input)
  258. {
  259. OBJECT_CONSTRUCTION;
  260. logGlobal->debug("Building widget ResourceAdditional");
  261. Rect area = readTargetArea(input["area"]);
  262. auto obj = std::make_shared<CIntObject>();
  263. int remainingSpace = area.w;
  264. int resElementSize = 84;
  265. int fitOffset = 2;
  266. for(const auto & resource : LIBRARY->resourceTypeHandler->getAllObjects())
  267. {
  268. if(resource.getNum() < GameConstants::RESOURCE_QUANTITY)
  269. continue;
  270. if(remainingSpace < resElementSize)
  271. break;
  272. auto res = std::make_shared<CResDataBar>(ImagePath::builtin("ResBarElement"), area.topRight() + Point(remainingSpace - area.w - resElementSize + fitOffset, 0));
  273. res->setResourcePosition(resource, Point(35, 3));
  274. addWidget("", res);
  275. obj->addChild(res.get());
  276. auto resIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(resource), 0, res->pos.x + 4, res->pos.y + 2);
  277. addWidget("", resIcon);
  278. obj->addChild(resIcon.get());
  279. remainingSpace -= resElementSize;
  280. }
  281. area.w = remainingSpace + fitOffset;
  282. auto texture = std::make_shared<FilledTexturePlayerColored>(area);
  283. addWidget("", texture);
  284. obj->addChild(texture.get());
  285. return obj;
  286. }
  287. std::shared_ptr<CHeroList> AdventureMapWidget::getHeroList()
  288. {
  289. return heroList;
  290. }
  291. std::shared_ptr<CTownList> AdventureMapWidget::getTownList()
  292. {
  293. return townList;
  294. }
  295. std::shared_ptr<CMinimap> AdventureMapWidget::getMinimap()
  296. {
  297. return minimap;
  298. }
  299. std::shared_ptr<MapView> AdventureMapWidget::getMapView()
  300. {
  301. return mapView;
  302. }
  303. std::shared_ptr<CInfoBar> AdventureMapWidget::getInfoBar()
  304. {
  305. return infoBar;
  306. }
  307. void AdventureMapWidget::setPlayerColor(const PlayerColor & player)
  308. {
  309. setPlayerChildren(this, player);
  310. }
  311. void AdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  312. {
  313. for(auto & entry : widget->children)
  314. {
  315. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  316. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  317. auto button = dynamic_cast<CButton *>(entry);
  318. auto resDataBar = dynamic_cast<CResDataBar *>(entry);
  319. auto textureColored = dynamic_cast<FilledTexturePlayerColored *>(entry);
  320. auto textureIndexed = dynamic_cast<FilledTexturePlayerIndexed *>(entry);
  321. if(button)
  322. button->setPlayerColor(player);
  323. if(resDataBar)
  324. resDataBar->setPlayerColor(player);
  325. if(icon)
  326. icon->setPlayerColor(player);
  327. if(container)
  328. setPlayerChildren(container, player);
  329. if(textureColored)
  330. textureColored->setPlayerColor(player);
  331. if(textureIndexed)
  332. textureIndexed->setPlayerColor(player);
  333. if(entry)
  334. setPlayerChildren(entry, player);
  335. }
  336. redraw();
  337. }
  338. CAdventureMapIcon::CAdventureMapIcon(const Point & position, const AnimationPath & animation, size_t index, size_t iconsPerPlayer)
  339. : index(index)
  340. , iconsPerPlayer(iconsPerPlayer)
  341. {
  342. OBJECT_CONSTRUCTION;
  343. pos += position;
  344. image = std::make_shared<CAnimImage>(animation, index);
  345. }
  346. void CAdventureMapIcon::setPlayerColor(const PlayerColor & player)
  347. {
  348. image->setFrame(index + player.getNum() * iconsPerPlayer);
  349. }
  350. void CAdventureMapOverlayWidget::show(Canvas & to)
  351. {
  352. CIntObject::showAll(to);
  353. }
  354. void AdventureMapWidget::updateActiveStateChildren(CIntObject * widget)
  355. {
  356. for(auto & entry : widget->children)
  357. {
  358. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  359. int mapLevels = GAME->interface()->cb->getMapHeader()->mapLevels;
  360. if (container)
  361. {
  362. if (container->disableCondition == "heroAwake")
  363. container->setEnabled(!shortcuts->optionHeroSleeping());
  364. if (container->disableCondition == "heroSleeping")
  365. container->setEnabled(shortcuts->optionHeroSleeping());
  366. if (container->disableCondition == "heroGround")
  367. container->setEnabled(!shortcuts->optionHeroBoat(EPathfindingLayer::SAIL) && !shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  368. if (container->disableCondition == "heroBoat")
  369. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::SAIL));
  370. if (container->disableCondition == "heroAirship")
  371. container->setEnabled(shortcuts->optionHeroBoat(EPathfindingLayer::AIR));
  372. if (container->disableCondition == "mapLayerSurface")
  373. container->setEnabled(shortcuts->optionMapLevel() == 0);
  374. if (container->disableCondition == "mapLayerUnderground")
  375. container->setEnabled(shortcuts->optionMapLevel() == mapLevels - 1);
  376. if (container->disableCondition == "mapLayerOther")
  377. container->setEnabled(shortcuts->optionMapLevel() > 0 && shortcuts->optionMapLevel() < mapLevels - 1);
  378. if (container->disableCondition == "mapViewMode")
  379. container->setEnabled(shortcuts->optionInWorldView());
  380. if (container->disableCondition == "worldViewMode")
  381. container->setEnabled(!shortcuts->optionInWorldView());
  382. updateActiveStateChildren(container);
  383. }
  384. }
  385. }
  386. void AdventureMapWidget::updateActiveState()
  387. {
  388. updateActiveStateChildren(this);
  389. for (auto entry: shortcuts->getShortcuts())
  390. setShortcutBlocked(entry.shortcut, !entry.isEnabled);
  391. }