Browse Source

"Object::SetBlockEvents(bool)" and "bool Object::GetBlockEvents()" APIs - allow blocking certain objects from sending and receiving events

Rokas Kupstys 8 years ago
parent
commit
c4c8e7cf6b
2 changed files with 14 additions and 1 deletions
  1. 6 1
      Source/Urho3D/Core/Object.cpp
  2. 8 0
      Source/Urho3D/Core/Object.h

+ 6 - 1
Source/Urho3D/Core/Object.cpp

@@ -72,7 +72,8 @@ bool TypeInfo::IsTypeOf(const TypeInfo* typeInfo) const
 }
 }
 
 
 Object::Object(Context* context) :
 Object::Object(Context* context) :
-    context_(context)
+    context_(context),
+    blockEvents_(false)
 {
 {
     assert(context_);
     assert(context_);
 }
 }
@@ -85,6 +86,8 @@ Object::~Object()
 
 
 void Object::OnEvent(Object* sender, StringHash eventType, VariantMap& eventData)
 void Object::OnEvent(Object* sender, StringHash eventType, VariantMap& eventData)
 {
 {
+    if (blockEvents_)
+        return;
     // Make a copy of the context pointer in case the object is destroyed during event handler invocation
     // Make a copy of the context pointer in case the object is destroyed during event handler invocation
     Context* context = context_;
     Context* context = context_;
     EventHandler* specific = nullptr;
     EventHandler* specific = nullptr;
@@ -299,6 +302,8 @@ void Object::SendEvent(StringHash eventType, VariantMap& eventData)
         return;
         return;
     }
     }
 
 
+    if (blockEvents_)
+        return;
     // Make a weak pointer to self to check for destruction during event handling
     // Make a weak pointer to self to check for destruction during event handling
     WeakPtr<Object> self(this);
     WeakPtr<Object> self(this);
     Context* context = context_;
     Context* context = context_;

+ 8 - 0
Source/Urho3D/Core/Object.h

@@ -161,6 +161,11 @@ public:
     /// Return object category. Categories are (optionally) registered along with the object factory. Return an empty string if the object category is not registered.
     /// Return object category. Categories are (optionally) registered along with the object factory. Return an empty string if the object category is not registered.
     const String& GetCategory() const;
     const String& GetCategory() const;
 
 
+    /// Block object from sending and receiving events.
+    void SetBlockEvents(bool block) { blockEvents_ = block; }
+    /// Return sending and receiving events blocking status.
+    bool GetBlockEvents() const { return blockEvents_; }
+
 protected:
 protected:
     /// Execution context.
     /// Execution context.
     Context* context_;
     Context* context_;
@@ -177,6 +182,9 @@ private:
 
 
     /// Event handlers. Sender is null for non-specific handlers.
     /// Event handlers. Sender is null for non-specific handlers.
     LinkedList<EventHandler> eventHandlers_;
     LinkedList<EventHandler> eventHandlers_;
+
+    /// Block object from sending and receiving any events.
+    bool blockEvents_;
 };
 };
 
 
 template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
 template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }