diff --git a/bin/layers/local_panel/local_panel.cpp b/bin/layers/local_panel/local_panel.cpp index e6bde8d..44fb369 100755 --- a/bin/layers/local_panel/local_panel.cpp +++ b/bin/layers/local_panel/local_panel.cpp @@ -5,21 +5,17 @@ namespace sandbox { void local_panel::on_attach() { - hack::log()("on_attach"); - } - - void local_panel::on_detach() - { + VE_CONNECT(local_panel, on_event); hack::log()("on_attach"); } 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") }; - execute(e); + EMIT(e); } VE_PUSH_FONT(ICON, 18); @@ -27,7 +23,7 @@ namespace sandbox 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") }; - execute(e); + EMIT(e); } ImGui::Text(VE::style::icon::ICON_PAINT_BRUSH, " Paint" ); diff --git a/bin/layers/opengl_panel/opengl_panel.cpp b/bin/layers/opengl_panel/opengl_panel.cpp index 7c5900e..ece4fb2 100755 --- a/bin/layers/opengl_panel/opengl_panel.cpp +++ b/bin/layers/opengl_panel/opengl_panel.cpp @@ -45,11 +45,7 @@ namespace sandbox void opengl_panel::on_attach() { - hack::log()("on_attach"); - } - - void opengl_panel::on_detach() - { + VE_CONNECT(opengl_panel, on_event); hack::log()("on_attach"); } diff --git a/bin/layers/test_panel/test_panel.cpp b/bin/layers/test_panel/test_panel.cpp index 70329c8..6c965c4 100755 --- a/bin/layers/test_panel/test_panel.cpp +++ b/bin/layers/test_panel/test_panel.cpp @@ -5,14 +5,10 @@ namespace sandbox { void test_panel::on_attach() { + VE_CONNECT(test_panel, on_event); hack::log()("on_attach", VE::application::get()->get_glfw()->width()); } - void test_panel::on_detach() - { - hack::log()("on_attach"); - } - void test_panel::render() { ImGui::SetNextWindowPos(m_pos); @@ -23,7 +19,7 @@ namespace sandbox if (ImGui::Button("RUN", ImVec2(28, 30))) { VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") }; - execute(e); + EMIT(e); } ImGui::End(); diff --git a/bin/layers/test_panel_2/test_panel_2.cpp b/bin/layers/test_panel_2/test_panel_2.cpp index c192a52..2db5cbb 100755 --- a/bin/layers/test_panel_2/test_panel_2.cpp +++ b/bin/layers/test_panel_2/test_panel_2.cpp @@ -5,11 +5,7 @@ namespace sandbox { void test_panel_2::on_attach() { - hack::log()("on_attach"); - } - - void test_panel_2::on_detach() - { + VE_CONNECT(test_panel_2, on_event); hack::log()("on_attach"); } diff --git a/bin/main.cpp b/bin/main.cpp index 1c178c1..f76ac75 100755 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -21,9 +21,6 @@ namespace sandbox namespace VE { - // HERE - // перенести в VE.hpp - // и сделать варитивными параметрами !!! inline application& create() { static sandbox::test_app app{ "vertex_engine_sandbox" }; diff --git a/src/application/application.cpp b/src/application/application.cpp index 519541e..478d2cd 100755 --- a/src/application/application.cpp +++ b/src/application/application.cpp @@ -7,10 +7,7 @@ namespace VE application::application(std::string app_name) : m_glfw{ std::make_unique() } { m_instance = std::unique_ptr(this); - m_glfw->init(app_name); - m_glfw->set_event_fn(VE_EVENT_FN); - m_gui = std::make_unique(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); - } } diff --git a/src/application/application.hpp b/src/application/application.hpp index 6c13045..991bc87 100755 --- a/src/application/application.hpp +++ b/src/application/application.hpp @@ -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); }; // реализация см. в проекте diff --git a/src/event/event.hpp b/src/event/event.hpp index ce6b46f..13094a0 100755 --- a/src/event/event.hpp +++ b/src/event/event.hpp @@ -39,4 +39,14 @@ namespace VE // как-то не прикольно так !!! bool is_parallele{ false }; }; + + struct event_manager : public hack::patterns::singleton + { + std::vector> funcs; + void connect(event_fn fn) { funcs.push_back(fn); }; + void emit(event e) + { + for(auto& f : funcs) f(e); + } + }; } diff --git a/src/glfw/glfw.cpp b/src/glfw/glfw.cpp index b407f6c..ba409c2 100755 --- a/src/glfw/glfw.cpp +++ b/src/glfw/glfw.cpp @@ -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& 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(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(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(glfwGetWindowUserPointer(w)); try { event e{ event_target::VE, event_type::MOUSE_SCROLL, std::pair{ static_cast(xOffset), static_cast(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(glfwGetWindowUserPointer(w)); try { event e{ event_target::VE, event_type::MOUSE_CURSOR_POSITION, std::pair{ static_cast(xPos), static_cast(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{ static_cast(width), static_cast(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(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(glfwGetWindowUserPointer(w)); try { event e{ event_target::VE, event_type::WINDOW_FOCUS, focused }; - d->execute(e); + EMIT(e); } catch(std::exception& e) { diff --git a/src/glfw/glfw.hpp b/src/glfw/glfw.hpp index c2ebd96..270a52c 100755 --- a/src/glfw/glfw.hpp +++ b/src/glfw/glfw.hpp @@ -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 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& fn); - private: void set_hint(); void set_window(); diff --git a/src/layer/layer.hpp b/src/layer/layer.hpp index a7585d6..afc1dd3 100755 --- a/src/layer/layer.hpp +++ b/src/layer/layer.hpp @@ -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& fn) { execute = fn; }; - protected: event_fn execute; }; diff --git a/src/utils/define.hpp b/src/utils/define.hpp index 7910a90..7ed1591 100755 --- a/src/utils/define.hpp +++ b/src/utils/define.hpp @@ -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()