BattleInterfaceClasses.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 CLabel;
  33. class CMultiLineLabel;
  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. const BattleInterface & owner;
  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(const BattleInterface & owner, 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. void clickPressed(const Point & cursorPosition) override;
  64. bool addText(const std::string &text); //adds text at the last position; returns false if failed (e.g. text longer than 70 characters)
  65. void scrollUp(ui32 by = 1); //scrolls console up by 'by' positions
  66. void scrollDown(ui32 by = 1); //scrolls console up by 'by' positions
  67. // IStatusBar interface
  68. void write(const std::string & Text) override;
  69. void clearIfMatching(const std::string & Text) override;
  70. void clear() override;
  71. void setEnteringMode(bool on) override;
  72. void setEnteredText(const std::string & text) override;
  73. };
  74. class BattleConsoleWindow : public CWindowObject
  75. {
  76. private:
  77. std::shared_ptr<CFilledTexture> backgroundTexture;
  78. std::shared_ptr<CButton> buttonOk;
  79. std::shared_ptr<TransparentFilledRectangle> textBoxBackgroundBorder;
  80. std::shared_ptr<CTextBox> textBox;
  81. public:
  82. BattleConsoleWindow(const std::string & text);
  83. };
  84. /// Hero battle animation
  85. class BattleHero : public CIntObject
  86. {
  87. bool defender;
  88. CFunctionList<void()> phaseFinishedCallback;
  89. std::shared_ptr<CAnimation> animation;
  90. std::shared_ptr<CAnimation> flagAnimation;
  91. const CGHeroInstance * hero; //this animation's hero instance
  92. const BattleInterface & owner; //battle interface to which this animation is assigned
  93. EHeroAnimType phase; //stage of animation
  94. EHeroAnimType nextPhase; //stage of animation to be set after current phase is fully displayed
  95. float currentSpeed;
  96. float currentFrame; //frame of animation
  97. float flagCurrentFrame;
  98. void switchToNextPhase();
  99. void render(Canvas & canvas); //prints next frame of animation to to
  100. public:
  101. const CGHeroInstance * instance();
  102. void setPhase(EHeroAnimType newPhase); //sets phase of hero animation
  103. void collectRenderableObjects(BattleRenderer & renderer);
  104. void tick(uint32_t msPassed) override;
  105. float getFrame() const;
  106. void onPhaseFinished(const std::function<void()> &);
  107. void pause();
  108. void play();
  109. void heroLeftClicked();
  110. void heroRightClicked();
  111. BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender);
  112. };
  113. class HeroInfoBasicPanel : public CIntObject //extracted from InfoWindow to fit better as non-popup embed element
  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. HeroInfoBasicPanel(const InfoAboutHero & hero, Point * position, bool initializeBackground = true);
  121. void show(Canvas & to) override;
  122. void initializeData(const InfoAboutHero & hero);
  123. void update(const InfoAboutHero & updatedInfo);
  124. };
  125. class StackInfoBasicPanel : public CIntObject
  126. {
  127. private:
  128. std::shared_ptr<CPicture> background;
  129. std::shared_ptr<CPicture> background2;
  130. std::vector<std::shared_ptr<CLabel>> labels;
  131. std::vector<std::shared_ptr<CMultiLineLabel>> labelsMultiline;
  132. std::vector<std::shared_ptr<CAnimImage>> icons;
  133. public:
  134. StackInfoBasicPanel(const CStack * stack, bool initializeBackground = true);
  135. void show(Canvas & to) override;
  136. void initializeData(const CStack * stack);
  137. void update(const CStack * updatedInfo);
  138. };
  139. class HeroInfoWindow : public CWindowObject
  140. {
  141. private:
  142. std::shared_ptr<HeroInfoBasicPanel> content;
  143. public:
  144. HeroInfoWindow(const InfoAboutHero & hero, Point * position);
  145. };
  146. /// Class which is responsible for showing the battle result window
  147. class BattleResultWindow : public WindowBase
  148. {
  149. private:
  150. std::shared_ptr<CPicture> background;
  151. std::vector<std::shared_ptr<CLabel>> labels;
  152. std::shared_ptr<CButton> exit;
  153. std::shared_ptr<CButton> repeat;
  154. std::vector<std::shared_ptr<CAnimImage>> icons;
  155. std::shared_ptr<CTextBox> description;
  156. CPlayerInterface & owner;
  157. enum BattleResultVideo
  158. {
  159. NONE,
  160. WIN,
  161. SURRENDER,
  162. RETREAT,
  163. RETREAT_LOOP,
  164. DEFEAT,
  165. DEFEAT_LOOP,
  166. DEFEAT_SIEGE,
  167. DEFEAT_SIEGE_LOOP,
  168. WIN_SIEGE,
  169. WIN_SIEGE_LOOP,
  170. };
  171. BattleResultVideo currentVideo;
  172. void playVideo(bool startLoop = false);
  173. void buttonPressed(int button); //internal function for button callbacks
  174. public:
  175. BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay = false);
  176. void bExitf(); //exit button callback
  177. void bRepeatf(); //repeat button callback
  178. std::function<void(int result)> resultCallback; //callback receiving which button was pressed
  179. void activate() override;
  180. void show(Canvas & to) override;
  181. };
  182. /// Shows the stack queue
  183. class StackQueue : public CIntObject
  184. {
  185. class StackBox : public CIntObject
  186. {
  187. StackQueue * owner;
  188. std::optional<uint32_t> boundUnitID;
  189. std::shared_ptr<CPicture> background;
  190. std::shared_ptr<CAnimImage> icon;
  191. std::shared_ptr<CLabel> amount;
  192. std::shared_ptr<CAnimImage> stateIcon;
  193. std::shared_ptr<CLabel> round;
  194. std::shared_ptr<TransparentFilledRectangle> roundRect;
  195. void show(Canvas & to) override;
  196. void showAll(Canvas & to) override;
  197. void showPopupWindow(const Point & cursorPosition) override;
  198. bool isBoundUnitHighlighted() const;
  199. public:
  200. StackBox(StackQueue * owner);
  201. void setUnit(const battle::Unit * unit, size_t turn = 0, std::optional<ui32> currentTurn = std::nullopt);
  202. std::optional<uint32_t> getBoundUnitID() const;
  203. };
  204. static const int QUEUE_SIZE_BIG = 10;
  205. std::shared_ptr<CFilledTexture> background;
  206. std::vector<std::shared_ptr<StackBox>> stackBoxes;
  207. BattleInterface & owner;
  208. std::shared_ptr<CAnimation> icons;
  209. std::shared_ptr<CAnimation> stateIcons;
  210. int32_t getSiegeShooterIconID();
  211. public:
  212. const bool embedded;
  213. StackQueue(bool Embedded, BattleInterface & owner);
  214. void update();
  215. std::optional<uint32_t> getHoveredUnitIdIfAny() const;
  216. void show(Canvas & to) override;
  217. };