AdventureMapWidget.cpp 14 KB

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