CMarketWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * CMarketWindow.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 "CMarketWindow.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/Shortcut.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../widgets/TextControls.h"
  16. #include "../widgets/markets/CArtifactsBuying.h"
  17. #include "../widgets/markets/CArtifactsSelling.h"
  18. #include "../widgets/markets/CFreelancerGuild.h"
  19. #include "../widgets/markets/CMarketResources.h"
  20. #include "../widgets/markets/CTransferResources.h"
  21. #include "../CGameInfo.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/mapObjects/CGTownInstance.h"
  25. #include "../../lib/mapObjects/CGMarket.h"
  26. #include "../../lib/mapObjects/CGHeroInstance.h"
  27. CMarketWindow::CMarketWindow(const IMarket * market, const CGHeroInstance * hero, const std::function<void()> & onWindowClosed, EMarketMode mode)
  28. : CStatusbarWindow(PLAYER_COLORED)
  29. , hero(hero)
  30. , windowClosedCallback(onWindowClosed)
  31. {
  32. assert(mode == EMarketMode::RESOURCE_RESOURCE || mode == EMarketMode::RESOURCE_PLAYER || mode == EMarketMode::CREATURE_RESOURCE ||
  33. mode == EMarketMode::RESOURCE_ARTIFACT || mode == EMarketMode::ARTIFACT_RESOURCE || mode == EMarketMode::ARTIFACT_EXP ||
  34. mode == EMarketMode::CREATURE_EXP);
  35. if(mode == EMarketMode::RESOURCE_RESOURCE)
  36. createMarketResources(market, hero);
  37. else if(mode == EMarketMode::RESOURCE_PLAYER)
  38. createTransferResources(market, hero);
  39. else if(mode == EMarketMode::CREATURE_RESOURCE)
  40. createFreelancersGuild(market, hero);
  41. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  42. createArtifactsBuying(market, hero);
  43. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  44. createArtifactsSelling(market, hero);
  45. else if(mode == EMarketMode::ARTIFACT_EXP)
  46. createAltarArtifacts(market, hero);
  47. else if(mode == EMarketMode::CREATURE_EXP)
  48. createAltarCreatures(market, hero);
  49. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  50. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  51. }
  52. void CMarketWindow::artifactsChanged()
  53. {
  54. //market->artifactsChanged(false);
  55. if(artsBuy)
  56. artsBuy->updateSlots();
  57. }
  58. void CMarketWindow::updateGarrisons()
  59. {
  60. if(guild)
  61. guild->updateSlots();
  62. }
  63. void CMarketWindow::resourceChanged()
  64. {
  65. if(resRes)
  66. resRes->updateSlots();
  67. if(trRes)
  68. trRes->updateSlots();
  69. }
  70. void CMarketWindow::close()
  71. {
  72. if(windowClosedCallback)
  73. windowClosedCallback();
  74. CWindowObject::close();
  75. }
  76. const CGHeroInstance * CMarketWindow::getHero() const
  77. {
  78. return hero;
  79. }
  80. void CMarketWindow::createChangeModeButtons(EMarketMode currentMode, const IMarket * market, const CGHeroInstance * hero)
  81. {
  82. auto isButton = [this, currentMode, market, hero](EMarketMode modeButton) -> bool
  83. {
  84. if(currentMode == modeButton)
  85. return false;
  86. if(!market->allowsTrade(modeButton))
  87. return false;
  88. if(modeButton == EMarketMode::RESOURCE_RESOURCE || modeButton == EMarketMode::RESOURCE_PLAYER)
  89. {
  90. if(const auto town = dynamic_cast<const CGTownInstance*>(market))
  91. return town->getOwner() == LOCPLINT->playerID;
  92. else
  93. return true;
  94. }
  95. else
  96. {
  97. return hero != nullptr;
  98. }
  99. };
  100. auto buttonPosY = 520;
  101. changeModeButtons.clear();
  102. if(isButton(EMarketMode::RESOURCE_PLAYER))
  103. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(18, buttonPosY), AnimationPath::builtin("TPMRKBU1.DEF"),
  104. CGI->generaltexth->zelp[612], std::bind(&CMarketWindow::createTransferResources, this, market, hero)));
  105. buttonPosY -= buttonHeightWithMargin;
  106. if(isButton(EMarketMode::ARTIFACT_RESOURCE))
  107. {
  108. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(18, buttonPosY), AnimationPath::builtin("TPMRKBU3.DEF"),
  109. CGI->generaltexth->zelp[613], std::bind(&CMarketWindow::createArtifactsSelling, this, market, hero)));
  110. buttonPosY -= buttonHeightWithMargin;
  111. }
  112. if(isButton(EMarketMode::CREATURE_RESOURCE))
  113. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(516, buttonPosY), AnimationPath::builtin("TPMRKBU4.DEF"),
  114. CGI->generaltexth->zelp[599], std::bind(&CMarketWindow::createFreelancersGuild, this, market, hero)));
  115. if(isButton(EMarketMode::RESOURCE_RESOURCE))
  116. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(516, buttonPosY), AnimationPath::builtin("TPMRKBU5.DEF"),
  117. CGI->generaltexth->zelp[605], std::bind(&CMarketWindow::createMarketResources, this, market, hero)));
  118. if(isButton(EMarketMode::RESOURCE_ARTIFACT))
  119. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(18, buttonPosY), AnimationPath::builtin("TPMRKBU2.DEF"),
  120. CGI->generaltexth->zelp[598], std::bind(&CMarketWindow::createArtifactsBuying, this, market, hero)));
  121. if(isButton(EMarketMode::CREATURE_EXP))
  122. {
  123. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTSACC.DEF"),
  124. CGI->generaltexth->zelp[572], std::bind(&CMarketWindow::createAltarCreatures, this, market, hero)));
  125. if(altar->hero->getAlignment() == EAlignment::GOOD)
  126. changeModeButtons.back()->block(true);
  127. }
  128. if(isButton(EMarketMode::ARTIFACT_EXP))
  129. {
  130. changeModeButtons.emplace_back(std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTART.DEF"),
  131. CGI->generaltexth->zelp[580], std::bind(&CMarketWindow::createAltarArtifacts, this, market, hero)));
  132. if(altar->hero->getAlignment() == EAlignment::EVIL)
  133. changeModeButtons.back()->block(true);
  134. }
  135. }
  136. void CMarketWindow::createInternals(EMarketMode mode, const IMarket * market, const CGHeroInstance * hero)
  137. {
  138. background->center();
  139. pos = background->pos;
  140. this->market->setRedrawParent(true);
  141. createChangeModeButtons(mode, market, hero);
  142. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  143. CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN);
  144. redraw();
  145. }
  146. void CMarketWindow::createArtifactsBuying(const IMarket * market, const CGHeroInstance * hero)
  147. {
  148. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  149. background = createBg(ImagePath::builtin("TPMRKABS.bmp"), PLAYER_COLORED);
  150. artsBuy = std::make_shared<CArtifactsBuying>(market, hero);
  151. background->center();
  152. pos = background->pos;
  153. artsBuy->setRedrawParent(true);
  154. artsBuy->moveTo(pos.topLeft());
  155. createChangeModeButtons(EMarketMode::RESOURCE_ARTIFACT, market, hero);
  156. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  157. CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN);
  158. redraw();
  159. }
  160. void CMarketWindow::createArtifactsSelling(const IMarket * market, const CGHeroInstance * hero)
  161. {
  162. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  163. artsBuy.reset();
  164. background = createBg(ImagePath::builtin("TPMRKASS.bmp"), PLAYER_COLORED);
  165. artsSel = std::make_shared<CArtifactsSelling>(market, hero);
  166. artSets.clear();
  167. addSetAndCallbacks(artsSel->getAOHset());
  168. background->center();
  169. pos = background->pos;
  170. artsSel->setRedrawParent(true);
  171. artsSel->moveTo(pos.topLeft());
  172. createChangeModeButtons(EMarketMode::ARTIFACT_RESOURCE, market, hero);
  173. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  174. CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN);
  175. redraw();
  176. }
  177. void CMarketWindow::createMarketResources(const IMarket * market, const CGHeroInstance * hero)
  178. {
  179. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  180. background = createBg(ImagePath::builtin("TPMRKRES.bmp"), PLAYER_COLORED);
  181. resRes = std::make_shared<CMarketResources>(market, hero);
  182. background->center();
  183. pos = background->pos;
  184. resRes->setRedrawParent(true);
  185. resRes->moveTo(pos.topLeft());
  186. createChangeModeButtons(EMarketMode::RESOURCE_RESOURCE, market, hero);
  187. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  188. CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN);
  189. redraw();
  190. }
  191. void CMarketWindow::createFreelancersGuild(const IMarket * market, const CGHeroInstance * hero)
  192. {
  193. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  194. background = createBg(ImagePath::builtin("TPMRKCRS.bmp"), PLAYER_COLORED);
  195. guild = std::make_shared<CFreelancerGuild>(market, hero);
  196. background->center();
  197. pos = background->pos;
  198. guild->setRedrawParent(true);
  199. guild->moveTo(pos.topLeft());
  200. createChangeModeButtons(EMarketMode::CREATURE_RESOURCE, market, hero);
  201. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  202. CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN);
  203. redraw();
  204. }
  205. void CMarketWindow::createTransferResources(const IMarket * market, const CGHeroInstance * hero)
  206. {
  207. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  208. background = createBg(ImagePath::builtin("TPMRKPTS.bmp"), PLAYER_COLORED);
  209. trRes = std::make_shared<CTransferResources>(market, hero);
  210. background->center();
  211. pos = background->pos;
  212. trRes->setRedrawParent(true);
  213. trRes->moveTo(pos.topLeft());
  214. createChangeModeButtons(EMarketMode::RESOURCE_PLAYER, market, hero);
  215. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  216. CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN);
  217. redraw();
  218. }
  219. void CMarketWindow::createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
  220. {
  221. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  222. background = createBg(ImagePath::builtin("ALTRART2.bmp"), PLAYER_COLORED);
  223. auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero);
  224. altar = altarArtifacts;
  225. artSets.clear();
  226. addSetAndCallbacks(altarArtifacts->getAOHset());
  227. background->center();
  228. pos = background->pos;
  229. altar->setRedrawParent(true);
  230. createChangeModeButtons(EMarketMode::ARTIFACT_EXP, market, hero);
  231. altar->moveTo(pos.topLeft());
  232. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  233. CGI->generaltexth->zelp[568], [this, altarArtifacts]()
  234. {
  235. altarArtifacts->putBackArtifacts();
  236. CMarketWindow::close();
  237. }, EShortcut::GLOBAL_RETURN);
  238. updateExpToLevel();
  239. redraw();
  240. }
  241. void CMarketWindow::createAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
  242. {
  243. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  244. background = createBg(ImagePath::builtin("ALTARMON.bmp"), PLAYER_COLORED);
  245. altar = std::make_shared<CAltarCreatures>(market, hero);
  246. background->center();
  247. pos = background->pos;
  248. altar->setRedrawParent(true);
  249. createChangeModeButtons(EMarketMode::CREATURE_EXP, market, hero);
  250. altar->moveTo(pos.topLeft());
  251. quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
  252. CGI->generaltexth->zelp[568], std::bind(&CMarketWindow::close, this), EShortcut::GLOBAL_RETURN);
  253. updateExpToLevel();
  254. redraw();
  255. }