From a2d430d35fc7fc168753975e68adc50c31b67807 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 1 Oct 2014 23:27:31 +0300 Subject: [PATCH] server/state, builtin/entitysync: Tracking of node and component changes works --- builtin/entitysync/entitysync.cpp | 39 +++++++++++++++++++++++++++---- src/interface/magic_event.h | 8 +++---- src/server/state.cpp | 28 +++++++++++++++++++++- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/builtin/entitysync/entitysync.cpp b/builtin/entitysync/entitysync.cpp index 908a302..a807bd6 100644 --- a/builtin/entitysync/entitysync.cpp +++ b/builtin/entitysync/entitysync.cpp @@ -50,6 +50,12 @@ struct Module: public interface::Module, public entitysync::Interface m_server->sub_magic_event(this, magic::E_NODEADDED, Event::t("entitysync:node_added")); + m_server->sub_magic_event(this, magic::E_NODEREMOVED, + Event::t("entitysync:node_removed")); + m_server->sub_magic_event(this, magic::E_COMPONENTADDED, + Event::t("entitysync:component_added")); + m_server->sub_magic_event(this, magic::E_COMPONENTREMOVED, + Event::t("entitysync:component_removed")); m_server->access_scene([&](magic::Scene *scene) { @@ -71,7 +77,14 @@ struct Module: public interface::Module, public entitysync::Interface EVENT_VOIDN("core:start", on_start) EVENT_VOIDN("core:unload", on_unload) EVENT_VOIDN("core:continue", on_continue) - EVENT_TYPEN("entitysync:node_added", on_node_added, interface::MagicEvent) + EVENT_TYPEN("entitysync:node_added", + on_node_added, interface::MagicEvent) + EVENT_TYPEN("entitysync:node_removed", + on_node_removed, interface::MagicEvent) + EVENT_TYPEN("entitysync:component_added", + on_component_added, interface::MagicEvent) + EVENT_TYPEN("entitysync:component_removed", + on_component_removed, interface::MagicEvent) } void on_start() @@ -88,10 +101,26 @@ struct Module: public interface::Module, public entitysync::Interface void on_node_added(const interface::MagicEvent &event) { - log_w(MODULE, "Node added"); - /*m_server->access_scene([&](magic::Scene *scene) - { - });*/ + magic::VariantMap event_data = event.magic_data; + log_w(MODULE, "Node added: %i", event_data["NodeID"].GetInt()); + } + + void on_node_removed(const interface::MagicEvent &event) + { + magic::VariantMap event_data = event.magic_data; + log_w(MODULE, "Node removed: %i", event_data["NodeID"].GetInt()); + } + + void on_component_added(const interface::MagicEvent &event) + { + magic::VariantMap event_data = event.magic_data; + log_w(MODULE, "Component added: %i", event_data["ComponentID"].GetInt()); + } + + void on_component_removed(const interface::MagicEvent &event) + { + magic::VariantMap event_data = event.magic_data; + log_w(MODULE, "Component removed: %i", event_data["ComponentID"].GetInt()); } // Interface diff --git a/src/interface/magic_event.h b/src/interface/magic_event.h index 908fbb4..92e103d 100644 --- a/src/interface/magic_event.h +++ b/src/interface/magic_event.h @@ -16,10 +16,10 @@ namespace interface namespace magic = Urho3D; struct MagicEvent: public interface::Event::Private { - magic::StringHash event_type; - magic::VariantMap event_data; - MagicEvent(magic::StringHash event_type, const magic::VariantMap &event_data): - event_type(event_type), event_data(event_data){} + magic::StringHash magic_type; + magic::VariantMap magic_data; + MagicEvent(magic::StringHash magic_type, const magic::VariantMap &magic_data): + magic_type(magic_type), magic_data(magic_data){} }; } // vim: set noet ts=4 sw=4: diff --git a/src/server/state.cpp b/src/server/state.cpp index 85d6e6f..fecde13 100644 --- a/src/server/state.cpp +++ b/src/server/state.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include #pragma GCC diagnostic pop #include #include @@ -98,6 +100,30 @@ struct MagicEventHandler: public magic::Object log_d(MODULE, "MagicEventHandler::on_event(): %s (%zu)", cs(evreg->name(m_buildat_event_type)), m_buildat_event_type); } + // Convert pointers to IDs because the event will be queued for later + // handling and pointers may become invalid + if(event_type == magic::E_NODEADDED || + event_type == magic::E_NODEREMOVED){ + magic::Node *parent = static_cast( + event_data["Parent"].Get()); + event_data["ParentID"] = parent->GetID(); + event_data.Erase("Parent"); + magic::Node *node = static_cast( + event_data["Node"].Get()); + event_data["NodeID"] = node->GetID(); + event_data.Erase("Node"); + } + if(event_type == magic::E_COMPONENTADDED || + event_type == magic::E_COMPONENTREMOVED){ + magic::Node *node = static_cast( + event_data["Node"].Get()); + event_data["NodeID"] = node->GetID(); + event_data.Erase("Node"); + magic::Component *c = static_cast( + event_data["Component"].Get()); + event_data["ComponentID"] = c->GetID(); + event_data.Erase("Component"); + } m_server->emit_event(m_buildat_event_type, new interface::MagicEvent( event_type, event_data)); } @@ -571,7 +597,7 @@ struct CState: public State, public interface::Server { { interface::MutexScope ms(m_magic_mutex); - m_magic_event_handlers[event_type] = new MagicEventHandler( + m_magic_event_handlers[buildat_event_type] = new MagicEventHandler( m_magic_context, this, event_type, buildat_event_type); } sub_event(module, buildat_event_type);