add new implementation event

This commit is contained in:
2025-04-18 14:49:33 +03:00
parent 1081fa70a7
commit d6470893ac
12 changed files with 200 additions and 122 deletions

View File

@@ -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<VE::event>(e.get_data());
// hack::log()(data.m_id);
// }
EMIT(test_event::TEST_EVEN, std::string("test event message"), 1);
VE_POP_FONT();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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

View File

@@ -1,7 +1,5 @@
#include "application.hpp"
#include "utils/utils.hpp"
namespace VE
{
application::application(std::string app_name) : m_glfw{ std::make_unique<glfw>() }

View File

@@ -1,29 +0,0 @@
#pragma once
#include "event_manager.hpp"
namespace VE
{
template <typename T>
void CONNECT(T* obj)
{
event_manager::instance().connect(obj, &T::on_event);
}
template <typename T>
void DISCONNECT(T* obj)
{
event_manager::instance().disconnect(obj, &T::on_event);
}
template <typename TYPE, typename DATA>
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);
}
}

70
src/event/connector.hpp Executable file
View File

@@ -0,0 +1,70 @@
#pragma once
#include "event_manager.hpp"
namespace VE
{
class connector
{
protected:
template <typename T>
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 <typename TYPE, typename DATA>
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 <typename T>
// void CONNECT(T* obj)
// {
// event_manager::instance().connect(obj, &T::on_event);
// }
//
// template <typename T>
// void DISCONNECT(T* obj)
// {
// event_manager::instance().disconnect(obj, &T::on_event);
// }
// template <typename TYPE, typename DATA>
// 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);
// }
}

View File

@@ -1,18 +1,16 @@
#pragma once
#include <any>
#include <string>
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;
};
}

View File

@@ -1,80 +1,132 @@
#pragma once
#include <functional>
#include <hack/patterns/singleton.hpp>
#include <hack/logger/logger.hpp>
#include <hack/security/uuid.hpp>
#include <hack/exception/exception.hpp>
#include "event.hpp"
#include "utils/using.hpp"
namespace VE
{
struct event_manager : public hack::patterns::singleton<event_manager>
{
template<typename T>
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<void(event)> m_func;
void* m_obj;
};
std::vector<EventHandler> m_funcs;
template<typename T>
void connect(T* obj, void (T::*method)(event))
{
m_funcs.push_back({
std::bind(method, obj, std::placeholders::_1),
static_cast<void*>(obj)
});
hack::warn()("connect (size)", m_funcs.size());
}
template<typename T>
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<std::string, event_fn<event>> m_funcs;
};
}
// #pragma once
//
// #include <functional>
// #include <hack/patterns/singleton.hpp>
// #include <hack/logger/logger.hpp>
// #include <hack/security/uuid.hpp>
// #include <hack/exception/exception.hpp>
//
// #include "event.hpp"
//
// namespace VE
// {
// struct event_manager : public hack::patterns::singleton<event_manager>
// {
// 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<typename T>
// void connect(T* obj, void (T::*method)(event))
// {
// m_funcs.push_back({
// std::bind(method, obj, std::placeholders::_1),
// static_cast<void*>(obj)
// });
// hack::warn()("connect (size)", m_funcs.size());
// }
//
// template<typename T>
// 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<void(event)> m_func;
// void* m_obj;
// };
//
// std::vector<EventHandler> m_funcs;
//
// };
// }

View File

@@ -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<double, std::milli> 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<float, float>{ static_cast<float>(xOffset), static_cast<float>(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<float, float>{ static_cast<float>(xPos), static_cast<float>(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<float, float>{ static_cast<float>(width), static_cast<float>(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)
{

View File

@@ -1,5 +1,6 @@
#pragma once
#include <string>
#include "event/event.hpp"
#include "utils/using.hpp"
@@ -23,6 +24,7 @@ namespace VE
protected:
event_fn<event> execute;
std::string m_key;
};
}