From d3c85a19cbfe831e319d5632f8e6a7e57acff8fb Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sun, 29 Jan 2023 14:34:47 +0200 Subject: [PATCH] Merged CClickableHex class into BattleFieldController --- client/battle/BattleFieldController.cpp | 88 +++++++++++++++--------- client/battle/BattleFieldController.h | 7 +- client/battle/BattleInterface.h | 1 - client/battle/BattleInterfaceClasses.cpp | 41 ----------- client/battle/BattleInterfaceClasses.h | 16 ----- 5 files changed, 59 insertions(+), 94 deletions(-) diff --git a/client/battle/BattleFieldController.cpp b/client/battle/BattleFieldController.cpp index 12c6c50c3..77b514f72 100644 --- a/client/battle/BattleFieldController.cpp +++ b/client/battle/BattleFieldController.cpp @@ -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(Point(background->width(), background->height())); - for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h) - { - auto hex = std::make_shared(); - 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(selectedStack, true); + } +} void BattleFieldController::renderBattlefield(Canvas & canvas) { @@ -230,15 +245,15 @@ std::set 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 BattleFieldController::getHighlightedHexesMovementTarget() void BattleFieldController::showHighlightedHexes(Canvas & canvas) { std::set hoveredStack = getHighlightedHexesStackRange(); - std::set hoveredSpell = getHighlightedHexesSpellRange(); - std::set hoveredMove = getHighlightedHexesMovementTarget(); - - if (getHoveredHex() == BattleHex::INVALID) - return; - - auto const & hoveredMouse = owner.actionsController->currentActionSpellcasting(getHoveredHex()) ? hoveredSpell : hoveredMove; - - for(int b=0; b hoveredSpell = getHighlightedHexesSpellRange(); + std::set hoveredMove = getHighlightedHexesMovementTarget(); + + if (getHoveredHex() == BattleHex::INVALID) + return; + + auto const & hoveredMouse = owner.actionsController->currentActionSpellcasting(getHoveredHex()) ? hoveredSpell : hoveredMove; + + for(int b=0; bhovered && 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; } diff --git a/client/battle/BattleFieldController.h b/client/battle/BattleFieldController.h index bcb16b361..0fa0e2910 100644 --- a/client/battle/BattleFieldController.h +++ b/client/battle/BattleFieldController.h @@ -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 stackCountOutsideHexes; - std::vector> bfield; - void showHighlightedHex(Canvas & to, BattleHex hex, bool darkBorder); std::set 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); diff --git a/client/battle/BattleInterface.h b/client/battle/BattleInterface.h index adec40210..55cce087f 100644 --- a/client/battle/BattleInterface.h +++ b/client/battle/BattleInterface.h @@ -37,7 +37,6 @@ class Canvas; class BattleResultWindow; class StackQueue; class CPlayerInterface; -class ClickableHex; class CAnimation; struct BattleEffect; class IImage; diff --git a/client/battle/BattleInterfaceClasses.cpp b/client/battle/BattleInterfaceClasses.cpp index 30b7ff447..2d396f0d5 100644 --- a/client/battle/BattleInterfaceClasses.cpp +++ b/client/battle/BattleInterfaceClasses.cpp @@ -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(myst, true); - } - } -} - StackQueue::StackQueue(bool Embedded, BattleInterface & owner) : embedded(Embedded), owner(owner) diff --git a/client/battle/BattleInterfaceClasses.h b/client/battle/BattleInterfaceClasses.h index a785510bd..f22484a5a 100644 --- a/client/battle/BattleInterfaceClasses.h +++ b/client/battle/BattleInterfaceClasses.h @@ -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 {