BattleInterfaceClasses.h 6.3 KB

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