1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Minor code reorganization

This commit is contained in:
Ivan Savenko 2023-05-18 13:31:26 +03:00
parent ff1c94541f
commit 6012e0cb45
9 changed files with 183 additions and 151 deletions

View File

@ -30,7 +30,8 @@ set(client_SRCS
gui/CGuiHandler.cpp gui/CGuiHandler.cpp
gui/CIntObject.cpp gui/CIntObject.cpp
gui/CursorHandler.cpp gui/CursorHandler.cpp
gui/InterfaceEventDispatcher.cpp gui/EventDispatcher.cpp
gui/EventsReceiver.cpp
gui/InterfaceObjectConfigurable.cpp gui/InterfaceObjectConfigurable.cpp
gui/FramerateManager.cpp gui/FramerateManager.cpp
gui/NotificationHandler.cpp gui/NotificationHandler.cpp
@ -164,8 +165,9 @@ set(client_HEADERS
gui/CGuiHandler.h gui/CGuiHandler.h
gui/CIntObject.h gui/CIntObject.h
gui/CursorHandler.h gui/CursorHandler.h
gui/EventDispatcher.h
gui/EventsReceiver.h
gui/InterfaceObjectConfigurable.h gui/InterfaceObjectConfigurable.h
gui/InterfaceEventDispatcher.h
gui/FramerateManager.h gui/FramerateManager.h
gui/MouseButton.h gui/MouseButton.h
gui/NotificationHandler.h gui/NotificationHandler.h

View File

@ -16,7 +16,7 @@
#include "ShortcutHandler.h" #include "ShortcutHandler.h"
#include "FramerateManager.h" #include "FramerateManager.h"
#include "WindowHandler.h" #include "WindowHandler.h"
#include "InterfaceEventDispatcher.h" #include "EventDispatcher.h"
#include "../CGameInfo.h" #include "../CGameInfo.h"
#include "../render/Colors.h" #include "../render/Colors.h"
@ -80,7 +80,7 @@ SSetCaptureState::~SSetCaptureState()
void CGuiHandler::init() void CGuiHandler::init()
{ {
eventDispatcherInstance = std::make_unique<InterfaceEventDispatcher>(); eventDispatcherInstance = std::make_unique<EventDispatcher>();
windowHandlerInstance = std::make_unique<WindowHandler>(); windowHandlerInstance = std::make_unique<WindowHandler>();
screenHandlerInstance = std::make_unique<ScreenHandler>(); screenHandlerInstance = std::make_unique<ScreenHandler>();
shortcutsHandlerInstance = std::make_unique<ShortcutHandler>(); shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
@ -613,7 +613,7 @@ IScreenHandler & CGuiHandler::screenHandler()
return *screenHandlerInstance; return *screenHandlerInstance;
} }
InterfaceEventDispatcher & CGuiHandler::eventDispatcher() EventDispatcher & CGuiHandler::eventDispatcher()
{ {
return *eventDispatcherInstance; return *eventDispatcherInstance;
} }

View File

@ -30,7 +30,7 @@ class IUpdateable;
class IShowActivatable; class IShowActivatable;
class IScreenHandler; class IScreenHandler;
class WindowHandler; class WindowHandler;
class InterfaceEventDispatcher; class EventDispatcher;
// TODO: event handling need refactoring // TODO: event handling need refactoring
enum class EUserEvent enum class EUserEvent
@ -63,7 +63,7 @@ private:
std::unique_ptr<IScreenHandler> screenHandlerInstance; std::unique_ptr<IScreenHandler> screenHandlerInstance;
std::unique_ptr<FramerateManager> framerateManagerInstance; std::unique_ptr<FramerateManager> framerateManagerInstance;
std::unique_ptr<InterfaceEventDispatcher> eventDispatcherInstance; std::unique_ptr<EventDispatcher> eventDispatcherInstance;
void handleCurrentEvent(SDL_Event &current); void handleCurrentEvent(SDL_Event &current);
void convertTouchToMouse(SDL_Event * current); void convertTouchToMouse(SDL_Event * current);
@ -89,7 +89,7 @@ public:
ShortcutHandler & shortcutsHandler(); ShortcutHandler & shortcutsHandler();
FramerateManager & framerateManager(); FramerateManager & framerateManager();
InterfaceEventDispatcher & eventDispatcher(); EventDispatcher & eventDispatcher();
/// Returns current logical screen dimensions /// Returns current logical screen dimensions
/// May not match size of window if user has UI scaling different from 100% /// May not match size of window if user has UI scaling different from 100%

View File

@ -11,7 +11,6 @@
#include "CIntObject.h" #include "CIntObject.h"
#include "CGuiHandler.h" #include "CGuiHandler.h"
#include "InterfaceEventDispatcher.h"
#include "WindowHandler.h" #include "WindowHandler.h"
#include "Shortcut.h" #include "Shortcut.h"
#include "../renderSDL/SDL_Extensions.h" #include "../renderSDL/SDL_Extensions.h"
@ -20,70 +19,6 @@
#include <SDL_pixels.h> #include <SDL_pixels.h>
AEventsReceiver::AEventsReceiver()
: activeState(0)
, hoveredState(false)
, strongInterestState(false)
{
}
bool AEventsReceiver::isHovered() const
{
return hoveredState;
}
bool AEventsReceiver::isActive() const
{
return activeState;
}
bool AEventsReceiver::isActive(int flags) const
{
return activeState & flags;
}
bool AEventsReceiver::isMouseButtonPressed(MouseButton btn) const
{
return currentMouseState.count(btn) ? currentMouseState.at(btn) : false;
}
void AEventsReceiver::setMoveEventStrongInterest(bool on)
{
strongInterestState = on;
}
void AEventsReceiver::activateEvents(ui16 what)
{
assert((what & GENERAL) || (activeState & GENERAL));
activeState |= GENERAL;
GH.eventDispatcher().handleElementActivate(this, what);
}
void AEventsReceiver::deactivateEvents(ui16 what)
{
if (what & GENERAL)
activeState &= ~GENERAL;
GH.eventDispatcher().handleElementDeActivate(this, what & activeState);
}
void AEventsReceiver::click(MouseButton btn, tribool down, bool previousState)
{
switch(btn)
{
default:
case MouseButton::LEFT:
clickLeft(down, previousState);
break;
case MouseButton::MIDDLE:
clickMiddle(down, previousState);
break;
case MouseButton::RIGHT:
clickRight(down, previousState);
break;
}
}
CIntObject::CIntObject(int used_, Point pos_): CIntObject::CIntObject(int used_, Point pos_):
parent_m(nullptr), parent_m(nullptr),
parent(parent_m), parent(parent_m),

View File

@ -9,17 +9,13 @@
*/ */
#pragma once #pragma once
#include "MouseButton.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../../lib/Rect.h" #include "../../lib/Rect.h"
#include "EventsReceiver.h"
struct SDL_Surface; struct SDL_Surface;
class CGuiHandler; class CGuiHandler;
class CPicture; class CPicture;
class InterfaceEventDispatcher;
enum class EShortcut;
using boost::logic::tribool;
class IUpdateable class IUpdateable
{ {
@ -42,55 +38,6 @@ public:
virtual ~IShowActivatable() = default; virtual ~IShowActivatable() = default;
}; };
class AEventsReceiver
{
friend class InterfaceEventDispatcher;
ui16 activeState;
std::map<MouseButton, bool> currentMouseState;
bool hoveredState; //for determining if object is hovered
bool strongInterestState; //if true - report all mouse movements, if not - only when hovered
void click(MouseButton btn, tribool down, bool previousState);
protected:
void setMoveEventStrongInterest(bool on);
void activateEvents(ui16 what);
void deactivateEvents(ui16 what);
virtual void clickLeft(tribool down, bool previousState) {}
virtual void clickRight(tribool down, bool previousState) {}
virtual void clickMiddle(tribool down, bool previousState) {}
virtual void textInputed(const std::string & enteredText) {}
virtual void textEdited(const std::string & enteredText) {}
virtual void tick(uint32_t msPassed) {}
virtual void wheelScrolled(bool down, bool in) {}
virtual void mouseMoved(const Point & cursorPosition) {}
virtual void hover(bool on) {}
virtual void onDoubleClick() {}
virtual void keyPressed(EShortcut key) {}
virtual void keyReleased(EShortcut key) {}
virtual bool captureThisKey(EShortcut key) = 0;
virtual bool isInside(const Point & position) = 0;
public:
AEventsReceiver();
virtual ~AEventsReceiver() = default;
// These are the arguments that can be used to determine what kind of input the CIntObject will receive
enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, TEXTINPUT=512, MCLICK=1024, ALL=0xffff};
bool isHovered() const;
bool isActive() const;
bool isActive(int flags) const;
bool isMouseButtonPressed(MouseButton btn) const;
};
// Base UI element // Base UI element
class CIntObject : public IShowActivatable, public AEventsReceiver //interface object class CIntObject : public IShowActivatable, public AEventsReceiver //interface object
{ {

View File

@ -1,5 +1,5 @@
/* /*
* CGuiHandler.cpp, part of VCMI engine * EventDispatcher.cpp, part of VCMI engine
* *
* Authors: listed in file AUTHORS in main folder * Authors: listed in file AUTHORS in main folder
* *
@ -8,23 +8,24 @@
* *
*/ */
#include "StdInc.h" #include "StdInc.h"
#include "InterfaceEventDispatcher.h" #include "EventDispatcher.h"
#include "CIntObject.h"
#include "CGuiHandler.h"
#include "FramerateManager.h"
void InterfaceEventDispatcher::allowEventHandling(bool enable) #include "EventsReceiver.h"
#include "FramerateManager.h"
#include "CGuiHandler.h"
void EventDispatcher::allowEventHandling(bool enable)
{ {
eventHandlingAllowed = enable; eventHandlingAllowed = enable;
} }
void InterfaceEventDispatcher::processList(const ui16 mask, const ui16 flag, CIntObjectList *lst, std::function<void (CIntObjectList *)> cb) void EventDispatcher::processList(const ui16 mask, const ui16 flag, CIntObjectList *lst, std::function<void (CIntObjectList *)> cb)
{ {
if (mask & flag) if (mask & flag)
cb(lst); cb(lst);
} }
void InterfaceEventDispatcher::processLists(ui16 activityFlag, std::function<void (CIntObjectList *)> cb) void EventDispatcher::processLists(ui16 activityFlag, std::function<void (CIntObjectList *)> cb)
{ {
processList(AEventsReceiver::LCLICK,activityFlag,&lclickable,cb); processList(AEventsReceiver::LCLICK,activityFlag,&lclickable,cb);
processList(AEventsReceiver::RCLICK,activityFlag,&rclickable,cb); processList(AEventsReceiver::RCLICK,activityFlag,&rclickable,cb);
@ -38,7 +39,7 @@ void InterfaceEventDispatcher::processLists(ui16 activityFlag, std::function<voi
processList(AEventsReceiver::TEXTINPUT,activityFlag,&textInterested,cb); processList(AEventsReceiver::TEXTINPUT,activityFlag,&textInterested,cb);
} }
void InterfaceEventDispatcher::handleElementActivate(AEventsReceiver * elem, ui16 activityFlag) void EventDispatcher::handleElementActivate(AEventsReceiver * elem, ui16 activityFlag)
{ {
processLists(activityFlag,[&](CIntObjectList * lst){ processLists(activityFlag,[&](CIntObjectList * lst){
lst->push_front(elem); lst->push_front(elem);
@ -46,7 +47,7 @@ void InterfaceEventDispatcher::handleElementActivate(AEventsReceiver * elem, ui1
elem->activeState |= activityFlag; elem->activeState |= activityFlag;
} }
void InterfaceEventDispatcher::handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag) void EventDispatcher::handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag)
{ {
processLists(activityFlag,[&](CIntObjectList * lst){ processLists(activityFlag,[&](CIntObjectList * lst){
auto hlp = std::find(lst->begin(),lst->end(),elem); auto hlp = std::find(lst->begin(),lst->end(),elem);
@ -56,7 +57,7 @@ void InterfaceEventDispatcher::handleElementDeActivate(AEventsReceiver * elem, u
elem->activeState &= ~activityFlag; elem->activeState &= ~activityFlag;
} }
void InterfaceEventDispatcher::dispatchTimer(uint32_t msPassed) void EventDispatcher::dispatchTimer(uint32_t msPassed)
{ {
CIntObjectList hlp = timeinterested; CIntObjectList hlp = timeinterested;
for (auto & elem : hlp) for (auto & elem : hlp)
@ -66,7 +67,7 @@ void InterfaceEventDispatcher::dispatchTimer(uint32_t msPassed)
} }
} }
void InterfaceEventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & shortcutsVector) void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & shortcutsVector)
{ {
bool keysCaptured = false; bool keysCaptured = false;
@ -88,7 +89,7 @@ void InterfaceEventDispatcher::dispatchShortcutPressed(const std::vector<EShortc
} }
} }
void InterfaceEventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector) void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector)
{ {
bool keysCaptured = false; bool keysCaptured = false;
@ -110,7 +111,7 @@ void InterfaceEventDispatcher::dispatchShortcutReleased(const std::vector<EShort
} }
} }
InterfaceEventDispatcher::CIntObjectList & InterfaceEventDispatcher::getListForMouseButton(MouseButton button) EventDispatcher::CIntObjectList & EventDispatcher::getListForMouseButton(MouseButton button)
{ {
switch (button) switch (button)
{ {
@ -124,7 +125,7 @@ InterfaceEventDispatcher::CIntObjectList & InterfaceEventDispatcher::getListForM
throw std::runtime_error("Invalid mouse button in getListForMouseButton"); throw std::runtime_error("Invalid mouse button in getListForMouseButton");
} }
void InterfaceEventDispatcher::dispatchMouseDoubleClick(const Point & position) void EventDispatcher::dispatchMouseDoubleClick(const Point & position)
{ {
bool doubleClicked = false; bool doubleClicked = false;
auto hlp = doubleClickInterested; auto hlp = doubleClickInterested;
@ -148,17 +149,17 @@ void InterfaceEventDispatcher::dispatchMouseDoubleClick(const Point & position)
dispatchMouseButtonPressed(MouseButton::LEFT, position); dispatchMouseButtonPressed(MouseButton::LEFT, position);
} }
void InterfaceEventDispatcher::dispatchMouseButtonPressed(const MouseButton & button, const Point & position) void EventDispatcher::dispatchMouseButtonPressed(const MouseButton & button, const Point & position)
{ {
handleMouseButtonClick(getListForMouseButton(button), button, true); handleMouseButtonClick(getListForMouseButton(button), button, true);
} }
void InterfaceEventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, const Point & position) void EventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, const Point & position)
{ {
handleMouseButtonClick(getListForMouseButton(button), button, false); handleMouseButtonClick(getListForMouseButton(button), button, false);
} }
void InterfaceEventDispatcher::handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed) void EventDispatcher::handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed)
{ {
auto hlp = interestedObjs; auto hlp = interestedObjs;
for(auto & i : hlp) for(auto & i : hlp)
@ -183,7 +184,7 @@ void InterfaceEventDispatcher::handleMouseButtonClick(CIntObjectList & intereste
} }
} }
void InterfaceEventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position) void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
{ {
CIntObjectList hlp = wheelInterested; CIntObjectList hlp = wheelInterested;
for(auto i = hlp.begin(); i != hlp.end() && eventHandlingAllowed; i++) for(auto i = hlp.begin(); i != hlp.end() && eventHandlingAllowed; i++)
@ -194,7 +195,7 @@ void InterfaceEventDispatcher::dispatchMouseScrolled(const Point & distance, con
} }
} }
void InterfaceEventDispatcher::dispatchTextInput(const std::string & text) void EventDispatcher::dispatchTextInput(const std::string & text)
{ {
for(auto it : textInterested) for(auto it : textInterested)
{ {
@ -202,7 +203,7 @@ void InterfaceEventDispatcher::dispatchTextInput(const std::string & text)
} }
} }
void InterfaceEventDispatcher::dispatchTextEditing(const std::string & text) void EventDispatcher::dispatchTextEditing(const std::string & text)
{ {
for(auto it : textInterested) for(auto it : textInterested)
{ {
@ -210,7 +211,7 @@ void InterfaceEventDispatcher::dispatchTextEditing(const std::string & text)
} }
} }
void InterfaceEventDispatcher::dispatchMouseMoved(const Point & position) void EventDispatcher::dispatchMouseMoved(const Point & position)
{ {
//sending active, hovered hoverable objects hover() call //sending active, hovered hoverable objects hover() call
CIntObjectList hlp; CIntObjectList hlp;

View File

@ -1,5 +1,5 @@
/* /*
* CGuiHandler.h, part of VCMI engine * EventDispatcher.h, part of VCMI engine
* *
* Authors: listed in file AUTHORS in main folder * Authors: listed in file AUTHORS in main folder
* *
@ -17,7 +17,7 @@ class AEventsReceiver;
enum class MouseButton; enum class MouseButton;
enum class EShortcut; enum class EShortcut;
class InterfaceEventDispatcher class EventDispatcher
{ {
using CIntObjectList = std::list<AEventsReceiver *>; using CIntObjectList = std::list<AEventsReceiver *>;

View File

@ -0,0 +1,79 @@
/*
* EventsReceiver.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "EventsReceiver.h"
#include "MouseButton.h"
#include "CGuiHandler.h"
#include "EventDispatcher.h"
AEventsReceiver::AEventsReceiver()
: activeState(0)
, hoveredState(false)
, strongInterestState(false)
{
}
bool AEventsReceiver::isHovered() const
{
return hoveredState;
}
bool AEventsReceiver::isActive() const
{
return activeState;
}
bool AEventsReceiver::isActive(int flags) const
{
return activeState & flags;
}
bool AEventsReceiver::isMouseButtonPressed(MouseButton btn) const
{
return currentMouseState.count(btn) ? currentMouseState.at(btn) : false;
}
void AEventsReceiver::setMoveEventStrongInterest(bool on)
{
strongInterestState = on;
}
void AEventsReceiver::activateEvents(ui16 what)
{
assert((what & GENERAL) || (activeState & GENERAL));
activeState |= GENERAL;
GH.eventDispatcher().handleElementActivate(this, what);
}
void AEventsReceiver::deactivateEvents(ui16 what)
{
if (what & GENERAL)
activeState &= ~GENERAL;
GH.eventDispatcher().handleElementDeActivate(this, what & activeState);
}
void AEventsReceiver::click(MouseButton btn, tribool down, bool previousState)
{
switch(btn)
{
default:
case MouseButton::LEFT:
clickLeft(down, previousState);
break;
case MouseButton::MIDDLE:
clickMiddle(down, previousState);
break;
case MouseButton::RIGHT:
clickRight(down, previousState);
break;
}
}

View File

@ -0,0 +1,68 @@
/*
* EventsReceiver.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
class Point;
VCMI_LIB_NAMESPACE_END
class EventDispatcher;
enum class MouseButton;
enum class EShortcut;
using boost::logic::tribool;
class AEventsReceiver
{
friend class EventDispatcher;
ui16 activeState;
std::map<MouseButton, bool> currentMouseState;
bool hoveredState; //for determining if object is hovered
bool strongInterestState; //if true - report all mouse movements, if not - only when hovered
void click(MouseButton btn, tribool down, bool previousState);
protected:
void setMoveEventStrongInterest(bool on);
void activateEvents(ui16 what);
void deactivateEvents(ui16 what);
virtual void clickLeft(tribool down, bool previousState) {}
virtual void clickRight(tribool down, bool previousState) {}
virtual void clickMiddle(tribool down, bool previousState) {}
virtual void textInputed(const std::string & enteredText) {}
virtual void textEdited(const std::string & enteredText) {}
virtual void tick(uint32_t msPassed) {}
virtual void wheelScrolled(bool down, bool in) {}
virtual void mouseMoved(const Point & cursorPosition) {}
virtual void hover(bool on) {}
virtual void onDoubleClick() {}
virtual void keyPressed(EShortcut key) {}
virtual void keyReleased(EShortcut key) {}
virtual bool captureThisKey(EShortcut key) = 0;
virtual bool isInside(const Point & position) = 0;
public:
AEventsReceiver();
virtual ~AEventsReceiver() = default;
// These are the arguments that can be used to determine what kind of input the CIntObject will receive
enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, TEXTINPUT=512, MCLICK=1024, ALL=0xffff};
bool isHovered() const;
bool isActive() const;
bool isActive(int flags) const;
bool isMouseButtonPressed(MouseButton btn) const;
};