Removed dispatcher, Added window events
This commit is contained in:
parent
b6464c714c
commit
6864a907fe
15 changed files with 134 additions and 57 deletions
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Core/Logger.h"
|
||||
|
||||
#if defined(LT_PLATFORM_WINDOWS)
|
||||
#define LT_BUILD_PLATFORM "Windows"
|
||||
#elif defined(LT_PLATFORM_LINUX)
|
||||
|
@ -8,4 +10,5 @@
|
|||
#error "Unsupported platform: Unknown"
|
||||
#endif
|
||||
|
||||
#include "Core/Logger.h"
|
||||
|
||||
#define BIT(x) 1 << x
|
|
@ -28,12 +28,18 @@ namespace Light {
|
|||
|
||||
void Application::GameLoop()
|
||||
{
|
||||
while (true) { m_Window->OnUpdate(); }
|
||||
while (m_Window->IsOpen())
|
||||
{
|
||||
m_Window->OnUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
void Application::OnEvent(Event& event)
|
||||
{
|
||||
m_Dispatcher.Dispatch(event, m_LayerStack.begin(), m_LayerStack.end());
|
||||
if (event.IsInCategory(WindowEventCategory))
|
||||
m_Window->OnEvent(event);
|
||||
|
||||
m_LayerStack.OnEvent(event);
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,6 @@
|
|||
#include "Base.h"
|
||||
|
||||
#include "Events/Event.h"
|
||||
#include "Events/Dispatcher.h"
|
||||
|
||||
#include "Layer/LayerStack.h"
|
||||
|
||||
|
@ -18,7 +17,6 @@ namespace Light {
|
|||
private:
|
||||
std::unique_ptr<Window> m_Window = nullptr;
|
||||
LayerStack m_LayerStack;
|
||||
Dispatcher m_Dispatcher;
|
||||
public:
|
||||
virtual ~Application();
|
||||
|
||||
|
|
|
@ -17,10 +17,15 @@ namespace Light {
|
|||
|
||||
class Window
|
||||
{
|
||||
protected:
|
||||
bool b_Open;
|
||||
public:
|
||||
virtual ~Window() = default;
|
||||
|
||||
inline bool IsOpen() const { return b_Open; }
|
||||
|
||||
virtual void OnUpdate() = 0;
|
||||
virtual void OnEvent(Event& event) = 0;
|
||||
|
||||
virtual unsigned int GetHeight() = 0;
|
||||
virtual unsigned int GetWidth() = 0;
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
#include "Layer/Layer.h"
|
||||
|
||||
namespace Light {
|
||||
|
||||
class Dispatcher
|
||||
{
|
||||
public:
|
||||
void Dispatch(Event& event, std::vector<Layer*>::iterator begin, std::vector<Layer*>::iterator end)
|
||||
{
|
||||
switch (event.GetEventType())
|
||||
{
|
||||
case EventType::MouseMoved:
|
||||
for (auto it = begin; it != end; it++)
|
||||
if ((*it)->OnMouseMoved((MouseMovedEvent&)event)) return;
|
||||
return;
|
||||
case EventType::ButtonPressed:
|
||||
for (auto it = begin; it != end; it++)
|
||||
if ((*it)->OnButtonPressed((ButtonPressedEvent&)event)) return;
|
||||
return;
|
||||
case EventType::ButtonReleased:
|
||||
for (auto it = begin; it != end; it++)
|
||||
if ((*it)->OnButtonReleased((ButtonReleasedEvent&)event)) return;
|
||||
return;
|
||||
case EventType::KeyPressed:
|
||||
for (auto it = begin; it != end; it++)
|
||||
if ((*it)->OnKeyPressed((KeyPressedEvent&)event)) return;
|
||||
return;
|
||||
case EventType::KeyReleased:
|
||||
for (auto it = begin; it != end; it++)
|
||||
if ((*it)->OnKeyReleased((KeyReleasedEvent&)event)) return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -20,13 +20,25 @@ namespace Light {
|
|||
WindowMoved, WindowResized, WindowClosed,
|
||||
};
|
||||
|
||||
enum EventCategory
|
||||
{
|
||||
None = 0,
|
||||
|
||||
WindowEventCategory = BIT(0),
|
||||
InputEventCategory = BIT(1),
|
||||
KeyboardEventCategory = BIT(2),
|
||||
MouseEventCategory = BIT(3),
|
||||
};
|
||||
|
||||
#define EVENT_TYPE(type) EventType GetEventType() const override { return EventType::##type; }
|
||||
#define EVENT_CATEGORY(eCategory) inline bool IsInCategory(EventCategory category) const override { return (eCategory) & category; }
|
||||
|
||||
class Event
|
||||
{
|
||||
public:
|
||||
virtual EventType GetEventType() const = 0;
|
||||
virtual std::string GetInfoLog() const = 0;
|
||||
virtual bool IsInCategory(EventCategory category) const = 0;
|
||||
|
||||
friend std::ostream & operator<<(std::ostream & os, const Event& e)
|
||||
{
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
#include "Event.h"
|
||||
#include "KeyboardEvents.h"
|
||||
#include "MouseEvents.h"
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "Base.h"
|
||||
|
||||
#include "Events/Event.h"
|
||||
#include "Event.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
|
@ -25,6 +25,7 @@ namespace Light {
|
|||
return ss.str();
|
||||
}
|
||||
EVENT_TYPE(KeyPressed)
|
||||
EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory)
|
||||
};
|
||||
|
||||
class KeyReleasedEvent : public Event
|
||||
|
@ -44,6 +45,7 @@ namespace Light {
|
|||
return ss.str();
|
||||
}
|
||||
EVENT_TYPE(KeyReleased)
|
||||
EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory)
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace Light {
|
|||
return ss.str();
|
||||
}
|
||||
EVENT_TYPE(MouseMoved)
|
||||
EVENT_CATEGORY(InputEventCategory | MouseEventCategory)
|
||||
};
|
||||
|
||||
class ButtonPressedEvent : public Event
|
||||
|
@ -45,6 +46,7 @@ namespace Light {
|
|||
return ss.str();
|
||||
}
|
||||
EVENT_TYPE(ButtonPressed)
|
||||
EVENT_CATEGORY(InputEventCategory | MouseEventCategory)
|
||||
};
|
||||
|
||||
class ButtonReleasedEvent : public Event
|
||||
|
@ -64,7 +66,8 @@ namespace Light {
|
|||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_TYPE(ButtonReleased);
|
||||
EVENT_TYPE(ButtonReleased)
|
||||
EVENT_CATEGORY(InputEventCategory | MouseEventCategory)
|
||||
};
|
||||
|
||||
}
|
22
Engine/src/Engine/Events/WindowEvents.h
Normal file
22
Engine/src/Engine/Events/WindowEvents.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class WindowClosedEvent : public Event
|
||||
{
|
||||
public:
|
||||
virtual std::string GetInfoLog() const override
|
||||
{
|
||||
return "WindowClosedEvent";
|
||||
}
|
||||
EVENT_TYPE(WindowClosed)
|
||||
EVENT_CATEGORY(WindowEventCategory)
|
||||
};
|
||||
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "Events/MouseEvents.h"
|
||||
#include "Events/KeyboardEvents.h"
|
||||
#include "Events/WindowEvents.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
@ -27,6 +28,10 @@ namespace Light {
|
|||
// Keyboard events
|
||||
virtual bool OnKeyPressed(const KeyPressedEvent& event) { return false; }
|
||||
virtual bool OnKeyReleased(const KeyReleasedEvent& event) { return false; }
|
||||
|
||||
// Window Events
|
||||
virtual bool OnWindowClosed(const WindowClosedEvent& event) { return false; }
|
||||
|
||||
};
|
||||
|
||||
class TestLayer : public Layer
|
||||
|
@ -35,13 +40,16 @@ namespace Light {
|
|||
TestLayer(const std::string& name): Layer(name) {}
|
||||
|
||||
// Mouse events
|
||||
virtual bool OnMouseMoved(const MouseMovedEvent& event) { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; }
|
||||
virtual bool OnButtonPressed(const ButtonPressedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
virtual bool OnButtonReleased(const ButtonReleasedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
virtual bool OnMouseMoved(const MouseMovedEvent& event) override { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; }
|
||||
virtual bool OnButtonPressed(const ButtonPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
virtual bool OnButtonReleased(const ButtonReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
|
||||
// Keyboard events
|
||||
virtual bool OnKeyPressed(const KeyPressedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
virtual bool OnKeyReleased(const KeyReleasedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
virtual bool OnKeyPressed(const KeyPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
virtual bool OnKeyReleased(const KeyReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
|
||||
// Window events
|
||||
virtual bool OnWindowClosed(const WindowClosedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
|
||||
};
|
||||
|
||||
}
|
|
@ -11,13 +11,53 @@ namespace Light {
|
|||
void LayerStack::PushLayer(Layer* layer)
|
||||
{
|
||||
m_Layers.push_back(layer);
|
||||
m_Begin = m_Layers.begin();
|
||||
m_End = m_Layers.end();
|
||||
|
||||
LT_ENGINE_TRACE("LayerStack::PushLayer: Attached [{}]", layer->GetName());
|
||||
}
|
||||
|
||||
void LayerStack::PopLayer(Layer* layer)
|
||||
{
|
||||
m_Layers.erase(std::find(m_Layers.begin(), m_Layers.end(), layer));
|
||||
m_Begin = m_Layers.begin();
|
||||
m_End = m_Layers.end();
|
||||
|
||||
LT_ENGINE_TRACE("LayerStack::PushLayer: Detatched[{}]", layer->GetName());
|
||||
}
|
||||
|
||||
void LayerStack::OnEvent(Event& event)
|
||||
{
|
||||
switch (event.GetEventType())
|
||||
{
|
||||
case EventType::MouseMoved:
|
||||
for (auto it = m_Begin; it != m_End; it++)
|
||||
if ((*it)->OnMouseMoved((MouseMovedEvent&)event)) return;
|
||||
return;
|
||||
|
||||
case EventType::ButtonPressed:
|
||||
for (auto it = m_Begin; it != m_End; it++)
|
||||
if ((*it)->OnButtonPressed((ButtonPressedEvent&)event)) return;
|
||||
return;
|
||||
case EventType::ButtonReleased:
|
||||
for (auto it = m_Begin; it != m_End; it++)
|
||||
if ((*it)->OnButtonReleased((ButtonReleasedEvent&)event)) return;
|
||||
return;
|
||||
|
||||
case EventType::KeyPressed:
|
||||
for (auto it = m_Begin; it != m_End; it++)
|
||||
if ((*it)->OnKeyPressed((KeyPressedEvent&)event)) return;
|
||||
return;
|
||||
case EventType::KeyReleased:
|
||||
for (auto it = m_Begin; it != m_End; it++)
|
||||
if ((*it)->OnKeyReleased((KeyReleasedEvent&)event)) return;
|
||||
return;
|
||||
|
||||
case EventType::WindowClosed:
|
||||
for (auto it = m_Begin; it != m_End; it++)
|
||||
if ((*it)->OnWindowClosed((WindowClosedEvent&)event)) return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include "Layer.h"
|
||||
|
||||
#include "Events/Event.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Light {
|
||||
|
@ -13,12 +15,16 @@ namespace Light {
|
|||
private:
|
||||
std::vector<Layer*> m_Layers;
|
||||
|
||||
std::vector<Layer*>::iterator m_Begin;
|
||||
std::vector<Layer*>::iterator m_End;
|
||||
public:
|
||||
~LayerStack();
|
||||
|
||||
void PushLayer(Layer* layer);
|
||||
void PopLayer(Layer* layer);
|
||||
|
||||
void OnEvent(Event& event);
|
||||
|
||||
std::vector<Layer*>::iterator begin() { return m_Layers.begin(); }
|
||||
std::vector<Layer*>::iterator end() { return m_Layers.end(); }
|
||||
};
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "Events/KeyboardEvents.h"
|
||||
#include "Events/MouseEvents.h"
|
||||
#include "Events/WindowEvents.h"
|
||||
|
||||
namespace Light {
|
||||
|
||||
|
@ -34,6 +35,15 @@ namespace Light {
|
|||
glfwSwapBuffers(m_Handle);
|
||||
}
|
||||
|
||||
void wWindow::OnEvent(Event& event)
|
||||
{
|
||||
switch (event.GetEventType())
|
||||
{
|
||||
case EventType::WindowClosed:
|
||||
b_Open = false;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int wWindow::GetWidth()
|
||||
{
|
||||
return m_Properties.width;
|
||||
|
@ -71,6 +81,12 @@ namespace Light {
|
|||
else
|
||||
callback(KeyReleasedEvent(key));
|
||||
});
|
||||
|
||||
glfwSetWindowCloseCallback(m_Handle, [](GLFWwindow* window)
|
||||
{
|
||||
std::function<void(Event&)> callback = *(std::function<void(Event&)>*)glfwGetWindowUserPointer(window);
|
||||
callback(WindowClosedEvent());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@ namespace Light {
|
|||
~wWindow();
|
||||
|
||||
virtual void OnUpdate() override;
|
||||
virtual void OnEvent(Event& event) override;
|
||||
|
||||
virtual unsigned int GetWidth() override;
|
||||
virtual unsigned int GetHeight() override;
|
||||
|
|
Loading…
Add table
Reference in a new issue