1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Better documentation & cleanup

This commit is contained in:
Ivan Savenko
2023-05-20 01:51:10 +03:00
parent a4000f2fc8
commit 6981848288
9 changed files with 69 additions and 52 deletions

View File

@@ -44,7 +44,9 @@ public:
InputHandler();
~InputHandler();
/// Fetches events from SDL input system and prepares them for processing
void fetchEvents();
/// Performs actual processing and dispatching of previously fetched events
void processEvents();
/// drops all incoming events without processing them
@@ -52,11 +54,20 @@ public:
bool ignoreEventsUntilInput();
void fakeMoveCursor(float dx, float dy);
/// Initiates text input in selected area, potentially creating IME popup (mobile systems only at the moment)
void startTextInput(const Rect & where);
/// Ends any existing text input state
void stopTextInput();
/// Returns true if selected mouse button is pressed at the moment
bool isMouseButtonPressed(MouseButton button) const;
/// Generates new user event that will be processed on next frame
void pushUserEvent(EUserEvent usercode, void * userdata);
/// Returns current position of cursor, in VCMI logical screen coordinates
const Point & getCursorPosition() const;
/// returns true if chosen keyboard key is currently pressed down

View File

@@ -12,6 +12,7 @@
struct SDL_KeyboardEvent;
/// Class that handles keyboard input from SDL events
class InputSourceKeyboard
{
public:

View File

@@ -14,6 +14,7 @@ struct SDL_MouseWheelEvent;
struct SDL_MouseMotionEvent;
struct SDL_MouseButtonEvent;
/// Class that handles mouse input from SDL events
class InputSourceMouse
{
public:

View File

@@ -17,6 +17,7 @@ VCMI_LIB_NAMESPACE_END
struct SDL_TextEditingEvent;
struct SDL_TextInputEvent;
/// Class that handles text input (e.g. IME or direct input from physical keyboard) from SDL events
class InputSourceText
{
public:

View File

@@ -16,6 +16,7 @@ VCMI_LIB_NAMESPACE_END
struct SDL_TouchFingerEvent;
/// Class that handles touchscreen input from SDL events
class InputSourceTouch
{
bool multifinger;

View File

@@ -12,6 +12,7 @@
struct SDL_UserEvent;
/// Class for handling events of type SDL_UserEvent
class UserEventHandler
{
public:

View File

@@ -17,47 +17,48 @@
#include "../../lib/Point.h"
void EventDispatcher::processList(const ui16 mask, const ui16 flag, CIntObjectList *lst, std::function<void (CIntObjectList *)> cb)
template<typename Functor>
void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
{
if (mask & flag)
auto processList = [&](ui16 mask, EventReceiversList & lst)
{
if(mask & activityFlag)
cb(lst);
};
processList(AEventsReceiver::LCLICK, lclickable);
processList(AEventsReceiver::RCLICK, rclickable);
processList(AEventsReceiver::MCLICK, mclickable);
processList(AEventsReceiver::HOVER, hoverable);
processList(AEventsReceiver::MOVE, motioninterested);
processList(AEventsReceiver::KEYBOARD, keyinterested);
processList(AEventsReceiver::TIME, timeinterested);
processList(AEventsReceiver::WHEEL, wheelInterested);
processList(AEventsReceiver::DOUBLECLICK, doubleClickInterested);
processList(AEventsReceiver::TEXTINPUT, textInterested);
}
void EventDispatcher::processLists(ui16 activityFlag, std::function<void (CIntObjectList *)> cb)
void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag)
{
processList(AEventsReceiver::LCLICK,activityFlag,&lclickable,cb);
processList(AEventsReceiver::RCLICK,activityFlag,&rclickable,cb);
processList(AEventsReceiver::MCLICK,activityFlag,&mclickable,cb);
processList(AEventsReceiver::HOVER,activityFlag,&hoverable,cb);
processList(AEventsReceiver::MOVE,activityFlag,&motioninterested,cb);
processList(AEventsReceiver::KEYBOARD,activityFlag,&keyinterested,cb);
processList(AEventsReceiver::TIME,activityFlag,&timeinterested,cb);
processList(AEventsReceiver::WHEEL,activityFlag,&wheelInterested,cb);
processList(AEventsReceiver::DOUBLECLICK,activityFlag,&doubleClickInterested,cb);
processList(AEventsReceiver::TEXTINPUT,activityFlag,&textInterested,cb);
}
void EventDispatcher::handleElementActivate(AEventsReceiver * elem, ui16 activityFlag)
{
processLists(activityFlag,[&](CIntObjectList * lst){
lst->push_front(elem);
processLists(activityFlag,[&](EventReceiversList & lst){
lst.push_front(elem);
});
elem->activeState |= activityFlag;
}
void EventDispatcher::handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag)
void EventDispatcher::deactivateElement(AEventsReceiver * elem, ui16 activityFlag)
{
processLists(activityFlag,[&](CIntObjectList * lst){
auto hlp = std::find(lst->begin(),lst->end(),elem);
assert(hlp != lst->end());
lst->erase(hlp);
processLists(activityFlag,[&](EventReceiversList & lst){
auto hlp = std::find(lst.begin(),lst.end(),elem);
assert(hlp != lst.end());
lst.erase(hlp);
});
elem->activeState &= ~activityFlag;
}
void EventDispatcher::dispatchTimer(uint32_t msPassed)
{
CIntObjectList hlp = timeinterested;
EventReceiversList hlp = timeinterested;
for (auto & elem : hlp)
{
if(!vstd::contains(timeinterested,elem)) continue;
@@ -74,7 +75,7 @@ void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & sho
if(i->captureThisKey(shortcut))
keysCaptured = true;
CIntObjectList miCopy = keyinterested;
EventReceiversList miCopy = keyinterested;
for(auto & i : miCopy)
{
@@ -97,7 +98,7 @@ void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & sh
if(i->captureThisKey(shortcut))
keysCaptured = true;
CIntObjectList miCopy = keyinterested;
EventReceiversList miCopy = keyinterested;
for(auto & i : miCopy)
{
@@ -111,7 +112,7 @@ void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & sh
}
}
EventDispatcher::CIntObjectList & EventDispatcher::getListForMouseButton(MouseButton button)
EventDispatcher::EventReceiversList & EventDispatcher::getListForMouseButton(MouseButton button)
{
switch (button)
{
@@ -156,7 +157,7 @@ void EventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, co
handleMouseButtonClick(getListForMouseButton(button), button, false);
}
void EventDispatcher::handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed)
void EventDispatcher::handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed)
{
auto hlp = interestedObjs;
for(auto & i : hlp)
@@ -180,7 +181,7 @@ void EventDispatcher::handleMouseButtonClick(CIntObjectList & interestedObjs, Mo
void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
{
CIntObjectList hlp = wheelInterested;
EventReceiversList hlp = wheelInterested;
for(auto & i : hlp)
{
if(!vstd::contains(wheelInterested,i))
@@ -208,7 +209,7 @@ void EventDispatcher::dispatchTextEditing(const std::string & text)
void EventDispatcher::dispatchMouseMoved(const Point & position)
{
//sending active, hovered hoverable objects hover() call
CIntObjectList hlp;
EventReceiversList hlp;
auto hoverableCopy = hoverable;
for(auto & elem : hoverableCopy)
@@ -232,7 +233,7 @@ void EventDispatcher::dispatchMouseMoved(const Point & position)
}
//sending active, MotionInterested objects mouseMoved() call
CIntObjectList miCopy = motioninterested;
EventReceiversList miCopy = motioninterested;
for(auto & elem : miCopy)
{
if(elem->strongInterestState || elem->isInside(position)) //checking bounds including border fixes bug #2476

View File

@@ -20,33 +20,33 @@ enum class EShortcut;
/// Class that receives events from event producers and dispatches it to UI elements that are interested in this event
class EventDispatcher
{
using CIntObjectList = std::list<AEventsReceiver *>;
using EventReceiversList = std::list<AEventsReceiver *>;
/// list of UI elements that are interested in particular event
CIntObjectList lclickable;
CIntObjectList rclickable;
CIntObjectList mclickable;
CIntObjectList hoverable;
CIntObjectList keyinterested;
CIntObjectList motioninterested;
CIntObjectList timeinterested;
CIntObjectList wheelInterested;
CIntObjectList doubleClickInterested;
CIntObjectList textInterested;
EventReceiversList lclickable;
EventReceiversList rclickable;
EventReceiversList mclickable;
EventReceiversList hoverable;
EventReceiversList keyinterested;
EventReceiversList motioninterested;
EventReceiversList timeinterested;
EventReceiversList wheelInterested;
EventReceiversList doubleClickInterested;
EventReceiversList textInterested;
CIntObjectList & getListForMouseButton(MouseButton button);
EventReceiversList & getListForMouseButton(MouseButton button);
void handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed);
void handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed);
void processList(const ui16 mask, const ui16 flag, CIntObjectList * lst, std::function<void(CIntObjectList *)> cb);
void processLists(ui16 activityFlag, std::function<void(CIntObjectList *)> cb);
template<typename Functor>
void processLists(ui16 activityFlag, const Functor & cb);
public:
/// add specified UI element as interested. Uses unnamed enum from AEventsReceiver for activity flags
void handleElementActivate(AEventsReceiver * elem, ui16 activityFlag);
void activateElement(AEventsReceiver * elem, ui16 activityFlag);
/// removes specified UI element as interested for specified activities
void handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag);
void deactivateElement(AEventsReceiver * elem, ui16 activityFlag);
/// Regular timer event
void dispatchTimer(uint32_t msPassed);

View File

@@ -46,14 +46,14 @@ void AEventsReceiver::activateEvents(ui16 what)
assert((what & GENERAL) || (activeState & GENERAL));
activeState |= GENERAL;
GH.events().handleElementActivate(this, what);
GH.events().activateElement(this, what);
}
void AEventsReceiver::deactivateEvents(ui16 what)
{
if (what & GENERAL)
activeState &= ~GENERAL;
GH.events().handleElementDeActivate(this, what & activeState);
GH.events().deactivateElement(this, what & activeState);
}
void AEventsReceiver::click(MouseButton btn, tribool down, bool previousState)