add event target

This commit is contained in:
chatlanin
2025-01-30 23:10:34 +03:00
parent a998115414
commit 5155fdbd49
11 changed files with 31 additions and 22 deletions

View File

@@ -22,7 +22,7 @@ namespace sandbox
if (ImGui::Button("RUN", ImVec2(28, 30))) if (ImGui::Button("RUN", ImVec2(28, 30)))
{ {
VE::event e { test_event::TEST_EVEN , std::string("test event message") }; VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") };
execute(e); execute(e);
} }

View File

@@ -24,7 +24,7 @@ namespace sandbox
if (!ImGui::Begin("#test_panel_2", &m_p_open, m_window_flags)) ImGui::End(); if (!ImGui::Begin("#test_panel_2", &m_p_open, m_window_flags)) ImGui::End();
if (ImGui::Button("RUN", ImVec2(28, 30))) if (ImGui::Button("RUN", ImVec2(28, 30)))
{ {
VE::event e { test_event::TEST_EVEN , std::string("test event message") }; VE::event e { "target_id", test_event::TEST_EVEN , std::string("test event message") };
execute(e); execute(e);
} }
@@ -32,7 +32,7 @@ namespace sandbox
if (ImGui::Button(VE::style::icon::ICON_STOP, ImVec2(28, 30))) if (ImGui::Button(VE::style::icon::ICON_STOP, ImVec2(28, 30)))
{ {
VE::event e { test_event::TEST_EVEN , std::string("test icon button") }; VE::event e { "target_id", test_event::TEST_EVEN , std::string("test icon button") };
execute(e); execute(e);
} }

View File

@@ -23,15 +23,15 @@ namespace VE
{ {
inline application& create() inline application& create()
{ {
static sandbox::test_app e{ "vertex_engine_sandbox" }; static sandbox::test_app app{ "vertex_engine_sandbox" };
e.push_layer( app.push_layer(
new sandbox::test_panel{}, new sandbox::test_panel{},
new sandbox::test_panel_2{}, new sandbox::test_panel_2{},
new sandbox::opengl_panel{} new sandbox::opengl_panel{}
); );
return e; return app;
} }
} }

View File

@@ -48,7 +48,7 @@ namespace VE
for (const auto l : m_layers_stack) l->set_event_fn(VE_EVENT_FN); for (const auto l : m_layers_stack) l->set_event_fn(VE_EVENT_FN);
} }
void application::on_event(event& e) void application::on_event(event e)
{ {
if (e.is_parallele) if (e.is_parallele)
for (auto l : m_layers_stack) auto future = std::async(std::launch::async, [&]() { l->on_event(e); }); for (auto l : m_layers_stack) auto future = std::async(std::launch::async, [&]() { l->on_event(e); });

View File

@@ -36,7 +36,7 @@ namespace VE
void clear(); void clear();
void attach_layers(); void attach_layers();
void set_event_fn(); void set_event_fn();
void on_event(event& e); void on_event(event e);
}; };
// реализация см. в проекте // реализация см. в проекте

View File

@@ -22,12 +22,21 @@ namespace VE
WINDOW_FOCUS, WINDOW_FOCUS,
}; };
enum class event_target
{
VE
};
struct event struct event
{ {
event(std::any t, std::any d) : m_type{ t }, m_data{ d } {} event(std::any target, std::any type, std::any data) : m_target { target }, m_type{ type }, m_data{ data } {}
~event() = default; ~event() = default;
std::any m_target;
std::any m_type; std::any m_type;
std::any m_data; std::any m_data;
// HERE
// как-то не прикольно так !!!
bool is_parallele{ false }; bool is_parallele{ false };
}; };
} }

View File

@@ -121,7 +121,7 @@ namespace VE
glfwSetKeyCallback(m_win, [](GLFWwindow* w, int key, int scancode, int action, int mods) glfwSetKeyCallback(m_win, [](GLFWwindow* w, int key, int scancode, int action, int mods)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w)); auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ action, key }; event e{ event_target::VE, action, key };
// HERE // HERE
// тут одноврекменно срабатывает только один сигнал // тут одноврекменно срабатывает только один сигнал
@@ -157,7 +157,7 @@ namespace VE
glfwSetMouseButtonCallback(m_win, [](GLFWwindow* w, int button, int action, int mods) glfwSetMouseButtonCallback(m_win, [](GLFWwindow* w, int button, int action, int mods)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w)); auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ action, button }; event e{ event_target::VE, action, button };
switch (action) switch (action)
{ {
@@ -188,14 +188,14 @@ namespace VE
glfwSetScrollCallback(m_win, [](GLFWwindow* w, double xOffset, double yOffset) glfwSetScrollCallback(m_win, [](GLFWwindow* w, double xOffset, double yOffset)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w)); auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ event_type::MOUSE_SCROLL, std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) } }; event e{ event_target::VE, event_type::MOUSE_SCROLL, std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) } };
d->execute(e); d->execute(e);
}); });
glfwSetCursorPosCallback(m_win, [](GLFWwindow* w, double xPos, double yPos) glfwSetCursorPosCallback(m_win, [](GLFWwindow* w, double xPos, double yPos)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w)); auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ event_type::MOUSE_CURSOR_POSITION, std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) } }; event e{ event_target::VE, event_type::MOUSE_CURSOR_POSITION, std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) } };
d->execute(e); d->execute(e);
}); });
} }
@@ -210,21 +210,21 @@ namespace VE
d->m_width = width; d->m_width = width;
d->m_height = height; d->m_height = height;
event e{ event_type::WINDOW_RESIZE, std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) } }; event e{ event_target::VE, event_type::WINDOW_RESIZE, std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) } };
d->execute(e); d->execute(e);
}); });
glfwSetWindowCloseCallback(m_win, [](GLFWwindow* w) glfwSetWindowCloseCallback(m_win, [](GLFWwindow* w)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w)); auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ event_type::WINDOW_CLOSE, nullptr }; event e{ event_target::VE, event_type::WINDOW_CLOSE, nullptr };
d->execute(e); d->execute(e);
}); });
glfwSetWindowFocusCallback(m_win, [](GLFWwindow* w, int focused) glfwSetWindowFocusCallback(m_win, [](GLFWwindow* w, int focused)
{ {
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w)); auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
event e{ event_type::WINDOW_FOCUS, focused }; event e{ event_target::VE, event_type::WINDOW_FOCUS, focused };
d->execute(e); d->execute(e);
}); });

View File

@@ -40,7 +40,7 @@ namespace VE
void update(); void update();
void clear() const; void clear() const;
void set_event_fn(const event_fn<event&>& fn); void set_event_fn(const event_fn<event>& fn);
private: private:
void set_hint(); void set_hint();

View File

@@ -43,8 +43,8 @@ namespace VE
void gui::end_frame() void gui::end_frame()
{ {
ImGuiIO& io = ImGui::GetIO(); // ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)m_glfw->width(), (float)m_glfw->height()); // io.DisplaySize = ImVec2((float)m_glfw->width(), (float)m_glfw->height());
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

View File

@@ -22,10 +22,10 @@ namespace VE
virtual void render() {}; virtual void render() {};
virtual void on_event(event e) {}; virtual void on_event(event e) {};
void set_event_fn(const event_fn<event&>& fn) { execute = fn; }; void set_event_fn(const event_fn<event>& fn) { execute = fn; };
protected: protected:
event_fn<event&> execute; event_fn<event> execute;
}; };
} }

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*>;