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