add returned event field

This commit is contained in:
2025-06-09 13:54:56 +03:00
parent 1d0bb54fae
commit 677ead1195
10 changed files with 63 additions and 39 deletions

View File

@@ -47,14 +47,25 @@ namespace example
if (!ImGui::Begin(VE_NAME("Test signal"), nullptr, m_win_flags)) ImGui::End(); if (!ImGui::Begin(VE_NAME("Test signal"), nullptr, m_win_flags)) ImGui::End();
if (ImGui::Button("Test signal", ImVec2(128, 130))) if (ImGui::Button("Test signal", ImVec2(128, 130)))
EMIT(test_event::TEST_EVEN, std::string("test event message"), "asdf-asdf-asdf", "test event log msg"); {
VE::event e { test_event::TEST_EVEN_1, std::string("test event 1 message"), "asdf-asdf-asdf" };
EMIT(e);
auto r = std::any_cast<std::string>(e.m_result);
hack::log()(r);
e = { test_event::TEST_EVEN_2, std::string("test event 2 message") };
EMIT(e);
r = std::any_cast<std::string>(e.m_result);
hack::log()(r);
}
VE_POP_FONT(); VE_POP_FONT();
ImGui::End(); ImGui::End();
} }
void ui_layer::on_event(VE::event e) void ui_layer::on_event(VE::event& e)
{ {
// для событий от перефирии // для событий от перефирии
// if (e.m_type.type() == typeid(VE::event_type)) // if (e.m_type.type() == typeid(VE::event_type))
@@ -67,8 +78,11 @@ namespace example
if (e.m_type.type() == typeid(test_event)) if (e.m_type.type() == typeid(test_event))
{ {
auto t = std::any_cast<test_event>(e.m_type); auto t = std::any_cast<test_event>(e.m_type);
if (t == test_event::TEST_EVEN) if (t == test_event::TEST_EVEN_1)
{
hack::log()(std::any_cast<std::string>(e.m_data)); hack::log()(std::any_cast<std::string>(e.m_data));
e.m_result = std::string("test_event_1");
}
} }
} }

View File

@@ -26,7 +26,10 @@ namespace example
VE_PUSH_FONT(REGULAR, 20); VE_PUSH_FONT(REGULAR, 20);
if (ImGui::Button("Test signal_double", ImVec2(128, 130))) if (ImGui::Button("Test signal_double", ImVec2(128, 130)))
EMIT(test_event::TEST_EVEN, std::string("test event message ui_layer_double")); {
VE::event e { test_event::TEST_EVEN_2, std::string("test event message ui_layer_double") };
EMIT(e);
}
if (ImGui::Button("Test on_detach", ImVec2(128, 130))) on_detach(); if (ImGui::Button("Test on_detach", ImVec2(128, 130))) on_detach();
if (ImGui::Button("Test on_attach", ImVec2(128, 130))) on_attach(); if (ImGui::Button("Test on_attach", ImVec2(128, 130))) on_attach();
@@ -39,7 +42,7 @@ namespace example
ImGui::End(); ImGui::End();
} }
void ui_layer_double::on_event(VE::event e) void ui_layer_double::on_event(VE::event& e)
{ {
// для событий от перефирии // для событий от перефирии
// if (e.m_type.type() == typeid(VE::event_type)) // if (e.m_type.type() == typeid(VE::event_type))
@@ -52,8 +55,11 @@ namespace example
if (e.m_type.type() == typeid(test_event)) if (e.m_type.type() == typeid(test_event))
{ {
auto t = std::any_cast<test_event>(e.m_type); auto t = std::any_cast<test_event>(e.m_type);
if (t == test_event::TEST_EVEN) if (t == test_event::TEST_EVEN_2)
{
hack::log()(std::any_cast<std::string>(e.m_data)); hack::log()(std::any_cast<std::string>(e.m_data));
e.m_result = std::string("test_event_2");
}
} }
} }

View File

@@ -4,6 +4,7 @@ namespace example
{ {
enum class test_event enum class test_event
{ {
TEST_EVEN TEST_EVEN_1,
TEST_EVEN_2
}; };
} }

View File

@@ -30,24 +30,11 @@ namespace VE
} }
public: public:
template <typename TYPE, typename DATA> static void EMIT(event& e)
static void EMIT(TYPE t, DATA d, std::string id = hack::security::generate_uuid(), std::string msg = "")
{
event e{ t, d, id };
if (msg != "") e.m_event_log_message = msg;
event_manager::instance().emit(e);
}
static void EMIT(event e)
{ {
event_manager::instance().emit(e); event_manager::instance().emit(e);
} }
void log()
{
hack::log()("key = ", m_key);
}
private: private:
std::string m_key; std::string m_key;
}; };

View File

