diff --git a/example/layers/ui_layer/ui_layer.cpp b/example/layers/ui_layer/ui_layer.cpp index b96af52..367b790 100755 --- a/example/layers/ui_layer/ui_layer.cpp +++ b/example/layers/ui_layer/ui_layer.cpp @@ -6,14 +6,14 @@ namespace example { void ui_layer::on_attach() { - VE::CONNECT(this); + CONNECT(this); hack::log()("on_attach"); } void ui_layer::on_detach() { - VE::DISCONNECT(this); + DISCONNECT(); hack::log()("on_detach"); } @@ -26,20 +26,7 @@ namespace example VE_PUSH_FONT(REGULAR, 20); if (ImGui::Button("Test signal", ImVec2(128, 130))) - VE::EMIT(test_event::TEST_EVEN, std::string("test event message"), 1, "event MSG"); - - // try - // { - // if (ImGui::Button("Test signal log error", ImVec2(128, 130))) - // VE::EMIT(test_event::TEST_EVEN, std::string("test event message")); - // } - // catch(hack::exception& e) - // { - // e.log(); - // auto data = std::any_cast(e.get_data()); - // hack::log()(data.m_id); - // } - + EMIT(test_event::TEST_EVEN, std::string("test event message"), 1); VE_POP_FONT(); diff --git a/example/layers/ui_layer/ui_layer.hpp b/example/layers/ui_layer/ui_layer.hpp index c2b1cc8..53ac3c6 100755 --- a/example/layers/ui_layer/ui_layer.hpp +++ b/example/layers/ui_layer/ui_layer.hpp @@ -4,7 +4,7 @@ namespace example { - class ui_layer : public VE::layer, public VE::flags + class ui_layer : public VE::layer, public VE::flags, public VE::connector { VE_OVERIDE(); diff --git a/example/layers/ui_layer_double/ui_layer_double.cpp b/example/layers/ui_layer_double/ui_layer_double.cpp index 653bf4f..ab5fe10 100755 --- a/example/layers/ui_layer_double/ui_layer_double.cpp +++ b/example/layers/ui_layer_double/ui_layer_double.cpp @@ -6,14 +6,14 @@ namespace example { void ui_layer_double::on_attach() { - VE::CONNECT(this); + CONNECT(this); hack::log()("on_attach"); } void ui_layer_double::on_detach() { - VE::DISCONNECT(this); + DISCONNECT(); hack::log()("on_detach"); } @@ -26,7 +26,7 @@ namespace example VE_PUSH_FONT(REGULAR, 20); if (ImGui::Button("Test signal_double", ImVec2(128, 130))) - VE::EMIT(test_event::TEST_EVEN, std::string("test event message ui_layer_double")); + EMIT(test_event::TEST_EVEN, std::string("test event message ui_layer_double")); if (ImGui::Button("Test on_detach", ImVec2(128, 130))) on_detach(); if (ImGui::Button("Test on_attach", ImVec2(128, 130))) on_attach(); diff --git a/example/layers/ui_layer_double/ui_layer_double.hpp b/example/layers/ui_layer_double/ui_layer_double.hpp index 499e354..14b70f7 100755 --- a/example/layers/ui_layer_double/ui_layer_double.hpp +++ b/example/layers/ui_layer_double/ui_layer_double.hpp @@ -4,7 +4,7 @@ namespace example { - class ui_layer_double : public VE::layer, public VE::flags + class ui_layer_double : public VE::layer, public VE::flags, public VE::connector { VE_OVERIDE(); diff --git a/src/VE.hpp b/src/VE.hpp index fd3db06..62450cd 100755 --- a/src/VE.hpp +++ b/src/VE.hpp @@ -1,7 +1,7 @@ #pragma once #include "event/event.hpp" // IWYU pragma: keep -#include "event/api.hpp" // IWYU pragma: keep +#include "event/connector.hpp" // IWYU pragma: keep #include "event/event_manager.hpp" // IWYU pragma: keep #include "event/type.hpp" // IWYU pragma: keep diff --git a/src/application/application.cpp b/src/application/application.cpp index 3361565..43f41dc 100755 --- a/src/application/application.cpp +++ b/src/application/application.cpp @@ -1,7 +1,5 @@ #include "application.hpp" -#include "utils/utils.hpp" - namespace VE { application::application(std::string app_name) : m_glfw{ std::make_unique() } diff --git a/src/event/api.hpp b/src/event/api.hpp deleted file mode 100755 index b01c3e7..0000000 --- a/src/event/api.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "event_manager.hpp" - -namespace VE -{ - template - void CONNECT(T* obj) - { - event_manager::instance().connect(obj, &T::on_event); - } - - template - void DISCONNECT(T* obj) - { - event_manager::instance().disconnect(obj, &T::on_event); - } - - template - void EMIT(TYPE t, DATA d, int id = -1, std::string msg = "") - { - event_manager::instance().emit(event{ t, d, id, msg}); - } - - inline void EMIT(event e) - { - event_manager::instance().emit(e); - } -} diff --git a/src/event/connector.hpp b/src/event/connector.hpp new file mode 100755 index 0000000..1a75388 --- /dev/null +++ b/src/event/connector.hpp @@ -0,0 +1,70 @@ +#pragma once + +#include "event_manager.hpp" + +namespace VE +{ + class connector + { + protected: + template + void CONNECT(T* obj) + { + if (!m_key.empty()) + { + hack::warn()("key not empty", m_key); + return; + } + m_key = event_manager::instance().connect(obj, &T::on_event); + } + + inline void DISCONNECT() + { + if (m_key.empty()) + { + hack::warn()("key is empty", m_key); + return; + } + event_manager::instance().disconnect(m_key); + m_key.clear(); + } + + public: + template + static void EMIT(TYPE t, DATA d, int id = -1) + { + event_manager::instance().emit(event{ t, d, id}); + } + + static void EMIT(event e) + { + event_manager::instance().emit(e); + } + + private: + std::string m_key; + }; + + // template + // void CONNECT(T* obj) + // { + // event_manager::instance().connect(obj, &T::on_event); + // } + // + // template + // void DISCONNECT(T* obj) + // { + // event_manager::instance().disconnect(obj, &T::on_event); + // } + + // template + // void EMIT(TYPE t, DATA d, int id = -1) + // { + // event_manager::instance().emit(event{ t, d, id}); + // } + // + // inline void EMIT(event e) + // { + // event_manager::instance().emit(e); + // } +} diff --git a/src/event/event.hpp b/src/event/event.hpp index bfe6b8a..5a98afc 100755 --- a/src/event/event.hpp +++ b/src/event/event.hpp @@ -1,18 +1,16 @@ #pragma once #include -#include namespace VE { struct event { - event(std::any type, std::any data, int id, std::string msg) : m_type{ type }, m_data{ data }, m_id { id }, m_msg { msg } {} + event(std::any type, std::any data, int id) : m_type{ type }, m_data{ data }, m_id { id } {} ~event() = default; std::any m_type; std::any m_data; int m_id; - std::string m_msg; }; } diff --git a/src/event/event_manager.hpp b/src/event/event_manager.hpp index 64b9cff..76827e1 100755 --- a/src/event/event_manager.hpp +++ b/src/event/event_manager.hpp @@ -1,80 +1,132 @@ #pragma once -#include #include #include #include -#include #include "event.hpp" +#include "utils/using.hpp" namespace VE { struct event_manager : public hack::patterns::singleton { + template + std::string connect(T* obj, void (T::*method)(event)) + { + std::string key = hack::security::generate_uuid(); + m_funcs[key] = std::bind(method, obj, std::placeholders::_1); + return key; + } + + void disconnect(std::string key) + { + auto it = m_funcs.find(key); + if (it != m_funcs.end()) + m_funcs.erase(it); + hack::log()(m_funcs.size()); + } + void emit(event e) { - if (m_funcs.size() == 0) - { - hack::warn()("funs is empty"); - return; - } - else if (!e.m_msg.empty()) - { - hack::warn()("EMIT MSG:", e.m_msg, m_funcs.size()); - } - - std::size_t i = 0; try { - for(;i < m_funcs.size();++i) m_funcs[i].m_func(e); + for(auto [key, f] : m_funcs) m_funcs[key](e); } - catch(std::exception& ext) + catch(const std::exception& ex) { - hack::error()("call function is error:", ext.what()); - hack::exception ex; - ex.system_error(ext); - ex.set_data(e); - throw ex; + hack::error()(ex.what()); + hack::log()("size funcs", m_funcs.size()); } catch(...) { - hack::error()("call function is error:", "oopps..."); - hack::exception ex; - ex.set_data(e); - throw ex; + hack::error()("ooops..."); } } - struct EventHandler - { - std::function m_func; - void* m_obj; - }; - - std::vector m_funcs; - - template - void connect(T* obj, void (T::*method)(event)) - { - m_funcs.push_back({ - std::bind(method, obj, std::placeholders::_1), - static_cast(obj) - }); - hack::warn()("connect (size)", m_funcs.size()); - } - - template - void disconnect(T* obj, void (T::*method)(event)) - { - m_funcs.erase( - std::remove_if(m_funcs.begin(), m_funcs.end(), - [obj](const auto& handler) { - return handler.m_obj == obj; - }), - m_funcs.end() - ); - hack::warn()("disconnect (size)", m_funcs.size()); - } + std::map> m_funcs; }; } + + +// #pragma once +// +// #include +// #include +// #include +// #include +// #include +// +// #include "event.hpp" +// +// namespace VE +// { +// struct event_manager : public hack::patterns::singleton +// { +// void emit(event e) +// { +// if (m_funcs.size() == 0) +// { +// hack::warn()("funs is empty"); +// return; +// } +// else if (!e.m_msg.empty()) +// { +// hack::warn()("EMIT MSG:", e.m_msg, m_funcs.size()); +// } +// +// std::size_t i = 0; +// try +// { +// for(;i < m_funcs.size();++i) m_funcs[i].m_func(e); +// } +// catch(std::exception& ext) +// { +// hack::error()("call function is error:", ext.what()); +// hack::exception ex; +// ex.system_error(ext); +// ex.set_data(e); +// throw ex; +// } +// catch(...) +// { +// hack::error()("call function is error:", "oopps..."); +// hack::exception ex; +// ex.set_data(e); +// throw ex; +// } +// } +// +// template +// void connect(T* obj, void (T::*method)(event)) +// { +// m_funcs.push_back({ +// std::bind(method, obj, std::placeholders::_1), +// static_cast(obj) +// }); +// hack::warn()("connect (size)", m_funcs.size()); +// } +// +// template +// void disconnect(T* obj, void (T::*method)(event)) +// { +// m_funcs.erase( +// std::remove_if(m_funcs.begin(), m_funcs.end(), +// [obj](const auto& handler) { +// return handler.m_obj == obj; +// }), +// m_funcs.end() +// ); +// hack::warn()("disconnect (size)", m_funcs.size()); +// } +// +// struct EventHandler +// { +// std::function m_func; +// void* m_obj; +// }; +// +// std::vector m_funcs; +// +// }; +// } diff --git a/src/glfw/glfw.cpp b/src/glfw/glfw.cpp index ff1e0e6..8972018 100755 --- a/src/glfw/glfw.cpp +++ b/src/glfw/glfw.cpp @@ -1,7 +1,7 @@ #include "glfw.hpp" #include "event/type.hpp" -#include "event/api.hpp" +#include "event/connector.hpp" namespace VE { @@ -124,17 +124,17 @@ namespace VE { case GLFW_PRESS: { - VE::EMIT(event_type::KEY_PRESSED, key); + connector::EMIT(event_type::KEY_PRESSED, key); break; } case GLFW_RELEASE: { - VE::EMIT(event_type::KEY_RELEASED, key); + connector::EMIT(event_type::KEY_RELEASED, key); break; } case GLFW_REPEAT: { - VE::EMIT(event_type::KEY_REPEATE, key); + connector::EMIT(event_type::KEY_REPEATE, key); break; } } @@ -155,16 +155,16 @@ namespace VE std::chrono::duration duration = local - time; if (duration.count() > 10 && duration.count() < 200) - VE::EMIT(event_type::MOUSE_BUTTON_DOUBLE_PRESSED, button); + connector::EMIT(event_type::MOUSE_BUTTON_DOUBLE_PRESSED, button); else - VE::EMIT(event_type::MOUSE_BUTTON_PRESSED, button); + connector::EMIT(event_type::MOUSE_BUTTON_PRESSED, button); time = local; break; } case GLFW_RELEASE: { - VE::EMIT(event_type::MOUSE_BUTTON_RELEASED, button); + connector::EMIT(event_type::MOUSE_BUTTON_RELEASED, button); break; } } @@ -175,7 +175,7 @@ namespace VE try { auto data = std::pair{ static_cast(xOffset), static_cast(yOffset) }; - VE::EMIT(event_type::MOUSE_SCROLL, data); + connector::EMIT(event_type::MOUSE_SCROLL, data); } catch(std::exception& e) { @@ -188,7 +188,7 @@ namespace VE try { auto data = std::pair{ static_cast(xPos), static_cast(yPos) }; - VE::EMIT(event_type::MOUSE_CURSOR_POSITION, data); + connector::EMIT(event_type::MOUSE_CURSOR_POSITION, data); } catch(std::exception& e) { @@ -210,7 +210,7 @@ namespace VE try { auto data = std::pair{ static_cast(width), static_cast(height) }; - VE::EMIT(event_type::WINDOW_RESIZE, data); + connector::EMIT(event_type::WINDOW_RESIZE, data); } catch(std::exception& e) { @@ -222,7 +222,7 @@ namespace VE { try { - VE::EMIT(event_type::WINDOW_CLOSE, nullptr); + connector::EMIT(event_type::WINDOW_CLOSE, nullptr); } catch(std::exception& e) { @@ -234,7 +234,7 @@ namespace VE { try { - VE::EMIT(event_type::WINDOW_FOCUS, focused); + connector::EMIT(event_type::WINDOW_FOCUS, focused); } catch(std::exception& e) { diff --git a/src/layer/layer.hpp b/src/layer/layer.hpp index 91a49b9..07de696 100755 --- a/src/layer/layer.hpp +++ b/src/layer/layer.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include "event/event.hpp" #include "utils/using.hpp" @@ -23,6 +24,7 @@ namespace VE protected: event_fn execute; + std::string m_key; }; }