BattleInterfaceClasses.h 5.8 KB

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