@@ -8,12 +8,13 @@ namespace VE
{ {
struct event struct event
{ {
event(std::any type, std::any data, std::string id) : m_type{ type }, m_data{ data }, m_id { id } {} event(std::any type, std::any data, std::string id = hack::security::generate_uuid()) : m_type{ type }, m_data{ data }, m_id { id } {}
~event() = default; ~event() = default;
std::any m_type; std::any m_type;
std::any m_data; std::any m_data;
std::string m_id = hack::security::generate_uuid(); std::any m_result;
std::string m_id;
std::string m_event_log_message; std::string m_event_log_message;
std::string m_event_id = hack::security::generate_uuid(); std::string m_event_id = hack::security::generate_uuid();
}; };

View File

@@ -11,7 +11,7 @@ namespace VE
struct event_manager : public hack::patterns::singleton<event_manager> struct event_manager : public hack::patterns::singleton<event_manager>
{ {
template<typename T> template<typename T>
std::string connect(T* obj, void (T::*method)(event)) std::string connect(T* obj, void (T::*method)(event&))
{ {
std::string key = hack::security::generate_uuid(); std::string key = hack::security::generate_uuid();
m_funcs[key] = std::bind(method, obj, std::placeholders::_1); m_funcs[key] = std::bind(method, obj, std::placeholders::_1);
@@ -25,7 +25,7 @@ namespace VE
m_funcs.erase(it); m_funcs.erase(it);
} }
void emit(event e) void emit(event& e)
{ {
if (m_funcs.size() == 0) if (m_funcs.size() == 0)
{ {

View File

@@ -124,17 +124,20 @@ namespace VE
{ {
case GLFW_PRESS: case GLFW_PRESS:
{ {
connector::EMIT(event_type::KEY_PRESSED, key); event e { event_type::KEY_PRESSED, key };
connector::EMIT(e);
break; break;
} }
case GLFW_RELEASE: case GLFW_RELEASE:
{ {
connector::EMIT(event_type::KEY_RELEASED, key); event e { event_type::KEY_RELEASED, key };
connector::EMIT(e);
break; break;
} }
case GLFW_REPEAT: case GLFW_REPEAT:
{ {
connector::EMIT(event_type::KEY_REPEATE, key); event e { event_type::KEY_REPEATE, key };
connector::EMIT(e);
break; break;
} }
} }
@@ -155,16 +158,23 @@ namespace VE
std::chrono::duration<double, std::milli> duration = local - time; std::chrono::duration<double, std::milli> duration = local - time;
if (duration.count() > 10 && duration.count() < 200) if (duration.count() > 10 && duration.count() < 200)
connector::EMIT(event_type::MOUSE_BUTTON_DOUBLE_PRESSED, button); {
event e { event_type::MOUSE_BUTTON_DOUBLE_PRESSED, button };
connector::EMIT(e);
}
else else
connector::EMIT(event_type::MOUSE_BUTTON_PRESSED, button); {
event e { event_type::MOUSE_BUTTON_PRESSED, button };
connector::EMIT(e);
}
time = local; time = local;
break; break;
} }
case GLFW_RELEASE: case GLFW_RELEASE:
{ {
connector::EMIT(event_type::MOUSE_BUTTON_RELEASED, button); event e { event_type::MOUSE_BUTTON_RELEASED, button };
connector::EMIT(e);
break; break;
} }
} }
@@ -175,7 +185,8 @@ namespace VE
try try
{ {
auto data = std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) }; auto data = std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) };
connector::EMIT(event_type::MOUSE_SCROLL, data); event e { event_type::MOUSE_SCROLL, data };
connector::EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {
@@ -188,7 +199,8 @@ namespace VE
try try
{ {
auto data = std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) }; auto data = std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) };
connector::EMIT(event_type::MOUSE_CURSOR_POSITION, data); event e { event_type::MOUSE_CURSOR_POSITION, data };
connector::EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {
@@ -210,7 +222,8 @@ namespace VE
try try
{ {
auto data = std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) }; auto data = std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) };
connector::EMIT(event_type::WINDOW_RESIZE, data); event e { event_type::WINDOW_RESIZE, data };
connector::EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {
@@ -222,7 +235,8 @@ namespace VE
{ {
try try
{ {
connector::EMIT(event_type::WINDOW_CLOSE, nullptr); event e { event_type::WINDOW_CLOSE, nullptr };
connector::EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {
@@ -234,7 +248,8 @@ namespace VE
{ {
try try
{ {
connector::EMIT(event_type::WINDOW_FOCUS, focused); event e { event_type::WINDOW_FOCUS, focused };
connector::EMIT(e);
} }
catch(std::exception& e) catch(std::exception& e)
{ {

View File

@@ -19,7 +19,7 @@ namespace VE
virtual void on_detach() {}; virtual void on_detach() {};
virtual void render() {}; virtual void render() {};
virtual void update() {}; virtual void update() {};
virtual void on_event(event e) {}; virtual void on_event(event& e) {};
protected: protected:
event_fn<event> execute; event_fn<event> execute;

View File

@@ -9,7 +9,7 @@
void on_detach() override;\ void on_detach() override;\
void render() override;\ void render() override;\
void update() override;\ void update() override;\
void on_event(VE::event e) override void on_event(VE::event& e) override
#define VE_PUSH_FONT(def_font, def_size)\ #define VE_PUSH_FONT(def_font, def_size)\
ImGui::PushFont(VE::style::fonts::get_font(VE::style::fonts::font_type::def_font, def_size)) ImGui::PushFont(VE::style::fonts::get_font(VE::style::fonts::font_type::def_font, def_size))

View File

@@ -5,7 +5,7 @@
namespace VE namespace VE
{ {
template<typename Event> template<typename Event>
using event_fn = std::function<void(Event)>; using event_fn = std::function<void(Event&)>;
template<typename Layer> template<typename Layer>
using layers_stack = std::vector<Layer*>; using layers_stack = std::vector<Layer*>;