AdventureMapWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "../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 = GH.screenDimensions().x;
  37. pos.h = GH.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. playerColorerImages.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<IImage> AdventureMapWidget::loadImage(const JsonNode & name)
  102. {
  103. ImagePath resource = ImagePath::fromJson(name);
  104. if(images.count(resource) == 0)
  105. images[resource] = GH.renderHandler().loadImage(resource);
  106. return images[resource];
  107. }
  108. std::shared_ptr<CAnimation> AdventureMapWidget::loadAnimation(const JsonNode & name)
  109. {
  110. AnimationPath resource = AnimationPath::fromJson(name);
  111. if(animations.count(resource) == 0)
  112. animations[resource] = GH.renderHandler().loadAnimation(resource);
  113. return animations[resource];
  114. }
  115. std::shared_ptr<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)
  116. {
  117. Rect area = readTargetArea(input["area"]);
  118. infoBar = std::make_shared<CInfoBar>(area);
  119. return infoBar;
  120. }
  121. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapImage(const JsonNode & input)
  122. {
  123. Rect targetArea = readTargetArea(input["area"]);
  124. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  125. return std::make_shared<CFilledTexture>(loadImage(input["image"]), targetArea, sourceArea);
  126. }
  127. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapButton(const JsonNode & input)
  128. {
  129. auto position = readTargetArea(input["area"]);
  130. auto image = AnimationPath::fromJson(input["image"]);
  131. auto help = readHintText(input["help"]);
  132. bool playerColored = input["playerColored"].Bool();
  133. auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
  134. loadButtonBorderColor(button, input["borderColor"]);
  135. loadButtonHotkey(button, input["hotkey"]);
  136. return button;
  137. }
  138. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapContainer(const JsonNode & input)
  139. {
  140. auto position = readTargetArea(input["area"]);
  141. std::shared_ptr<CAdventureMapContainerWidget> result;
  142. if (!input["exists"].isNull())
  143. {
  144. if (!input["exists"]["heightMin"].isNull() && input["exists"]["heightMin"].Integer() >= pos.h)
  145. return nullptr;
  146. if (!input["exists"]["heightMax"].isNull() && input["exists"]["heightMax"].Integer() < pos.h)
  147. return nullptr;
  148. if (!input["exists"]["widthMin"].isNull() && input["exists"]["widthMin"].Integer() >= pos.w)
  149. return nullptr;
  150. if (!input["exists"]["widthMax"].isNull() && input["exists"]["widthMax"].Integer() < pos.w)
  151. return nullptr;
  152. }
  153. if (input["overlay"].Bool())
  154. result = std::make_shared<CAdventureMapOverlayWidget>();
  155. else
  156. result = std::make_shared<CAdventureMapContainerWidget>();
  157. result->disableCondition = input["hideWhen"].String();
  158. result->moveBy(position.topLeft());
  159. subwidgetSizes.push_back(position);
  160. for(const auto & entry : input["items"].Vector())
  161. {
  162. auto widget = buildWidget(entry);
  163. addWidget(entry["name"].String(), widget);
  164. result->ownedChildren.push_back(widget);
  165. // FIXME: remove cast and replace it with better check
  166. if (std::dynamic_pointer_cast<CLabel>(widget) || std::dynamic_pointer_cast<CLabelGroup>(widget))
  167. result->addChild(widget.get(), true);
  168. else
  169. result->addChild(widget.get(), false);
  170. }
  171. subwidgetSizes.pop_back();
  172. return result;
  173. }
  174. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapGameArea(const JsonNode & input)
  175. {
  176. Rect area = readTargetArea(input["area"]);
  177. mapView = std::make_shared<MapView>(area.topLeft(), area.dimensions());
  178. return mapView;
  179. }
  180. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapHeroList(const JsonNode & input)
  181. {
  182. Rect area = readTargetArea(input["area"]);
  183. subwidgetSizes.push_back(area);
  184. Rect item = readTargetArea(input["item"]);
  185. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  186. int itemsCount = input["itemsCount"].Integer();
  187. auto result = std::make_shared<CHeroList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, LOCPLINT->localState->getWanderingHeroes().size());
  188. if(!input["scrollUp"].isNull())
  189. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  190. if(!input["scrollDown"].isNull())
  191. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  192. subwidgetSizes.pop_back();
  193. heroList = result;
  194. return result;
  195. }
  196. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapIcon(const JsonNode & input)
  197. {
  198. Rect area = readTargetArea(input["area"]);
  199. size_t index = input["index"].Integer();
  200. size_t perPlayer = input["perPlayer"].Integer();
  201. return std::make_shared<CAdventureMapIcon>(area.topLeft(), loadAnimation(input["image"]), index, perPlayer);
  202. }
  203. std::shared_ptr<CIntObject> AdventureMapWidget::buildMapTownList(const JsonNode & input)
  204. {
  205. Rect area = readTargetArea(input["area"]);
  206. subwidgetSizes.push_back(area);
  207. Rect item = readTargetArea(input["item"]);
  208. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  209. int itemsCount = input["itemsCount"].Integer();
  210. auto result = std::make_shared<CTownList>(itemsCount, area, item.topLeft() - area.topLeft(), itemOffset, LOCPLINT->localState->getOwnedTowns().size());
  211. if(!input["scrollUp"].isNull())
  212. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  213. if(!input["scrollDown"].isNull())
  214. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  215. subwidgetSizes.pop_back();
  216. townList = result;
  217. return result;
  218. }
  219. std::shared_ptr<CIntObject> AdventureMapWidget::buildMinimap(const JsonNode & input)
  220. {
  221. Rect area = readTargetArea(input["area"]);
  222. minimap = std::make_shared<CMinimap>(area);
  223. return minimap;
  224. }
  225. std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  226. {
  227. Rect area = readTargetArea(input["area"]);
  228. auto image = ImagePath::fromJson(input["image"]);
  229. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  230. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  231. {
  232. const auto & node = input[GameConstants::RESOURCE_NAMES[i]];
  233. if(node.isNull())
  234. continue;
  235. result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
  236. }
  237. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  238. return result;
  239. }
  240. std::shared_ptr<CIntObject> AdventureMapWidget::buildStatusBar(const JsonNode & input)
  241. {
  242. Rect area = readTargetArea(input["area"]);
  243. auto image = ImagePath::fromJson(input["image"]);
  244. auto background = std::make_shared<CFilledTexture>(image, area);
  245. return CGStatusBar::create(background);
  246. }
  247. std::shared_ptr<CIntObject> AdventureMapWidget::buildTexturePlayerColored(const JsonNode & input)
  248. {
  249. logGlobal->debug("Building widget CFilledTexture");
  250. auto image = ImagePath::fromJson(input["image"]);
  251. Rect area = readTargetArea(input["area"]);
  252. return std::make_shared<FilledTexturePlayerColored>(image, area);
  253. }
  254. std::shared_ptr<CHeroList> AdventureMapWidget::getHeroList()
  255. {
  256. return heroList;
  257. }
  258. std::shared_ptr<CTownList> AdventureMapWidget::getTownList()
  259. {
  260. return townList;
  261. }
  262. std::shared_ptr<CMinimap> AdventureMapWidget::getMinimap()
  263. {
  264. return minimap;
  265. }
  266. std::shared_ptr<MapView> AdventureMapWidget::getMapView()
  267. {
  268. return mapView;
  269. }
  270. std::shared_ptr<CInfoBar> AdventureMapWidget::getInfoBar()
  271. {
  272. return infoBar;
  273. }
  274. void AdventureMapWidget::setPlayer(const PlayerColor & player)
  275. {
  276. setPlayerChildren(this, player);
  277. }
  278. void AdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  279. {
  280. for(auto & entry : widget->children)
  281. {
  282. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  283. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  284. auto button = dynamic_cast<CButton *>(entry);
  285. auto resDataBar = dynamic_cast<CResDataBar *>(entry);
  286. auto texture = dynamic_cast<FilledTexturePlayerColored *>(entry);
  287. if(button)
  288. button->setPlayerColor(player);
  289. if(resDataBar)
  290. resDataBar->colorize(player);
  291. if(icon)
  292. icon->setPlayer(player);
  293. if(container)
  294. setPlayerChildren(container, player);
  295. if (texture)
  296. texture->playerColored(player);
  297. }
  298. for(const auto & entry : playerColorerImages)
  299. {
  300. if(images.count(entry))
  301. images[entry]->playerColored(player);
  302. }
  303. redraw();
  304. }
  305. CAdventureMapIcon::CAdventureMapIcon(const Point & position, std::shared_ptr<CAnimation> animation, size_t index, size_t iconsPerPlayer)
  306. : index(index)
  307. , iconsPerPlayer(iconsPerPlayer)
  308. {
  309. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  310. pos += position;
  311. image = std::make_shared<CAnimImage>(animation, index);
  312. }
  313. void CAdventureMapIcon::setPlayer(const PlayerColor & player)
  314. {
  315. image->setFrame(index + player.getNum() * iconsPerPlayer);
  316. }
  317. void CAdventureMapOverlayWidget::show(Canvas & to)
  318. {
  319. CIntObject::showAll(to);
  320. }
  321. void AdventureMapWidget::updateActiveStateChildden(CIntObject * widget)
  322. {
  323. for(auto & entry : widget->children)
  324. {
  325. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  326. if (container)
  327. {
  328. if (container->disableCondition == "heroAwake")
  329. container->setEnabled(!shortcuts->optionHeroSleeping());
  330. if (container->disableCondition == "heroSleeping")
  331. container->setEnabled(shortcuts->optionHeroSleeping());
  332. if (container->disableCondition == "mapLayerSurface")
  333. container->setEnabled(shortcuts->optionMapLevelSurface());
  334. if (container->disableCondition == "mapLayerUnderground")
  335. container->setEnabled(!shortcuts->optionMapLevelSurface());
  336. if (container->disableCondition == "mapViewMode")
  337. container->setEnabled(shortcuts->optionInWorldView());
  338. if (container->disableCondition == "worldViewMode")
  339. container->setEnabled(!shortcuts->optionInWorldView());
  340. updateActiveStateChildden(container);
  341. }
  342. }
  343. }
  344. void AdventureMapWidget::updateActiveState()
  345. {
  346. updateActiveStateChildden(this);
  347. for (auto entry: shortcuts->getShortcuts())
  348. setShortcutBlocked(entry.shortcut, !entry.isEnabled);
  349. }