BattleInterfaceClasses.h 7.5 KB

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