CGarrisonInt.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * CGarrisonInt.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 "CGarrisonInt.h"
  12. #include "Buttons.h"
  13. #include "TextControls.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../render/IImage.h"
  17. #include "../windows/CCreatureWindow.h"
  18. #include "../windows/GUIClasses.h"
  19. #include "../CGameInfo.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../../CCallback.h"
  22. #include "../../lib/ArtifactUtils.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/CCreatureHandler.h"
  25. #include "../../lib/mapObjects/CGHeroInstance.h"
  26. #include "../../lib/TextOperations.h"
  27. #include "../../lib/gameState/CGameState.h"
  28. RadialMenuItem::RadialMenuItem(std::string imageName, std::function<void()> callback)
  29. : callback(callback)
  30. {
  31. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  32. image = IImage::createFromFile("radialMenu/" + imageName);
  33. picture = std::make_shared<CPicture>(image, Point(0,0));
  34. pos = picture->pos;
  35. }
  36. bool RadialMenuItem::isInside(const Point & position)
  37. {
  38. Point localPosition = position - pos.topLeft();
  39. return !image->isTransparent(localPosition);
  40. }
  41. void RadialMenuItem::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  42. {
  43. }
  44. void RadialMenuItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  45. {
  46. }
  47. RadialMenu::RadialMenu(CGarrisonInt * army, CGarrisonSlot * slot)
  48. {
  49. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  50. bool isExchange = army->armedObjs[0] && army->armedObjs[1]; // two armies exist
  51. addItem(ITEM_NW, "stackMerge", [=](){army->bulkMergeStacks(slot);});
  52. addItem(ITEM_NE, "stackInfo", [=](){slot->viewInfo();});
  53. addItem(ITEM_WW, "stackSplitOne", [=](){slot->splitIntoParts(slot->upg, 1); });
  54. addItem(ITEM_EE, "stackSplitEqual", [=](){army->bulkSmartSplitStack(slot);});
  55. if (isExchange)
  56. {
  57. addItem(ITEM_SW, "stackMove", [=](){army->moveStackToAnotherArmy(slot);});
  58. //FIXME: addItem(ITEM_SE, "stackSplitDialog", [=](){slot->split();});
  59. }
  60. for(const auto & item : items)
  61. pos = pos.include(item->pos);
  62. }
  63. void RadialMenu::addItem(const Point & offset, const std::string & path, std::function<void()> callback )
  64. {
  65. auto item = std::make_shared<RadialMenuItem>(path, callback);
  66. item->moveBy(offset);
  67. items.push_back(item);
  68. }
  69. void RadialMenu::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  70. {
  71. }
  72. void RadialMenu::show(Canvas & to)
  73. {
  74. showAll(to);
  75. }
  76. void RadialMenu::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  77. {
  78. if (!on)
  79. {
  80. for(const auto & item : items)
  81. {
  82. if (item->isInside(finalPosition))
  83. {
  84. item->callback();
  85. return;
  86. }
  87. }
  88. }
  89. }
  90. void CGarrisonSlot::setHighlight(bool on)
  91. {
  92. if (on)
  93. selectionImage->enable(); //show
  94. else
  95. selectionImage->disable(); //hide
  96. }
  97. void CGarrisonSlot::hover (bool on)
  98. {
  99. ////Hoverable::hover(on);
  100. if(on)
  101. {
  102. std::string temp;
  103. if(creature)
  104. {
  105. if(owner->getSelection())
  106. {
  107. if(owner->getSelection() == this)
  108. {
  109. temp = CGI->generaltexth->tcommands[4]; //View %s
  110. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  111. }
  112. else if (owner->getSelection()->creature == creature)
  113. {
  114. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  115. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  116. }
  117. else if (owner->getSelection()->creature)
  118. {
  119. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  120. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  121. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  122. }
  123. else
  124. {
  125. logGlobal->warn("Warning - shouldn't be - highlighted void slot %d", owner->getSelection()->ID.getNum());
  126. logGlobal->warn("Highlighted set to nullptr");
  127. owner->selectSlot(nullptr);
  128. }
  129. }
  130. else
  131. {
  132. const bool isHeroOnMap = owner->armedObjs[0] // Hero is not a visitor and not a garrison defender
  133. && owner->armedObjs[0]->ID == Obj::HERO
  134. && (!owner->armedObjs[1] || owner->armedObjs[1]->ID == Obj::HERO) // one hero or we are in the Heroes exchange window
  135. && !(static_cast<const CGHeroInstance*>(owner->armedObjs[0]))->inTownGarrison;
  136. if(isHeroOnMap)
  137. {
  138. temp = CGI->generaltexth->allTexts[481]; //Select %s
  139. }
  140. else if(upg == EGarrisonType::UP)
  141. {
  142. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  143. }
  144. else // Hero is visiting some object (town, mine, etc)
  145. {
  146. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  147. }
  148. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  149. }
  150. }
  151. else
  152. {
  153. if(owner->getSelection())
  154. {
  155. const CArmedInstance *highl = owner->getSelection()->getObj();
  156. if( highl->needsLastStack() //we are moving stack from hero's
  157. && highl->stacksCount() == 1 //it's only stack
  158. && owner->getSelection()->upg != upg //we're moving it to the other garrison
  159. )
  160. {
  161. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  162. }
  163. else
  164. {
  165. temp = CGI->generaltexth->tcommands[6]; //Move %s
  166. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  167. }
  168. }
  169. else
  170. {
  171. temp = CGI->generaltexth->tcommands[11]; //Empty
  172. }
  173. }
  174. GH.statusbar()->write(temp);
  175. }
  176. else
  177. {
  178. GH.statusbar()->clear();
  179. }
  180. }
  181. const CArmedInstance * CGarrisonSlot::getObj() const
  182. {
  183. return owner->armedObjs[upg];
  184. }
  185. /// @return Whether the unit in the slot belongs to the current player.
  186. bool CGarrisonSlot::our() const
  187. {
  188. return owner->owned[upg];
  189. }
  190. /// @return Whether the unit in the slot belongs to an ally but not to the current player.
  191. bool CGarrisonSlot::ally() const
  192. {
  193. if(!getObj())
  194. return false;
  195. return PlayerRelations::ALLIES == LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, getObj()->tempOwner);
  196. }
  197. std::function<void()> CGarrisonSlot::getDismiss() const
  198. {
  199. const bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID
  200. && (getObj()->stacksCount() > 1 ||
  201. !getObj()->needsLastStack());
  202. return canDismiss ? [=]()
  203. {
  204. LOCPLINT->cb->dismissCreature(getObj(), ID);
  205. } : (std::function<void()>)nullptr;
  206. }
  207. /// The creature slot has been clicked twice, therefore the creature info should be shown
  208. /// @return Whether the view should be refreshed
  209. bool CGarrisonSlot::viewInfo()
  210. {
  211. UpgradeInfo pom;
  212. LOCPLINT->cb->fillUpgradeInfo(getObj(), ID, pom);
  213. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  214. std::function<void(CreatureID)> upgr = nullptr;
  215. auto dism = getDismiss();
  216. if(canUpgrade) upgr = [=] (CreatureID newID) { LOCPLINT->cb->upgradeCreature(getObj(), ID, newID); };
  217. owner->selectSlot(nullptr);
  218. owner->setSplittingMode(false);
  219. for(auto & elem : owner->splitButtons)
  220. elem->block(true);
  221. redraw();
  222. GH.windows().createAndPushWindow<CStackWindow>(myStack, dism, pom, upgr);
  223. return true;
  224. }
  225. /// The selection is empty, therefore the creature should be moved
  226. /// @return Whether the view should be refreshed
  227. bool CGarrisonSlot::highlightOrDropArtifact()
  228. {
  229. bool artSelected = false;
  230. if (auto chw = GH.windows().topWindow<CWindowWithArtifacts>()) //dirty solution
  231. {
  232. const auto pickedArtInst = chw->getPickedArtifact();
  233. if(pickedArtInst)
  234. {
  235. const auto * srcHero = chw->getHeroPickedArtifact();
  236. artSelected = true;
  237. if (myStack) // try dropping the artifact only if the slot isn't empty
  238. {
  239. ArtifactLocation src(srcHero, ArtifactPosition::TRANSITION_POS);
  240. ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
  241. if(pickedArtInst->canBePutAt(dst, true))
  242. { //equip clicked stack
  243. if(dst.getArt())
  244. {
  245. //creature can wear only one active artifact
  246. //if we are placing a new one, the old one will be returned to the hero's backpack
  247. LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero,
  248. ArtifactUtils::getArtBackpackPosition(srcHero, dst.getArt()->getTypeId())));
  249. }
  250. LOCPLINT->cb->swapArtifacts(src, dst);
  251. }
  252. }
  253. }
  254. }
  255. if (!artSelected && creature)
  256. {
  257. owner->selectSlot(this);
  258. if(creature)
  259. {
  260. for(auto & elem : owner->splitButtons)
  261. elem->block(!our());
  262. }
  263. }
  264. redraw();
  265. return true;
  266. }
  267. /// The creature is only being partially moved
  268. /// @return Whether the view should be refreshed
  269. bool CGarrisonSlot::split()
  270. {
  271. const CGarrisonSlot * selection = owner->getSelection();
  272. owner->setSplittingMode(false);
  273. int minLeft=0, minRight=0;
  274. if(upg != selection->upg) // not splitting within same army
  275. {
  276. if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
  277. && selection->getObj()->needsLastStack() )
  278. {
  279. minLeft = 1;
  280. }
  281. // destination army can't be emptied, unless we're rebalancing two stacks of same creature
  282. if(getObj()->stacksCount() == 1
  283. && selection->creature == creature
  284. && getObj()->needsLastStack() )
  285. {
  286. minRight = 1;
  287. }
  288. }
  289. int countLeft = selection->myStack ? selection->myStack->count : 0;
  290. int countRight = myStack ? myStack->count : 0;
  291. auto splitFunctor = [this, selection](int amountLeft, int amountRight)
  292. {
  293. owner->splitStacks(selection, owner->armedObjs[upg], ID, amountRight);
  294. };
  295. GH.windows().createAndPushWindow<CSplitWindow>(selection->creature, splitFunctor, minLeft, minRight, countLeft, countRight);
  296. return true;
  297. }
  298. /// If certain creates cannot be moved, the selection should change
  299. /// Force reselection in these cases
  300. /// * When attempting to take creatures from ally
  301. /// * When attempting to swap creatures with an ally
  302. /// * When attempting to take unremovable units
  303. /// @return Whether reselection must be done
  304. bool CGarrisonSlot::mustForceReselection() const
  305. {
  306. const CGarrisonSlot * selection = owner->getSelection();
  307. bool withAlly = selection->our() ^ our();
  308. if (!creature || !selection->creature)
  309. return false;
  310. // Attempt to take creatures from ally (select theirs first)
  311. if (!selection->our())
  312. return true;
  313. // Attempt to swap creatures with ally (select ours first)
  314. if (selection->creature != creature && withAlly)
  315. return true;
  316. if (!owner->removableUnits)
  317. {
  318. if (selection->upg == EGarrisonType::UP)
  319. return true;
  320. else
  321. return creature || upg == EGarrisonType::UP;
  322. }
  323. return false;
  324. }
  325. void CGarrisonSlot::showPopupWindow(const Point & cursorPosition)
  326. {
  327. if(creature)
  328. {
  329. GH.windows().createAndPushWindow<CStackWindow>(myStack, true);
  330. }
  331. }
  332. void CGarrisonSlot::clickPressed(const Point & cursorPosition)
  333. {
  334. bool refr = false;
  335. const CGarrisonSlot * selection = owner->getSelection();
  336. if(!selection)
  337. {
  338. refr = highlightOrDropArtifact(); // Affects selection
  339. handleSplittingShortcuts();
  340. }
  341. else if(selection == this)
  342. {
  343. if(!handleSplittingShortcuts())
  344. refr = viewInfo(); // Affects selection
  345. }
  346. // Re-highlight if troops aren't removable or not ours.
  347. else if (mustForceReselection())
  348. {
  349. if(creature)
  350. owner->selectSlot(this);
  351. redraw();
  352. refr = true;
  353. }
  354. else
  355. {
  356. const CArmedInstance * selectedObj = owner->armedObjs[selection->upg];
  357. bool lastHeroStackSelected = false;
  358. if(selectedObj->stacksCount() == 1
  359. && owner->getSelection()->upg != upg
  360. && selectedObj->needsLastStack())
  361. {
  362. lastHeroStackSelected = true;
  363. }
  364. if((owner->getSplittingMode() || GH.isKeyboardShiftDown()) // split window
  365. && (!creature || creature == selection->creature))
  366. {
  367. refr = split();
  368. }
  369. else if(!creature && lastHeroStackSelected) // split all except last creature
  370. LOCPLINT->cb->splitStack(selectedObj, owner->armedObjs[upg], selection->ID, ID, selection->myStack->count - 1);
  371. else if(creature != selection->creature) // swap
  372. LOCPLINT->cb->swapCreatures(owner->armedObjs[upg], selectedObj, ID, selection->ID);
  373. else if(lastHeroStackSelected) // merge last stack to other hero stack
  374. refr = split();
  375. else // merge
  376. LOCPLINT->cb->mergeStacks(selectedObj, owner->armedObjs[upg], selection->ID, ID);
  377. }
  378. if(refr)
  379. {
  380. // Refresh Statusbar
  381. hover(false);
  382. hover(true);
  383. }
  384. }
  385. void CGarrisonSlot::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  386. {
  387. assert(radialMenu);
  388. if (radialMenu)
  389. radialMenu->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  390. }
  391. void CGarrisonSlot::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  392. {
  393. if (on)
  394. {
  395. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  396. radialMenu = std::make_shared<RadialMenu>(owner, this);
  397. radialMenu->center(pos.center());
  398. auto topParent = parent;
  399. while (topParent->parent)
  400. topParent = topParent->parent;
  401. // Add radial menu to current window / topmost parent for proper rendering order
  402. topParent->addChild(radialMenu.get(), false);
  403. }
  404. else
  405. {
  406. radialMenu->gesture(on, initialPosition, finalPosition);
  407. radialMenu.reset();
  408. }
  409. GH.windows().totalRedraw();
  410. }
  411. void CGarrisonSlot::update()
  412. {
  413. if(getObj() != nullptr)
  414. {
  415. addUsedEvents(LCLICK | SHOW_POPUP | GESTURE | HOVER);
  416. myStack = getObj()->getStackPtr(ID);
  417. creature = myStack ? myStack->type : nullptr;
  418. }
  419. else
  420. {
  421. removeUsedEvents(LCLICK | SHOW_POPUP | GESTURE | HOVER);
  422. myStack = nullptr;
  423. creature = nullptr;
  424. }
  425. if(creature)
  426. {
  427. creatureImage->enable();
  428. creatureImage->setFrame(creature->getIconIndex());
  429. stackCount->enable();
  430. stackCount->setText(TextOperations::formatMetric(myStack->count, 4));
  431. }
  432. else
  433. {
  434. creatureImage->disable();
  435. stackCount->disable();
  436. }
  437. }
  438. CGarrisonSlot::CGarrisonSlot(CGarrisonInt * Owner, int x, int y, SlotID IID, CGarrisonSlot::EGarrisonType Upg, const CStackInstance * creature_)
  439. : ID(IID),
  440. owner(Owner),
  441. myStack(creature_),
  442. creature(creature_ ? creature_->type : nullptr),
  443. upg(Upg)
  444. {
  445. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  446. pos.x += x;
  447. pos.y += y;
  448. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  449. creatureImage = std::make_shared<CAnimImage>(imgName, 0);
  450. creatureImage->disable();
  451. selectionImage = std::make_shared<CAnimImage>(imgName, 1);
  452. selectionImage->disable();
  453. if(Owner->smallIcons)
  454. {
  455. pos.w = 32;
  456. pos.h = 32;
  457. }
  458. else
  459. {
  460. pos.w = 58;
  461. pos.h = 64;
  462. }
  463. int labelPosW = pos.w;
  464. int labelPosH = pos.h;
  465. if(Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS) //labels under icon
  466. {
  467. labelPosW = pos.w / 2 + 1;
  468. labelPosH += 7;
  469. }
  470. ETextAlignment labelAlignment = Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS
  471. ? ETextAlignment::CENTER
  472. : ETextAlignment::BOTTOMRIGHT;
  473. stackCount = std::make_shared<CLabel>(labelPosW, labelPosH, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, labelAlignment, Colors::WHITE);
  474. update();
  475. }
  476. void CGarrisonSlot::splitIntoParts(CGarrisonSlot::EGarrisonType type, int amount)
  477. {
  478. auto empty = owner->getEmptySlot(type);
  479. if(empty == SlotID())
  480. return;
  481. owner->splitStacks(this, owner->armedObjs[type], empty, amount);
  482. }
  483. bool CGarrisonSlot::handleSplittingShortcuts()
  484. {
  485. const bool isAlt = GH.isKeyboardAltDown();
  486. const bool isLShift = GH.isKeyboardShiftDown();
  487. const bool isLCtrl = GH.isKeyboardCtrlDown();
  488. if(!isAlt && !isLShift && !isLCtrl)
  489. return false; // This is only case when return false
  490. auto selected = owner->getSelection();
  491. if(!selected)
  492. return true; // Some Shortcusts are pressed but there are no appropriate actions
  493. auto units = selected->myStack->count;
  494. if(units < 1)
  495. return true;
  496. if (isLShift && isLCtrl && isAlt)
  497. {
  498. owner->bulkMoveArmy(selected);
  499. }
  500. else if(isLCtrl && isAlt)
  501. {
  502. owner->moveStackToAnotherArmy(selected);
  503. }
  504. else if(isLShift && isAlt)
  505. {
  506. auto dismiss = getDismiss();
  507. if(dismiss)
  508. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], dismiss, nullptr);
  509. }
  510. else if(isAlt)
  511. {
  512. owner->bulkMergeStacks(selected);
  513. }
  514. else
  515. {
  516. if(units <= 1)
  517. return true;
  518. if(isLCtrl && isLShift)
  519. owner->bulkSplitStack(selected);
  520. else if(isLShift)
  521. owner->bulkSmartSplitStack(selected);
  522. else
  523. splitIntoParts(selected->upg, 1); // LCtrl
  524. }
  525. return true;
  526. }
  527. void CGarrisonInt::addSplitBtn(std::shared_ptr<CButton> button)
  528. {
  529. addChild(button.get());
  530. button->recActions &= ~DISPOSE;
  531. splitButtons.push_back(button);
  532. button->block(getSelection() == nullptr);
  533. }
  534. void CGarrisonInt::createSlots()
  535. {
  536. int distance = interx + (smallIcons ? 32 : 58);
  537. for(int i = 0; i < 2; i++)
  538. {
  539. std::vector<std::shared_ptr<CGarrisonSlot>> garrisonSlots;
  540. garrisonSlots.resize(7);
  541. if(armedObjs[i])
  542. {
  543. for(auto & elem : armedObjs[i]->Slots())
  544. {
  545. garrisonSlots[elem.first.getNum()] = std::make_shared<CGarrisonSlot>(this, i*garOffset.x + (elem.first.getNum()*distance), i*garOffset.y, elem.first, static_cast<CGarrisonSlot::EGarrisonType>(i), elem.second);
  546. }
  547. }
  548. for(int j = 0; j < 7; j++)
  549. {
  550. if(!garrisonSlots[j])
  551. garrisonSlots[j] = std::make_shared<CGarrisonSlot>(this, i*garOffset.x + (j*distance), i*garOffset.y, SlotID(j), static_cast<CGarrisonSlot::EGarrisonType>(i), nullptr);
  552. if(layout == ESlotsLayout::TWO_ROWS && j >= 4)
  553. {
  554. garrisonSlots[j]->moveBy(Point(-126, 37));
  555. }
  556. else if(layout == ESlotsLayout::REVERSED_TWO_ROWS)
  557. {
  558. if(j >= 3)
  559. {
  560. garrisonSlots[j]->moveBy(Point(-90, 49));
  561. }
  562. else
  563. {
  564. garrisonSlots[j]->moveBy(Point(36, 0));
  565. }
  566. }
  567. }
  568. vstd::concatenate(availableSlots, garrisonSlots);
  569. }
  570. }
  571. void CGarrisonInt::recreateSlots()
  572. {
  573. selectSlot(nullptr);
  574. setSplittingMode(false);
  575. for(auto & elem : splitButtons)
  576. elem->block(true);
  577. for(auto slot : availableSlots)
  578. slot->update();
  579. }
  580. void CGarrisonInt::splitClick()
  581. {
  582. if(!getSelection())
  583. return;
  584. setSplittingMode(!getSplittingMode());
  585. redraw();
  586. }
  587. void CGarrisonInt::splitStacks(const CGarrisonSlot * from, const CArmedInstance * armyDest, SlotID slotDest, int amount )
  588. {
  589. LOCPLINT->cb->splitStack(armedObjs[from->upg], armyDest, from->ID, slotDest, amount);
  590. }
  591. bool CGarrisonInt::checkSelected(const CGarrisonSlot * selected, TQuantity min) const
  592. {
  593. return selected && selected->myStack && selected->myStack->count > min && selected->creature;
  594. }
  595. void CGarrisonInt::moveStackToAnotherArmy(const CGarrisonSlot * selected)
  596. {
  597. if(!checkSelected(selected))
  598. return;
  599. const auto srcArmyType = selected->upg;
  600. const auto destArmyType = srcArmyType == CGarrisonSlot::UP
  601. ? CGarrisonSlot::DOWN
  602. : CGarrisonSlot::UP;
  603. auto srcArmy = armedObjs[srcArmyType];
  604. auto destArmy = armedObjs[destArmyType];
  605. if(!destArmy)
  606. return;
  607. auto destSlot = destArmy->getSlotFor(selected->creature);
  608. if(destSlot == SlotID())
  609. return;
  610. const auto srcSlot = selected->ID;
  611. const bool isDestSlotEmpty = !destArmy->getStackCount(destSlot);
  612. if(isDestSlotEmpty && !destArmy->getStackCount(srcSlot))
  613. destSlot = srcSlot; // Same place is more preferable
  614. const bool isLastStack = srcArmy->stacksCount() == 1 && srcArmy->needsLastStack();
  615. auto srcAmount = selected->myStack->count - (isLastStack ? 1 : 0);
  616. if(!srcAmount)
  617. return;
  618. if(!isDestSlotEmpty || isLastStack)
  619. {
  620. srcAmount += destArmy->getStackCount(destSlot); // Due to 'split' implementation in the 'CGameHandler::arrangeStacks'
  621. LOCPLINT->cb->splitStack(srcArmy, destArmy, srcSlot, destSlot, srcAmount);
  622. }
  623. else
  624. {
  625. LOCPLINT->cb->swapCreatures(srcArmy, destArmy, srcSlot, destSlot);
  626. }
  627. }
  628. void CGarrisonInt::bulkMoveArmy(const CGarrisonSlot * selected)
  629. {
  630. if(!checkSelected(selected))
  631. return;
  632. const auto srcArmyType = selected->upg;
  633. const auto destArmyType = (srcArmyType == CGarrisonSlot::UP)
  634. ? CGarrisonSlot::DOWN
  635. : CGarrisonSlot::UP;
  636. auto srcArmy = armedObjs[srcArmyType];
  637. auto destArmy = armedObjs[destArmyType];
  638. if(!destArmy)
  639. return;
  640. const auto srcSlot = selected->ID;
  641. LOCPLINT->cb->bulkMoveArmy(srcArmy->id, destArmy->id, srcSlot);
  642. }
  643. void CGarrisonInt::bulkMergeStacks(const CGarrisonSlot * selected)
  644. {
  645. if(!checkSelected(selected))
  646. return;
  647. const auto type = selected->upg;
  648. if(!armedObjs[type]->hasCreatureSlots(selected->creature, selected->ID))
  649. return;
  650. LOCPLINT->cb->bulkMergeStacks(armedObjs[type]->id, selected->ID);
  651. }
  652. void CGarrisonInt::bulkSplitStack(const CGarrisonSlot * selected)
  653. {
  654. if(!checkSelected(selected, 1)) // check if > 1
  655. return;
  656. const auto type = selected->upg;
  657. if(!hasEmptySlot(type))
  658. return;
  659. LOCPLINT->cb->bulkSplitStack(armedObjs[type]->id, selected->ID);
  660. }
  661. void CGarrisonInt::bulkSmartSplitStack(const CGarrisonSlot * selected)
  662. {
  663. if(!checkSelected(selected, 1))
  664. return;
  665. const auto type = selected->upg;
  666. // Do not disturb the server if the creature is already balanced
  667. if(!hasEmptySlot(type) && armedObjs[type]->isCreatureBalanced(selected->creature))
  668. return;
  669. LOCPLINT->cb->bulkSmartSplitStack(armedObjs[type]->id, selected->ID);
  670. }
  671. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point & garsOffset, const CArmedInstance * s1, const CArmedInstance * s2, bool _removableUnits, bool smallImgs, ESlotsLayout _layout)
  672. : highlighted(nullptr)
  673. , inSplittingMode(false)
  674. , interx(inx)
  675. , garOffset(garsOffset)
  676. , smallIcons(smallImgs)
  677. , removableUnits(_removableUnits)
  678. , layout(_layout)
  679. {
  680. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  681. setArmy(s1, false);
  682. setArmy(s2, true);
  683. pos.x += x;
  684. pos.y += y;
  685. createSlots();
  686. }
  687. const CGarrisonSlot * CGarrisonInt::getSelection() const
  688. {
  689. return highlighted;
  690. }
  691. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  692. {
  693. if(slot != highlighted)
  694. {
  695. if(highlighted)
  696. highlighted->setHighlight(false);
  697. highlighted = slot;
  698. for(auto button : splitButtons)
  699. button->block(highlighted == nullptr || !slot->our());
  700. if(highlighted)
  701. highlighted->setHighlight(true);
  702. }
  703. }
  704. void CGarrisonInt::setSplittingMode(bool on)
  705. {
  706. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  707. if(inSplittingMode || on)
  708. {
  709. for(auto slot : availableSlots)
  710. {
  711. if(slot.get() != getSelection())
  712. slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  713. }
  714. inSplittingMode = on;
  715. }
  716. }
  717. bool CGarrisonInt::getSplittingMode()
  718. {
  719. return inSplittingMode;
  720. }
  721. SlotID CGarrisonInt::getEmptySlot(CGarrisonSlot::EGarrisonType type) const
  722. {
  723. assert(armedObjs[type]);
  724. return armedObjs[type] ? armedObjs[type]->getFreeSlot() : SlotID();
  725. }
  726. bool CGarrisonInt::hasEmptySlot(CGarrisonSlot::EGarrisonType type) const
  727. {
  728. return getEmptySlot(type) != SlotID();
  729. }
  730. void CGarrisonInt::setArmy(const CArmedInstance * army, bool bottomGarrison)
  731. {
  732. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  733. armedObjs[bottomGarrison] = army;
  734. }