BattleInterfaceClasses.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. class CPlayerBattleCallback;
  23. namespace battle
  24. {
  25. class Unit;
  26. }
  27. VCMI_LIB_NAMESPACE_END
  28. class CAnimation;
  29. class Canvas;
  30. class BattleInterface;
  31. class CPicture;
  32. class CFilledTexture;
  33. class CButton;
  34. class CLabel;
  35. class CMultiLineLabel;
  36. class CTextBox;
  37. class CAnimImage;
  38. class TransparentFilledRectangle;
  39. class CPlayerInterface;
  40. class BattleRenderer;
  41. class VideoWidget;
  42. class QuickSpellPanel;
  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 QuickSpellPanel : 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. std::vector<std::shared_ptr<TransparentFilledRectangle>> buttonsDisabled;
  124. std::vector<std::shared_ptr<CLabel>> labels;
  125. BattleInterface & owner;
  126. public:
  127. bool isEnabled; // isActive() is not working on multiple conditions, because of this we need a seperate flag
  128. QuickSpellPanel(BattleInterface & owner);
  129. void create();
  130. void show(Canvas & to) override;
  131. };
  132. class HeroInfoBasicPanel : public CIntObject //extracted from InfoWindow to fit better as non-popup embed element
  133. {
  134. private:
  135. std::shared_ptr<CPicture> background;
  136. std::vector<std::shared_ptr<CLabel>> labels;
  137. std::vector<std::shared_ptr<CAnimImage>> icons;
  138. public:
  139. HeroInfoBasicPanel(const InfoAboutHero & hero, Point * position, bool initializeBackground = true);
  140. void show(Canvas & to) override;
  141. void initializeData(const InfoAboutHero & hero);
  142. void update(const InfoAboutHero & updatedInfo);
  143. };
  144. class StackInfoBasicPanel : public CIntObject
  145. {
  146. private:
  147. std::shared_ptr<CPicture> background;
  148. std::shared_ptr<CPicture> background2;
  149. std::vector<std::shared_ptr<CLabel>> labels;
  150. std::vector<std::shared_ptr<CMultiLineLabel>> labelsMultiline;
  151. std::vector<std::shared_ptr<CAnimImage>> icons;
  152. public:
  153. StackInfoBasicPanel(const CStack * stack, bool initializeBackground = true);
  154. void show(Canvas & to) override;
  155. void initializeData(const CStack * stack);
  156. void update(const CStack * updatedInfo);
  157. };
  158. class HeroInfoWindow : public CWindowObject
  159. {
  160. private:
  161. std::shared_ptr<HeroInfoBasicPanel> content;
  162. public:
  163. HeroInfoWindow(const InfoAboutHero & hero, Point * position);
  164. };
  165. struct BattleResultResources
  166. {
  167. VideoPath prologueVideo;
  168. VideoPath loopedVideo;
  169. AudioPath musicName;
  170. MetaString resultText;
  171. };
  172. /// Class which is responsible for showing the battle result window
  173. class BattleResultWindow : public WindowBase
  174. {
  175. private:
  176. std::shared_ptr<CPicture> background;
  177. std::vector<std::shared_ptr<CLabel>> labels;
  178. std::shared_ptr<CButton> exit;
  179. std::shared_ptr<CButton> repeat;
  180. std::vector<std::shared_ptr<CAnimImage>> icons;
  181. std::shared_ptr<CTextBox> description;
  182. std::shared_ptr<VideoWidget> videoPlayer;
  183. CPlayerInterface & owner;
  184. BattleResultResources getResources(const BattleResult & br);
  185. void buttonPressed(int button); //internal function for button callbacks
  186. public:
  187. BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay = false);
  188. void bExitf(); //exit button callback
  189. void bRepeatf(); //repeat button callback
  190. std::function<void(int result)> resultCallback; //callback receiving which button was pressed
  191. void activate() override;
  192. };
  193. /// Shows the stack queue
  194. class StackQueue : public CIntObject
  195. {
  196. class StackBox : public CIntObject
  197. {
  198. StackQueue * owner;
  199. std::optional<uint32_t> boundUnitID;
  200. std::shared_ptr<CPicture> background;
  201. std::shared_ptr<CAnimImage> icon;
  202. std::shared_ptr<CLabel> amount;
  203. std::shared_ptr<CAnimImage> stateIcon;
  204. std::shared_ptr<CLabel> round;
  205. std::shared_ptr<TransparentFilledRectangle> roundRect;
  206. void show(Canvas & to) override;
  207. void showAll(Canvas & to) override;
  208. void showPopupWindow(const Point & cursorPosition) override;
  209. bool isBoundUnitHighlighted() const;
  210. public:
  211. StackBox(StackQueue * owner);
  212. void setUnit(const battle::Unit * unit, size_t turn = 0, std::optional<ui32> currentTurn = std::nullopt);
  213. std::optional<uint32_t> getBoundUnitID() const;
  214. };
  215. static const int QUEUE_SIZE_BIG = 10;
  216. std::shared_ptr<CFilledTexture> background;
  217. std::vector<std::shared_ptr<StackBox>> stackBoxes;
  218. BattleInterface & owner;
  219. std::shared_ptr<CAnimation> icons;
  220. std::shared_ptr<CAnimation> stateIcons;
  221. int32_t getSiegeShooterIconID();
  222. public:
  223. const bool embedded;
  224. StackQueue(bool Embedded, BattleInterface & owner);
  225. void update();
  226. std::optional<uint32_t> getHoveredUnitIdIfAny() const;
  227. void show(Canvas & to) override;
  228. };