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

@@ -5,21 +5,17 @@ namespace sandbox
{ {
void local_panel::on_attach() void local_panel::on_attach()
{ {
hack::log()("on_attach"); VE_CONNECT(local_panel, on_event);
}
void local_panel::on_detach()
{
hack::log()("on_attach"); hack::log()("on_attach");
} }
void local_panel::render() void local_panel::render()
{ {
if (ImGui::Button("RUN", ImVec2(28, 30))) if (ImGui::Button("RUN 2", ImVec2(28, 30)))
{ {
VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") }; VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") };
execute(e); EMIT(e);
} }
VE_PUSH_FONT(ICON, 18); VE_PUSH_FONT(ICON, 18);
@@ -27,7 +23,7 @@ namespace sandbox
if (ImGui::Button(VE::style::icon::ICON_STOP, ImVec2(28, 30))) if (ImGui::Button(VE::style::icon::ICON_STOP, ImVec2(28, 30)))
{ {
VE::event e { "target_id", test_event::TEST_EVEN , std::string("test icon button") }; VE::event e { "target_id", test_event::TEST_EVEN , std::string("test icon button") };
execute(e); EMIT(e);
} }
ImGui::Text(VE::style::icon::ICON_PAINT_BRUSH, " Paint" ); ImGui::Text(VE::style::icon::ICON_PAINT_BRUSH, " Paint" );

View File

@@ -45,11 +45,7 @@ namespace sandbox
void opengl_panel::on_attach() void opengl_panel::on_attach()
{ {
hack::log()("on_attach"); VE_CONNECT(opengl_panel, on_event);
}
void opengl_panel::on_detach()
{
hack::log()("on_attach"); hack::log()("on_attach");
} }

View File

@@ -5,14 +5,10 @@ namespace sandbox
{ {
void test_panel::on_attach() void test_panel::on_attach()
{ {
VE_CONNECT(test_panel, on_event);
hack::log()("on_attach", VE::application::get()->get_glfw()->width()); hack::log()("on_attach", VE::application::get()->get_glfw()->width());
} }
void test_panel::on_detach()
{
hack::log()("on_attach");
}
void test_panel::render() void test_panel::render()
{ {
ImGui::SetNextWindowPos(m_pos); ImGui::SetNextWindowPos(m_pos);
@@ -23,7 +19,7 @@ namespace sandbox
if (ImGui::Button("RUN", ImVec2(28, 30))) if (ImGui::Button("RUN", ImVec2(28, 30)))
{ {
VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") }; VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") };
execute(e); EMIT(e);
} }
ImGui::End(); ImGui::End();

View File

@@ -5,11 +5,7 @@ namespace sandbox
{ {
void test_panel_2::on_attach() void test_panel_2::on_attach()
{ {
hack::log()("on_attach"); VE_CONNECT(test_panel_2, on_event);
}
void test_panel_2::on_detach()
{
hack::log()("on_attach"); hack::log()("on_attach");
} }

View File

@@ -21,9 +21,6 @@ namespace sandbox
namespace VE namespace VE
{ {
// HERE
// перенести в VE.hpp
// и сделать варитивными параметрами !!!
inline application& create() inline application& create()
{ {
static sandbox::test_app app{ "vertex_engine_sandbox" }; static sandbox::test_app app{ "vertex_engine_sandbox" };

View File

@@ -7,10 +7,7 @@ namespace VE
application::application(std::string app_name) : m_glfw{ std::make_unique<glfw>() } application::application(std::string app_name) : m_glfw{ std::make_unique<glfw>() }
{ {
m_instance = std::unique_ptr<application>(this); m_instance = std::unique_ptr<application>(this);
m_glfw->init(app_name); m_glfw->init(app_name);
m_glfw->set_event_fn(VE_EVENT_FN);
m_gui = std::make_unique<gui>(m_glfw); m_gui = std::make_unique<gui>(m_glfw);
} }
@@ -42,17 +39,4 @@ namespace VE
{ {
for (auto l : m_layers_stack) l->on_attach(); 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), ...); (m_layers_stack.push_back(args), ...);
attach_layers(); attach_layers();
set_event_fn();
} }
private: private:
void clear(); void clear();
void attach_layers(); void attach_layers();
void set_event_fn();
void on_event(event e);
}; };
// реализация см. в проекте // реализация см. в проекте

View File

@@ -39,4 +39,14 @@ namespace VE
// как-то не прикольно так !!! // как-то не прикольно так !!!
bool is_parallele{ false }; 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 "glfw.hpp"
// #include "renderer/renderer.hpp" #include "event/event.hpp"
// #include "event/system_event/category/key_event.hpp"
// #include "event/system_event/category/window_event.hpp"
namespace VE namespace VE
{ {
@@ -111,16 +109,10 @@ namespace VE
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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() void glfw::set_key_callback()
{ {
glfwSetKeyCallback(m_win, [](GLFWwindow* w, int key, int scancode, int action, int mods) 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 }; event e{ event_target::VE, action, key };
// HERE // 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) 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 }; event e{ event_target::VE, action, button };
switch (action) switch (action)
@@ -182,23 +173,15 @@ namespace VE
} }
} }
try EMIT(e);
{
d->execute(e);
}
catch(std::exception& e)
{
hack::log()("VE_ERROR: ", e.what());
}
}); });
glfwSetScrollCallback(m_win, [](GLFWwindow* w, double xOffset, double yOffset) glfwSetScrollCallback(m_win, [](GLFWwindow* w, double xOffset, double yOffset)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try try
{ {
event e{ event_target::VE, event_type::MOUSE_SCROLL, std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) } }; 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) catch(std::exception& e)
{ {
@@ -208,11 +191,10 @@ namespace VE
glfwSetCursorPosCallback(m_win, [](GLFWwindow* w, double xPos, double yPos) glfwSetCursorPosCallback(m_win, [](GLFWwindow* w, double xPos, double yPos)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try try
{ {
event e{ event_target::VE, event_type::MOUSE_CURSOR_POSITION, std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) } }; 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) catch(std::exception& e)
{ {
@@ -234,7 +216,7 @@ namespace VE
try try
{ {
event e{ event_target::VE, event_type::WINDOW_RESIZE, std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) } }; 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) catch(std::exception& e)
{ {
@@ -244,11 +226,10 @@ namespace VE
glfwSetWindowCloseCallback(m_win, [](GLFWwindow* w) glfwSetWindowCloseCallback(m_win, [](GLFWwindow* w)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try try
{ {
event e{ event_target::VE, event_type::WINDOW_CLOSE, nullptr }; event e{ event_target::VE, event_type::WINDOW_CLOSE, nullptr };
d->execute(e); EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {
@@ -258,11 +239,10 @@ namespace VE
glfwSetWindowFocusCallback(m_win, [](GLFWwindow* w, int focused) glfwSetWindowFocusCallback(m_win, [](GLFWwindow* w, int focused)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
try try
{ {
event e{ event_target::VE, event_type::WINDOW_FOCUS, focused }; event e{ event_target::VE, event_type::WINDOW_FOCUS, focused };
d->execute(e); EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {

View File

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

View File

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

View File

@@ -3,16 +3,15 @@
#define BIT(x)\ #define BIT(x)\
(1 << x) (1 << x)
#define VE_EVENT_FN\
std::bind(&application::on_event, this, std::placeholders::_1)
#define VE_FN_OVERIDE()\ #define VE_FN_OVERIDE()\
public:\ public:\
void on_attach() override;\ void on_attach() override;\
void on_detach() override;\
void render() override;\ void render() override;\
void on_event(VE::event e) 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)\ #define VE_PUSH_FONT(def_font, def_size)\
ImGui::PushFont(VE::style::fonts::get_font(VE::style::fonts::font_type::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() #define VE_POP_FONT() ImGui::PopFont()