TradePanels.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * TradePanels.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 "TradePanels.h"
  12. #include "../../GameEngine.h"
  13. #include "../../GameInstance.h"
  14. #include "../../render/Canvas.h"
  15. #include "../../widgets/TextControls.h"
  16. #include "../../windows/InfoWindows.h"
  17. #include "../../CPlayerInterface.h"
  18. #include "../../../CCallback.h"
  19. #include "../../../lib/GameLibrary.h"
  20. #include "../../../lib/entities/artifact/CArtHandler.h"
  21. #include "../../../lib/texts/CGeneralTextHandler.h"
  22. #include "../../../lib/mapObjects/CGHeroInstance.h"
  23. CTradeableItem::CTradeableItem(const Rect & area, EType Type, int32_t ID, int32_t serial)
  24. : SelectableSlot(area, Point(1, 1))
  25. , type(EType(-1)) // set to invalid, will be corrected in setType
  26. , id(ID)
  27. , serial(serial)
  28. {
  29. OBJECT_CONSTRUCTION;
  30. addUsedEvents(LCLICK);
  31. addUsedEvents(HOVER);
  32. addUsedEvents(SHOW_POPUP);
  33. subtitle = std::make_shared<CLabel>(0, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  34. setType(Type);
  35. this->pos.w = area.w;
  36. this->pos.h = area.h;
  37. }
  38. void CTradeableItem::setType(EType newType)
  39. {
  40. if(type != newType)
  41. {
  42. OBJECT_CONSTRUCTION;
  43. type = newType;
  44. if(getIndex() < 0)
  45. {
  46. image = std::make_shared<CAnimImage>(getFilename(), 0);
  47. image->disable();
  48. }
  49. else
  50. {
  51. image = std::make_shared<CAnimImage>(getFilename(), getIndex());
  52. }
  53. switch(type)
  54. {
  55. case EType::RESOURCE:
  56. subtitle->moveTo(pos.topLeft() + Point(35, 55));
  57. image->moveTo(pos.topLeft() + Point(19, 8));
  58. break;
  59. case EType::CREATURE:
  60. subtitle->moveTo(pos.topLeft() + Point(30, 77));
  61. break;
  62. case EType::PLAYER:
  63. subtitle->moveTo(pos.topLeft() + Point(31, 76));
  64. break;
  65. case EType::ARTIFACT:
  66. subtitle->moveTo(pos.topLeft() + Point(21, 55));
  67. break;
  68. case EType::ARTIFACT_TYPE:
  69. subtitle->moveTo(pos.topLeft() + Point(35, 57));
  70. image->moveTo(pos.topLeft() + Point(13, 0));
  71. break;
  72. }
  73. }
  74. }
  75. void CTradeableItem::setID(int32_t newID)
  76. {
  77. if(id != newID)
  78. {
  79. id = newID;
  80. if(image)
  81. {
  82. const auto index = getIndex();
  83. if(index < 0)
  84. image->disable();
  85. else
  86. {
  87. image->enable();
  88. image->setFrame(index);
  89. }
  90. }
  91. }
  92. }
  93. void CTradeableItem::clear()
  94. {
  95. setID(-1);
  96. image->setFrame(0);
  97. image->disable();
  98. subtitle->clear();
  99. }
  100. AnimationPath CTradeableItem::getFilename()
  101. {
  102. switch(type)
  103. {
  104. case EType::RESOURCE:
  105. return AnimationPath::builtin("RESOURCE");
  106. case EType::PLAYER:
  107. return AnimationPath::builtin("CREST58");
  108. case EType::ARTIFACT_TYPE:
  109. case EType::ARTIFACT:
  110. return AnimationPath::builtin("artifact");
  111. case EType::CREATURE:
  112. return AnimationPath::builtin("TWCRPORT");
  113. default:
  114. return {};
  115. }
  116. }
  117. int CTradeableItem::getIndex()
  118. {
  119. if(id < 0)
  120. return -1;
  121. switch(type)
  122. {
  123. case EType::RESOURCE:
  124. case EType::PLAYER:
  125. return id;
  126. case EType::ARTIFACT_TYPE:
  127. case EType::ARTIFACT:
  128. return LIBRARY->artifacts()->getByIndex(id)->getIconIndex();
  129. case EType::CREATURE:
  130. return LIBRARY->creatures()->getByIndex(id)->getIconIndex();
  131. default:
  132. return -1;
  133. }
  134. }
  135. void CTradeableItem::clickPressed(const Point & cursorPosition)
  136. {
  137. if(clickPressedCallback)
  138. clickPressedCallback(shared_from_this());
  139. }
  140. void CTradeableItem::hover(bool on)
  141. {
  142. if(!on || id == -1)
  143. {
  144. ENGINE->statusbar()->clear();
  145. return;
  146. }
  147. switch(type)
  148. {
  149. case EType::CREATURE:
  150. ENGINE->statusbar()->write(boost::str(boost::format(LIBRARY->generaltexth->allTexts[481]) % LIBRARY->creh->objects[id]->getNamePluralTranslated()));
  151. break;
  152. case EType::ARTIFACT_TYPE:
  153. case EType::ARTIFACT:
  154. if(id < 0)
  155. ENGINE->statusbar()->write(LIBRARY->generaltexth->zelp[582].first);
  156. else
  157. ENGINE->statusbar()->write(LIBRARY->artifacts()->getByIndex(id)->getNameTranslated());
  158. break;
  159. case EType::RESOURCE:
  160. ENGINE->statusbar()->write(LIBRARY->generaltexth->restypes[id]);
  161. break;
  162. case EType::PLAYER:
  163. ENGINE->statusbar()->write(LIBRARY->generaltexth->capColors[id]);
  164. break;
  165. }
  166. }
  167. void CTradeableItem::showPopupWindow(const Point & cursorPosition)
  168. {
  169. switch(type)
  170. {
  171. case EType::CREATURE:
  172. break;
  173. case EType::ARTIFACT_TYPE:
  174. case EType::ARTIFACT:
  175. if (id >= 0)
  176. CRClickPopup::createAndPush(LIBRARY->artifacts()->getByIndex(id)->getDescriptionTranslated());
  177. break;
  178. }
  179. }
  180. void TradePanelBase::update()
  181. {
  182. if(deleteSlotsCheck)
  183. slots.erase(std::remove_if(slots.begin(), slots.end(), deleteSlotsCheck), slots.end());
  184. if(updateSlotsCallback)
  185. updateSlotsCallback();
  186. }
  187. void TradePanelBase::deselect()
  188. {
  189. for(const auto & slot : slots)
  190. slot->selectSlot(false);
  191. }
  192. void TradePanelBase::clearSubtitles()
  193. {
  194. for(const auto & slot : slots)
  195. slot->subtitle->clear();
  196. }
  197. void TradePanelBase::updateOffer(CTradeableItem & slot, int cost, int qty)
  198. {
  199. std::string subtitle = std::to_string(qty);
  200. if(cost != 1)
  201. {
  202. subtitle.append("/");
  203. subtitle.append(std::to_string(cost));
  204. }
  205. slot.subtitle->setText(subtitle);
  206. }
  207. void TradePanelBase::setShowcaseSubtitle(const std::string & text)
  208. {
  209. showcaseSlot->subtitle->setText(text);
  210. }
  211. int32_t TradePanelBase::getHighlightedItemId() const
  212. {
  213. if(highlightedSlot)
  214. return highlightedSlot->id;
  215. else
  216. return -1;
  217. }
  218. void TradePanelBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot)
  219. {
  220. assert(vstd::contains(slots, newSlot));
  221. if(newSlot == highlightedSlot)
  222. return;
  223. if(highlightedSlot)
  224. highlightedSlot->selectSlot(false);
  225. highlightedSlot = newSlot;
  226. newSlot->selectSlot(true);
  227. }
  228. bool TradePanelBase::isHighlighted() const
  229. {
  230. return highlightedSlot != nullptr;
  231. }
  232. ResourcesPanel::ResourcesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  233. const UpdateSlotsFunctor & updateSubtitles)
  234. {
  235. assert(resourcesForTrade.size() == slotsPos.size());
  236. OBJECT_CONSTRUCTION;
  237. for(const auto & res : resourcesForTrade)
  238. {
  239. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(Rect(slotsPos[res.num], slotDimension), EType::RESOURCE, res.num, res.num));
  240. slot->clickPressedCallback = clickPressedCallback;
  241. slot->setSelectionWidth(selectionWidth);
  242. }
  243. updateSlotsCallback = updateSubtitles;
  244. showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::RESOURCE, 0, 0);
  245. }
  246. ArtifactsPanel::ArtifactsPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  247. const UpdateSlotsFunctor & updateSubtitles, const std::vector<TradeItemBuy> & arts)
  248. {
  249. assert(slotsForTrade == slotsPos.size());
  250. assert(slotsForTrade == arts.size());
  251. OBJECT_CONSTRUCTION;
  252. for(auto slotIdx = 0; slotIdx < slotsForTrade; slotIdx++)
  253. {
  254. auto artType = arts[slotIdx].getNum();
  255. if(artType != ArtifactID::NONE)
  256. {
  257. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(Rect(slotsPos[slotIdx], slotDimension),
  258. EType::ARTIFACT_TYPE, artType, slotIdx));
  259. slot->clickPressedCallback = clickPressedCallback;
  260. slot->setSelectionWidth(selectionWidth);
  261. }
  262. }
  263. updateSlotsCallback = updateSubtitles;
  264. showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::ARTIFACT_TYPE, 0, 0);
  265. showcaseSlot->subtitle->moveBy(Point(0, 1));
  266. }
  267. PlayersPanel::PlayersPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback)
  268. {
  269. assert(PlayerColor::PLAYER_LIMIT_I <= slotsPos.size() + 1);
  270. OBJECT_CONSTRUCTION;
  271. std::vector<PlayerColor> players;
  272. for(auto player = PlayerColor(0); player < PlayerColor::PLAYER_LIMIT_I; player++)
  273. {
  274. if(player != GAME->interface()->playerID && GAME->interface()->cb->getPlayerStatus(player) == EPlayerStatus::INGAME)
  275. players.emplace_back(player);
  276. }
  277. slots.resize(players.size());
  278. int slotNum = 0;
  279. for(auto & slot : slots)
  280. {
  281. slot = std::make_shared<CTradeableItem>(Rect(slotsPos[slotNum], slotDimension), EType::PLAYER, players[slotNum].num, slotNum);
  282. slot->clickPressedCallback = clickPressedCallback;
  283. slot->setSelectionWidth(selectionWidth);
  284. slot->subtitle->setText(LIBRARY->generaltexth->capColors[players[slotNum].num]);
  285. slotNum++;
  286. }
  287. showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::PLAYER, 0, 0);
  288. }
  289. CreaturesPanel::CreaturesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, const slotsData & initialSlots)
  290. {
  291. assert(initialSlots.size() <= GameConstants::ARMY_SIZE);
  292. assert(slotsPos.size() <= GameConstants::ARMY_SIZE);
  293. OBJECT_CONSTRUCTION;
  294. for(const auto & [creatureId, slotId, creaturesNum] : initialSlots)
  295. {
  296. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(Rect(slotsPos[slotId.num], slotDimension),
  297. EType::CREATURE, creaturesNum == 0 ? -1 : creatureId.num, slotId));
  298. slot->clickPressedCallback = clickPressedCallback;
  299. if(creaturesNum != 0)
  300. slot->subtitle->setText(std::to_string(creaturesNum));
  301. slot->setSelectionWidth(selectionWidth);
  302. }
  303. showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::CREATURE, 0, 0);
  304. }
  305. CreaturesPanel::CreaturesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  306. const std::vector<std::shared_ptr<CTradeableItem>> & srcSlots, bool emptySlots)
  307. {
  308. assert(slots.size() <= GameConstants::ARMY_SIZE);
  309. OBJECT_CONSTRUCTION;
  310. for(const auto & srcSlot : srcSlots)
  311. {
  312. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(Rect(slotsPos[srcSlot->serial], srcSlot->pos.dimensions()),
  313. EType::CREATURE, emptySlots ? -1 : srcSlot->id, srcSlot->serial));
  314. slot->clickPressedCallback = clickPressedCallback;
  315. slot->subtitle->setText(emptySlots ? "" : srcSlot->subtitle->getText());
  316. slot->setSelectionWidth(selectionWidth);
  317. }
  318. showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::CREATURE, 0, 0);
  319. }
  320. ArtifactsAltarPanel::ArtifactsAltarPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback)
  321. {
  322. OBJECT_CONSTRUCTION;
  323. int slotNum = 0;
  324. for(auto & altarSlotPos : slotsPos)
  325. {
  326. auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(Rect(altarSlotPos, Point(44, 44)), EType::ARTIFACT, -1, slotNum));
  327. slot->clickPressedCallback = clickPressedCallback;
  328. slot->subtitle->clear();
  329. slot->subtitle->moveBy(Point(0, -1));
  330. slotNum++;
  331. }
  332. showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::ARTIFACT_TYPE, 0, 0);
  333. showcaseSlot->subtitle->moveBy(Point(0, 3));
  334. }