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 13:28:41 +03:30
|
|
|
: m_KeyboadKeys {}
|
|
|
|
, m_MouseButtons {}
|
|
|
|
, m_MousePosition {}
|
|
|
|
, m_MouseDelta {}
|
|
|
|
, m_MouseWheelDelta {}
|
|
|
|
, m_UserInterfaceEvents(true)
|
|
|
|
, m_GameEvents(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 */)
|
|
|
|
{
|
|
|
|
m_UserInterfaceEvents = toggle ? !m_UserInterfaceEvents : receive;
|
|
|
|
}
|
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 13:28:41 +03:30
|
|
|
bool prev = m_GameEvents;
|
2022-03-07 21:57:00 +03:30
|
|
|
m_GameEvents = toggle ? !m_UserInterfaceEvents : receive;
|
|
|
|
|
|
|
|
if (m_GameEvents != prev)
|
|
|
|
RestartInputState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::RestartInputState()
|
|
|
|
{
|
|
|
|
m_KeyboadKeys.fill(false);
|
|
|
|
m_MouseButtons.fill(false);
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
m_MousePosition = glm::vec2(0.0f);
|
|
|
|
m_MouseDelta = glm::vec2(0.0f);
|
2022-03-07 21:57:00 +03:30
|
|
|
m_MouseWheelDelta = 0.0f;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_GameEvents)
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
m_MouseDelta = event.GetPosition() - m_MousePosition;
|
2022-03-07 21:57:00 +03:30
|
|
|
m_MousePosition = event.GetPosition();
|
|
|
|
}
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_UserInterfaceEvents)
|
|
|
|
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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_GameEvents)
|
|
|
|
m_MouseButtons[event.GetButton()] = true;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_UserInterfaceEvents)
|
|
|
|
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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_GameEvents)
|
|
|
|
m_MouseButtons[event.GetButton()] = false;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_UserInterfaceEvents)
|
|
|
|
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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_GameEvents)
|
|
|
|
m_MouseWheelDelta = event.GetOffset();
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_UserInterfaceEvents)
|
|
|
|
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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_GameEvents)
|
|
|
|
m_KeyboadKeys[event.GetKey()] = true;
|
2021-07-21 20:03:39 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_UserInterfaceEvents)
|
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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_GameEvents)
|
|
|
|
m_KeyboadKeys[event.GetKey()] = false;
|
2021-08-01 16:43:59 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (m_UserInterfaceEvents)
|
|
|
|
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:
|
|
|
|
{
|
|
|
|
if (m_UserInterfaceEvents)
|
|
|
|
{
|
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
|