-
Notifications
You must be signed in to change notification settings - Fork 3
Handling Input
The engine provides options for the developer to handle both keyboard and mouse input. The core functions for processing these user inputs are located in Input.cpp
.
Once input has been processed, they should ideally be handled by the event system, to ensure that the input system remains decoupled from the other engine systems.
The function for updating keyboard inputs provides a set of while
loops for handling single key presses, character inputs, and continuous key presses.
void Input::UpdateKeyboard( const float dt )
{
// Handle input for single key presses
while ( !m_keyboard.KeyBufferIsEmpty() )
{
Keyboard::KeyboardEvent kbe = m_keyboard.ReadKey();
unsigned char keycode = kbe.GetKeyCode();
}
// Handle character input (used by 'Input' widget)
while ( !m_keyboard.CharBufferIsEmpty() )
{
unsigned char keycode = m_keyboard.ReadChar();
}
// Handle continuous key presses
if ( m_keyboard.KeyIsPressed( 'W' ) )
EventSystem::Instance()->AddEvent( EVENTID::PlayerUp );
}
The function for updating mouse input can be used to check for mouse events, like clicking and releasing, and can handle single or continuous mouse clicks.
void Input::UpdateMouse( const float dt )
{
// update camera orientation
while ( !m_mouse.EventBufferIsEmpty() )
{
Mouse::MouseEvent me = m_mouse.ReadEvent();
if ( me.GetType() == Mouse::MouseEvent::EventType::RawMove )
{
// Raw mouse movement
}
if ( me.GetType() == Mouse::MouseEvent::EventType::LPress )
{
// Left mouse button pressed
}
else if ( me.GetType() == Mouse::MouseEvent::EventType::LRelease )
{
// Left mouse button released
}
}
}
All input in the system is first processed in the WindowContainer.cpp
class, before it is offloaded to the input Input.cpp
class. ImGui inputs are prioritized over Win32 inputs, such that ImGui windows will always consume mouse input while the editor is active.
Raw mouse and keyboard are handled here, with events to fire them directly out to the rest of the code base. For more refined keyboard and mouse handling, this is where the Input
class should be used instead.
Page Author: Kyle Robinson
© NINE BYTE WARRIORS | MIDNIGHT HARVEST