fix event

This commit is contained in:
2025-02-26 15:09:30 +03:00
parent 121d4af325
commit 7e419c9b79
12 changed files with 29 additions and 85 deletions

View File

@@ -7,10 +7,7 @@ namespace VE
application::application(std::string app_name) : m_glfw{ std::make_unique<glfw>() }
{
m_instance = std::unique_ptr<application>(this);
m_glfw->init(app_name);
m_glfw->set_event_fn(VE_EVENT_FN);
m_gui = std::make_unique<gui>(m_glfw);
}
@@ -42,17 +39,4 @@ namespace VE
{
for (auto l : m_layers_stack) l->on_attach();
}
void application::set_event_fn()
{
for (const auto l : m_layers_stack) l->set_event_fn(VE_EVENT_FN);
}
void application::on_event(event e)
{
if (e.is_parallele)
for (auto l : m_layers_stack) auto future = std::async(std::launch::async, [&]() { l->on_event(e); });
else
for (auto l : m_layers_stack) l->on_event(e);
}
}

View File

@@ -29,14 +29,11 @@ namespace VE
{
(m_layers_stack.push_back(args), ...);
attach_layers();
set_event_fn();
}
private:
void clear();
void attach_layers();
void set_event_fn();
void on_event(event e);
};
// реализация см. в проекте

View File

@@ -39,4 +39,14 @@ namespace VE
// как-то не прикольно так !!!
bool is_parallele{ false };
};
struct event_manager : public hack::patterns::singleton<event_manager>
{
std::vector<VE::event_fn<VE::event>> funcs;
void connect(event_fn<event> fn) { funcs.push_back(fn); };
void emit(event e)
{
for(auto& f : funcs) f(e);
}
};
}

View File

@@ -1,8 +1,6 @@
#include "glfw.hpp"
// #include "renderer/renderer.hpp"
// #include "event/system_event/category/key_event.hpp"
// #include "event/system_event/category/window_event.hpp"
#include "event/event.hpp"
namespace VE
{
@@ -111,16 +109,10 @@ namespace VE
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void glfw::set_event_fn(const event_fn<event>& fn)
{
m_win_data.execute = fn;
}
void glfw::set_key_callback()
{
glfwSetKeyCallback(m_win, [](GLFWwindow* w, int key, int scancode, int action, int mods)
{
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ event_target::VE, action, key };
// HERE
@@ -148,7 +140,7 @@ namespace VE
}
}
d->execute(e);
EMIT(e);
});
}
@@ -156,7 +148,6 @@ namespace VE
{
glfwSetMouseButtonCallback(m_win, [](GLFWwindow* w, int button, int action, int mods)
{
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ event_target::VE, action, button };
switch (action)
@@ -182,23 +173,15 @@ namespace VE
}
}
try
{
d->execute(e);
}
catch(std::exception& e)
{
hack::log()("VE_ERROR: ", e.what());
}
EMIT(e);
});
glfwSetScrollCallback(m_win, [](GLFWwindow* w, double xOffset, double yOffset)
{
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try
{
event e{ event_target::VE, event_type::MOUSE_SCROLL, std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) } };
d->execute(e);
EMIT(e);
}
catch(std::exception& e)
{
@@ -208,11 +191,10 @@ namespace VE
glfwSetCursorPosCallback(m_win, [](GLFWwindow* w, double xPos, double yPos)
{
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try
{
event e{ event_target::VE, event_type::MOUSE_CURSOR_POSITION, std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) } };
d->execute(e);
EMIT(e);
}
catch(std::exception& e)
{
@@ -234,7 +216,7 @@ namespace VE
try
{
event e{ event_target::VE, event_type::WINDOW_RESIZE, std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) } };
d->execute(e);
EMIT(e);
}
catch(std::exception& e)
{
@@ -244,11 +226,10 @@ namespace VE
glfwSetWindowCloseCallback(m_win, [](GLFWwindow* w)
{
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try
{
event e{ event_target::VE, event_type::WINDOW_CLOSE, nullptr };
d->execute(e);
EMIT(e);
}
catch(std::exception& e)
{
@@ -258,11 +239,10 @@ namespace VE
glfwSetWindowFocusCallback(m_win, [](GLFWwindow* w, int focused)
{
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try
{
event e{ event_target::VE, event_type::WINDOW_FOCUS, focused };
d->execute(e);
EMIT(e);
}
catch(std::exception& e)
{

View File

@@ -2,7 +2,6 @@
#include "utils/utils.hpp" // IWYU pragma: keep
#include "context/context.hpp"
#include "event/event.hpp"
namespace VE
{
@@ -27,7 +26,6 @@ namespace VE
{
std::string m_name;
int m_width, m_height;
event_fn<event&> execute; // в using.hpp
} m_win_data;
public:
@@ -40,8 +38,6 @@ namespace VE
void update();
void clear() const;
void set_event_fn(const event_fn<event>& fn);
private:
void set_hint();
void set_window();

View File

@@ -18,12 +18,9 @@ namespace VE
public:
virtual void on_attach() {};
virtual void on_detach() {};
virtual void render() {};
virtual void on_event(event e) {};
void set_event_fn(const event_fn<event>& fn) { execute = fn; };
protected:
event_fn<event> execute;
};

View File

@@ -3,16 +3,15 @@
#define BIT(x)\
(1 << x)
#define VE_EVENT_FN\
std::bind(&application::on_event, this, std::placeholders::_1)
#define VE_FN_OVERIDE()\
public:\
void on_attach() override;\
void on_detach() override;\
void render() override;\
void on_event(VE::event e) override
#define VE_CONNECT(DEF_CLASS, DEF_FUNCTION) VE::event_manager::instance().connect(std::bind(&DEF_CLASS::DEF_FUNCTION, this, std::placeholders::_1));
#define EMIT(e) VE::event_manager::instance().emit(e)
#define VE_PUSH_FONT(def_font, def_size)\
ImGui::PushFont(VE::style::fonts::get_font(VE::style::fonts::font_type::def_font, def_size))
#define VE_POP_FONT() ImGui::PopFont()