BattleFieldController.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * BattleFieldController.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 "../../lib/battle/BattleHex.h"
  12. #include "../../lib/Point.h"
  13. #include "../gui/CIntObject.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CStack;
  16. class Rect;
  17. VCMI_LIB_NAMESPACE_END
  18. class BattleHero;
  19. class Canvas;
  20. class IImage;
  21. class BattleInterface;
  22. /// Handles battlefield grid as well as rendering of background layer of battle interface
  23. class BattleFieldController : public CIntObject
  24. {
  25. BattleInterface & owner;
  26. std::shared_ptr<IImage> background;
  27. std::shared_ptr<IImage> cellBorder;
  28. std::shared_ptr<IImage> cellUnitMovementHighlight;
  29. std::shared_ptr<IImage> cellUnitMaxMovementHighlight;
  30. std::shared_ptr<IImage> cellShade;
  31. std::shared_ptr<CAnimation> attackCursors;
  32. /// Canvas that contains background, hex grid (if enabled), absolute obstacles and movement range of active stack
  33. std::unique_ptr<Canvas> backgroundWithHexes;
  34. /// direction which will be used to perform attack with current cursor position
  35. Point currentAttackOriginPoint;
  36. /// hex currently under mouse hover
  37. BattleHex hoveredHex;
  38. /// hexes to which currently active stack can move
  39. std::vector<BattleHex> occupiableHexes;
  40. /// hexes that when in front of a unit cause it's amount box to move back
  41. std::array<bool, GameConstants::BFIELD_SIZE> stackCountOutsideHexes;
  42. void showHighlightedHex(Canvas & to, std::shared_ptr<IImage> highlight, BattleHex hex, bool darkBorder);
  43. std::set<BattleHex> getHighlightedHexesForActiveStack();
  44. std::set<BattleHex> getMovementRangeForHoveredStack();
  45. std::set<BattleHex> getHighlightedHexesForSpellRange();
  46. std::set<BattleHex> getHighlightedHexesForMovementTarget();
  47. void showBackground(Canvas & canvas);
  48. void showBackgroundImage(Canvas & canvas);
  49. void showBackgroundImageWithHexes(Canvas & canvas);
  50. void showHighlightedHexes(Canvas & canvas);
  51. void updateAccessibleHexes();
  52. BattleHex getHexAtPosition(Point hoverPosition);
  53. /// Checks whether selected pixel is transparent, uses local coordinates of a hex
  54. bool isPixelInHex(Point const & position);
  55. size_t selectBattleCursor(BattleHex myNumber);
  56. void panning(bool on, const Point & initialPosition, const Point & finalPosition) override;
  57. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  58. void mouseMoved(const Point & cursorPosition) override;
  59. void clickLeft(tribool down, bool previousState) override;
  60. void clickRight(tribool down, bool previousState) override;
  61. void activate() override;
  62. void showAll(Canvas & to) override;
  63. void show(Canvas & to) override;
  64. void tick(uint32_t msPassed) override;
  65. bool receiveEvent(const Point & position, int eventType) const override;
  66. public:
  67. BattleFieldController(BattleInterface & owner);
  68. void createHeroes();
  69. void redrawBackgroundWithHexes();
  70. void renderBattlefield(Canvas & canvas);
  71. /// Returns position of hex relative to owner (BattleInterface)
  72. Rect hexPositionLocal(BattleHex hex) const;
  73. /// Returns position of hex relative to game window
  74. Rect hexPositionAbsolute(BattleHex hex) const;
  75. /// Returns ID of currently hovered hex or BattleHex::INVALID if none
  76. BattleHex getHoveredHex();
  77. /// returns true if selected tile can be attacked in melee by current stack
  78. bool isTileAttackable(const BattleHex & number) const;
  79. /// returns true if stack should render its stack count image in default position - outside own hex
  80. bool stackCountOutsideHex(const BattleHex & number) const;
  81. BattleHex::EDir selectAttackDirection(BattleHex myNumber);
  82. BattleHex fromWhichHexAttack(BattleHex myNumber);
  83. };