CAdventureMapWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 "CAdventureMapWidget.h"
  12. #include "AdventureMapShortcuts.h"
  13. #include "CInfoBar.h"
  14. #include "CList.h"
  15. #include "CMinimap.h"
  16. #include "CResDataBar.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../mapView/MapView.h"
  20. #include "../render/CAnimation.h"
  21. #include "../render/IImage.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/Images.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../CPlayerInterface.h"
  26. #include "../PlayerLocalState.h"
  27. #include "../../lib/StringConstants.h"
  28. #include "../../lib/filesystem/ResourceID.h"
  29. CAdventureMapWidget::CAdventureMapWidget( std::shared_ptr<AdventureMapShortcuts> shortcuts )
  30. : state(EGameState::NOT_INITIALIZED)
  31. , shortcuts(shortcuts)
  32. {
  33. pos.x = pos.y = 0;
  34. pos.w = GH.screenDimensions().x;
  35. pos.h = GH.screenDimensions().y;
  36. REGISTER_BUILDER("adventureInfobar", &CAdventureMapWidget::buildInfobox );
  37. REGISTER_BUILDER("adventureMapImage", &CAdventureMapWidget::buildMapImage );
  38. REGISTER_BUILDER("adventureMapButton", &CAdventureMapWidget::buildMapButton );
  39. REGISTER_BUILDER("adventureMapContainer", &CAdventureMapWidget::buildMapContainer );
  40. REGISTER_BUILDER("adventureMapGameArea", &CAdventureMapWidget::buildMapGameArea );
  41. REGISTER_BUILDER("adventureMapHeroList", &CAdventureMapWidget::buildMapHeroList );
  42. REGISTER_BUILDER("adventureMapIcon", &CAdventureMapWidget::buildMapIcon );
  43. REGISTER_BUILDER("adventureMapTownList", &CAdventureMapWidget::buildMapTownList );
  44. REGISTER_BUILDER("adventureMinimap", &CAdventureMapWidget::buildMinimap );
  45. REGISTER_BUILDER("adventureResourceDateBar", &CAdventureMapWidget::buildResourceDateBar );
  46. REGISTER_BUILDER("adventureStatusBar", &CAdventureMapWidget::buildStatusBar );
  47. for (const auto & entry : shortcuts->getFunctors())
  48. addShortcut(entry.first, entry.second);
  49. const JsonNode config(ResourceID("config/widgets/adventureMap.json"));
  50. for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
  51. {
  52. ResourceID resourceName(entry.String(), EResType::IMAGE);
  53. playerColorerImages.push_back(resourceName.getName());
  54. }
  55. build(config);
  56. addUsedEvents(KEYBOARD);
  57. }
  58. Rect CAdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
  59. {
  60. const auto & input = source.isNull() ? sourceCommon : source;
  61. return readArea(input, Rect(Point(0, 0), Point(800, 600)));
  62. }
  63. Rect CAdventureMapWidget::readTargetArea(const JsonNode & source)
  64. {
  65. if(subwidgetSizes.empty())
  66. return readArea(source, pos);
  67. return readArea(source, subwidgetSizes.back());
  68. }
  69. Rect CAdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
  70. {
  71. const auto & object = source.Struct();
  72. if(object.count("left") + object.count("width") + object.count("right") != 2)
  73. logGlobal->error("Invalid area definition in widget! Unable to load width!");
  74. if(object.count("top") + object.count("height") + object.count("bottom") != 2)
  75. logGlobal->error("Invalid area definition in widget! Unable to load height!");
  76. int left = source["left"].Integer();
  77. int width = source["width"].Integer();
  78. int right = source["right"].Integer();
  79. int top = source["top"].Integer();
  80. int height = source["height"].Integer();
  81. int bottom = source["bottom"].Integer();
  82. Point topLeft(left, top);
  83. Point dimensions(width, height);
  84. if(source["left"].isNull())
  85. topLeft.x = boundingBox.w - right - width;
  86. if(source["width"].isNull())
  87. dimensions.x = boundingBox.w - right - left;
  88. if(source["top"].isNull())
  89. topLeft.y = boundingBox.h - bottom - height;
  90. if(source["height"].isNull())
  91. dimensions.y = boundingBox.h - bottom - top;
  92. return Rect(topLeft + boundingBox.topLeft(), dimensions);
  93. }
  94. std::shared_ptr<IImage> CAdventureMapWidget::loadImage(const std::string & name)
  95. {
  96. ResourceID resource(name, EResType::IMAGE);
  97. if(images.count(resource.getName()) == 0)
  98. images[resource.getName()] = IImage::createFromFile(resource.getName());
  99. ;
  100. return images[resource.getName()];
  101. }
  102. std::shared_ptr<CAnimation> CAdventureMapWidget::loadAnimation(const std::string & name)
  103. {
  104. ResourceID resource(name, EResType::ANIMATION);
  105. if(animations.count(resource.getName()) == 0)
  106. animations[resource.getName()] = std::make_shared<CAnimation>(resource.getName());
  107. return animations[resource.getName()];
  108. }
  109. std::shared_ptr<CIntObject> CAdventureMapWidget::buildInfobox(const JsonNode & input)
  110. {
  111. Rect area = readTargetArea(input["area"]);
  112. infoBar = std::make_shared<CInfoBar>(area);
  113. return infoBar;
  114. }
  115. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapImage(const JsonNode & input)
  116. {
  117. Rect targetArea = readTargetArea(input["area"]);
  118. Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
  119. std::string image = input["image"].String();
  120. return std::make_shared<CFilledTexture>(loadImage(image), targetArea, sourceArea);
  121. }
  122. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapButton(const JsonNode & input)
  123. {
  124. auto position = readTargetArea(input["area"]);
  125. auto image = input["image"].String();
  126. auto help = readHintText(input["help"]);
  127. bool playerColored = input["playerColored"].Bool();
  128. auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
  129. loadButtonHotkey(button, input["hotkey"]);
  130. return button;
  131. }
  132. std::shared_ptr<CIntObject> CAdventureMapWidget::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->moveBy(position.topLeft());
  152. subwidgetSizes.push_back(position);
  153. for(const auto & entry : input["items"].Vector())
  154. {
  155. result->ownedChildren.push_back(buildWidget(entry));
  156. result->addChild(result->ownedChildren.back().get(), false);
  157. }
  158. subwidgetSizes.pop_back();
  159. return result;
  160. }
  161. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapGameArea(const JsonNode & input)
  162. {
  163. Rect area = readTargetArea(input["area"]);
  164. mapView = std::make_shared<MapView>(area.topLeft(), area.dimensions());
  165. return mapView;
  166. }
  167. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapHeroList(const JsonNode & input)
  168. {
  169. Rect area = readTargetArea(input["area"]);
  170. subwidgetSizes.push_back(area);
  171. Rect item = readTargetArea(input["item"]);
  172. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  173. int itemsCount = input["itemsCount"].Integer();
  174. auto result = std::make_shared<CHeroList>(itemsCount, item.topLeft(), itemOffset, LOCPLINT->localState->getWanderingHeroes().size());
  175. if(!input["scrollUp"].isNull())
  176. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  177. if(!input["scrollDown"].isNull())
  178. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  179. subwidgetSizes.pop_back();
  180. heroList = result;
  181. return result;
  182. }
  183. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapIcon(const JsonNode & input)
  184. {
  185. Rect area = readTargetArea(input["area"]);
  186. size_t index = input["index"].Integer();
  187. size_t perPlayer = input["perPlayer"].Integer();
  188. std::string image = input["image"].String();
  189. return std::make_shared<CAdventureMapIcon>(area.topLeft(), loadAnimation(image), index, perPlayer);
  190. }
  191. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapTownList(const JsonNode & input)
  192. {
  193. Rect area = readTargetArea(input["area"]);
  194. subwidgetSizes.push_back(area);
  195. Rect item = readTargetArea(input["item"]);
  196. Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
  197. int itemsCount = input["itemsCount"].Integer();
  198. auto result = std::make_shared<CTownList>(itemsCount, item.topLeft(), itemOffset, LOCPLINT->localState->getOwnedTowns().size());
  199. if(!input["scrollUp"].isNull())
  200. result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
  201. if(!input["scrollDown"].isNull())
  202. result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
  203. subwidgetSizes.pop_back();
  204. townList = result;
  205. return result;
  206. }
  207. std::shared_ptr<CIntObject> CAdventureMapWidget::buildMinimap(const JsonNode & input)
  208. {
  209. Rect area = readTargetArea(input["area"]);
  210. minimap = std::make_shared<CMinimap>(area);
  211. return minimap;
  212. }
  213. std::shared_ptr<CIntObject> CAdventureMapWidget::buildResourceDateBar(const JsonNode & input)
  214. {
  215. Rect area = readTargetArea(input["area"]);
  216. std::string image = input["image"].String();
  217. auto result = std::make_shared<CResDataBar>(image, area.topLeft());
  218. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  219. {
  220. const auto & node = input[GameConstants::RESOURCE_NAMES[i]];
  221. if(node.isNull())
  222. continue;
  223. result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
  224. }
  225. result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
  226. return result;
  227. }
  228. std::shared_ptr<CIntObject> CAdventureMapWidget::buildStatusBar(const JsonNode & input)
  229. {
  230. Rect area = readTargetArea(input["area"]);
  231. std::string image = input["image"].String();
  232. auto background = std::make_shared<CFilledTexture>(image, area);
  233. return CGStatusBar::create(background);
  234. }
  235. std::shared_ptr<CHeroList> CAdventureMapWidget::getHeroList()
  236. {
  237. return heroList;
  238. }
  239. std::shared_ptr<CTownList> CAdventureMapWidget::getTownList()
  240. {
  241. return townList;
  242. }
  243. std::shared_ptr<CMinimap> CAdventureMapWidget::getMinimap()
  244. {
  245. return minimap;
  246. }
  247. std::shared_ptr<MapView> CAdventureMapWidget::getMapView()
  248. {
  249. return mapView;
  250. }
  251. std::shared_ptr<CInfoBar> CAdventureMapWidget::getInfoBar()
  252. {
  253. return infoBar;
  254. }
  255. void CAdventureMapWidget::setPlayer(const PlayerColor & player)
  256. {
  257. setPlayerChildren(this, player);
  258. }
  259. void CAdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
  260. {
  261. for(auto & entry : widget->children)
  262. {
  263. auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
  264. auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
  265. auto button = dynamic_cast<CButton *>(entry);
  266. if(button)
  267. button->setPlayerColor(player);
  268. if(icon)
  269. icon->setPlayer(player);
  270. if(container)
  271. setPlayerChildren(container, player);
  272. }
  273. for(const auto & entry : playerColorerImages)
  274. {
  275. if(images.count(entry))
  276. images[entry]->playerColored(player);
  277. }
  278. redraw();
  279. }
  280. void CAdventureMapWidget::setState(EGameState newState)
  281. {
  282. state = newState;
  283. if(newState == EGameState::WORLD_VIEW)
  284. widget<CIntObject>("worldViewContainer")->enable();
  285. else
  286. widget<CIntObject>("worldViewContainer")->disable();
  287. }
  288. EGameState CAdventureMapWidget::getState()
  289. {
  290. return state;
  291. }
  292. void CAdventureMapWidget::setOptionHasQuests(bool on)
  293. {
  294. }
  295. void CAdventureMapWidget::setOptionHasUnderground(bool on)
  296. {
  297. }
  298. void CAdventureMapWidget::setOptionUndergroundLevel(bool on)
  299. {
  300. }
  301. void CAdventureMapWidget::setOptionHeroSleeping(bool on)
  302. {
  303. }
  304. void CAdventureMapWidget::setOptionHeroSelected(bool on)
  305. {
  306. }
  307. void CAdventureMapWidget::setOptionHeroCanMove(bool on)
  308. {
  309. }
  310. void CAdventureMapWidget::setOptionHasNextHero(bool on)
  311. {
  312. }
  313. CAdventureMapIcon::CAdventureMapIcon(const Point & position, std::shared_ptr<CAnimation> animation, size_t index, size_t iconsPerPlayer)
  314. : index(index)
  315. , iconsPerPlayer(iconsPerPlayer)
  316. {
  317. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  318. pos += position;
  319. image = std::make_shared<CAnimImage>(animation, index);
  320. }
  321. void CAdventureMapIcon::setPlayer(const PlayerColor & player)
  322. {
  323. image->setFrame(index + player.getNum() * iconsPerPlayer);
  324. }
  325. void CAdventureMapOverlayWidget::show(SDL_Surface * to)
  326. {
  327. CIntObject::showAll(to);
  328. }