TradePanels.cpp 11 KB

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