CTradeBase.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * CTradeBase.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 "CTradeBase.h"
  12. #include "MiscWidgets.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../render/Canvas.h"
  15. #include "../widgets/Buttons.h"
  16. #include "../widgets/TextControls.h"
  17. #include "../windows/InfoWindows.h"
  18. #include "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/CGeneralTextHandler.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial)
  24. : CIntObject(LCLICK | HOVER | SHOW_POPUP, pos)
  25. , type(EType(-1)) // set to invalid, will be corrected in setType
  26. , id(ID)
  27. , serial(Serial)
  28. , left(Left)
  29. {
  30. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  31. downSelection = false;
  32. hlp = nullptr;
  33. setType(Type);
  34. if(image)
  35. {
  36. this->pos.w = image->pos.w;
  37. this->pos.h = image->pos.h;
  38. }
  39. }
  40. void CTradeableItem::setType(EType newType)
  41. {
  42. if(type != newType)
  43. {
  44. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  45. type = newType;
  46. if(getIndex() < 0)
  47. {
  48. image = std::make_shared<CAnimImage>(getFilename(), 0);
  49. image->disable();
  50. }
  51. else
  52. {
  53. image = std::make_shared<CAnimImage>(getFilename(), getIndex());
  54. }
  55. }
  56. }
  57. void CTradeableItem::setID(int newID)
  58. {
  59. if(id != newID)
  60. {
  61. id = newID;
  62. if(image)
  63. {
  64. int index = getIndex();
  65. if(index < 0)
  66. image->disable();
  67. else
  68. {
  69. image->enable();
  70. image->setFrame(index);
  71. }
  72. }
  73. }
  74. }
  75. AnimationPath CTradeableItem::getFilename()
  76. {
  77. switch(type)
  78. {
  79. case RESOURCE:
  80. return AnimationPath::builtin("RESOURCE");
  81. case PLAYER:
  82. return AnimationPath::builtin("CREST58");
  83. case ARTIFACT_TYPE:
  84. case ARTIFACT_PLACEHOLDER:
  85. case ARTIFACT_INSTANCE:
  86. return AnimationPath::builtin("artifact");
  87. case CREATURE:
  88. return AnimationPath::builtin("TWCRPORT");
  89. default:
  90. return {};
  91. }
  92. }
  93. int CTradeableItem::getIndex()
  94. {
  95. if(id < 0)
  96. return -1;
  97. switch(type)
  98. {
  99. case RESOURCE:
  100. case PLAYER:
  101. return id;
  102. case ARTIFACT_TYPE:
  103. case ARTIFACT_INSTANCE:
  104. case ARTIFACT_PLACEHOLDER:
  105. return CGI->artifacts()->getByIndex(id)->getIconIndex();
  106. case CREATURE:
  107. return CGI->creatures()->getByIndex(id)->getIconIndex();
  108. default:
  109. return -1;
  110. }
  111. }
  112. void CTradeableItem::showAll(Canvas & to)
  113. {
  114. Point posToBitmap;
  115. Point posToSubCenter;
  116. switch (type)
  117. {
  118. case RESOURCE:
  119. posToBitmap = Point(19, 9);
  120. posToSubCenter = Point(35, 57);
  121. break;
  122. case CREATURE_PLACEHOLDER:
  123. case CREATURE:
  124. posToSubCenter = Point(29, 77);
  125. break;
  126. case PLAYER:
  127. posToSubCenter = Point(31, 77);
  128. break;
  129. case ARTIFACT_PLACEHOLDER:
  130. case ARTIFACT_INSTANCE:
  131. posToSubCenter = Point(22, 51);
  132. if (downSelection)
  133. posToSubCenter.y += 8;
  134. break;
  135. case ARTIFACT_TYPE:
  136. posToSubCenter = Point(35, 57);
  137. posToBitmap = Point(13, 0);
  138. break;
  139. }
  140. if(image)
  141. {
  142. image->moveTo(pos.topLeft() + posToBitmap);
  143. CIntObject::showAll(to);
  144. }
  145. to.drawText(pos.topLeft() + posToSubCenter, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, subtitle);
  146. }
  147. void CTradeableItem::clickPressed(const Point & cursorPosition)
  148. {
  149. if(clickPressedCallback)
  150. clickPressedCallback(shared_from_this());
  151. }
  152. void CTradeableItem::showAllAt(const Point & dstPos, const std::string & customSub, Canvas & to)
  153. {
  154. Rect oldPos = pos;
  155. std::string oldSub = subtitle;
  156. downSelection = true;
  157. moveTo(dstPos);
  158. subtitle = customSub;
  159. showAll(to);
  160. downSelection = false;
  161. moveTo(oldPos.topLeft());
  162. subtitle = oldSub;
  163. }
  164. void CTradeableItem::hover(bool on)
  165. {
  166. if(!on)
  167. {
  168. GH.statusbar()->clear();
  169. return;
  170. }
  171. switch(type)
  172. {
  173. case CREATURE:
  174. case CREATURE_PLACEHOLDER:
  175. GH.statusbar()->write(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->getNamePluralTranslated()));
  176. break;
  177. case ARTIFACT_PLACEHOLDER:
  178. if(id < 0)
  179. GH.statusbar()->write(CGI->generaltexth->zelp[582].first);
  180. else
  181. GH.statusbar()->write(CGI->artifacts()->getByIndex(id)->getNameTranslated());
  182. break;
  183. }
  184. }
  185. void CTradeableItem::showPopupWindow(const Point & cursorPosition)
  186. {
  187. switch(type)
  188. {
  189. case CREATURE:
  190. case CREATURE_PLACEHOLDER:
  191. break;
  192. case ARTIFACT_TYPE:
  193. case ARTIFACT_PLACEHOLDER:
  194. //TODO: it's would be better for market to contain actual CArtifactInstance and not just ids of certain artifact type so we can use getEffectiveDescription.
  195. if (id >= 0)
  196. CRClickPopup::createAndPush(CGI->artifacts()->getByIndex(id)->getDescriptionTranslated());
  197. break;
  198. }
  199. }
  200. std::string CTradeableItem::getName(int number) const
  201. {
  202. switch(type)
  203. {
  204. case PLAYER:
  205. return CGI->generaltexth->capColors[id];
  206. case RESOURCE:
  207. return CGI->generaltexth->restypes[id];
  208. case CREATURE:
  209. if (number == 1)
  210. return CGI->creh->objects[id]->getNameSingularTranslated();
  211. else
  212. return CGI->creh->objects[id]->getNamePluralTranslated();
  213. case ARTIFACT_TYPE:
  214. case ARTIFACT_INSTANCE:
  215. return CGI->artifacts()->getByIndex(id)->getNameTranslated();
  216. }
  217. logGlobal->error("Invalid trade item type: %d", (int)type);
  218. return "";
  219. }
  220. const CArtifactInstance * CTradeableItem::getArtInstance() const
  221. {
  222. switch(type)
  223. {
  224. case ARTIFACT_PLACEHOLDER:
  225. case ARTIFACT_INSTANCE:
  226. return hlp;
  227. default:
  228. return nullptr;
  229. }
  230. }
  231. void CTradeableItem::setArtInstance(const CArtifactInstance * art)
  232. {
  233. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  234. hlp = art;
  235. if(art)
  236. setID(art->artType->getId());
  237. else
  238. setID(-1);
  239. }
  240. void STradePanel::updateSlots()
  241. {
  242. if(updateSlotsCallback)
  243. updateSlotsCallback();
  244. }
  245. void STradePanel::deselect()
  246. {
  247. for(auto & slot : slots)
  248. slot->selection->selectSlot(false);
  249. }
  250. void STradePanel::clearSubtitles()
  251. {
  252. for(auto & slot : slots)
  253. slot->subtitle.clear();
  254. }
  255. void STradePanel::updateOffer(CTradeableItem & slot, int cost, int qty)
  256. {
  257. slot.subtitle = std::to_string(qty);
  258. if(cost != 1)
  259. slot.subtitle += "/" + std::to_string(cost);
  260. }
  261. void STradePanel::deleteSlots()
  262. {
  263. if(deleteSlotsCheck)
  264. slots.erase(std::remove_if(slots.begin(), slots.end(), deleteSlotsCheck), slots.end());
  265. }
  266. SResourcesPanel::SResourcesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, UpdateSlotsFunctor updateSubtitles)
  267. {
  268. assert(resourcesForTrade.size() == slotsPos.size());
  269. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  270. for(const auto & res : resourcesForTrade)
  271. {
  272. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(slotsPos[res.num], EType::RESOURCE, res.num, true, res.num));
  273. slot->clickPressedCallback = clickPressedCallback;
  274. slot->pos.w = 69; slots.back()->pos.h = 66;
  275. slot->selection = std::make_unique<SelectableSlot>(Rect(slotsPos[res.num], slots.back()->pos.dimensions()), Point(1, 1), selectionWidth);
  276. }
  277. updateSlotsCallback = updateSubtitles;
  278. }
  279. SArtifactsPanel::SArtifactsPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, UpdateSlotsFunctor updateSubtitles,
  280. std::vector<TradeItemBuy> & arts)
  281. {
  282. assert(slotsForTrade == slotsPos.size());
  283. assert(slotsForTrade == arts.size());
  284. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  285. for(auto slotIdx = 0; slotIdx < slotsForTrade; slotIdx++)
  286. {
  287. auto artType = arts[slotIdx].getNum();
  288. if(artType != ArtifactID::NONE)
  289. {
  290. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(slotsPos[slotIdx], EType::ARTIFACT_TYPE, artType, false, slotIdx));
  291. slot->clickPressedCallback = clickPressedCallback;
  292. slot->pos.w = 69; slot->pos.h = 66;
  293. slot->selection = std::make_unique<SelectableSlot>(Rect(slotsPos[slotIdx], slot->pos.dimensions()), Point(1, 1), selectionWidth);
  294. }
  295. }
  296. updateSlotsCallback = updateSubtitles;
  297. }
  298. SPlayersPanel::SPlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback)
  299. {
  300. assert(PlayerColor::PLAYER_LIMIT_I <= slotsPos.size());
  301. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  302. std::vector<PlayerColor> players;
  303. for(auto player = PlayerColor(0); player < PlayerColor::PLAYER_LIMIT_I; player++)
  304. {
  305. if(player != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(player) == EPlayerStatus::INGAME)
  306. players.emplace_back(player);
  307. }
  308. slots.resize(players.size());
  309. int slotNum = 0;
  310. for(auto & slot : slots)
  311. {
  312. slot = std::make_shared<CTradeableItem>(slotsPos[slotNum], EType::PLAYER, players[slotNum].num, false, slotNum);
  313. slot->clickPressedCallback = clickPressedCallback;
  314. slot->selection = std::make_unique<SelectableSlot>(Rect(slotsPos[slotNum], slot->pos.dimensions()), Point(1, 1), selectionWidth);
  315. slot->subtitle = CGI->generaltexth->capColors[players[slotNum++].num];
  316. }
  317. }
  318. SCreaturesPanel::SCreaturesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, slotsData & initialSlots)
  319. {
  320. assert(initialSlots.size() <= GameConstants::ARMY_SIZE);
  321. assert(slotsPos.size() <= GameConstants::ARMY_SIZE);
  322. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  323. for(const auto & slotData : initialSlots)
  324. {
  325. auto slotId = std::get<1>(slotData);
  326. auto creaturesNum = std::get<2>(slotData);
  327. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(slotsPos[slotId.num],
  328. creaturesNum == 0 ? EType::CREATURE_PLACEHOLDER : EType::CREATURE, std::get<0>(slotData).num, true, slotId));
  329. slot->clickPressedCallback = clickPressedCallback;
  330. if(creaturesNum != 0)
  331. slot->subtitle = std::to_string(std::get<2>(slotData));
  332. slot->pos.w = 58; slot->pos.h = 64;
  333. slot->selection = std::make_unique<SelectableSlot>(Rect(slotsPos[slotId.num], slot->pos.dimensions()), Point(1, 1), selectionWidth);
  334. }
  335. }
  336. SCreaturesPanel::SCreaturesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback,
  337. std::vector<std::shared_ptr<CTradeableItem>> & stsSlots, bool emptySlots)
  338. {
  339. assert(slots.size() <= GameConstants::ARMY_SIZE);
  340. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  341. for(const auto & srcSlot : stsSlots)
  342. {
  343. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(slotsPos[srcSlot->serial],
  344. emptySlots ? EType::CREATURE_PLACEHOLDER : EType::CREATURE, srcSlot->id, true, srcSlot->serial));
  345. slot->clickPressedCallback = clickPressedCallback;
  346. slot->subtitle = emptySlots ? "" : srcSlot->subtitle;
  347. slot->pos.w = srcSlot->pos.w; slot->pos.h = srcSlot->pos.h;
  348. slot->selection = std::make_unique<SelectableSlot>(Rect(slotsPos[slot->serial], slot->pos.dimensions()), Point(1, 1), selectionWidth);
  349. }
  350. }
  351. CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero)
  352. : market(market)
  353. , hero(hero)
  354. {
  355. deal = std::make_shared<CButton>(Point(), AnimationPath::builtin("ALTSACR.DEF"),
  356. CGI->generaltexth->zelp[585], std::bind(&CTradeBase::makeDeal, this));
  357. }
  358. void CTradeBase::removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  359. {
  360. for(auto item : toRemove)
  361. removeItem(item);
  362. }
  363. void CTradeBase::removeItem(std::shared_ptr<CTradeableItem> item)
  364. {
  365. rightTradePanel->slots.erase(std::remove(rightTradePanel->slots.begin(), rightTradePanel->slots.end(), item));
  366. if(hRight == item)
  367. hRight.reset();
  368. }
  369. void CTradeBase::getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  370. {
  371. for(auto item : leftTradePanel->slots)
  372. if(!hero->getStackCount(SlotID(item->serial)))
  373. toRemove.insert(item);
  374. }
  375. void CTradeBase::deselect()
  376. {
  377. if(hLeft)
  378. hLeft->selection->selectSlot(false);
  379. if(hRight)
  380. hRight->selection->selectSlot(false);
  381. hLeft = hRight = nullptr;
  382. deal->block(true);
  383. }
  384. void CTradeBase::onSlotClickPressed(std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  385. {
  386. if(newSlot == hCurSlot)
  387. return;
  388. if(hCurSlot)
  389. hCurSlot->selection->selectSlot(false);
  390. hCurSlot = newSlot;
  391. newSlot->selection->selectSlot(true);
  392. }
  393. CExpAltar::CExpAltar()
  394. {
  395. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  396. // Experience needed to reach next level
  397. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  398. // Total experience on the Altar
  399. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  400. deal->moveBy(dealButtonPos);
  401. expToLevel = std::make_shared<CLabel>(75, 477, FONT_SMALL, ETextAlignment::CENTER);
  402. expForHero = std::make_shared<CLabel>(75, 545, FONT_SMALL, ETextAlignment::CENTER);
  403. }
  404. CCreaturesSelling::CCreaturesSelling()
  405. {
  406. assert(hero);
  407. SCreaturesPanel::slotsData slots;
  408. for(auto slotId = SlotID(0); slotId.num < GameConstants::ARMY_SIZE; slotId++)
  409. {
  410. if(const auto & creature = hero->getCreature(slotId))
  411. slots.emplace_back(std::make_tuple(creature->getId(), slotId, hero->getStackCount(slotId)));
  412. }
  413. leftTradePanel = std::make_shared<SCreaturesPanel>([this](std::shared_ptr<CTradeableItem> altarSlot) -> void
  414. {
  415. onSlotClickPressed(altarSlot, hLeft);
  416. }, slots);
  417. }
  418. bool CCreaturesSelling::slotDeletingCheck(std::shared_ptr<CTradeableItem> & slot)
  419. {
  420. return hero->getStackCount(SlotID(slot->serial)) == 0 ? true : false;
  421. }
  422. void CCreaturesSelling::updateSubtitle()
  423. {
  424. for(auto & heroSlot : leftTradePanel->slots)
  425. heroSlot->subtitle = std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial)));
  426. }