CTradeBase.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "../gui/CGuiHandler.h"
  13. #include "../render/Canvas.h"
  14. #include "../widgets/TextControls.h"
  15. #include "../windows/InfoWindows.h"
  16. #include "../CGameInfo.h"
  17. #include "../../lib/CGeneralTextHandler.h"
  18. #include "../../lib/mapObjects/CGHeroInstance.h"
  19. CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial)
  20. : CIntObject(LCLICK | HOVER | SHOW_POPUP, pos)
  21. , type(EType(-1)) // set to invalid, will be corrected in setType
  22. , id(ID)
  23. , serial(Serial)
  24. , left(Left)
  25. {
  26. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  27. downSelection = false;
  28. hlp = nullptr;
  29. setType(Type);
  30. if(image)
  31. {
  32. this->pos.w = image->pos.w;
  33. this->pos.h = image->pos.h;
  34. }
  35. }
  36. void CTradeableItem::setType(EType newType)
  37. {
  38. if(type != newType)
  39. {
  40. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  41. type = newType;
  42. if(getIndex() < 0)
  43. {
  44. image = std::make_shared<CAnimImage>(getFilename(), 0);
  45. image->disable();
  46. }
  47. else
  48. {
  49. image = std::make_shared<CAnimImage>(getFilename(), getIndex());
  50. }
  51. }
  52. }
  53. void CTradeableItem::setID(int newID)
  54. {
  55. if(id != newID)
  56. {
  57. id = newID;
  58. if(image)
  59. {
  60. int index = getIndex();
  61. if(index < 0)
  62. image->disable();
  63. else
  64. {
  65. image->enable();
  66. image->setFrame(index);
  67. }
  68. }
  69. }
  70. }
  71. AnimationPath CTradeableItem::getFilename()
  72. {
  73. switch(type)
  74. {
  75. case RESOURCE:
  76. return AnimationPath::builtin("RESOURCE");
  77. case PLAYER:
  78. return AnimationPath::builtin("CREST58");
  79. case ARTIFACT_TYPE:
  80. case ARTIFACT_PLACEHOLDER:
  81. case ARTIFACT_INSTANCE:
  82. return AnimationPath::builtin("artifact");
  83. case CREATURE:
  84. return AnimationPath::builtin("TWCRPORT");
  85. default:
  86. return {};
  87. }
  88. }
  89. int CTradeableItem::getIndex()
  90. {
  91. if(id < 0)
  92. return -1;
  93. switch(type)
  94. {
  95. case RESOURCE:
  96. case PLAYER:
  97. return id;
  98. case ARTIFACT_TYPE:
  99. case ARTIFACT_INSTANCE:
  100. case ARTIFACT_PLACEHOLDER:
  101. return CGI->artifacts()->getByIndex(id)->getIconIndex();
  102. case CREATURE:
  103. return CGI->creatures()->getByIndex(id)->getIconIndex();
  104. default:
  105. return -1;
  106. }
  107. }
  108. void CTradeableItem::showAll(Canvas & to)
  109. {
  110. Point posToBitmap;
  111. Point posToSubCenter;
  112. switch (type)
  113. {
  114. case RESOURCE:
  115. posToBitmap = Point(19, 9);
  116. posToSubCenter = Point(36, 59);
  117. break;
  118. case CREATURE_PLACEHOLDER:
  119. case CREATURE:
  120. posToSubCenter = Point(29, 77);
  121. break;
  122. case PLAYER:
  123. posToSubCenter = Point(31, 76);
  124. break;
  125. case ARTIFACT_PLACEHOLDER:
  126. case ARTIFACT_INSTANCE:
  127. posToSubCenter = Point(19, 54);
  128. if (downSelection)
  129. posToSubCenter.y += 8;
  130. break;
  131. case ARTIFACT_TYPE:
  132. posToSubCenter = Point(19, 58);
  133. break;
  134. }
  135. if(image)
  136. {
  137. image->moveTo(pos.topLeft() + posToBitmap);
  138. CIntObject::showAll(to);
  139. }
  140. to.drawText(pos.topLeft() + posToSubCenter, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, subtitle);
  141. }
  142. void CTradeableItem::clickPressed(const Point & cursorPosition)
  143. {
  144. if(clickPressedCallback)
  145. clickPressedCallback(shared_from_this());
  146. }
  147. void CTradeableItem::showAllAt(const Point & dstPos, const std::string & customSub, Canvas & to)
  148. {
  149. Rect oldPos = pos;
  150. std::string oldSub = subtitle;
  151. downSelection = true;
  152. moveTo(dstPos);
  153. subtitle = customSub;
  154. showAll(to);
  155. downSelection = false;
  156. moveTo(oldPos.topLeft());
  157. subtitle = oldSub;
  158. }
  159. void CTradeableItem::hover(bool on)
  160. {
  161. if(!on)
  162. {
  163. GH.statusbar()->clear();
  164. return;
  165. }
  166. switch(type)
  167. {
  168. case CREATURE:
  169. case CREATURE_PLACEHOLDER:
  170. GH.statusbar()->write(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->getNamePluralTranslated()));
  171. break;
  172. case ARTIFACT_PLACEHOLDER:
  173. if(id < 0)
  174. GH.statusbar()->write(CGI->generaltexth->zelp[582].first);
  175. else
  176. GH.statusbar()->write(CGI->artifacts()->getByIndex(id)->getNameTranslated());
  177. break;
  178. }
  179. }
  180. void CTradeableItem::showPopupWindow(const Point & cursorPosition)
  181. {
  182. switch(type)
  183. {
  184. case CREATURE:
  185. case CREATURE_PLACEHOLDER:
  186. break;
  187. case ARTIFACT_TYPE:
  188. case ARTIFACT_PLACEHOLDER:
  189. //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.
  190. if (id >= 0)
  191. CRClickPopup::createAndPush(CGI->artifacts()->getByIndex(id)->getDescriptionTranslated());
  192. break;
  193. }
  194. }
  195. std::string CTradeableItem::getName(int number) const
  196. {
  197. switch(type)
  198. {
  199. case PLAYER:
  200. return CGI->generaltexth->capColors[id];
  201. case RESOURCE:
  202. return CGI->generaltexth->restypes[id];
  203. case CREATURE:
  204. if (number == 1)
  205. return CGI->creh->objects[id]->getNameSingularTranslated();
  206. else
  207. return CGI->creh->objects[id]->getNamePluralTranslated();
  208. case ARTIFACT_TYPE:
  209. case ARTIFACT_INSTANCE:
  210. return CGI->artifacts()->getByIndex(id)->getNameTranslated();
  211. }
  212. logGlobal->error("Invalid trade item type: %d", (int)type);
  213. return "";
  214. }
  215. const CArtifactInstance * CTradeableItem::getArtInstance() const
  216. {
  217. switch(type)
  218. {
  219. case ARTIFACT_PLACEHOLDER:
  220. case ARTIFACT_INSTANCE:
  221. return hlp;
  222. default:
  223. return nullptr;
  224. }
  225. }
  226. void CTradeableItem::setArtInstance(const CArtifactInstance * art)
  227. {
  228. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  229. hlp = art;
  230. if(art)
  231. setID(art->artType->getId());
  232. else
  233. setID(-1);
  234. }
  235. SResourcesPanel::SResourcesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, updatePanelFunctor updateSubtitles)
  236. : updateSubtitles(updateSubtitles)
  237. {
  238. assert(resourcesForTrade.size() == slotsPos.size());
  239. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  240. for(const auto & res : resourcesForTrade)
  241. {
  242. slots.emplace_back(std::make_shared<CTradeableItem>(slotsPos[res.num], EType::RESOURCE, res.num, true, res.num));
  243. slots.back()->clickPressedCallback = clickPressedCallback;
  244. slots.back()->pos.w = 69; slots.back()->pos.h = 66;
  245. }
  246. }
  247. void SResourcesPanel::updateSlots()
  248. {
  249. if(updateSubtitles)
  250. updateSubtitles();
  251. }
  252. CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero)
  253. : market(market)
  254. , hero(hero)
  255. {
  256. }
  257. void CTradeBase::removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  258. {
  259. for(auto item : toRemove)
  260. removeItem(item);
  261. }
  262. void CTradeBase::removeItem(std::shared_ptr<CTradeableItem> item)
  263. {
  264. items[item->left] -= item;
  265. if(hRight == item)
  266. hRight.reset();
  267. }
  268. void CTradeBase::getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  269. {
  270. for(auto item : items[1])
  271. if(!hero->getStackCount(SlotID(item->serial)))
  272. toRemove.insert(item);
  273. }