add new event

This commit is contained in:
2025-04-18 13:15:38 +03:00
parent a53056b6eb
commit 85500912a5
3 changed files with 24 additions and 17 deletions

View File

@@ -27,8 +27,22 @@ namespace example
VE_PUSH_FONT(REGULAR, 20); VE_PUSH_FONT(REGULAR, 20);
if (ImGui::Button("Test signal", ImVec2(128, 130))) if (ImGui::Button("Test signal", ImVec2(128, 130)))
VE::EMIT(test_event::TEST_EVEN, std::string("test event message")); VE::EMIT(test_event::TEST_EVEN, std::string("test event message"));
// 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<VE::event>(e.get_data());
// hack::log()(data.m_id);
// }
VE_POP_FONT(); VE_POP_FONT();
ImGui::End(); ImGui::End();
} }

View File

@@ -13,11 +13,4 @@ namespace VE
std::any m_data; std::any m_data;
int m_id; int m_id;
}; };
struct emitter
{
virtual ~emitter();
virtual void on_event(event e) = 0;
virtual void on_attach() = 0;
};
} }

View File

@@ -4,6 +4,7 @@
#include <hack/patterns/singleton.hpp> #include <hack/patterns/singleton.hpp>
#include <hack/logger/logger.hpp> #include <hack/logger/logger.hpp>
#include <hack/security/uuid.hpp> #include <hack/security/uuid.hpp>
#include <hack/exception/exception.hpp>
#include "event.hpp" #include "event.hpp"
@@ -13,25 +14,24 @@ namespace VE
{ {
void emit(event e) void emit(event e)
{ {
std::size_t i = 0;
try try
{ {
for(auto& f : m_funcs) f.func(e); for(;i < m_funcs.size();++i) m_funcs[i].m_func(e);
}
catch(const std::exception& ex)
{
hack::error()(ex.what());
hack::log()("size funcs", m_funcs.size());
} }
catch(...) catch(...)
{ {
hack::error()("ooops..."); hack::error()("call function is error...");
hack::exception ex;
ex.set_data(e);
throw ex;
} }
} }
struct EventHandler struct EventHandler
{ {
std::function<void(event)> func; std::function<void(event)> m_func;
void* obj; void* m_obj;
}; };
std::vector<EventHandler> m_funcs; std::vector<EventHandler> m_funcs;
@@ -51,7 +51,7 @@ namespace VE
m_funcs.erase( m_funcs.erase(
std::remove_if(m_funcs.begin(), m_funcs.end(), std::remove_if(m_funcs.begin(), m_funcs.end(),
[obj](const auto& handler) { [obj](const auto& handler) {
return handler.obj == obj; return handler.m_obj == obj;
}), }),
m_funcs.end() m_funcs.end()
); );