New Events, Fixes

- Added SetCharEvent
- Added KeyRepeatEVent
- Fixed ImGui not picking keys from io.KeyMap because it gets assigned to VK_**
      keys from windows after ImGui_ImplWin32_Init call, had to re-assign them back
      to Key::** (glfw key codes)...
- Removed UserInterface::OnInput
- Maintenance
This commit is contained in:
Light 2021-08-01 16:43:59 +04:30
parent 42f26ac59e
commit 0c8b26360a
14 changed files with 197 additions and 62 deletions

View file

@ -110,6 +110,8 @@ namespace Light {
void Application::OnEvent(const Event& event)
{
LT_ENGINE_TRACE(event.GetInfoLog());
// window
if (event.HasCategory(WindowEventCategory))
{
@ -121,13 +123,14 @@ namespace Light {
// input
if (event.HasCategory(InputEventCategory))
{
m_Input->OnEvent(event);
/* layers */
// return if the event is an input event and 'Input' has disabled the game events
if (event.HasCategory(InputEventCategory) && !m_Input->IsReceivingGameEvents())
return;
if(!m_Input->IsReceivingGameEvents()) // return if the event is an input event and 'Input' has disabled the game events
return;
}
/* layers */
for (auto it = m_LayerStack->rbegin(); it != m_LayerStack->rend(); it++)
if ((*it)->OnEvent(event)) return;
}

View file

@ -0,0 +1,34 @@
#pragma once
#include "Event.h"
#include "Base/Base.h"
#include <sstream>
namespace Light {
class SetCharEvent : public Event
{
private:
const unsigned int m_Character;
public:
SetCharEvent(unsigned int character)
: m_Character(character)
{
}
inline int GetCharacter() const { return m_Character; }
virtual std::string GetInfoLog() const override
{
std::stringstream ss;
ss << "CharSet: " << m_Character;
return ss.str();
}
EVENT_TYPE(SetChar)
EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory)
};
}

View file

@ -10,7 +10,8 @@ namespace Light {
// input
MouseMoved, WheelScrolled, ButtonPressed, ButtonReleased,
KeyPressed, KeyReleased,
KeyPressed, KeyRepeated, KeyReleased,
SetChar,
// window
WindowMoved, WindowResized, WindowClosed, WindowLostFocus, WindowGainFocus,

View file

@ -31,6 +31,29 @@ namespace Light {
EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory)
};
class KeyRepeatEvent : public Event
{
private:
const int m_Key;
public:
KeyRepeatEvent(int key)
: m_Key(key)
{
}
inline int GetKey() const { return m_Key; }
virtual std::string GetInfoLog() const override
{
std::stringstream ss;
ss << "KeyRepeated: " << m_Key;
return ss.str();
}
EVENT_TYPE(KeyRepeated)
EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory)
};
class KeyReleasedEvent : public Event
{
private:

View file

@ -2,11 +2,14 @@
#include "Input.h"
#include "Events/Event.h"
#include "Events/CharEvent.h"
#include "Events/MouseEvents.h"
#include "Events/KeyboardEvents.h"
#include <imgui.h>
#include "Input/KeyCodes.h"
namespace Light {
Input* Input::s_Context = nullptr;
@ -72,7 +75,7 @@ namespace Light {
}
if(m_UserInterfaceEvents)
ImGui::GetIO().MousePos = ImVec2(event.GetX(), event.GetY());
io.MousePos = ImVec2(event.GetX(), event.GetY());
return;
}
@ -84,7 +87,7 @@ namespace Light {
m_MouseButtons[event.GetButton()] = true;
if (m_UserInterfaceEvents)
ImGui::GetIO().MouseDown[event.GetButton()] = true;
io.MouseDown[event.GetButton()] = true;
return;
}
@ -96,7 +99,7 @@ namespace Light {
m_MouseButtons[event.GetButton()] = false;
if (m_UserInterfaceEvents)
ImGui::GetIO().MouseDown[event.GetButton()] = false;
io.MouseDown[event.GetButton()] = false;
return;
}
@ -108,11 +111,11 @@ namespace Light {
m_MouseWheelDelta = event.GetOffset();
if (m_UserInterfaceEvents)
ImGui::GetIO().MouseWheel = event.GetOffset();
io.MouseWheel = event.GetOffset();
return;
}
//** MOUSE_EVENTS **//
//** KEYBOARD_EVENTS **//
case EventType::KeyPressed:
{
const KeyPressedEvent& event = (const KeyPressedEvent&)inputEvent;
@ -121,7 +124,11 @@ namespace Light {
m_KeyboadKeys[event.GetKey()] = true;
if (m_UserInterfaceEvents)
ImGui::GetIO().KeysDown[event.GetKey()] = true;
{
io.KeysDown[event.GetKey()] = true;
//if (event.GetKey() == Key::BackSpace)
// io.AddInputCharacter(Key::BackSpace);
}
return;
}
@ -133,7 +140,17 @@ namespace Light {
m_KeyboadKeys[event.GetKey()] = false;
if (m_UserInterfaceEvents)
ImGui::GetIO().KeysDown[event.GetKey()] = false;
io.KeysDown[event.GetKey()] = false;
return;
}
case EventType::SetChar:
{
if(m_UserInterfaceEvents)
{
const SetCharEvent& event = (const SetCharEvent&)inputEvent;
io.AddInputCharacter(event.GetCharacter());
}
return;
}

View file

@ -2,6 +2,7 @@
#include "Layer.h"
#include "Events/Event.h"
#include "Events/CharEvent.h"
#include "Events/KeyboardEvents.h"
#include "Events/MouseEvents.h"
#include "Events/WindowEvents.h"
@ -18,28 +19,40 @@ namespace Light {
switch (event.GetEventType())
{
/* mouse */
// cursor
case EventType::MouseMoved:
return OnMouseMoved((MouseMovedEvent&)event);
// button
case EventType::ButtonPressed:
return OnButtonPressed((ButtonPressedEvent&)event);
case EventType::ButtonReleased:
return OnButtonReleased((ButtonReleasedEvent&)event);
// wheel
case EventType::WheelScrolled:
return OnWheelScrolled((WheelScrolledEvent&)event);
/* keyboard */
// key
case EventType::KeyPressed:
return OnKeyPressed((KeyPressedEvent&)event);
case EventType::KeyRepeated:
return OnKeyRepeat((KeyRepeatEvent&)event);
case EventType::KeyReleased:
return OnKeyReleased((KeyReleasedEvent&)event);
// char
case EventType::SetChar:
return OnSetChar((SetCharEvent&)event);
/* window */
// termination
case EventType::WindowClosed:
return OnWindowClosed((WindowClosedEvent&)event);
// size/position
case EventType::WindowResized:
return OnWindowResized((WindowResizedEvent&)event);
case EventType::WindowMoved:
return OnWindowMoved((WindowMovedEvent&)event);
// focus
case EventType::WindowLostFocus:
return OnWindowLostFocus((WindowLostFocusEvent&)event);
case EventType::WindowGainFocus:

View file

@ -13,8 +13,12 @@ namespace Light {
class WheelScrolledEvent;
// keyboard
// key
class KeyPressedEvent;
class KeyRepeatEvent;
class KeyReleasedEvent;
// char
class SetCharEvent;
// window
class WindowClosedEvent;
@ -44,19 +48,29 @@ namespace Light {
protected:
/* mouse */
// cursor
virtual bool OnMouseMoved(const MouseMovedEvent& event) { return false; }
// button
virtual bool OnButtonPressed(const ButtonPressedEvent& event) { return false; }
virtual bool OnButtonReleased(const ButtonReleasedEvent& event) { return false; }
// wheel
virtual bool OnWheelScrolled(const WheelScrolledEvent& event) { return false; }
/* keyboard */
// key
virtual bool OnKeyPressed(const KeyPressedEvent& event) { return false; }
virtual bool OnKeyRepeat(const KeyRepeatEvent& event) { return false; }
virtual bool OnKeyReleased(const KeyReleasedEvent& event) { return false; }
// char
virtual bool OnSetChar(const SetCharEvent& event) { return false; }
/* window */
// termination
virtual bool OnWindowClosed(const WindowClosedEvent& event) { return false; }
// size/position
virtual bool OnWindowResized(const WindowResizedEvent& event) { return false; }
virtual bool OnWindowMoved(const WindowMovedEvent& event) { return false; }
// focus
virtual bool OnWindowLostFocus(const WindowLostFocusEvent& event) { return false; }
virtual bool OnWindowGainFocus(const WindowGainFocusEvent& event) { return false; }
};

View file

@ -8,6 +8,7 @@
#endif
#include "Events/Event.h"
#include "Events/CharEvent.h"
#include "Events/MouseEvents.h"
#include "Events/KeyboardEvents.h"
@ -33,50 +34,4 @@ namespace Light {
}
}
void UserInterface::OnInput(const Event& inputEvent)
{
ImGuiIO& io = ImGui::GetIO();
switch (inputEvent.GetEventType())
{
/* mouse events */
case EventType::MouseMoved:
{
const MouseMovedEvent& event = (const MouseMovedEvent&)inputEvent;
ImGui::GetIO().MousePos = ImVec2(event.GetX(), event.GetY());
return;
}
case EventType::ButtonPressed:
{
const ButtonPressedEvent& event = (const ButtonPressedEvent&)inputEvent;
ImGui::GetIO().MouseDown[event.GetButton()] = true;
return;
}
case EventType::ButtonReleased:
{
const ButtonReleasedEvent& event = (const ButtonReleasedEvent&)inputEvent;
ImGui::GetIO().MouseDown[event.GetButton()] = false;
return;
}
case EventType::WheelScrolled:
{
const WheelScrolledEvent& event = (const WheelScrolledEvent&)inputEvent;
ImGui::GetIO().MouseWheel = event.GetOffset();
return;
}
/* keyboard events */
case EventType::KeyPressed:
{
const KeyPressedEvent& event = (const KeyPressedEvent&)inputEvent;
ImGui::GetIO().MouseDown[event.GetKey()] = true;
return;
}
case EventType::KeyReleased:
{
const KeyReleasedEvent& event = (const KeyReleasedEvent&)inputEvent;
ImGui::GetIO().MouseDown[event.GetKey()] = false;
return;
}
}
}
}

View file

@ -20,8 +20,6 @@ namespace Light {
virtual ~UserInterface() = default;
void OnInput(const Event& inputEvent);
virtual void Begin() = 0;
virtual void End() = 0;

View file

@ -2,6 +2,8 @@
#include "dxUserInterface.h"
#include "dxSharedContext.h"
#include "Input/KeyCodes.h"
#define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw/glfw3.h>
#include <glfw/glfw3native.h>
@ -40,6 +42,30 @@ namespace Light {
// init
ImGui_ImplWin32_Init(glfwGetWin32Window(windowHandle));
ImGui_ImplDX11_Init(sharedContext->GetDevice().Get(), sharedContext->GetDeviceContext().Get());
// keyboard map
io.KeyMap[ImGuiKey_Tab] = Key::Tab;
io.KeyMap[ImGuiKey_LeftArrow] = Key::LeftArrow;
io.KeyMap[ImGuiKey_RightArrow] = Key::RightArrow;
io.KeyMap[ImGuiKey_UpArrow] = Key::UpArrow;
io.KeyMap[ImGuiKey_DownArrow] = Key::DownArrow;
io.KeyMap[ImGuiKey_PageUp] = Key::PageUp;
io.KeyMap[ImGuiKey_PageDown] = Key::PageDown;
io.KeyMap[ImGuiKey_Home] = Key::Home;
io.KeyMap[ImGuiKey_End] = Key::End;
io.KeyMap[ImGuiKey_Insert] = Key::Insert;
io.KeyMap[ImGuiKey_Delete] = Key::Delete;
io.KeyMap[ImGuiKey_Backspace] = Key::BackSpace;
io.KeyMap[ImGuiKey_Space] = Key::Space;
io.KeyMap[ImGuiKey_Enter] = Key::Enter;
io.KeyMap[ImGuiKey_Escape] = Key::Escape;
io.KeyMap[ImGuiKey_KeyPadEnter] = Key::Enter;
io.KeyMap[ImGuiKey_A] = Key::A;
io.KeyMap[ImGuiKey_C] = Key::C;
io.KeyMap[ImGuiKey_V] = Key::V;
io.KeyMap[ImGuiKey_X] = Key::X;
io.KeyMap[ImGuiKey_Y] = Key::Y;
io.KeyMap[ImGuiKey_Z] = Key::Z;
}
dxUserInterface::~dxUserInterface()

View file

@ -2,6 +2,8 @@
#include "ltpch.h"
#include "glUserInterface.h"
#include "Input/KeyCodes.h"
#include <GLFW/glfw3.h>
#include <imgui.h>
@ -38,6 +40,30 @@ namespace Light {
// init
ImGui_ImplGlfw_InitForOpenGL(windowHandle, false);
ImGui_ImplOpenGL3_Init();
// keyboard map
io.KeyMap[ImGuiKey_Tab] = Key::Tab;
io.KeyMap[ImGuiKey_LeftArrow] = Key::LeftArrow;
io.KeyMap[ImGuiKey_RightArrow] = Key::RightArrow;
io.KeyMap[ImGuiKey_UpArrow] = Key::UpArrow;
io.KeyMap[ImGuiKey_DownArrow] = Key::DownArrow;
io.KeyMap[ImGuiKey_PageUp] = Key::PageUp;
io.KeyMap[ImGuiKey_PageDown] = Key::PageDown;
io.KeyMap[ImGuiKey_Home] = Key::Home;
io.KeyMap[ImGuiKey_End] = Key::End;
io.KeyMap[ImGuiKey_Insert] = Key::Insert;
io.KeyMap[ImGuiKey_Delete] = Key::Delete;
io.KeyMap[ImGuiKey_Backspace] = Key::BackSpace;
io.KeyMap[ImGuiKey_Space] = Key::Space;
io.KeyMap[ImGuiKey_Enter] = Key::Enter;
io.KeyMap[ImGuiKey_Escape] = Key::Escape;
io.KeyMap[ImGuiKey_KeyPadEnter] = Key::Enter;
io.KeyMap[ImGuiKey_A] = Key::A;
io.KeyMap[ImGuiKey_C] = Key::C;
io.KeyMap[ImGuiKey_V] = Key::V;
io.KeyMap[ImGuiKey_X] = Key::X;
io.KeyMap[ImGuiKey_Y] = Key::Y;
io.KeyMap[ImGuiKey_Z] = Key::Z;
}
glUserInterface::~glUserInterface()

View file

@ -2,6 +2,7 @@
#include "lWindow.h"
#include "Events/Event.h"
#include "Events/CharEvent.h"
#include "Events/MouseEvents.h"
#include "Events/KeyboardEvents.h"
#include "Events/WindowEvents.h"
@ -176,6 +177,15 @@ namespace Light {
callback(event);
}
});
/* char */
glfwSetCharCallback(m_Handle, [](GLFWwindow* window, unsigned int character)
{
std::function<void(Event&)> callback = *(std::function<void(Event&)>*)glfwGetWindowUserPointer(window);
SetCharEvent event(character);
callback(event);
});
//============================== KEYBOARD_EVENTS ==============================//
//============================== WINDOW_EVENTS ==============================//

View file

@ -34,7 +34,7 @@ namespace Light {
void SetSize(const glm::uvec2& size, bool additive = false) override;
void SetVSync(bool vsync, bool toggle = false) override;
void SetVisibility(bool visible, bool toggle = false);
void SetVisibility(bool visible, bool toggle = false) override;
//======================================== SETTERS ========================================//
private:

View file

@ -2,10 +2,12 @@
#include "wWindow.h"
#include "Events/Event.h"
#include "Events/CharEvent.h"
#include "Events/MouseEvents.h"
#include "Events/KeyboardEvents.h"
#include "Events/WindowEvents.h"
#include "Graphics/GraphicsContext.h"
#include <GLFW/glfw3.h>
@ -157,9 +159,22 @@ namespace Light {
if (action == GLFW_PRESS)
callback(KeyPressedEvent(key));
else if(action == GLFW_RELEASE)
else if (action == GLFW_RELEASE)
callback(KeyReleasedEvent(key));
else if (action == GLFW_REPEAT)
callback(KeyRepeatEvent(key));
});
/* char */
glfwSetCharCallback(m_Handle, [](GLFWwindow* window, unsigned int character)
{
std::function<void(Event&)> callback = *(std::function<void(Event&)>*)glfwGetWindowUserPointer(window);
callback(SetCharEvent(character));
});
// ImGuiIO& io = ImGui::GetIO();
// io.AddInputCharacter(c);
//============================== KEYBOARD_EVENTS ==============================//
//============================== WINDOW_EVENTS ==============================//