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:
@@ -44,7 +44,9 @@ public:
|
|||||||
InputHandler();
|
InputHandler();
|
||||||
~InputHandler();
|
~InputHandler();
|
||||||
|
|
||||||
|
/// Fetches events from SDL input system and prepares them for processing
|
||||||
void fetchEvents();
|
void fetchEvents();
|
||||||
|
/// Performs actual processing and dispatching of previously fetched events
|
||||||
void processEvents();
|
void processEvents();
|
||||||
|
|
||||||
/// drops all incoming events without processing them
|
/// drops all incoming events without processing them
|
||||||
@@ -52,11 +54,20 @@ public:
|
|||||||
bool ignoreEventsUntilInput();
|
bool ignoreEventsUntilInput();
|
||||||
|
|
||||||
void fakeMoveCursor(float dx, float dy);
|
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);
|
void startTextInput(const Rect & where);
|
||||||
|
|
||||||
|
/// Ends any existing text input state
|
||||||
void stopTextInput();
|
void stopTextInput();
|
||||||
|
|
||||||
|
/// Returns true if selected mouse button is pressed at the moment
|
||||||
bool isMouseButtonPressed(MouseButton button) const;
|
bool isMouseButtonPressed(MouseButton button) const;
|
||||||
|
|
||||||
|
/// Generates new user event that will be processed on next frame
|
||||||
void pushUserEvent(EUserEvent usercode, void * userdata);
|
void pushUserEvent(EUserEvent usercode, void * userdata);
|
||||||
|
|
||||||
|
/// Returns current position of cursor, in VCMI logical screen coordinates
|
||||||
const Point & getCursorPosition() const;
|
const Point & getCursorPosition() const;
|
||||||
|
|
||||||
/// returns true if chosen keyboard key is currently pressed down
|
/// returns true if chosen keyboard key is currently pressed down
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
struct SDL_KeyboardEvent;
|
struct SDL_KeyboardEvent;
|
||||||
|
|
||||||
|
/// Class that handles keyboard input from SDL events
|
||||||
class InputSourceKeyboard
|
class InputSourceKeyboard
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@@ -14,6 +14,7 @@ struct SDL_MouseWheelEvent;
|
|||||||
struct SDL_MouseMotionEvent;
|
struct SDL_MouseMotionEvent;
|
||||||
struct SDL_MouseButtonEvent;
|
struct SDL_MouseButtonEvent;
|
||||||
|
|
||||||
|
/// Class that handles mouse input from SDL events
|
||||||
class InputSourceMouse
|
class InputSourceMouse
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@@ -17,6 +17,7 @@ VCMI_LIB_NAMESPACE_END
|
|||||||
struct SDL_TextEditingEvent;
|
struct SDL_TextEditingEvent;
|
||||||
struct SDL_TextInputEvent;
|
struct SDL_TextInputEvent;
|
||||||
|
|
||||||
|
/// Class that handles text input (e.g. IME or direct input from physical keyboard) from SDL events
|
||||||
class InputSourceText
|
class InputSourceText
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@@ -16,6 +16,7 @@ VCMI_LIB_NAMESPACE_END
|
|||||||
|
|
||||||
struct SDL_TouchFingerEvent;
|
struct SDL_TouchFingerEvent;
|
||||||
|
|
||||||
|
/// Class that handles touchscreen input from SDL events
|
||||||
class InputSourceTouch
|
class InputSourceTouch
|
||||||
{
|
{
|
||||||
bool multifinger;
|
bool multifinger;
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
struct SDL_UserEvent;
|
struct SDL_UserEvent;
|
||||||
|
|
||||||
|
/// Class for handling events of type SDL_UserEvent
|
||||||
class UserEventHandler
|
class UserEventHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@@ -17,47 +17,48 @@
|
|||||||
|
|
||||||
#include "../../lib/Point.h"
|
#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)
|
||||||
cb(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);
|
processLists(activityFlag,[&](EventReceiversList & lst){
|
||||||
processList(AEventsReceiver::RCLICK,activityFlag,&rclickable,cb);
|
lst.push_front(elem);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
elem->activeState |= activityFlag;
|
elem->activeState |= activityFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventDispatcher::handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag)
|
void EventDispatcher::deactivateElement(AEventsReceiver * elem, ui16 activityFlag)
|
||||||
{
|
{
|
||||||
processLists(activityFlag,[&](CIntObjectList * lst){
|
processLists(activityFlag,[&](EventReceiversList & lst){
|
||||||
auto hlp = std::find(lst->begin(),lst->end(),elem);
|
auto hlp = std::find(lst.begin(),lst.end(),elem);
|
||||||
assert(hlp != lst->end());
|
assert(hlp != lst.end());
|
||||||
lst->erase(hlp);
|
lst.erase(hlp);
|
||||||
});
|
});
|
||||||
elem->activeState &= ~activityFlag;
|
elem->activeState &= ~activityFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventDispatcher::dispatchTimer(uint32_t msPassed)
|
void EventDispatcher::dispatchTimer(uint32_t msPassed)
|
||||||
{
|
{
|
||||||
CIntObjectList hlp = timeinterested;
|
EventReceiversList hlp = timeinterested;
|
||||||
for (auto & elem : hlp)
|
for (auto & elem : hlp)
|
||||||
{
|
{
|
||||||
if(!vstd::contains(timeinterested,elem)) continue;
|
if(!vstd::contains(timeinterested,elem)) continue;
|
||||||
@@ -74,7 +75,7 @@ void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & sho
|
|||||||
if(i->captureThisKey(shortcut))
|
if(i->captureThisKey(shortcut))
|
||||||
keysCaptured = true;
|
keysCaptured = true;
|
||||||
|
|
||||||
CIntObjectList miCopy = keyinterested;
|
EventReceiversList miCopy = keyinterested;
|
||||||
|
|
||||||
for(auto & i : miCopy)
|
for(auto & i : miCopy)
|
||||||
{
|
{
|
||||||
@@ -97,7 +98,7 @@ void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & sh
|
|||||||
if(i->captureThisKey(shortcut))
|
if(i->captureThisKey(shortcut))
|
||||||
keysCaptured = true;
|
keysCaptured = true;
|
||||||
|
|
||||||
CIntObjectList miCopy = keyinterested;
|
EventReceiversList miCopy = keyinterested;
|
||||||
|
|
||||||
for(auto & i : miCopy)
|
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)
|
switch (button)
|
||||||
{
|
{
|
||||||
@@ -156,7 +157,7 @@ void EventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, co
|
|||||||
handleMouseButtonClick(getListForMouseButton(button), button, false);
|
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;
|
auto hlp = interestedObjs;
|
||||||
for(auto & i : hlp)
|
for(auto & i : hlp)
|
||||||
@@ -180,7 +181,7 @@ void EventDispatcher::handleMouseButtonClick(CIntObjectList & interestedObjs, Mo
|
|||||||
|
|
||||||
void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
|
void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
|
||||||
{
|
{
|
||||||
CIntObjectList hlp = wheelInterested;
|
EventReceiversList hlp = wheelInterested;
|
||||||
for(auto & i : hlp)
|
for(auto & i : hlp)
|
||||||
{
|
{
|
||||||
if(!vstd::contains(wheelInterested,i))
|
if(!vstd::contains(wheelInterested,i))
|
||||||
@@ -208,7 +209,7 @@ void EventDispatcher::dispatchTextEditing(const std::string & text)
|
|||||||
void EventDispatcher::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;
|
EventReceiversList hlp;
|
||||||
|
|
||||||
auto hoverableCopy = hoverable;
|
auto hoverableCopy = hoverable;
|
||||||
for(auto & elem : hoverableCopy)
|
for(auto & elem : hoverableCopy)
|
||||||
@@ -232,7 +233,7 @@ void EventDispatcher::dispatchMouseMoved(const Point & position)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//sending active, MotionInterested objects mouseMoved() call
|
//sending active, MotionInterested objects mouseMoved() call
|
||||||
CIntObjectList miCopy = motioninterested;
|
EventReceiversList miCopy = motioninterested;
|
||||||
for(auto & elem : miCopy)
|
for(auto & elem : miCopy)
|
||||||
{
|
{
|
||||||
if(elem->strongInterestState || elem->isInside(position)) //checking bounds including border fixes bug #2476
|
if(elem->strongInterestState || elem->isInside(position)) //checking bounds including border fixes bug #2476
|
||||||
|
@@ -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 that receives events from event producers and dispatches it to UI elements that are interested in this event
|
||||||
class EventDispatcher
|
class EventDispatcher
|
||||||
{
|
{
|
||||||
using CIntObjectList = std::list<AEventsReceiver *>;
|
using EventReceiversList = std::list<AEventsReceiver *>;
|
||||||
|
|
||||||
/// list of UI elements that are interested in particular event
|
/// list of UI elements that are interested in particular event
|
||||||
CIntObjectList lclickable;
|
EventReceiversList lclickable;
|
||||||
CIntObjectList rclickable;
|
EventReceiversList rclickable;
|
||||||
CIntObjectList mclickable;
|
EventReceiversList mclickable;
|
||||||
CIntObjectList hoverable;
|
EventReceiversList hoverable;
|
||||||
CIntObjectList keyinterested;
|
EventReceiversList keyinterested;
|
||||||
CIntObjectList motioninterested;
|
EventReceiversList motioninterested;
|
||||||
CIntObjectList timeinterested;
|
EventReceiversList timeinterested;
|
||||||
CIntObjectList wheelInterested;
|
EventReceiversList wheelInterested;
|
||||||
CIntObjectList doubleClickInterested;
|
EventReceiversList doubleClickInterested;
|
||||||
CIntObjectList textInterested;
|
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);
|
template<typename Functor>
|
||||||
void processLists(ui16 activityFlag, std::function<void(CIntObjectList *)> cb);
|
void processLists(ui16 activityFlag, const Functor & cb);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// add specified UI element as interested. Uses unnamed enum from AEventsReceiver for activity flags
|
/// 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
|
/// removes specified UI element as interested for specified activities
|
||||||
void handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag);
|
void deactivateElement(AEventsReceiver * elem, ui16 activityFlag);
|
||||||
|
|
||||||
/// Regular timer event
|
/// Regular timer event
|
||||||
void dispatchTimer(uint32_t msPassed);
|
void dispatchTimer(uint32_t msPassed);
|
||||||
|
@@ -46,14 +46,14 @@ void AEventsReceiver::activateEvents(ui16 what)
|
|||||||
assert((what & GENERAL) || (activeState & GENERAL));
|
assert((what & GENERAL) || (activeState & GENERAL));
|
||||||
|
|
||||||
activeState |= GENERAL;
|
activeState |= GENERAL;
|
||||||
GH.events().handleElementActivate(this, what);
|
GH.events().activateElement(this, what);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEventsReceiver::deactivateEvents(ui16 what)
|
void AEventsReceiver::deactivateEvents(ui16 what)
|
||||||
{
|
{
|
||||||
if (what & GENERAL)
|
if (what & GENERAL)
|
||||||
activeState &= ~GENERAL;
|
activeState &= ~GENERAL;
|
||||||
GH.events().handleElementDeActivate(this, what & activeState);
|
GH.events().deactivateElement(this, what & activeState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEventsReceiver::click(MouseButton btn, tribool down, bool previousState)
|
void AEventsReceiver::click(MouseButton btn, tribool down, bool previousState)
|
||||||
|
Reference in New Issue
Block a user