add events
This commit is contained in:
parent
8cbcec09fa
commit
7662364f72
|
@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.24)
|
|||
|
||||
project(quark)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
set(SPDLOG_BUILD_SHARED ON CACHE INTERNAL "")
|
||||
|
|
|
@ -15,6 +15,11 @@ target_sources(quark
|
|||
src/quark.hpp
|
||||
src/quark/Application.hpp
|
||||
src/quark/Logger.hpp
|
||||
|
||||
src/quark/events/Event.hpp
|
||||
src/quark/events/ApplicationEvent.hpp
|
||||
src/quark/events/KeyEvent.hpp
|
||||
src/quark/events/MouseEvent.hpp
|
||||
)
|
||||
|
||||
target_include_directories(quark INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
#include "Application.hpp"
|
||||
#include "quark/Logger.hpp"
|
||||
#include "quark/events/ApplicationEvent.hpp"
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace qk {
|
||||
|
||||
void Application::Run() const {
|
||||
WindowResizeEvent e(1200, 720);
|
||||
QK_TRACE(e);
|
||||
|
||||
for(;;) {
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <memory>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/fmt/ostr.h>
|
||||
|
||||
namespace qk {
|
||||
class Logger {
|
||||
|
@ -36,8 +37,9 @@ namespace qk {
|
|||
#ifndef NDEBUG
|
||||
# define QK_TRACE(...) ::qk::Logger::GetClientLogger()->trace(__VA_ARGS__)
|
||||
#else
|
||||
# define QK_INFO(...) ::qk::Logger::GetClientLogger()->info(__VA_ARGS__)
|
||||
# define QK_TRACE
|
||||
#endif
|
||||
#define QK_INFO(...) ::qk::Logger::GetClientLogger()->info(__VA_ARGS__)
|
||||
#define QK_WARN(...) ::qk::Logger::GetClientLogger()->warn(__VA_ARGS__)
|
||||
#define QK_ERROR(...) ::qk::Logger::GetClientLogger()->error(__VA_ARGS__)
|
||||
#define QK_CRITICAL(...) ::qk::Logger::GetClientLogger()->critical(__VA_ARGS__)
|
||||
|
|
39
quark/src/quark/events/ApplicationEvent.hpp
Normal file
39
quark/src/quark/events/ApplicationEvent.hpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
// IWYU pragma: private <quark.h>
|
||||
|
||||
#include "Event.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace qk {
|
||||
class WindowResizeEvent :
|
||||
public Event
|
||||
{
|
||||
public:
|
||||
WindowResizeEvent(unsigned int width, unsigned int height) :
|
||||
width(width), height(height)
|
||||
{ }
|
||||
|
||||
inline unsigned int GetWidth() const { return width; }
|
||||
inline unsigned int GetHeight() const { return height; }
|
||||
|
||||
virtual operator std::string() const override {
|
||||
std::stringstream ss;
|
||||
ss << "WindowResizEvent: " << width << ", " << height;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CLASS_TYPE(WindowResize)
|
||||
EVENT_CLASS_CATEGORY(EventCategoryApplication)
|
||||
|
||||
private:
|
||||
unsigned int width, height;
|
||||
};
|
||||
|
||||
class WindowCloseEvent :
|
||||
public Event
|
||||
{
|
||||
public:
|
||||
EVENT_CLASS_TYPE(WindowClose)
|
||||
EVENT_CLASS_CATEGORY(EventCategoryApplication)
|
||||
};
|
||||
}
|
100
quark/src/quark/events/Event.hpp
Normal file
100
quark/src/quark/events/Event.hpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
#pragma once
|
||||
// IWYU pragma: private <quark.h>
|
||||
|
||||
#include <fmt/base.h>
|
||||
#include <fmt/ostream.h>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
namespace qk {
|
||||
enum class EventType {
|
||||
None = 0,
|
||||
|
||||
WindowClose,
|
||||
WindowResize,
|
||||
WindowFocus,
|
||||
WindowLostFocus,
|
||||
WindowMoved,
|
||||
|
||||
KeyPressed,
|
||||
KeyReleased,
|
||||
|
||||
MouseButtonPressed,
|
||||
MouseButtonReleased,
|
||||
MouseMoved,
|
||||
MouseScrolled
|
||||
};
|
||||
|
||||
enum EventCategory {
|
||||
None = 0,
|
||||
|
||||
EventCategoryApplication = 0x1,
|
||||
EventCategoryInput = 0x2,
|
||||
EventCategoryKeyboard = 0x4,
|
||||
EventCategoryMouse = 0x8,
|
||||
EventCategoryMouseButton = 0x10,
|
||||
};
|
||||
|
||||
#define EVENT_CLASS_TYPE(type) \
|
||||
static EventType GetStaticType() { return (EventType::type); } \
|
||||
virtual EventType GetEventType() const override { return GetStaticType(); } \
|
||||
virtual const char* GetName() const override { return #type; }
|
||||
|
||||
#define EVENT_CLASS_CATEGORY(category) \
|
||||
virtual int GetCategoryFlags() const override { return category; }
|
||||
|
||||
class Event {
|
||||
friend class EventDispatcher;
|
||||
|
||||
public:
|
||||
virtual EventType GetEventType() const = 0;
|
||||
virtual const char* GetName() const = 0;
|
||||
virtual int GetCategoryFlags() const = 0;
|
||||
|
||||
virtual operator std::string() const {
|
||||
return GetName();
|
||||
}
|
||||
|
||||
inline bool IsInCagegory(EventCategory category) const {
|
||||
return (GetCategoryFlags() & category) != 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool handled = false;
|
||||
};
|
||||
|
||||
class EventDispatcher {
|
||||
template <typename T>
|
||||
using EventFn = std::function<bool(T&)>;
|
||||
|
||||
public:
|
||||
EventDispatcher(Event& event) :
|
||||
event(event)
|
||||
{ }
|
||||
|
||||
template <typename T>
|
||||
requires std::is_base_of_v<Event, T>
|
||||
bool Dispatch(EventFn<T> func) {
|
||||
if (event.GetEventType() == T::GetStaticType()) {
|
||||
event.handled = func(*(T*)&event);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
Event& event;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Event& event) {
|
||||
os << std::string(event);
|
||||
return os;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct fmt::formatter<T, std::enable_if_t<std::is_base_of_v<qk::Event, T>, char>> :
|
||||
fmt::ostream_formatter {};
|
49
quark/src/quark/events/KeyEvent.hpp
Normal file
49
quark/src/quark/events/KeyEvent.hpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#pragma once
|
||||
// IWYU pragma: private <quark.h>
|
||||
|
||||
#include "Event.hpp"
|
||||
|
||||
namespace qk {
|
||||
|
||||
class KeyEvent :
|
||||
public Event
|
||||
{
|
||||
public:
|
||||
inline int GetKeyCode() const { return keyCode; }
|
||||
|
||||
EVENT_CLASS_CATEGORY(EventCategoryKeyboard | EventCategoryInput)
|
||||
|
||||
protected:
|
||||
KeyEvent(int keyCode) :
|
||||
keyCode(keyCode)
|
||||
{ }
|
||||
|
||||
int keyCode;
|
||||
};
|
||||
|
||||
class KeyPressedEvent
|
||||
: public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyPressedEvent(int keyCode, int repeatCount) :
|
||||
KeyEvent(keyCode), repeatCount(repeatCount)
|
||||
{ }
|
||||
|
||||
inline int GetRepeatCount() const { return repeatCount; }
|
||||
|
||||
EVENT_CLASS_TYPE(KeyPressed)
|
||||
private:
|
||||
int repeatCount;
|
||||
};
|
||||
|
||||
class KeyReleasedEvent
|
||||
: public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyReleasedEvent(int keyCode) :
|
||||
KeyEvent(keyCode)
|
||||
{ }
|
||||
|
||||
EVENT_CLASS_TYPE(KeyReleased)
|
||||
};
|
||||
}
|
102
quark/src/quark/events/MouseEvent.hpp
Normal file
102
quark/src/quark/events/MouseEvent.hpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
#pragma once
|
||||
// IWYU pragma: private <quark.h>
|
||||
|
||||
#include "Event.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace qk {
|
||||
class MouseMovedEvent :
|
||||
public Event
|
||||
{
|
||||
public:
|
||||
MouseMovedEvent(float x, float y) :
|
||||
mouseX(x), mouseY(y)
|
||||
{ }
|
||||
|
||||
inline float GetX() const { return mouseX; }
|
||||
inline float GetY() const { return mouseY; }
|
||||
|
||||
virtual operator std::string() const override {
|
||||
std::stringstream ss;
|
||||
ss << "MouseMovedEvent: " << mouseX << ", " << mouseY;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CLASS_TYPE(MouseMoved)
|
||||
EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput)
|
||||
|
||||
private:
|
||||
float mouseX, mouseY;
|
||||
};
|
||||
|
||||
class MouseScrolledEvent :
|
||||
public Event
|
||||
{
|
||||
public:
|
||||
MouseScrolledEvent(float offsetX, float offsetY) :
|
||||
offsetX(offsetX), offsetY(offsetY)
|
||||
{ }
|
||||
|
||||
virtual operator std::string() const override {
|
||||
std::stringstream ss;
|
||||
ss << "MouseScrolledEvent: " << offsetX << ", " << offsetY;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CLASS_TYPE(MouseScrolled)
|
||||
EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput)
|
||||
|
||||
private:
|
||||
float offsetX, offsetY;
|
||||
};
|
||||
|
||||
class MouseButtonEvent :
|
||||
public Event
|
||||
{
|
||||
public:
|
||||
inline int GetMouseButton() const { return button; }
|
||||
|
||||
EVENT_CLASS_CATEGORY(EventCategoryMouseButton | EventCategoryInput)
|
||||
|
||||
protected:
|
||||
MouseButtonEvent(int button) :
|
||||
button(button)
|
||||
{ }
|
||||
|
||||
int button;
|
||||
};
|
||||
|
||||
class MouseButtonPressedEvent :
|
||||
public MouseButtonEvent
|
||||
{
|
||||
public:
|
||||
MouseButtonPressedEvent(int button) :
|
||||
MouseButtonEvent(button)
|
||||
{ }
|
||||
|
||||
virtual operator std::string() const override {
|
||||
std::stringstream ss;
|
||||
ss << "MouseButtonPressedEvent: " << button;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CLASS_TYPE(MouseButtonPressed)
|
||||
};
|
||||
|
||||
class MouseButtonReleasedEvent :
|
||||
public MouseButtonEvent
|
||||
{
|
||||
public:
|
||||
MouseButtonReleasedEvent(int button) :
|
||||
MouseButtonEvent(button)
|
||||
{ }
|
||||
|
||||
virtual operator std::string() const override {
|
||||
std::stringstream ss;
|
||||
ss << "MouseButtonReleasedEvent: " << button;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
EVENT_CLASS_TYPE(MouseButtonReleased)
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue