StackQueue.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * StackQueue.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 "StackQueue.h"
  12. #include "BattleInterface.h"
  13. #include "BattleSiegeController.h"
  14. #include "BattleStacksController.h"
  15. #include "../GameEngine.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../render/Canvas.h"
  18. #include "../render/IFont.h"
  19. #include "../render/IRenderHandler.h"
  20. #include "../widgets/GraphicalPrimitiveCanvas.h"
  21. #include "../widgets/Images.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../windows/CCreatureWindow.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/CStack.h"
  26. #include "../../lib/battle/CPlayerBattleCallback.h"
  27. #include "../../lib/mapObjects/CGTownInstance.h"
  28. #include "../../lib/texts/TextOperations.h"
  29. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  30. : owner(owner)
  31. , embedded(Embedded)
  32. {
  33. OBJECT_CONSTRUCTION;
  34. uint32_t queueSize = QUEUE_SIZE_BIG;
  35. if(embedded)
  36. {
  37. int32_t queueSmallOutsideYOffset = 65;
  38. bool queueSmallOutside = settings["battle"]["queueSmallOutside"].Bool() && (pos.y - queueSmallOutsideYOffset) >= 0;
  39. queueSize = std::clamp(static_cast<int>(settings["battle"]["queueSmallSlots"].Float()), 1, queueSmallOutside ? ENGINE->screenDimensions().x / 41 : 19);
  40. pos.w = queueSize * 41;
  41. pos.h = 49;
  42. pos.x += parent->pos.w/2 - pos.w/2;
  43. pos.y += queueSmallOutside ? -queueSmallOutsideYOffset : 10;
  44. }
  45. else
  46. {
  47. pos.w = 800;
  48. pos.h = 85;
  49. pos.x += 0;
  50. pos.y -= pos.h;
  51. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, pos.w, pos.h));
  52. }
  53. stackBoxes.resize(queueSize);
  54. for (int i = 0; i < stackBoxes.size(); i++)
  55. {
  56. stackBoxes[i] = std::make_shared<StackBox>(this);
  57. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  58. }
  59. }
  60. void StackQueue::show(Canvas & to)
  61. {
  62. if(embedded)
  63. showAll(to);
  64. CIntObject::show(to);
  65. }
  66. void StackQueue::update()
  67. {
  68. std::vector<battle::Units> queueData;
  69. owner.getBattle()->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  70. size_t boxIndex = 0;
  71. ui32 tmpTurn = -1;
  72. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  73. {
  74. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  75. {
  76. ui32 currentTurn = owner.round + turn;
  77. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn, tmpTurn != currentTurn && owner.round != 0 && (!embedded || tmpTurn != -1) ? (std::optional<ui32>)currentTurn : std::nullopt);
  78. tmpTurn = currentTurn;
  79. }
  80. }
  81. for(; boxIndex < stackBoxes.size(); boxIndex++)
  82. stackBoxes[boxIndex]->setUnit(nullptr);
  83. }
  84. int32_t StackQueue::getSiegeShooterIconID() const
  85. {
  86. return owner.siegeController->getSiegedTown()->getFactionID().getNum();
  87. }
  88. std::optional<uint32_t> StackQueue::getHoveredUnitIdIfAny() const
  89. {
  90. for(const auto & stackBox : stackBoxes)
  91. {
  92. if(stackBox->isHovered())
  93. {
  94. return stackBox->getBoundUnitID();
  95. }
  96. }
  97. return std::nullopt;
  98. }
  99. StackQueue::StackBox::StackBox(StackQueue * owner)
  100. : CIntObject(SHOW_POPUP | HOVER)
  101. , owner(owner)
  102. {
  103. OBJECT_CONSTRUCTION;
  104. background = std::make_shared<CPicture>(ImagePath::builtin(owner->embedded ? "StackQueueSmall" : "StackQueueLarge"));
  105. pos.w = background->pos.w;
  106. pos.h = background->pos.h;
  107. if(owner->embedded)
  108. {
  109. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), 0, 0, 5, 2);
  110. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  111. roundRect = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, 2, 48), ColorRGBA(0, 0, 0, 255), ColorRGBA(0, 255, 0, 255));
  112. }
  113. else
  114. {
  115. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), 0, 0, 9, 1);
  116. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  117. roundRect = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, 15, 18), ColorRGBA(0, 0, 0, 255), ColorRGBA(241, 216, 120, 255));
  118. round = std::make_shared<CLabel>(6, 9, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  119. Point iconPos(pos.w - 16, pos.h - 16);
  120. defendIcon = std::make_shared<CPicture>(ImagePath::builtin("battle/QueueDefend"), iconPos);
  121. waitIcon = std::make_shared<CPicture>(ImagePath::builtin("battle/QueueWait"), iconPos);
  122. defendIcon->setEnabled(false);
  123. waitIcon->setEnabled(false);
  124. }
  125. roundRect->disable();
  126. }
  127. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn, std::optional<ui32> currentTurn)
  128. {
  129. if(unit)
  130. {
  131. boundUnitID = unit->unitId();
  132. background->setPlayerColor(unit->unitOwner());
  133. icon->visible = true;
  134. // temporary code for mod compatibility:
  135. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  136. // second, try to switch to icon that should be provided by mod
  137. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  138. // for 1.2 release & later next line should be moved into 'else' block
  139. icon->setFrame(unit->creatureIconIndex(), 0);
  140. if(unit->unitType()->getId() == CreatureID::ARROW_TOWERS)
  141. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  142. roundRect->setEnabled(currentTurn.has_value());
  143. if(!owner->embedded)
  144. round->setEnabled(currentTurn.has_value());
  145. amount->setText(TextOperations::formatMetric(unit->getCount(), 4));
  146. if(currentTurn && !owner->embedded)
  147. {
  148. std::string tmp = std::to_string(*currentTurn);
  149. const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL);
  150. int len = font->getStringWidth(tmp);
  151. roundRect->pos.w = len + 6;
  152. round->pos = Rect(roundRect->pos.center().x, roundRect->pos.center().y, 0, 0);
  153. round->setText(tmp);
  154. }
  155. if(!owner->embedded)
  156. {
  157. bool defended = unit->defended(turn) || (turn > 0 && unit->defended(turn - 1));
  158. bool waited = unit->waited(turn) && !defended;
  159. defendIcon->setEnabled(defended);
  160. waitIcon->setEnabled(waited);
  161. }
  162. }
  163. else
  164. {
  165. boundUnitID = std::nullopt;
  166. background->setPlayerColor(PlayerColor::NEUTRAL);
  167. icon->visible = false;
  168. icon->setFrame(0);
  169. amount->setText("");
  170. if(!owner->embedded)
  171. {
  172. defendIcon->setEnabled(false);
  173. waitIcon->setEnabled(false);
  174. }
  175. }
  176. }
  177. std::optional<uint32_t> StackQueue::StackBox::getBoundUnitID() const
  178. {
  179. return boundUnitID;
  180. }
  181. bool StackQueue::StackBox::isBoundUnitHighlighted() const
  182. {
  183. auto unitIdsToHighlight = owner->owner.stacksController->getHoveredStacksUnitIds();
  184. return vstd::contains(unitIdsToHighlight, getBoundUnitID());
  185. }
  186. void StackQueue::StackBox::showAll(Canvas & to)
  187. {
  188. CIntObject::showAll(to);
  189. if(isBoundUnitHighlighted())
  190. to.drawBorder(background->pos, Colors::CYAN, 2);
  191. }
  192. void StackQueue::StackBox::show(Canvas & to)
  193. {
  194. CIntObject::show(to);
  195. if(isBoundUnitHighlighted())
  196. to.drawBorder(background->pos, Colors::CYAN, 2);
  197. }
  198. void StackQueue::StackBox::showPopupWindow(const Point & cursorPosition)
  199. {
  200. auto stacks = owner->owner.getBattle()->battleGetAllStacks();
  201. for(const CStack * stack : stacks)
  202. if(boundUnitID.has_value() && stack->unitId() == *boundUnitID)
  203. ENGINE->windows().createAndPushWindow<CStackWindow>(stack, true);
  204. }