AdventureMapWidget.cpp 14 KB

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