1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-15 01:24:45 +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;
}