BattleInterfaceClasses.h 6.2 KB

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