2025-07-05 13:28:41 +03:30
|
|
|
#include <engine/events/char.hpp>
|
|
|
|
#include <engine/events/event.hpp>
|
|
|
|
#include <engine/events/keyboard.hpp>
|
|
|
|
#include <engine/events/mouse.hpp>
|
|
|
|
#include <engine/input/input.hpp>
|
|
|
|
#include <engine/input/key_codes.hpp>
|
2021-07-21 20:03:39 +04:30
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
Input *Input::s_Context = nullptr;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
Scope<Input> Input::Create()
|
|
|
|
{
|
|
|
|
return MakeScope(new Input);
|
|
|
|
}
|
2021-07-26 11:43:37 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
Input::Input()
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_keyboad_keys {}
|
|
|
|
, m_mouse_buttons {}
|
|
|
|
, m_mouse_position {}
|
|
|
|
, m_mouse_delta {}
|
|
|
|
, m_mouse_wheel_delta {}
|
|
|
|
, m_user_interface_events(true)
|
|
|
|
, m_game_events(true)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
ASSERT(
|
|
|
|
!s_Context,
|
|
|
|
"Input::Input: an instance of 'Input' already exists, do not construct this class!"
|
|
|
|
);
|
2022-03-07 21:57:00 +03:30
|
|
|
s_Context = this;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
RestartInputState();
|
|
|
|
}
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void Input::ReceiveUserInterfaceEventsImpl(bool receive, bool toggle /* = false */)
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_user_interface_events = toggle ? !m_user_interface_events : receive;
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void Input::ReceieveGameEventsImpl(bool receive, bool toggle /*= false*/)
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
bool prev = m_game_events;
|
|
|
|
m_game_events = toggle ? !m_user_interface_events : receive;
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events != prev)
|
2022-03-07 21:57:00 +03:30
|
|
|
RestartInputState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::RestartInputState()
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_keyboad_keys.fill(false);
|
|
|
|
m_mouse_buttons.fill(false);
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_mouse_position = glm::vec2(0.0f);
|
|
|
|
m_mouse_delta = glm::vec2(0.0f);
|
|
|
|
m_mouse_wheel_delta = 0.0f;
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
void Input::OnEvent(const Event &inputEvent)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
2022-03-07 21:57:00 +03:30
|
|
|
switch (inputEvent.GetEventType())
|
2021-07-21 20:03:39 +04:30
|
|
|
{
|
2022-03-07 21:57:00 +03:30
|
|
|
//** MOUSE_EVENTS **//
|
|
|
|
case EventType::MouseMoved:
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const MouseMovedEvent &event = (const MouseMovedEvent &)inputEvent;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_mouse_delta = event.GetPosition() - m_mouse_position;
|
|
|
|
m_mouse_position = event.GetPosition();
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
io.MousePos = ImVec2(event.GetX(), event.GetY());
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
return;
|
2021-07-21 20:03:39 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
case EventType::ButtonPressed:
|
2021-07-21 20:03:39 +04:30
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const ButtonPressedEvent &event = (const ButtonPressedEvent &)inputEvent;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events)
|
|
|
|
m_mouse_buttons[event.GetButton()] = true;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
io.MouseDown[event.GetButton()] = true;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
case EventType::ButtonReleased:
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const ButtonReleasedEvent &event = (const ButtonReleasedEvent &)inputEvent;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events)
|
|
|
|
m_mouse_buttons[event.GetButton()] = false;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
io.MouseDown[event.GetButton()] = false;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
case EventType::WheelScrolled:
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const WheelScrolledEvent &event = (const WheelScrolledEvent &)inputEvent;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events)
|
|
|
|
m_mouse_wheel_delta = event.GetOffset();
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
io.MouseWheel = event.GetOffset();
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
//** KEYBOARD_EVENTS **//
|
|
|
|
case EventType::KeyPressed:
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const KeyPressedEvent &event = (const KeyPressedEvent &)inputEvent;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events)
|
|
|
|
m_keyboad_keys[event.GetKey()] = true;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2021-07-21 20:03:39 +04:30
|
|
|
{
|
2022-03-07 21:57:00 +03:30
|
|
|
io.KeysDown[event.GetKey()] = true;
|
2025-07-05 13:28:41 +03:30
|
|
|
// if (event.GetKey() == Key::BackSpace)
|
2022-03-07 21:57:00 +03:30
|
|
|
// io.AddInputCharacter(Key::BackSpace);
|
2021-07-21 20:03:39 +04:30
|
|
|
}
|
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
case EventType::KeyReleased:
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const KeyReleasedEvent &event = (const KeyReleasedEvent &)inputEvent;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_game_events)
|
|
|
|
m_keyboad_keys[event.GetKey()] = false;
|
2021-08-01 16:43:59 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
io.KeysDown[event.GetKey()] = false;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
case EventType::SetChar:
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_user_interface_events)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
const SetCharEvent &event = (const SetCharEvent &)inputEvent;
|
2022-03-07 21:57:00 +03:30
|
|
|
io.AddInputCharacter(event.GetCharacter());
|
2021-07-21 20:03:39 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 20:03:39 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
} // namespace Light
|