mirror of
https://github.com/gwm17/Specter.git
synced 2024-11-22 18:28:52 -05:00
Added Event system basics
This commit is contained in:
parent
45abdd122a
commit
58a0d46009
|
@ -13,7 +13,18 @@ namespace Navigator {
|
|||
Application::~Application() {}
|
||||
|
||||
|
||||
void Application::OnEvent() {}
|
||||
void Application::OnEvent(Event& event)
|
||||
{
|
||||
EventDispatcher dispatch(event);
|
||||
dispatch.Dispatch<WindowCloseEvent>(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent));
|
||||
}
|
||||
|
||||
bool Application::OnWindowCloseEvent(WindowCloseEvent& event)
|
||||
{
|
||||
m_runFlag = false;
|
||||
NAV_WARN("Closing the window!");
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::PushLayer() {}
|
||||
|
||||
|
@ -24,7 +35,8 @@ namespace Navigator {
|
|||
while(m_runFlag)
|
||||
{
|
||||
NAV_TRACE("Doing a run.");
|
||||
m_runFlag = false;
|
||||
WindowCloseEvent event;
|
||||
OnEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
#ifndef APPLICATION_H
|
||||
#define APPLICATION_H
|
||||
|
||||
#include "NavCore.h"
|
||||
#include "Events/Event.h"
|
||||
#include "Events/AppEvent.h"
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
|
@ -12,7 +15,7 @@ namespace Navigator {
|
|||
|
||||
void Run();
|
||||
|
||||
void OnEvent();
|
||||
void OnEvent(Event& event);
|
||||
void PushLayer();
|
||||
void PushOverlay();
|
||||
|
||||
|
@ -21,7 +24,8 @@ namespace Navigator {
|
|||
inline void GetWindow() { return; }
|
||||
|
||||
private:
|
||||
|
||||
bool OnWindowCloseEvent(WindowCloseEvent& event);
|
||||
|
||||
bool m_runFlag;
|
||||
|
||||
static Application* s_instance;
|
||||
|
|
52
Navigator/src/Navigator/Events/AppEvent.h
Normal file
52
Navigator/src/Navigator/Events/AppEvent.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
#ifndef APP_EVENT_H
|
||||
#define APP_EVENT_H
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
class WindowCloseEvent : public Event
|
||||
{
|
||||
public:
|
||||
WindowCloseEvent() {};
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryApp)
|
||||
EVENT_TYPE_SETUP(WindowClose)
|
||||
};
|
||||
|
||||
class WindowResizeEvent : public Event
|
||||
{
|
||||
public:
|
||||
WindowResizeEvent(float x, float y) :
|
||||
m_xSize(x), m_ySize(y)
|
||||
{
|
||||
}
|
||||
|
||||
inline float GetXSize() { return m_xSize; }
|
||||
inline float GetYSize() { return m_ySize; }
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " to size: (" << m_xSize << ", " << m_ySize << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryApp)
|
||||
EVENT_TYPE_SETUP(WindowResize)
|
||||
|
||||
private:
|
||||
float m_xSize, m_ySize;
|
||||
};
|
||||
|
||||
class AppUpdateEvent : public Event
|
||||
{
|
||||
public:
|
||||
AppUpdateEvent() = default;
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryApp)
|
||||
EVENT_TYPE_SETUP(AppUpdate)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
82
Navigator/src/Navigator/Events/Event.h
Normal file
82
Navigator/src/Navigator/Events/Event.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
#ifndef EVENT_H
|
||||
#define EVENT_H
|
||||
|
||||
#include "NavCore.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
enum class EventType
|
||||
{
|
||||
None=0,
|
||||
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
|
||||
KeyPressed, KeyReleased, KeyTyped,
|
||||
MouseButtonPressed, MouseButtonReleased, MouseScrolled, MouseMoved,
|
||||
AppUpdate
|
||||
};
|
||||
|
||||
enum EventCategory
|
||||
{
|
||||
EventCategoryNone=0,
|
||||
EventCategoryApp=BIT(0),
|
||||
EventCategoryInput=BIT(1),
|
||||
EventCategoryKey=BIT(2),
|
||||
EventCategoryMouse=BIT(3),
|
||||
EventCategoryWindow=BIT(4)
|
||||
};
|
||||
|
||||
//Some function generation automation to reduce code written for all events
|
||||
#define EVENT_CATEGORY_SETUP(cat) virtual int GetCategoryFlags() const override { return cat; }
|
||||
|
||||
#define EVENT_TYPE_SETUP(type) static EventType GetStaticType() { return EventType::type; } \
|
||||
virtual EventType GetEventType() const override { return GetStaticType(); } \
|
||||
virtual const char* GetName() const override { return #type; }
|
||||
|
||||
class Event
|
||||
{
|
||||
friend class EventDispatcher;
|
||||
public:
|
||||
virtual EventType GetEventType() const = 0;
|
||||
virtual const char* GetName() const = 0;
|
||||
virtual int GetCategoryFlags() const = 0;
|
||||
virtual std::string ToString() const { return GetName(); }
|
||||
inline bool IsCategory(EventCategory cat) const { return GetCategoryFlags() & cat; }
|
||||
bool handledFlag = false;
|
||||
};
|
||||
|
||||
class EventDispatcher
|
||||
{
|
||||
template<typename T>
|
||||
using EventFunc = std::function<bool(T&)>;
|
||||
|
||||
public:
|
||||
EventDispatcher(Event& e) :
|
||||
m_event(e)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Dispatch(EventFunc<T> function)
|
||||
{
|
||||
if(m_event.GetEventType() == T::GetStaticType())
|
||||
{
|
||||
m_event.handledFlag = function(*(T*)&m_event);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Event& m_event;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Event& e)
|
||||
{
|
||||
return os << e.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
88
Navigator/src/Navigator/Events/KeyEvent.h
Normal file
88
Navigator/src/Navigator/Events/KeyEvent.h
Normal file
|
@ -0,0 +1,88 @@
|
|||
#ifndef KEY_EVENT_H
|
||||
#define KEY_EVENT_H
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
class KeyEvent : public Event
|
||||
{
|
||||
public:
|
||||
inline int GetKeycode() const { return m_keycode; }
|
||||
EVENT_CATEGORY_SETUP(EventCategoryKey | EventCategoryInput)
|
||||
protected:
|
||||
KeyEvent(int code) :
|
||||
m_keycode(code)
|
||||
{
|
||||
}
|
||||
|
||||
int m_keycode;
|
||||
|
||||
};
|
||||
|
||||
class KeyPressedEvent : public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyPressedEvent(int code, int count) :
|
||||
KeyEvent(code), m_repeatCount(count)
|
||||
{
|
||||
}
|
||||
|
||||
EVENT_TYPE_SETUP(KeyPressed)
|
||||
|
||||
inline int GetRepeatCount() const { return m_repeatCount; }
|
||||
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << GetName() << " with code "<<m_keycode << " pressed "<<m_repeatCount<<" times.";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int m_repeatCount;
|
||||
};
|
||||
|
||||
class KeyReleasedEvent : public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyReleasedEvent(int code) :
|
||||
KeyEvent(code)
|
||||
{
|
||||
};
|
||||
|
||||
EVENT_TYPE_SETUP(KeyReleased)
|
||||
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " with code " << m_keycode;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class KeyTypedEvent : public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyTypedEvent(int code) :
|
||||
KeyEvent(code)
|
||||
{
|
||||
};
|
||||
|
||||
EVENT_TYPE_SETUP(KeyTyped)
|
||||
|
||||
unsigned int GetCharacter() { return (unsigned int)m_keycode; }
|
||||
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " with code " << m_keycode;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
104
Navigator/src/Navigator/Events/MouseEvent.h
Normal file
104
Navigator/src/Navigator/Events/MouseEvent.h
Normal file
|
@ -0,0 +1,104 @@
|
|||
#ifndef MOUSE_EVENT_H
|
||||
#define MOUSE_EVENT_H
|
||||
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
class MouseMovedEvent : public Event
|
||||
{
|
||||
public:
|
||||
MouseMovedEvent(float x, float y) :
|
||||
m_xPos(x), m_yPos(y)
|
||||
{
|
||||
}
|
||||
|
||||
inline float GetXPosition() { return m_xPos; }
|
||||
inline float GetYPosition() { return m_yPos; }
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " to position (" << m_xPos << ", " << m_yPos << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryMouse | EventCategoryInput)
|
||||
EVENT_TYPE_SETUP(MouseMoved)
|
||||
|
||||
private:
|
||||
float m_xPos, m_yPos;
|
||||
};
|
||||
|
||||
class MouseScrolledEvent : public Event
|
||||
{
|
||||
public:
|
||||
MouseScrolledEvent(float x, float y) :
|
||||
m_xOffset(x), m_yOffset(y)
|
||||
{
|
||||
}
|
||||
|
||||
inline float GetXOffset() { return m_xOffset; }
|
||||
inline float GetYOffset() { return m_yOffset; }
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " to offset (" << m_xOffset << ", " << m_yOffset << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryMouse | EventCategoryInput)
|
||||
EVENT_TYPE_SETUP(MouseScrolled)
|
||||
|
||||
private:
|
||||
float m_xOffset, m_yOffset;
|
||||
};
|
||||
|
||||
class MouseButtonPressedEvent : public Event
|
||||
{
|
||||
public:
|
||||
MouseButtonPressedEvent(int code) :
|
||||
m_buttonCode(code)
|
||||
{
|
||||
}
|
||||
|
||||
inline int GetButtonCode() { return m_buttonCode; }
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " with button code " << m_buttonCode;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryMouse | EventCategoryInput)
|
||||
EVENT_TYPE_SETUP(MouseButtonPressed)
|
||||
|
||||
private:
|
||||
int m_buttonCode;
|
||||
};
|
||||
|
||||
class MouseButtonReleasedEvent : public Event
|
||||
{
|
||||
public:
|
||||
MouseButtonReleasedEvent(int code) :
|
||||
m_buttonCode(code)
|
||||
{
|
||||
}
|
||||
|
||||
inline int GetButtonCode() { return m_buttonCode; }
|
||||
std::string ToString() const override
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << GetName() << " with button code " << m_buttonCode;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CATEGORY_SETUP(EventCategoryMouse | EventCategoryInput)
|
||||
EVENT_TYPE_SETUP(MouseButtonReleased)
|
||||
|
||||
private:
|
||||
int m_buttonCode;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
9
Navigator/src/Navigator/NavCore.h
Normal file
9
Navigator/src/Navigator/NavCore.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef NAVCORE_H
|
||||
#define NAVCORE_H
|
||||
|
||||
//Bit field setter
|
||||
#define BIT(x) (1<<x)
|
||||
|
||||
#define BIND_EVENT_FUNCTION(x) std::bind(&x, this, std::placeholders::_1)
|
||||
|
||||
#endif
|
|
@ -7,6 +7,6 @@
|
|||
],
|
||||
"settings":
|
||||
{
|
||||
"SublimeLinter.linters.g++.args": ["-c","-Wall","-fsyntax-only","-std=c++17","-include${folder}/src/navpch.h","-Isrc/", "-Isrc/vendor/spdlog/include"]
|
||||
"SublimeLinter.linters.g++.args": ["-c","-Wall","-fsyntax-only","-std=c++17","-include${folder}/Navigator/src/navpch.h","-INavigator/src/", "-INavigator/src/Navigator/","-INavigator/vendor/spdlog/include"]
|
||||
}
|
||||
}
|
|
@ -24,23 +24,25 @@ project "Navigator"
|
|||
staticruntime "on"
|
||||
targetdir ("lib/" .. outputdir .. "/%{prj.name}")
|
||||
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")
|
||||
pchheader "navpch.h"
|
||||
pchsource "Navigator/src/navpch.cpp"
|
||||
|
||||
filter "system:windows"
|
||||
defines "NAV_WINDOWS"
|
||||
pchheader "navpch.h"
|
||||
pchsource "Navigator/src/navpch.cpp"
|
||||
forceincludes {"navpch.h"}
|
||||
includedirs {
|
||||
"%{prj.name}/src/",
|
||||
"%{prj.name}/src/%{prj.name}/",
|
||||
"%{prj.name}/vendor/spdlog/include/",
|
||||
"%{IncludeDirs.glfw}",
|
||||
"%{IncludeDirs.ImGui}",
|
||||
}
|
||||
filter "system:linux or macosx"
|
||||
defines "NAV_UNIX"
|
||||
defines "NAV_PCH"
|
||||
pchheader "%{prj.name}/src/navpch.h"
|
||||
includedirs {
|
||||
"%{prj.name}/vendor/spdlog/include/",
|
||||
"%{prj.name}/src/%{prj.name}/",
|
||||
"%{IncludeDirs.glfw}",
|
||||
"%{IncludeDirs.ImGui}",
|
||||
}
|
||||
|
@ -93,6 +95,7 @@ project "NavProject"
|
|||
|
||||
includedirs {
|
||||
"Navigator/src",
|
||||
"Navigator/src/Navigator",
|
||||
"Navigator/vendor/spdlog/include/",
|
||||
"Navigator/vendor"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user