BattleInterfaceClasses.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. class CStack;
  21. namespace battle
  22. {
  23. class Unit;
  24. }
  25. VCMI_LIB_NAMESPACE_END
  26. class CAnimation;
  27. class Canvas;
  28. class BattleInterface;
  29. class CPicture;
  30. class CFilledTexture;
  31. class CButton;
  32. class CToggleButton;
  33. class CLabel;
  34. class CTextBox;
  35. class CAnimImage;
  36. class TransparentFilledRectangle;
  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 HeroInfoWindow : public CWindowObject
  114. {
  115. private:
  116. std::shared_ptr<HeroInfoBasicPanel> content;
  117. public:
  118. HeroInfoWindow(const InfoAboutHero & hero, Point * position);
  119. };
  120. /// Class which is responsible for showing the battle result window
  121. class BattleResultWindow : public WindowBase
  122. {
  123. private:
  124. std::shared_ptr<CPicture> background;
  125. std::vector<std::shared_ptr<CLabel>> labels;
  126. std::shared_ptr<CButton> exit;
  127. std::shared_ptr<CButton> repeat;
  128. std::vector<std::shared_ptr<CAnimImage>> icons;
  129. std::shared_ptr<CTextBox> description;
  130. CPlayerInterface & owner;
  131. enum BattleResultVideo
  132. {
  133. NONE,
  134. WIN,
  135. SURRENDER,
  136. RETREAT,
  137. RETREAT_LOOP,
  138. DEFEAT,
  139. DEFEAT_LOOP,
  140. DEFEAT_SIEGE,
  141. DEFEAT_SIEGE_LOOP,
  142. WIN_SIEGE,
  143. WIN_SIEGE_LOOP,
  144. };
  145. BattleResultVideo currentVideo;
  146. void playVideo(bool startLoop = false);
  147. void buttonPressed(int button); //internal function for button callbacks
  148. public:
  149. BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay = false);
  150. void bExitf(); //exit button callback
  151. void bRepeatf(); //repeat button callback
  152. std::function<void(int result)> resultCallback; //callback receiving which button was pressed
  153. void activate() override;
  154. void show(Canvas & to) override;
  155. };
  156. /// Shows the stack queue
  157. class StackQueue : public CIntObject
  158. {
  159. class StackBox : public CIntObject
  160. {
  161. StackQueue * owner;
  162. std::optional<uint32_t> boundUnitID;
  163. std::shared_ptr<CPicture> background;
  164. std::shared_ptr<CAnimImage> icon;
  165. std::shared_ptr<CLabel> amount;
  166. std::shared_ptr<CAnimImage> stateIcon;
  167. std::shared_ptr<CLabel> round;
  168. std::shared_ptr<TransparentFilledRectangle> roundRect;
  169. void show(Canvas & to) override;
  170. void showAll(Canvas & to) override;
  171. bool isBoundUnitHighlighted() const;
  172. public:
  173. StackBox(StackQueue * owner);
  174. void setUnit(const battle::Unit * unit, size_t turn = 0, std::optional<ui32> currentTurn = std::nullopt);
  175. std::optional<uint32_t> getBoundUnitID() const;
  176. };
  177. static const int QUEUE_SIZE = 10;
  178. std::shared_ptr<CFilledTexture> background;
  179. std::vector<std::shared_ptr<StackBox>> stackBoxes;
  180. BattleInterface & owner;
  181. std::shared_ptr<CAnimation> icons;
  182. std::shared_ptr<CAnimation> stateIcons;
  183. int32_t getSiegeShooterIconID();
  184. public:
  185. const bool embedded;
  186. StackQueue(bool Embedded, BattleInterface & owner);
  187. void update();
  188. std::optional<uint32_t> getHoveredUnitIdIfAny() const;
  189. void show(Canvas & to) override;
  190. };