CAdventureMapWidget.cpp 13 KB

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