Add Event system
- Add abstract Event class - Add EventDispatcher - Add Mouse, Key and Window Events
This commit is contained in:
@@ -175,6 +175,16 @@
|
||||
<ClCompile Include="src\copium\core\Device.cpp" />
|
||||
<ClCompile Include="src\copium\core\Vulkan.cpp" />
|
||||
<ClCompile Include="src\copium\core\Window.cpp" />
|
||||
<ClCompile Include="src\copium\event\Event.cpp" />
|
||||
<ClCompile Include="src\copium\event\EventDispatcher.cpp" />
|
||||
<ClCompile Include="src\copium\event\KeyPressEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\KeyReleaseEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\MouseMoveEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\MousePressEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\MouseReleaseEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\MouseScrollEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\WindowFocusEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\WindowResizeEvent.cpp" />
|
||||
<ClCompile Include="src\copium\mesh\Mesh.cpp" />
|
||||
<ClCompile Include="src\copium\pipeline\ShaderBinding.cpp" />
|
||||
<ClCompile Include="src\copium\renderer\Batch.cpp" />
|
||||
@@ -221,6 +231,19 @@
|
||||
<ClInclude Include="src\copium\core\Device.h" />
|
||||
<ClInclude Include="src\copium\core\Vulkan.h" />
|
||||
<ClInclude Include="src\copium\core\Window.h" />
|
||||
<ClInclude Include="src\copium\event\Event.h" />
|
||||
<ClInclude Include="src\copium\event\EventDispatcher.h" />
|
||||
<ClInclude Include="src\copium\event\EventHandler.h" />
|
||||
<ClInclude Include="src\copium\event\EventResult.h" />
|
||||
<ClInclude Include="src\copium\event\EventType.h" />
|
||||
<ClInclude Include="src\copium\event\KeyPressEvent.h" />
|
||||
<ClInclude Include="src\copium\event\KeyReleaseEvent.h" />
|
||||
<ClInclude Include="src\copium\event\MouseMoveEvent.h" />
|
||||
<ClInclude Include="src\copium\event\MousePressEvent.h" />
|
||||
<ClInclude Include="src\copium\event\MouseReleaseEvent.h" />
|
||||
<ClInclude Include="src\copium\event\MouseScrollEvent.h" />
|
||||
<ClInclude Include="src\copium\event\WindowFocusEvent.h" />
|
||||
<ClInclude Include="src\copium\event\WindowResizeEvent.h" />
|
||||
<ClInclude Include="src\copium\mesh\Mesh.h" />
|
||||
<ClInclude Include="src\copium\pipeline\ShaderBinding.h" />
|
||||
<ClInclude Include="src\copium\renderer\Batch.h" />
|
||||
|
||||
@@ -150,6 +150,36 @@
|
||||
<ClCompile Include="src\copium\util\RuntimeException.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\Event.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\MousePressEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\MouseReleaseEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\MouseMoveEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\KeyPressEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\KeyReleaseEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\EventDispatcher.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\WindowResizeEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\WindowFocusEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\MouseScrollEvent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
|
||||
@@ -293,5 +323,44 @@
|
||||
<ClInclude Include="src\copium\util\RuntimeException.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\Event.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\EventType.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\MousePressEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\MouseReleaseEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\MouseMoveEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\KeyPressEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\KeyReleaseEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\EventDispatcher.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\EventHandler.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\EventResult.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\WindowResizeEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\WindowFocusEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\MouseScrollEvent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,6 +1,13 @@
|
||||
#include "copium/core/Application.h"
|
||||
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/event/EventDispatcher.h"
|
||||
#include "copium/event/MouseMoveEvent.h"
|
||||
#include "copium/event/MouseScrollEvent.h"
|
||||
#include "copium/event/MousePressEvent.h"
|
||||
#include "copium/event/KeyPressEvent.h"
|
||||
#include "copium/event/WindowFocusEvent.h"
|
||||
#include "copium/event/WindowResizeEvent.h"
|
||||
#include "copium/mesh/Vertex.h"
|
||||
#include "copium/mesh/VertexPassthrough.h"
|
||||
#include "copium/asset/AssetManager.h"
|
||||
@@ -38,6 +45,7 @@ namespace Copium
|
||||
|
||||
Application::Application()
|
||||
{
|
||||
EventDispatcher::AddEventHandler(this);
|
||||
InitializeFrameBuffer();
|
||||
InitializeRenderer();
|
||||
InitializeGraphicsPipeline();
|
||||
@@ -55,6 +63,7 @@ namespace Copium
|
||||
AssetManager::UnloadAsset(graphicsPipeline);
|
||||
AssetManager::UnloadAsset(graphicsPipelinePassthrough);
|
||||
AssetManager::UnloadAsset(framebuffer);
|
||||
EventDispatcher::RemoveEventHandler(this);
|
||||
}
|
||||
|
||||
bool Application::Update()
|
||||
@@ -69,6 +78,58 @@ namespace Copium
|
||||
return !glfwWindowShouldClose(Vulkan::GetWindow().GetWindow());
|
||||
}
|
||||
|
||||
EventResult Application::OnEvent(const Event& event)
|
||||
{
|
||||
switch (event.GetType())
|
||||
{
|
||||
case EventType::WindowResize:
|
||||
{
|
||||
const WindowResizeEvent& windowResizeEvent = static_cast<const WindowResizeEvent&>(event);
|
||||
AssetManager::GetAsset<Framebuffer>(framebuffer).Resize(windowResizeEvent.GetWidth(), windowResizeEvent.GetHeight());
|
||||
descriptorSetPassthrough->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
|
||||
|
||||
return EventResult::Continue;
|
||||
}
|
||||
case EventType::MouseMove:
|
||||
{
|
||||
const MouseMoveEvent& mouseMoveEvent = static_cast<const MouseMoveEvent&>(event);
|
||||
mousePos = {mouseMoveEvent.GetPos().x / Vulkan::GetSwapChain().GetExtent().width, mouseMoveEvent.GetPos().y / Vulkan::GetSwapChain().GetExtent().height};
|
||||
|
||||
return EventResult::Continue;
|
||||
}
|
||||
case EventType::MousePress:
|
||||
{
|
||||
const MousePressEvent& mousePressEvent = static_cast<const MousePressEvent&>(event);
|
||||
CP_INFO("%d", mousePressEvent.GetButton());
|
||||
|
||||
return EventResult::Focus;
|
||||
}
|
||||
case EventType::KeyPress:
|
||||
{
|
||||
const KeyPressEvent& keyPressEvent = static_cast<const KeyPressEvent&>(event);
|
||||
CP_INFO("%d", keyPressEvent.GetButton());
|
||||
|
||||
return EventResult::Handled;
|
||||
}
|
||||
case EventType::MouseScroll:
|
||||
{
|
||||
const MouseScrollEvent& mouseScrollEvent = static_cast<const MouseScrollEvent&>(event);
|
||||
CP_INFO("%d %d", mouseScrollEvent.GetScrollX(), mouseScrollEvent.GetScrollY());
|
||||
|
||||
return EventResult::Continue;
|
||||
}
|
||||
case EventType::WindowFocus:
|
||||
{
|
||||
const WindowFocusEvent& windowFocusEvent = static_cast<const WindowFocusEvent&>(event);
|
||||
CP_INFO("Window Focused: %s", windowFocusEvent.IsFocused() ? "true" : "false");
|
||||
|
||||
return EventResult::Continue;
|
||||
}
|
||||
}
|
||||
|
||||
return EventResult::Continue;
|
||||
}
|
||||
|
||||
void Application::InitializeFrameBuffer()
|
||||
{
|
||||
framebuffer = AssetManager::LoadAsset<Framebuffer>("framebuffer.meta");
|
||||
@@ -143,6 +204,7 @@ namespace Copium
|
||||
}
|
||||
renderer->Quad(glm::vec2{-0.9, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Texture2D>(texture2D));
|
||||
renderer->Quad(glm::vec2{ 0.1, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Texture2D>(texture2D2));
|
||||
renderer->Quad(mousePos, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Texture2D>(texture2D2));
|
||||
renderer->End();
|
||||
|
||||
fb.Unbind(*commandBuffer);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "copium/asset/AssetMeta.h"
|
||||
#include "copium/buffer/Framebuffer.h"
|
||||
#include "copium/event/EventHandler.h"
|
||||
#include "copium/mesh/Mesh.h"
|
||||
#include "copium/pipeline/DescriptorPool.h"
|
||||
#include "copium/pipeline/DescriptorSet.h"
|
||||
@@ -10,7 +11,7 @@
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class Application final
|
||||
class Application final : EventHandler
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(Application);
|
||||
private:
|
||||
@@ -27,12 +28,14 @@ namespace Copium
|
||||
std::unique_ptr<Mesh> mesh;
|
||||
std::unique_ptr<Mesh> meshPassthrough;
|
||||
std::unique_ptr<CommandBuffer> commandBuffer;
|
||||
glm::vec2 mousePos;
|
||||
|
||||
public:
|
||||
Application();
|
||||
~Application();
|
||||
|
||||
bool Update();
|
||||
EventResult OnEvent(const Event& event) override;
|
||||
private:
|
||||
void InitializeFrameBuffer();
|
||||
void InitializeRenderer();
|
||||
|
||||
@@ -1,75 +1,120 @@
|
||||
#include "copium/core/Window.h"
|
||||
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/event/EventDispatcher.h"
|
||||
#include "copium/event/KeyPressEvent.h"
|
||||
#include "copium/event/KeyReleaseEvent.h"
|
||||
#include "copium/event/MouseMoveEvent.h"
|
||||
#include "copium/event/MousePressEvent.h"
|
||||
#include "copium/event/MouseReleaseEvent.h"
|
||||
#include "copium/event/MouseScrollEvent.h"
|
||||
#include "copium/event/WindowResizeEvent.h"
|
||||
#include "copium/event/WindowFocusEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Window::Window(const std::string& windowName, int width, int height, Mode mode)
|
||||
Window::Window(const std::string& windowName, int width, int height, Mode mode)
|
||||
{
|
||||
InitializeWindow(windowName, width, height, mode);
|
||||
InitializeSurface();
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
vkDestroySurfaceKHR(Vulkan::GetInstance(), surface, nullptr);
|
||||
glfwDestroyWindow(window);
|
||||
}
|
||||
|
||||
VkSurfaceKHR Window::GetSurface() const
|
||||
{
|
||||
return surface;
|
||||
}
|
||||
|
||||
GLFWwindow* Window::GetWindow()
|
||||
{
|
||||
return window;
|
||||
}
|
||||
|
||||
void Window::InitializeWindow(const std::string& windowName, int width, int height, Mode mode)
|
||||
{
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
InitializeWindow(windowName, width, height, mode);
|
||||
InitializeSurface();
|
||||
case Mode::Fullscreen:
|
||||
{
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
||||
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), glfwGetPrimaryMonitor(), nullptr);
|
||||
break;
|
||||
}
|
||||
case Mode::BorderlessWindowed:
|
||||
{
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
||||
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), nullptr, nullptr);
|
||||
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
|
||||
break;
|
||||
}
|
||||
case Mode::Windowed:
|
||||
{
|
||||
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
CP_ABORT("Unreachable switch case");
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
vkDestroySurfaceKHR(Vulkan::GetInstance(), surface, nullptr);
|
||||
glfwDestroyWindow(window);
|
||||
}
|
||||
CP_ASSERT(window, "Failed to initialize glfw window");
|
||||
|
||||
VkSurfaceKHR Window::GetSurface() const
|
||||
{
|
||||
return surface;
|
||||
}
|
||||
glfwSetWindowUserPointer(window, this);
|
||||
glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback);
|
||||
glfwSetKeyCallback(window, KeyCallback);
|
||||
glfwSetMouseButtonCallback(window, MouseButtonCallback);
|
||||
glfwSetCursorPosCallback(window, MouseMoveCallback);
|
||||
glfwSetWindowFocusCallback(window, WindowFocusCallback);
|
||||
glfwSetScrollCallback(window, MouseScrollCallback);
|
||||
}
|
||||
|
||||
GLFWwindow* Window::GetWindow()
|
||||
{
|
||||
return window;
|
||||
}
|
||||
void Window::InitializeSurface()
|
||||
{
|
||||
CP_VK_ASSERT(glfwCreateWindowSurface(Vulkan::GetInstance(), window, nullptr, &surface), "Failed to create Vulkan surface");
|
||||
}
|
||||
|
||||
void Window::InitializeWindow(const std::string& windowName, int width, int height, Mode mode)
|
||||
{
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
void Window::FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height)
|
||||
{
|
||||
Vulkan::GetSwapChain().ResizeFramebuffer();
|
||||
EventDispatcher::QueueEvent(WindowResizeEvent{width, height});
|
||||
}
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case Mode::Fullscreen:
|
||||
{
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
||||
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), glfwGetPrimaryMonitor(), nullptr);
|
||||
break;
|
||||
}
|
||||
case Mode::BorderlessWindowed:
|
||||
{
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
||||
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), nullptr, nullptr);
|
||||
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
|
||||
break;
|
||||
}
|
||||
case Mode::Windowed:
|
||||
{
|
||||
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
CP_ABORT("Unreachable switch case");
|
||||
}
|
||||
void Window::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
if (action == GLFW_PRESS)
|
||||
EventDispatcher::QueueEvent(KeyPressEvent(key));
|
||||
else if (action == GLFW_RELEASE)
|
||||
EventDispatcher::QueueEvent(KeyReleaseEvent(key));
|
||||
}
|
||||
|
||||
CP_ASSERT(window, "Failed to initialize glfw window");
|
||||
void Window::MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
|
||||
{
|
||||
if (action == GLFW_PRESS)
|
||||
EventDispatcher::QueueEvent(MousePressEvent{button});
|
||||
else if (action == GLFW_RELEASE)
|
||||
EventDispatcher::QueueEvent(MouseReleaseEvent{button});
|
||||
}
|
||||
|
||||
glfwSetWindowUserPointer(window, this);
|
||||
glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback);
|
||||
}
|
||||
void Window::MouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
|
||||
{
|
||||
EventDispatcher::QueueEvent(MouseMoveEvent{glm::vec2{xpos, ypos}}); // TODO: Convert to Vulkan coordinates
|
||||
}
|
||||
|
||||
void Window::InitializeSurface()
|
||||
{
|
||||
CP_VK_ASSERT(glfwCreateWindowSurface(Vulkan::GetInstance(), window, nullptr, &surface), "Failed to create Vulkan surface");
|
||||
}
|
||||
|
||||
void Window::FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height)
|
||||
{
|
||||
Vulkan::GetSwapChain().ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
|
||||
}
|
||||
void Window::WindowFocusCallback(GLFWwindow* window, int focused)
|
||||
{
|
||||
EventDispatcher::QueueEvent(WindowFocusEvent{focused == GLFW_TRUE});
|
||||
}
|
||||
|
||||
void Window::MouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
EventDispatcher::QueueEvent(MouseScrollEvent{(int)xoffset, (int)yoffset});
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,12 @@ namespace Copium
|
||||
private:
|
||||
void InitializeWindow(const std::string& windowName, int width, int height, Mode mode);
|
||||
void InitializeSurface();
|
||||
static void FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height);
|
||||
|
||||
static void FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height);
|
||||
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
static void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
|
||||
static void MouseMoveCallback(GLFWwindow* window, double xpos, double ypos);
|
||||
static void WindowFocusCallback(GLFWwindow* window, int focused);
|
||||
static void MouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
Event::Event(EventType type)
|
||||
: type{type}
|
||||
{}
|
||||
|
||||
EventType Event::GetType() const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/EventType.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class Event
|
||||
{
|
||||
private:
|
||||
EventType type;
|
||||
public:
|
||||
Event(EventType type);
|
||||
|
||||
EventType GetType() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "copium/event/EventDispatcher.h"
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
EventHandler* EventDispatcher::focusedEventHandler = nullptr;
|
||||
std::vector<EventHandler*> EventDispatcher::eventHandlers;
|
||||
std::queue<std::unique_ptr<Event>> EventDispatcher::events;
|
||||
|
||||
void EventDispatcher::Dispatch()
|
||||
{
|
||||
while (!events.empty())
|
||||
{
|
||||
std::unique_ptr<Event> event = std::move(events.front());
|
||||
events.pop();
|
||||
if (focusedEventHandler)
|
||||
{
|
||||
EventResult result = focusedEventHandler->OnEvent(*event);
|
||||
if (result == EventResult::Handled || result == EventResult::Focus)
|
||||
return;
|
||||
}
|
||||
for (auto& eventHandler : eventHandlers)
|
||||
{
|
||||
if (eventHandler == focusedEventHandler)
|
||||
continue;
|
||||
EventResult result = eventHandler->OnEvent(*event);
|
||||
switch (result)
|
||||
{
|
||||
case EventResult::Continue:
|
||||
continue;
|
||||
case EventResult::Handled:
|
||||
return;
|
||||
case EventResult::Focus:
|
||||
focusedEventHandler = eventHandler;
|
||||
return;
|
||||
default:
|
||||
CP_ABORT("Unreachable switch case");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::AddEventHandler(EventHandler* eventHandler)
|
||||
{
|
||||
eventHandlers.emplace_back(eventHandler);
|
||||
}
|
||||
|
||||
void EventDispatcher::RemoveEventHandler(EventHandler* eventHandler)
|
||||
{
|
||||
for (auto it = eventHandlers.begin(); it != eventHandlers.end(); ++it)
|
||||
{
|
||||
if (*it == eventHandler)
|
||||
{
|
||||
eventHandlers.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
#include "copium/event/EventHandler.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class EventDispatcher
|
||||
{
|
||||
private:
|
||||
static EventHandler* focusedEventHandler;
|
||||
static std::vector<EventHandler*> eventHandlers;
|
||||
static std::queue<std::unique_ptr<Event>> events;
|
||||
public:
|
||||
template <typename EventType>
|
||||
static void QueueEvent(const EventType& event)
|
||||
{
|
||||
events.push(std::make_unique<EventType>(event));
|
||||
}
|
||||
static void Dispatch();
|
||||
static void AddEventHandler(EventHandler* eventHandler);
|
||||
static void RemoveEventHandler(EventHandler* eventHandler);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
#include "copium/event/EventResult.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class EventHandler
|
||||
{
|
||||
public:
|
||||
virtual EventResult OnEvent(const Event& event) = 0;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
enum class EventResult
|
||||
{
|
||||
Continue,
|
||||
Handled,
|
||||
Focus
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
enum class EventType
|
||||
{
|
||||
MouseMove, MousePress, MouseRelease, MouseScroll,
|
||||
KeyPress, KeyRelease,
|
||||
WindowResize, WindowFocus,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "copium/event/KeyPressEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
KeyPressEvent::KeyPressEvent(int button)
|
||||
: Event{EventType::KeyPress}, button{button}
|
||||
{}
|
||||
|
||||
int KeyPressEvent::GetButton() const
|
||||
{
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class KeyPressEvent : public Event
|
||||
{
|
||||
private:
|
||||
int button;
|
||||
public:
|
||||
KeyPressEvent(int button);
|
||||
|
||||
int GetButton() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "copium/event/KeyReleaseEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
KeyReleaseEvent::KeyReleaseEvent(int button)
|
||||
: Event{EventType::KeyRelease}, button{button}
|
||||
{}
|
||||
|
||||
int KeyReleaseEvent::GetButton() const
|
||||
{
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class KeyReleaseEvent : public Event
|
||||
{
|
||||
private:
|
||||
int button;
|
||||
public:
|
||||
KeyReleaseEvent(int button);
|
||||
|
||||
int GetButton() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "copium/event/MouseMoveEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
MouseMoveEvent::MouseMoveEvent(glm::vec2 pos)
|
||||
: Event{EventType::MouseMove}, pos{pos}
|
||||
{}
|
||||
|
||||
glm::vec2 MouseMoveEvent::GetPos() const
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class MouseMoveEvent : public Event
|
||||
{
|
||||
private:
|
||||
glm::vec2 pos;
|
||||
public:
|
||||
MouseMoveEvent(glm::vec2 pos);
|
||||
|
||||
glm::vec2 GetPos() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "copium/event/MousePressEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
MousePressEvent::MousePressEvent(int button)
|
||||
: Event{EventType::MousePress}, button{button}
|
||||
{}
|
||||
|
||||
int MousePressEvent::GetButton() const
|
||||
{
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class MousePressEvent : public Event
|
||||
{
|
||||
private:
|
||||
int button;
|
||||
public:
|
||||
MousePressEvent(int button);
|
||||
|
||||
int GetButton() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "copium/event/MouseReleaseEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
MouseReleaseEvent::MouseReleaseEvent(int button)
|
||||
: Event{EventType::MouseRelease}, button{button}
|
||||
{}
|
||||
|
||||
int MouseReleaseEvent::GetButton() const
|
||||
{
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class MouseReleaseEvent : public Event
|
||||
{
|
||||
private:
|
||||
int button;
|
||||
public:
|
||||
MouseReleaseEvent(int button);
|
||||
|
||||
int GetButton() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "copium/event/MouseScrollEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
MouseScrollEvent::MouseScrollEvent(int scrollX, int scrollY)
|
||||
: Event{EventType::MouseScroll}, scrollX{scrollX}, scrollY{scrollY}
|
||||
{}
|
||||
|
||||
int MouseScrollEvent::GetScrollX() const
|
||||
{
|
||||
return scrollX;
|
||||
}
|
||||
|
||||
int MouseScrollEvent::GetScrollY() const
|
||||
{
|
||||
return scrollY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class MouseScrollEvent : public Event
|
||||
{
|
||||
private:
|
||||
int scrollX;
|
||||
int scrollY;
|
||||
public:
|
||||
MouseScrollEvent(int scrollX, int scrollY);
|
||||
|
||||
int GetScrollX() const;
|
||||
int GetScrollY() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "copium/event/WindowFocusEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
WindowFocusEvent::WindowFocusEvent(bool focused)
|
||||
: Event{EventType::WindowFocus}, focused{focused}
|
||||
{}
|
||||
|
||||
bool WindowFocusEvent::IsFocused() const
|
||||
{
|
||||
return focused;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class WindowFocusEvent : public Event
|
||||
{
|
||||
private:
|
||||
bool focused;
|
||||
public:
|
||||
WindowFocusEvent(bool focused);
|
||||
|
||||
bool IsFocused() const;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "copium/event/WindowResizeEvent.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
WindowResizeEvent::WindowResizeEvent(int width, int height)
|
||||
: Event{EventType::WindowResize}, width{width}, height{height}
|
||||
{}
|
||||
|
||||
int WindowResizeEvent::GetWidth() const
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
int WindowResizeEvent::GetHeight() const
|
||||
{
|
||||
return height;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class WindowResizeEvent : public Event
|
||||
{
|
||||
private:
|
||||
int width;
|
||||
int height;
|
||||
public:
|
||||
WindowResizeEvent(int width, int height);
|
||||
|
||||
int GetWidth() const;
|
||||
int GetHeight() const;
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "copium/core/Application.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/event/EventDispatcher.h"
|
||||
#include "copium/util/Common.h"
|
||||
#include "copium/util/Timer.h"
|
||||
|
||||
@@ -17,6 +18,7 @@ int main(int argc, char** argv)
|
||||
while (application.Update())
|
||||
{
|
||||
glfwPollEvents();
|
||||
Copium::EventDispatcher::Dispatch();
|
||||
if (timer.Elapsed() >= 1.0)
|
||||
{
|
||||
CP_DEBUG("%d fps", frames);
|
||||
|
||||
Reference in New Issue
Block a user