1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-19 21:10:12 +02:00

Merged CClickableHex class into BattleFieldController

This commit is contained in:
Ivan Savenko 2023-01-29 14:34:47 +02:00
parent c3b79c786b
commit d3c85a19cb
5 changed files with 59 additions and 94 deletions

View File

@ -27,6 +27,7 @@
#include "../gui/CGuiHandler.h"
#include "../gui/CursorHandler.h"
#include "../adventureMap/CInGameConsole.h"
#include "../windows/CCreatureWindow.h"
#include "../../CCallback.h"
#include "../../lib/BattleFieldHandler.h"
@ -76,20 +77,11 @@ BattleFieldController::BattleFieldController(BattleInterface & owner):
backgroundWithHexes = std::make_unique<Canvas>(Point(background->width(), background->height()));
for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h)
{
auto hex = std::make_shared<ClickableHex>();
hex->myNumber = h;
hex->pos = hexPositionAbsolute(h);
hex->myInterface = &owner;
bfield.push_back(hex);
}
auto accessibility = owner.curInt->cb->getAccesibility();
for(int i = 0; i < accessibility.size(); i++)
stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE);
addUsedEvents(MOVE);
addUsedEvents(LCLICK | RCLICK | MOVE);
LOCPLINT->cingconsole->pos = this->pos;
}
@ -112,6 +104,29 @@ void BattleFieldController::mouseMoved(const SDL_MouseMotionEvent &event)
owner.actionsController->onHexHovered(selectedHex);
}
void BattleFieldController::clickLeft(tribool down, bool previousState)
{
if(!down)
{
BattleHex selectedHex = getHoveredHex();
if (selectedHex != BattleHex::INVALID)
owner.actionsController->onHexClicked(selectedHex);
}
}
void BattleFieldController::clickRight(tribool down, bool previousState)
{
if(down)
{
BattleHex selectedHex = getHoveredHex();
auto selectedStack = owner.curInt->cb->battleGetStackByPos(selectedHex, true);
if (selectedStack != nullptr)
GH.pushIntT<CStackWindow>(selectedStack, true);
}
}
void BattleFieldController::renderBattlefield(Canvas & canvas)
{
@ -230,15 +245,15 @@ std::set<BattleHex> BattleFieldController::getHighlightedHexesSpellRange()
if(!settings["battle"]["mouseShadow"].Bool())
return result;
const spells::Caster *caster = nullptr;
const CSpell *spell = nullptr;
spells::Mode mode = owner.actionsController->getCurrentCastMode();
spell = owner.actionsController->getCurrentSpell();
caster = owner.actionsController->getCurrentSpellcaster();
if(caster && spell) //when casting spell
{
const spells::Caster *caster = nullptr;
const CSpell *spell = nullptr;
spells::Mode mode = owner.actionsController->getCurrentCastMode();
spell = owner.actionsController->getCurrentSpell();
caster = owner.actionsController->getCurrentSpellcaster();
if(caster && spell) //when casting spell
{
// printing shaded hex(es)
spells::BattleCast event(owner.curInt->cb.get(), caster, mode, spell);
auto shaded = spell->battleMechanics(&event)->rangeInHexes(hoveredHex);
@ -297,16 +312,16 @@ std::set<BattleHex> BattleFieldController::getHighlightedHexesMovementTarget()
void BattleFieldController::showHighlightedHexes(Canvas & canvas)
{
std::set<BattleHex> hoveredStack = getHighlightedHexesStackRange();
std::set<BattleHex> hoveredSpell = getHighlightedHexesSpellRange();
std::set<BattleHex> hoveredMove = getHighlightedHexesMovementTarget();
if (getHoveredHex() == BattleHex::INVALID)
return;
auto const & hoveredMouse = owner.actionsController->currentActionSpellcasting(getHoveredHex()) ? hoveredSpell : hoveredMove;
for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
{
std::set<BattleHex> hoveredSpell = getHighlightedHexesSpellRange();
std::set<BattleHex> hoveredMove = getHighlightedHexesMovementTarget();
if (getHoveredHex() == BattleHex::INVALID)
return;
auto const & hoveredMouse = owner.actionsController->currentActionSpellcasting(getHoveredHex()) ? hoveredSpell : hoveredMove;
for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
{
bool stack = hoveredStack.count(b);
bool mouse = hoveredMouse.count(b);
@ -348,9 +363,18 @@ bool BattleFieldController::isPixelInHex(Point const & position)
BattleHex BattleFieldController::getHoveredHex()
{
for ( auto const & hex : bfield)
if (hex->hovered && hex->strictHovered)
return hex->myNumber;
Point hoverPos = GH.getCursorPosition();
for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h)
{
Rect hexPosition = hexPositionAbsolute(h);
if (!hexPosition.isInside(hoverPos))
continue;
if (isPixelInHex(hoverPos - hexPosition.topLeft()))
return h;
}
return BattleHex::INVALID;
}

View File

@ -20,7 +20,6 @@ class Point;
VCMI_LIB_NAMESPACE_END
class ClickableHex;
class BattleHero;
class Canvas;
class IImage;
@ -50,8 +49,6 @@ class BattleFieldController : public CIntObject
/// hexes that when in front of a unit cause it's amount box to move back
std::array<bool, GameConstants::BFIELD_SIZE> stackCountOutsideHexes;
std::vector<std::shared_ptr<ClickableHex>> bfield;
void showHighlightedHex(Canvas & to, BattleHex hex, bool darkBorder);
std::set<BattleHex> getHighlightedHexesStackRange();
@ -66,9 +63,11 @@ class BattleFieldController : public CIntObject
BattleHex::EDir selectAttackDirection(BattleHex myNumber, const Point & point);
void mouseMoved(const SDL_MouseMotionEvent &event) override;
void clickLeft(tribool down, bool previousState) override;
void clickRight(tribool down, bool previousState) override;
void showAll(SDL_Surface * to) override;
void show(SDL_Surface * to) override;
public:
BattleFieldController(BattleInterface & owner);

View File

@ -37,7 +37,6 @@ class Canvas;
class BattleResultWindow;
class StackQueue;
class CPlayerInterface;
class ClickableHex;
class CAnimation;
struct BattleEffect;
class IImage;

View File

@ -30,7 +30,6 @@
#include "../widgets/Images.h"
#include "../widgets/TextControls.h"
#include "../windows/CMessage.h"
#include "../windows/CCreatureWindow.h"
#include "../windows/CSpellWindow.h"
#include "../render/CAnimation.h"
#include "../adventureMap/CInGameConsole.h"
@ -681,46 +680,6 @@ void BattleResultWindow::bExitf()
CCS->videoh->close();
}
void ClickableHex::hover(bool on)
{
hovered = on;
//Hoverable::hover(on);
}
ClickableHex::ClickableHex()
: myNumber(-1)
, strictHovered(false)
, myInterface(nullptr)
{
addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
}
void ClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
{
strictHovered = myInterface->fieldController->isPixelInHex(Point(sEvent.x-pos.x, sEvent.y-pos.y));
}
void ClickableHex::clickLeft(tribool down, bool previousState)
{
if(!down && hovered && strictHovered) //we've been really clicked!
{
myInterface->actionsController->onHexClicked(myNumber);
}
}
void ClickableHex::clickRight(tribool down, bool previousState)
{
const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
if(hovered && strictHovered && myst!=nullptr)
{
if(!myst->alive()) return;
if(down)
{
GH.pushIntT<CStackWindow>(myst, true);
}
}
}
StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
: embedded(Embedded),
owner(owner)

View File

@ -172,22 +172,6 @@ public:
void show(SDL_Surface * to = 0) override;
};
/// Class which stands for a single hex field on a battlefield
class ClickableHex : public CIntObject
{
public:
ui32 myNumber; //number of hex in commonly used format
bool strictHovered; //for determining if hex is hovered by mouse (this is different problem than hex's graphic hovering)
BattleInterface * myInterface; //interface that owns me
//for user interactions
void hover (bool on) override;
void mouseMoved (const SDL_MouseMotionEvent &sEvent) override;
void clickLeft(tribool down, bool previousState) override;
void clickRight(tribool down, bool previousState) override;
ClickableHex();
};
/// Shows the stack queue
class StackQueue : public CIntObject
{