Mouse and touch input are both handled through the Pointer System. This provides an event driven API for working with “pointers” in a cross platform manor. A pointer represents a single finger on the screen on touch devices or a mouse on desktop.
There are 4 types of event exposed by the Pointer System:
- Pointer Down: This is called either when a finger is pressed down on the screen, or a mouse button is pressed down. An input type is provided which describes which button was pressed, or that it was a touch in the case of mobile.
- Pointer Moved: This is called whenever an active pointer moves. On mobile, this will only be sent between pointer down and pointer up events, but on desktop this will be sent when there is any mouse movement. A list of input types is provided to allow querying for which mouse buttons are currently down.
- Pointer Up: This is called whenever a finger is removed from the screen or a mouse button is released. Again, an input type is provided to describe which button or touch was pressed.
- Pointer Scrolled: This is called when the mouse wheel is scrolled. This will never be fired on mobile.
It is often desirable to only receive pointer events that have not already been used by things like UI.
To manage this there are “filtered” version of the down, up and scrolled events. These provide the same information as the standard events except that they will not be notified if an internal system like the UI has consumed it. The moved event cannot be consumed so the standard version of this should be used in both cases.
Each event receives a Pointer object. This contains information on the current state of the pointer, including the current and previous position and the currently active input types. It also provides two ways of identifying a pointer: The Id and the index. The Id is a unique identifier for that pointer. On desktop this will never change as there is only ever one pointer, the mouse, however this is useful on mobile to check pointer are the same. The index describes how many pointers were down at the time of creation.
Listening for mouse events involves working with Chilli Source’s event API. Events allow multiple listeners, all of which will be notified when the event is fired. While listening to an event a connection must be maintained, else the listener will be removed. This is handled by holding on to an Event Connection – once it goes out of scope the connection is terminated. Typically the listener stores the connection as a member so the connection ends when the listener goes out of scope.
class PointerEventListener
{
public:
void Listen();
private:
CSCore::EventConnectionUPtr m_pointerDownConnection;
CSCore::EventConnectionUPtr m_pointerMovedConnection;
CSCore::EventConnectionUPtr m_pointerUpConnection;
CSCore::EventConnectionUPtr m_pointerScrolledConnection;
};
The Pointer System exposes the events through getters. Listening is simply a case of adding either a function pointer or a lambda.
void PointerEventListener::Listen()
{
CSInput::PointerSystem* pointerSystem = CSCore::Application::Get()->GetSystem();
m_pointerDownConnection = pointerSystem->GetPointerDownEventFiltered().OpenConnection([](const CSInput::Pointer& in_pointer, f64 in_timestamp, CSInput::Pointer::InputType in_inputType)
{
CS_LOG_VERBOSE("Pointer #" + CSCore::ToString(in_pointer.GetId()) + " Down.");
});
m_pointerMovedConnection = pointerSystem->GetPointerMovedEvent().OpenConnection([](const CSInput::Pointer& in_pointer, f64 in_timestamp)
{
CS_LOG_VERBOSE("Pointer #" + CSCore::ToString(in_pointer.GetId()) + " Moved.");
});
m_pointerUpConnection = pointerSystem->GetPointerUpEventFiltered().OpenConnection([](const CSInput::Pointer& in_pointer, f64 in_timestamp, CSInput::Pointer::InputType in_inputType)
{
CS_LOG_VERBOSE("Pointer #" + CSCore::ToString(in_pointer.GetId()) + " Up.");
});
m_pointerScrolledConnection = pointerSystem->GetPointerScrollEventFiltered().OpenConnection([](const CSInput::Pointer& in_pointer, f64 in_timestamp, const CSCore::Vector2& in_scrollDelta)
{
CS_LOG_VERBOSE("Pointer #" + CSCore::ToString(in_pointer.GetId()) + " Scrolled.");
});
}