BattleInterfaceClasses.h 6.8 KB

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