Prechádzať zdrojové kódy

Move ParticleEmitter2D events into Urho2DEvents header file. Add event parameters and safeguard for event handling causing component destruction. Add contributor credit.

Lasse Öörni 8 rokov pred
rodič
commit
dd0caa6ab7

+ 1 - 0
Docs/Urho3D.dox

@@ -165,6 +165,7 @@ Urho3D development, contributions and bugfixes by:
 - neat3d
 - nemerle
 - ninjastone
+- orefkov
 - proller
 - raould
 - rasteron

+ 1 - 0
README.md

@@ -119,6 +119,7 @@ Urho3D development, contributions and bugfixes by:
 - neat3d
 - nemerle
 - ninjastone
+- orefkov
 - proller
 - raould
 - rasteron

+ 25 - 8
Source/Urho3D/Urho2D/ParticleEmitter2D.cpp

@@ -32,6 +32,7 @@
 #include "../Urho2D/ParticleEmitter2D.h"
 #include "../Urho2D/Renderer2D.h"
 #include "../Urho2D/Sprite2D.h"
+#include "../Urho2D/Urho2DEvents.h"
 
 #include "../DebugNew.h"
 
@@ -262,11 +263,6 @@ void ParticleEmitter2D::UpdateMaterial()
         sourceBatches_[0].material_ = 0;
 }
 
-URHO3D_EVENT(E_PARTICLESEND, ParticlesEnd) {
-}
-URHO3D_EVENT(E_PARTICLESDURATION, ParticlesDuration) {
-}
-
 void ParticleEmitter2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
 {
     using namespace ScenePostUpdate;
@@ -274,10 +270,31 @@ void ParticleEmitter2D::HandleScenePostUpdate(StringHash eventType, VariantMap&
     bool emitting = emissionTime_ > 0.0f;
     float timeStep = eventData[P_TIMESTEP].GetFloat();
     Update(timeStep);
+
     if (emitting && emissionTime_ == 0.0f)
-        SendEvent(E_PARTICLESDURATION); // emitting particles stoped
-    if (hasParticles && 0 == numParticles_)
-        SendEvent(E_PARTICLESEND);      // all particles over
+    {
+        // Make a weak pointer to self to check for destruction during event handling
+        WeakPtr<ParticleEmitter2D> self(this);
+        using namespace ParticlesDuration;
+
+        VariantMap& eventData = GetEventDataMap();
+        eventData[P_NODE] = node_;
+        eventData[P_EFFECT] = effect_;
+        SendEvent(E_PARTICLESDURATION, eventData); // Emitting particles stopped
+
+        if (self.Expired())
+            return;
+    }
+    if (hasParticles && numParticles_ == 0)
+    {
+        using namespace ParticlesEnd;
+
+        VariantMap& eventData = GetEventDataMap();
+        eventData[P_NODE] = node_;
+        eventData[P_EFFECT] = effect_;
+
+        SendEvent(E_PARTICLESEND, eventData);      // All particles over
+    }
 }
 
 void ParticleEmitter2D::Update(float timeStep)

+ 44 - 0
Source/Urho3D/Urho2D/Urho2DEvents.h

@@ -0,0 +1,44 @@
+//
+// Copyright (c) 2008-2017 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "../Core/Object.h"
+
+namespace Urho3D
+{
+
+/// Emitting ParticleEmitter2D particles stopped.
+URHO3D_EVENT(E_PARTICLESEND, ParticlesEnd)
+{
+    URHO3D_PARAM(P_NODE, Node);                    // Node pointer
+    URHO3D_PARAM(P_EFFECT, Effect);                // ParticleEffect2D pointer
+}
+
+/// All ParticleEmitter2D particles have been removed.
+URHO3D_EVENT(E_PARTICLESDURATION, ParticlesDuration)
+{
+    URHO3D_PARAM(P_NODE, Node);                    // Node pointer
+    URHO3D_PARAM(P_EFFECT, Effect);                // ParticleEffect2D pointer
+}
+
+}