BattleInterfaceClasses.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * BattleInterfaceClasses.h, 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. #pragma once
  11. #include "BattleConstants.h"
  12. #include "../gui/CIntObject.h"
  13. #include "../../lib/FunctionList.h"
  14. #include "../../lib/battle/BattleHex.h"
  15. #include "../windows/CWindowObject.h"
  16. #include "../../lib/MetaString.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CGHeroInstance;
  19. struct BattleResult;
  20. struct InfoAboutHero;
  21. class CStack;
  22. namespace battle
  23. {
  24. class Unit;
  25. }
  26. VCMI_LIB_NAMESPACE_END
  27. class CAnimation;
  28. class Canvas;
  29. class BattleInterface;
  30. class CPicture;
  31. class CFilledTexture;
  32. class CButton;
  33. class CLabel;
  34. class CMultiLineLabel;
  35. class CTextBox;
  36. class CAnimImage;
  37. class TransparentFilledRectangle;
  38. class CPlayerInterface;
  39. class BattleRenderer;
  40. class VideoWidget;
  41. class QuickSpellPanel;
  42. class CPlayerBattleCallback;
  43. /// Class which shows the console at the bottom of the battle screen and manages the text of the console
  44. class BattleConsole : public CIntObject, public IStatusBar
  45. {
  46. private:
  47. const BattleInterface & owner;
  48. std::shared_ptr<CPicture> background;
  49. /// List of all texts added during battle, essentially - log of entire battle
  50. std::vector< std::string > logEntries;
  51. /// Current scrolling position, to allow showing older entries via scroll buttons
  52. int scrollPosition;
  53. /// current hover text set on mouse move, takes priority over log entries
  54. std::string hoverText;
  55. /// current text entered via in-game console, takes priority over both log entries and hover text
  56. std::string consoleText;
  57. /// if true then we are currently entering console text
  58. bool enteringText;
  59. /// splits text into individual strings for battle log
  60. std::vector<std::string> splitText(const std::string &text);
  61. /// select line(s) that will be visible in UI
  62. std::vector<std::string> getVisibleText() const;
  63. public:
  64. BattleConsole(const BattleInterface & owner, std::shared_ptr<CPicture> backgroundSource, const Point & objectPos, const Point & imagePos, const Point &size);
  65. void showAll(Canvas & to) override;
  66. void deactivate() override;
  67. void clickPressed(const Point & cursorPosition) override;
  68. bool addText(const std::string &text); //adds text at the last position; returns false if failed (e.g. text longer than 70 characters)
  69. void scrollUp(ui32 by = 1); //scrolls console up by 'by' positions
  70. void scrollDown(ui32 by = 1); //scrolls console up by 'by' positions
  71. // IStatusBar interface
  72. void write(const std::string & Text) override;
  73. void clearIfMatching(const std::string & Text) override;
  74. void clear() override;
  75. void setEnteringMode(bool on) override;
  76. void setEnteredText(const std::string & text) override;
  77. };
  78. class BattleConsoleWindow : public CWindowObject
  79. {
  80. private:
  81. std::shared_ptr<CFilledTexture> backgroundTexture;
  82. std::shared_ptr<CButton> buttonOk;
  83. std::shared_ptr<TransparentFilledRectangle> textBoxBackgroundBorder;
  84. std::shared_ptr<CTextBox> textBox;
  85. public:
  86. BattleConsoleWindow(const std::string & text);
  87. };
  88. /// Hero battle animation
  89. class BattleHero : public CIntObject
  90. {
  91. bool defender;
  92. CFunctionList<void()> phaseFinishedCallback;
  93. std::shared_ptr<CAnimation> animation;
  94. std::shared_ptr<CAnimation> flagAnimation;
  95. const CGHeroInstance * hero; //this animation's hero instance
  96. const BattleInterface & owner; //battle interface to which this animation is assigned
  97. EHeroAnimType phase; //stage of animation
  98. EHeroAnimType nextPhase; //stage of animation to be set after current phase is fully displayed
  99. float currentSpeed;
  100. float currentFrame; //frame of animation
  101. float flagCurrentFrame;
  102. void switchToNextPhase();
  103. void render(Canvas & canvas); //prints next frame of animation to to
  104. public:
  105. const CGHeroInstance * instance();
  106. void setPhase(EHeroAnimType newPhase); //sets phase of hero animation
  107. void collectRenderableObjects(BattleRenderer & renderer);
  108. void tick(uint32_t msPassed) override;
  109. float getFrame() const;
  110. void onPhaseFinished(const std::function<void()> &);
  111. void pause();
  112. void play();
  113. void heroLeftClicked();
  114. void heroRightClicked();
  115. BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender);
  116. };
  117. class QuickSpellPanelSelect : public CIntObject
  118. {
  119. private:
  120. std::shared_ptr<CFilledTexture> background;
  121. std::shared_ptr<TransparentFilledRectangle> rect;
  122. std::vector<std::shared_ptr<CButton>> buttons;
  123. QuickSpellPanel * parent;
  124. public:
  125. QuickSpellPanelSelect(QuickSpellPanel * Parent);
  126. bool wasEnabled; // was the panel opened? -> don't close window because mouse is not in area
  127. int spellSlot;
  128. };
  129. class QuickSpellPanel : public CWindowObject
  130. {
  131. private:
  132. std::shared_ptr<CFilledTexture> background;
  133. std::shared_ptr<TransparentFilledRectangle> rect;
  134. std::vector<std::shared_ptr<CButton>> buttons;
  135. std::vector<std::shared_ptr<TransparentFilledRectangle>> buttonsDisabled;
  136. std::vector<std::shared_ptr<CLabel>> labels;
  137. std::shared_ptr<QuickSpellPanelSelect> panelSelect;
  138. bool receiveEvent(const Point & position, int eventType) const override;
  139. void clickReleased(const Point & cursorPosition) override;
  140. void showPopupWindow(const Point & cursorPosition) override;
  141. void mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance) override;
  142. std::shared_ptr<CButton> initWidget;
  143. BattleInterface & owner;
  144. public:
  145. QuickSpellPanel(std::shared_ptr<CButton> initWidget, BattleInterface & owner);
  146. void create();
  147. void show(Canvas & to) override;
  148. };
  149. class HeroInfoBasicPanel : public CIntObject //extracted from InfoWindow to fit better as non-popup embed element
  150. {
  151. private:
  152. std::shared_ptr<CPicture> background;
  153. std::vector<std::shared_ptr<CLabel>> labels;
  154. std::vector<std::shared_ptr<CAnimImage>> icons;
  155. public:
  156. HeroInfoBasicPanel(const InfoAboutHero & hero, Point * position, bool initializeBackground = true);
  157. void show(Canvas & to) override;
  158. void initializeData(const InfoAboutHero & hero);
  159. void update(const InfoAboutHero & updatedInfo);
  160. };
  161. class StackInfoBasicPanel : public CIntObject
  162. {
  163. private:
  164. std::shared_ptr<CPicture> background;
  165. std::shared_ptr<CPicture> background2;
  166. std::vector<std::shared_ptr<CLabel>> labels;
  167. std::vector<std::shared_ptr<CMultiLineLabel>> labelsMultiline;
  168. std::vector<std::shared_ptr<CAnimImage>> icons;
  169. public:
  170. StackInfoBasicPanel(const CStack * stack, bool initializeBackground = true);
  171. void show(Canvas & to) override;
  172. void initializeData(const CStack * stack);
  173. void update(const CStack * updatedInfo);
  174. };
  175. class HeroInfoWindow : public CWindowObject
  176. {
  177. private:
  178. std::shared_ptr<HeroInfoBasicPanel> content;
  179. public:
  180. HeroInfoWindow(const InfoAboutHero & hero, Point * position);
  181. };
  182. struct BattleResultResources
  183. {
  184. VideoPath prologueVideo;
  185. VideoPath loopedVideo;
  186. AudioPath musicName;
  187. MetaString resultText;
  188. };
  189. /// Class which is responsible for showing the battle result window
  190. class BattleResultWindow : public WindowBase
  191. {
  192. private:
  193. std::shared_ptr<CPicture> background;
  194. std::vector<std::shared_ptr<CLabel>> labels;
  195. std::shared_ptr<CButton> exit;
  196. std::shared_ptr<CButton> repeat;
  197. std::vector<std::shared_ptr<CAnimImage>> icons;
  198. std::shared_ptr<CTextBox> description;
  199. std::shared_ptr<VideoWidget> videoPlayer;
  200. CPlayerInterface & owner;
  201. BattleResultResources getResources(const BattleResult & br);
  202. void buttonPressed(int button); //internal function for button callbacks
  203. public:
  204. BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay = false);
  205. void bExitf(); //exit button callback
  206. void bRepeatf(); //repeat button callback
  207. std::function<void(int result)> resultCallback; //callback receiving which button was pressed
  208. void activate() override;
  209. };
  210. /// Shows the stack queue
  211. class StackQueue : public CIntObject
  212. {
  213. class StackBox : public CIntObject
  214. {
  215. StackQueue * owner;
  216. std::optional<uint32_t> boundUnitID;
  217. std::shared_ptr<CPicture> background;
  218. std::shared_ptr<CAnimImage> icon;
  219. std::shared_ptr<CLabel> amount;
  220. std::shared_ptr<CAnimImage> stateIcon;
  221. std::shared_ptr<CLabel> round;
  222. std::shared_ptr<TransparentFilledRectangle> roundRect;
  223. void show(Canvas & to) override;
  224. void showAll(Canvas & to) override;
  225. void showPopupWindow(const Point & cursorPosition) override;
  226. bool isBoundUnitHighlighted() const;
  227. public:
  228. StackBox(StackQueue * owner);
  229. void setUnit(const battle::Unit * unit, size_t turn = 0, std::optional<ui32> currentTurn = std::nullopt);
  230. std::optional<uint32_t> getBoundUnitID() const;
  231. };
  232. static const int QUEUE_SIZE_BIG = 10;
  233. std::shared_ptr<CFilledTexture> background;
  234. std::vector<std::shared_ptr<StackBox>> stackBoxes;
  235. BattleInterface & owner;
  236. std::shared_ptr<CAnimation> icons;
  237. std::shared_ptr<CAnimation> stateIcons;
  238. int32_t getSiegeShooterIconID();
  239. public:
  240. const bool embedded;
  241. StackQueue(bool Embedded, BattleInterface & owner);
  242. void update();
  243. std::optional<uint32_t> getHoveredUnitIdIfAny() const;
  244. void show(Canvas & to) override;
  245. };