add new strucn event
This commit is contained in:
@@ -6,14 +6,14 @@ namespace example
|
|||||||
{
|
{
|
||||||
void ui_layer::on_attach()
|
void ui_layer::on_attach()
|
||||||
{
|
{
|
||||||
VE::CONNECT(this);
|
m_key = VE::CONNECT(this);
|
||||||
|
|
||||||
hack::log()("on_attach");
|
hack::log()("on_attach");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_layer::on_detach()
|
void ui_layer::on_detach()
|
||||||
{
|
{
|
||||||
VE::DISCONNECT(this);
|
VE::DISCONNECT(m_key);
|
||||||
hack::log()("on_detach");
|
hack::log()("on_detach");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace example
|
|||||||
|
|
||||||
ImVec2 m_size = ImVec2{ 400.f, 400.f };
|
ImVec2 m_size = ImVec2{ 400.f, 400.f };
|
||||||
ImVec2 m_pos = ImVec2{ 400.f, 400.f };
|
ImVec2 m_pos = ImVec2{ 400.f, 400.f };
|
||||||
|
std::string m_key;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ namespace example
|
|||||||
{
|
{
|
||||||
void ui_layer_double::on_attach()
|
void ui_layer_double::on_attach()
|
||||||
{
|
{
|
||||||
VE::CONNECT(this);
|
m_key = VE::CONNECT(this);
|
||||||
|
|
||||||
hack::log()("on_attach");
|
hack::log()("on_attach");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_layer_double::on_detach()
|
void ui_layer_double::on_detach()
|
||||||
{
|
{
|
||||||
VE::DISCONNECT(this);
|
VE::DISCONNECT(m_key);
|
||||||
hack::log()("on_detach");
|
hack::log()("on_detach");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace example
|
|||||||
|
|
||||||
ImVec2 m_size = ImVec2{ 400.f, 400.f };
|
ImVec2 m_size = ImVec2{ 400.f, 400.f };
|
||||||
ImVec2 m_pos = ImVec2{ 400.f, 400.f };
|
ImVec2 m_pos = ImVec2{ 400.f, 400.f };
|
||||||
|
std::string m_key;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,15 +5,14 @@
|
|||||||
namespace VE
|
namespace VE
|
||||||
{
|
{
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void CONNECT(T* obj)
|
std::string CONNECT(T* obj)
|
||||||
{
|
{
|
||||||
event_manager::instance().connect(obj, &T::on_event);
|
return event_manager::instance().connect(obj, &T::on_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
inline void DISCONNECT(std::string key)
|
||||||
void DISCONNECT(T* obj)
|
|
||||||
{
|
{
|
||||||
event_manager::instance().disconnect(obj, &T::on_event);
|
event_manager::instance().disconnect(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TYPE, typename DATA>
|
template <typename TYPE, typename DATA>
|
||||||
|
|||||||
@@ -1,53 +1,36 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <algorithm>
|
|
||||||
#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 "event.hpp"
|
#include "event.hpp"
|
||||||
|
#include "utils/using.hpp"
|
||||||
|
|
||||||
namespace VE
|
namespace VE
|
||||||
{
|
{
|
||||||
struct event_manager : public hack::patterns::singleton<event_manager>
|
struct event_manager : public hack::patterns::singleton<event_manager>
|
||||||
{
|
{
|
||||||
struct event_handler
|
std::map<std::string, event_fn<event>> m_funcs;
|
||||||
{
|
|
||||||
std::function<void(event)> m_func;
|
|
||||||
void* m_obj;
|
|
||||||
void* m_func_ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<event_handler> m_funcs;
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void connect(T* obj, void (T::*method)(event))
|
std::string connect(T* obj, void (T::*method)(event))
|
||||||
{
|
{
|
||||||
event_handler handler;
|
std::string key = hack::security::generate_uuid();
|
||||||
handler.m_func = std::bind(method, obj, std::placeholders::_1);
|
m_funcs[key] = std::bind(method, obj, std::placeholders::_1);
|
||||||
handler.m_obj = obj;
|
return key;
|
||||||
m_funcs.push_back(handler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
void disconnect(std::string key)
|
||||||
void disconnect(T* obj, void (T::*method)(event))
|
|
||||||
{
|
{
|
||||||
m_funcs.erase(std::remove_if(m_funcs.begin(), m_funcs.end(),
|
m_funcs.erase(key);
|
||||||
[obj](const event_handler& handler) {
|
|
||||||
return handler.m_obj == obj;
|
|
||||||
}),
|
|
||||||
m_funcs.end()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit(event e)
|
void emit(event e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
for (std::size_t i = 0; i < m_funcs.size(); ++i)
|
for(auto [key, f] : m_funcs) m_funcs[key](e);
|
||||||
{
|
|
||||||
m_funcs[i].m_func(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch(const std::exception& ex)
|
catch(const std::exception& ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user