BattleInterfaceClasses.h 7.1 KB

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