Explorar o código

Added Node function to remove all components of type.

Lasse Öörni %!s(int64=10) %!d(string=hai) anos
pai
achega
c5a75e523a

+ 2 - 2
Source/Urho3D/LuaScript/pkgs/Scene/Node.pkg

@@ -94,9 +94,9 @@ class Node : public Animatable
     void RemoveComponent(Component* component);
     void RemoveComponent(StringHash type);
     void RemoveComponent(const String type);
-
-    void RemoveAllComponents();
     void RemoveComponents(bool removeReplicated, bool removeLocal);
+    void RemoveComponents(const String type);
+    void RemoveAllComponents();
 
     Node* Clone(CreateMode mode = REPLICATED);
 

+ 23 - 5
Source/Urho3D/Scene/Node.cpp

@@ -792,11 +792,6 @@ void Node::RemoveComponent(StringHash type)
     }
 }
 
-void Node::RemoveAllComponents()
-{
-    RemoveComponents(true, true);
-}
-
 void Node::RemoveComponents(bool removeReplicated, bool removeLocal)
 {
     unsigned numRemoved = 0;
@@ -823,6 +818,29 @@ void Node::RemoveComponents(bool removeReplicated, bool removeLocal)
         MarkReplicationDirty();
 }
 
+void Node::RemoveComponents(StringHash type)
+{
+    unsigned numRemoved = 0;
+
+    for (unsigned i = components_.Size() - 1; i < components_.Size(); --i)
+    {
+        if (components_[i]->GetType() == type)
+        {
+            RemoveComponent(components_.Begin() + i);
+            ++numRemoved;
+        }
+    }
+
+    // Mark node dirty in all replication states
+    if (numRemoved)
+        MarkReplicationDirty();
+}
+
+void Node::RemoveAllComponents()
+{
+    RemoveComponents(true, true);
+}
+
 Node* Node::Clone(CreateMode mode)
 {
     // The scene itself can not be cloned

+ 8 - 2
Source/Urho3D/Scene/Node.h

@@ -264,10 +264,12 @@ public:
     void RemoveComponent(Component* component);
     /// Remove the first component of specific type from this node.
     void RemoveComponent(StringHash type);
-    /// Remove all components from this node.
-    void RemoveAllComponents();
     /// Remove components that match criteria.
     void RemoveComponents(bool removeReplicated, bool removeLocal);
+    /// Remove all components of specific type.
+    void RemoveComponents(StringHash type);
+    /// Remove all components from this node.
+    void RemoveAllComponents();
     /// Clone scene node, components and child nodes. Return the clone.
     Node* Clone(CreateMode mode = REPLICATED);
     /// Remove from the parent node. If no other shared pointer references exist, causes immediate deletion.
@@ -286,6 +288,8 @@ public:
     template <class T> T* GetOrCreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
     /// Template version of removing a component.
     template <class T> void RemoveComponent();
+    /// Template version of removing all components of specific type.
+    template <class T> void RemoveComponents();
 
     /// Return ID.
     unsigned GetID() const { return id_; }
@@ -640,6 +644,8 @@ template <class T> T* Node::GetOrCreateComponent(CreateMode mode, unsigned id)
 
 template <class T> void Node::RemoveComponent() { RemoveComponent(T::GetTypeStatic()); }
 
+template <class T> void Node::RemoveComponents() { RemoveComponents(T::GetTypeStatic()); }
+
 template <class T> void Node::GetChildrenWithComponent(PODVector<Node*>& dest, bool recursive) const
 {
     GetChildrenWithComponent(dest, T::GetTypeStatic(), recursive);

+ 7 - 1
Source/Urho3D/Script/APITemplates.h

@@ -506,6 +506,11 @@ static void NodeRemoveComponent(const String& typeName, Node* ptr)
     ptr->RemoveComponent(typeName);
 }
 
+static void NodeRemoveComponents(const String& typeName, Node* ptr)
+{
+    ptr->RemoveComponents(typeName);
+}
+
 static Component* NodeGetComponent(unsigned index, Node* ptr)
 {
     const Vector<SharedPtr<Component> >& components = ptr->GetComponents();
@@ -659,8 +664,9 @@ template <class T> void RegisterNode(asIScriptEngine* engine, const char* classN
     engine->RegisterObjectMethod(className, "Component@+ CloneComponent(Component@+, CreateMode, uint id = 0)", asMETHODPR(T, CloneComponent, (Component*, CreateMode, unsigned), Component*), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void RemoveComponent(Component@+)", asMETHODPR(T, RemoveComponent, (Component*), void), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void RemoveComponent(const String&in)", asFUNCTION(NodeRemoveComponent), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod(className, "void RemoveComponents(bool, bool)", asMETHODPR(T, RemoveComponents, (bool, bool), void), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void RemoveComponents(const String&in)", asFUNCTION(NodeRemoveComponents), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod(className, "void RemoveAllComponents()", asMETHOD(T, RemoveAllComponents), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "void RemoveComponents(bool, bool)", asMETHOD(T, RemoveComponents), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Array<Node@>@ GetChildren(bool recursive = false) const", asFUNCTION(NodeGetChildren), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod(className, "Array<Node@>@ GetChildrenWithComponent(const String&in, bool recursive = false) const", asFUNCTION(NodeGetChildrenWithComponent), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod(className, "Array<Node@>@ GetChildrenWithScript(bool recursive = false) const", asFUNCTION(NodeGetChildrenWithScript), asCALL_CDECL_OBJLAST);