AdventureMapWidget.cpp 13 KB

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