Browse Source

Remove deprecated autoRemove bool from SoundSource. Replace with a more generic AutoRemoveMode enum which is now used by both SoundSource & ParticleEmitter.

Lasse Öörni 9 years ago
parent
commit
3fe5024cf7

+ 2 - 2
Source/Urho3D/AngelScript/APITemplates.h

@@ -872,8 +872,8 @@ template <class T> void RegisterSoundSource(asIScriptEngine* engine, const char*
     engine->RegisterObjectMethod(className, "Sound@+ get_sound() const", asMETHOD(T, GetSound), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "float get_timePosition() const", asMETHOD(T, GetTimePosition), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "float get_attenuation() const", asMETHOD(T, GetAttenuation), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "void set_autoRemove(bool)", asMETHOD(T, SetAutoRemove), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "bool get_autoRemove() const", asMETHOD(T, GetAutoRemove), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_autoRemoveMode(AutoRemoveMode)", asMETHOD(T, SetAutoRemoveMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "AutoRemoveMode get_autoRemoveMode() const", asMETHOD(T, GetAutoRemoveMode), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "bool get_playing() const", asMETHOD(T, IsPlaying), asCALL_THISCALL);
 }
 

+ 2 - 0
Source/Urho3D/AngelScript/GraphicsAPI.cpp

@@ -1605,6 +1605,8 @@ static void RegisterParticleEmitter(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ParticleEmitter", "FaceCameraMode get_faceCameraMode() const", asMETHOD(ParticleEmitter, GetFaceCameraMode), asCALL_THISCALL);
     engine->RegisterObjectMethod("ParticleEmitter", "void set_animationLodBias(float)", asMETHOD(ParticleEmitter, SetAnimationLodBias), asCALL_THISCALL);
     engine->RegisterObjectMethod("ParticleEmitter", "float get_animationLodBias() const", asMETHOD(ParticleEmitter, GetAnimationLodBias), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ParticleEmitter", "void set_autoRemoveMode(AutoRemoveMode)", asMETHOD(ParticleEmitter, SetAutoRemoveMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ParticleEmitter", "AutoRemoveMode get_autoRemoveMode() const", asMETHOD(ParticleEmitter, GetAutoRemoveMode), asCALL_THISCALL);
     engine->RegisterObjectMethod("ParticleEmitter", "Billboard@+ get_billboards(uint)", asMETHOD(ParticleEmitter, GetBillboard), asCALL_THISCALL);
     engine->RegisterObjectMethod("ParticleEmitter", "Zone@+ get_zone() const", asMETHOD(ParticleEmitter, GetZone), asCALL_THISCALL);
 

+ 5 - 0
Source/Urho3D/AngelScript/SceneAPI.cpp

@@ -45,6 +45,11 @@ static void RegisterSerializable(asIScriptEngine* engine)
     engine->RegisterGlobalProperty("const uint AM_COMPONENTID", (void*)&AM_COMPONENTID);
     engine->RegisterGlobalProperty("const uint AM_NODEIDVECTOR", (void*)&AM_NODEIDVECTOR);
 
+    engine->RegisterEnum("AutoRemoveMode");
+    engine->RegisterEnumValue("AutoRemoveMode", "REMOVE_DISABLED", REMOVE_DISABLED);
+    engine->RegisterEnumValue("AutoRemoveMode", "REMOVE_COMPONENT", REMOVE_COMPONENT);
+    engine->RegisterEnumValue("AutoRemoveMode", "REMOVE_NODE", REMOVE_NODE);
+
     RegisterSerializable<Serializable>(engine, "Serializable");
 }
 

+ 7 - 26
Source/Urho3D/Audio/SoundSource.cpp

@@ -94,12 +94,11 @@ namespace Urho3D
 
 #define GET_IP_SAMPLE_RIGHT() (((((int)pos[3] - (int)pos[1]) * fractPos) / 65536) + (int)pos[1])
 
-static const float AUTOREMOVE_DELAY = 0.25f;
-
 static const int STREAM_SAFETY_SAMPLES = 4;
 
 extern const char* AUDIO_CATEGORY;
 
+extern const char* autoRemoveModeNames[];
 
 SoundSource::SoundSource(Context* context) :
     Component(context),
@@ -108,9 +107,8 @@ SoundSource::SoundSource(Context* context) :
     gain_(1.0f),
     attenuation_(1.0f),
     panning_(0.0f),
-    autoRemoveTimer_(0.0f),
-    autoRemove_(false),
     sendFinishedEvent_(false),
+    autoRemove_(REMOVE_DISABLED),
     position_(0),
     fractPosition_(0),
     timePosition_(0.0f),
@@ -142,7 +140,7 @@ void SoundSource::RegisterObject(Context* context)
     URHO3D_ATTRIBUTE("Attenuation", float, attenuation_, 1.0f, AM_DEFAULT);
     URHO3D_ATTRIBUTE("Panning", float, panning_, 0.0f, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Is Playing", IsPlaying, SetPlayingAttr, bool, false, AM_DEFAULT);
-    URHO3D_ATTRIBUTE("Autoremove on Stop", bool, autoRemove_, false, AM_FILE);
+    URHO3D_ENUM_ATTRIBUTE("Autoremove Mode", autoRemove_, autoRemoveModeNames, REMOVE_DISABLED, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Play Position", GetPositionAttr, SetPositionAttr, int, 0, AM_FILE);
 }
 
@@ -283,12 +281,10 @@ void SoundSource::SetPanning(float panning)
     MarkNetworkUpdate();
 }
 
-void SoundSource::SetAutoRemove(bool enable)
+void SoundSource::SetAutoRemoveMode(AutoRemoveMode mode)
 {
-    if (enable == true)
-        URHO3D_LOGWARNING("SoundSource::SetAutoRemove is deprecated. Consider using the SoundFinished event instead");
-
-    autoRemove_ = enable;
+    autoRemove_ = mode;
+    MarkNetworkUpdate();
 }
 
 bool SoundSource::IsPlaying() const
@@ -338,23 +334,8 @@ void SoundSource::Update(float timeStep)
 
         if (self.Expired())
             return;
-    }
 
-    // Check for autoremove
-    if (autoRemove_)
-    {
-        if (!playing)
-        {
-            autoRemoveTimer_ += timeStep;
-            if (autoRemoveTimer_ > AUTOREMOVE_DELAY)
-            {
-                Remove();
-                // Note: this object is now deleted, so only returning immediately is safe
-                return;
-            }
-        }
-        else
-            autoRemoveTimer_ = 0.0f;
+        DoAutoRemove(autoRemove_);
     }
 }
 

+ 6 - 8
Source/Urho3D/Audio/SoundSource.h

@@ -70,8 +70,8 @@ public:
     void SetAttenuation(float attenuation);
     /// Set stereo panning. -1.0 is full left and 1.0 is full right.
     void SetPanning(float panning);
-    /// \deprecated Set whether sound source will be automatically removed from the scene node when playback stops. Note: this is deprecated, consider subscribing to the SoundFinished event instead.
-    URHO3D_DEPRECATED void SetAutoRemove(bool enable);
+    //// Set to remove either the sound source component or its owner node from the scene automatically on sound playback completion. Disabled by default.
+    void SetAutoRemoveMode(AutoRemoveMode mode);
     /// Set new playback position.
     void SetPlayPosition(signed char* pos);
 
@@ -99,8 +99,8 @@ public:
     /// Return stereo panning.
     float GetPanning() const { return panning_; }
 
-    /// \deprecated Return autoremove mode.
-    URHO3D_DEPRECATED bool GetAutoRemove() const { return autoRemove_; }
+    /// Return automatic removal mode on sound playback completion.
+    AutoRemoveMode GetAutoRemoveMode() const { return autoRemove_; }
 
     /// Return whether is playing.
     bool IsPlaying() const;
@@ -138,14 +138,12 @@ protected:
     float attenuation_;
     /// Stereo panning.
     float panning_;
-    /// Autoremove timer.
-    float autoRemoveTimer_;
     /// Effective master gain.
     float masterGain_;
-    /// Autoremove flag.
-    bool autoRemove_;
     /// Whether finished event should be sent on playback stop.
     bool sendFinishedEvent_;
+    /// Automatic removal mode.
+    AutoRemoveMode autoRemove_;
 
 private:
     /// Play a sound without locking the audio mutex. Called internally.

+ 23 - 5
Source/Urho3D/Graphics/ParticleEmitter.cpp

@@ -41,6 +41,8 @@ extern const char* GEOMETRY_CATEGORY;
 extern const char* faceCameraModeNames[];
 static const unsigned MAX_PARTICLES_IN_FRAME = 100;
 
+extern const char* autoRemoveModeNames[];
+
 ParticleEmitter::ParticleEmitter(Context* context) :
     BillboardSet(context),
     periodTimer_(0.0f),
@@ -50,7 +52,8 @@ ParticleEmitter::ParticleEmitter(Context* context) :
     emitting_(true),
     needUpdate_(false),
     serializeParticles_(true),
-    sendFinishEvent_(true)
+    sendFinishedEvent_(true),
+    autoRemove_(REMOVE_DISABLED)
 {
     SetNumParticles(DEFAULT_NUM_PARTICLES);
 }
@@ -74,6 +77,7 @@ void ParticleEmitter::RegisterObject(Context* context)
     URHO3D_ATTRIBUTE("Is Emitting", bool, emitting_, true, AM_FILE);
     URHO3D_ATTRIBUTE("Period Timer", float, periodTimer_, 0.0f, AM_FILE | AM_NOEDIT);
     URHO3D_ATTRIBUTE("Emission Timer", float, emissionTimer_, 0.0f, AM_FILE | AM_NOEDIT);
+    URHO3D_ENUM_ATTRIBUTE("Autoremove Mode", autoRemove_, autoRemoveModeNames, REMOVE_DISABLED, AM_DEFAULT);
     URHO3D_COPY_BASE_ATTRIBUTES(Drawable);
     URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Particles", GetParticlesAttr, SetParticlesAttr, VariantVector, Variant::emptyVariantVector,
         AM_FILE | AM_NOEDIT);
@@ -128,7 +132,7 @@ void ParticleEmitter::Update(const FrameInfo& frame)
         if (inactiveTime && periodTimer_ >= inactiveTime)
         {
             emitting_ = true;
-            sendFinishEvent_ = true;
+            sendFinishedEvent_ = true;
             periodTimer_ -= inactiveTime;
         }
         // If emitter has an indefinite stop interval, keep period timer reset to allow restarting emission in the editor
@@ -295,7 +299,7 @@ void ParticleEmitter::SetEmitting(bool enable)
     if (enable != emitting_)
     {
         emitting_ = enable;
-        sendFinishEvent_ = enable;
+        sendFinishedEvent_ = enable;
         periodTimer_ = 0.0f;
         // Note: network update does not need to be marked as this is a file only attribute
     }
@@ -307,6 +311,12 @@ void ParticleEmitter::SetSerializeParticles(bool enable)
     // Note: network update does not need to be marked as this is a file only attribute
 }
 
+void ParticleEmitter::SetAutoRemoveMode(AutoRemoveMode mode)
+{
+    autoRemove_ = mode;
+    MarkNetworkUpdate();
+}
+
 void ParticleEmitter::ResetEmissionTimer()
 {
     emissionTimer_ = 0.0f;
@@ -538,7 +548,7 @@ void ParticleEmitter::HandleScenePostUpdate(StringHash eventType, VariantMap& ev
         MarkForUpdate();
     }
 
-    if (node_ && !emitting_ && sendFinishEvent_)
+    if (node_ && !emitting_ && sendFinishedEvent_)
     {
         // Send finished event only once all billboards are gone
         bool hasEnabledBillboards = false;
@@ -554,7 +564,10 @@ void ParticleEmitter::HandleScenePostUpdate(StringHash eventType, VariantMap& ev
 
         if (!hasEnabledBillboards)
         {
-            sendFinishEvent_ = false;
+            sendFinishedEvent_ = false;
+
+            // Make a weak pointer to self to check for destruction during event handling
+            WeakPtr<ParticleEmitter> self(this);
 
             using namespace ParticleEffectFinished;
 
@@ -563,6 +576,11 @@ void ParticleEmitter::HandleScenePostUpdate(StringHash eventType, VariantMap& ev
             eventData[P_EFFECT] = effect_;
 
             node_->SendEvent(E_PARTICLEEFFECTFINISHED, eventData);
+
+            if (self.Expired())
+                return;
+
+            DoAutoRemove(autoRemove_);
         }
     }
 }

+ 8 - 1
Source/Urho3D/Graphics/ParticleEmitter.h

@@ -76,6 +76,8 @@ public:
     void SetEmitting(bool enable);
     /// Set whether particles should be serialized. Default true, set false to reduce scene file size.
     void SetSerializeParticles(bool enable);
+    //// Set to remove either the emitter component or its owner node from the scene automatically on particle effect completion. Disabled by default.
+    void SetAutoRemoveMode(AutoRemoveMode mode);
     /// Reset the emission period timer.
     void ResetEmissionTimer();
     /// Remove all current particles.
@@ -97,6 +99,9 @@ public:
     /// Return whether particles are to be serialized.
     bool GetSerializeParticles() const { return serializeParticles_; }
 
+    /// Return automatic removal mode on particle effect completion.
+    AutoRemoveMode GetAutoRemoveMode() const { return autoRemove_; }
+
     /// Set particles effect attribute.
     void SetEffectAttr(const ResourceRef& value);
     /// Set particles effect attribute.
@@ -142,7 +147,9 @@ private:
     /// Serialize particles flag.
     bool serializeParticles_;
     /// Ready to send effect finish event flag.
-    bool sendFinishEvent_;
+    bool sendFinishedEvent_;
+    /// Automatic removal mode.
+    AutoRemoveMode autoRemove_;
 };
 
 }

+ 1 - 1
Source/Urho3D/Graphics/Technique.h

@@ -33,7 +33,7 @@ class ShaderVariation;
 /// Lighting mode of a pass.
 enum PassLightingMode
 {
-    LIGHTING_UNLIT,
+    LIGHTING_UNLIT = 0,
     LIGHTING_PERVERTEX,
     LIGHTING_PERPIXEL
 };

+ 3 - 3
Source/Urho3D/LuaScript/pkgs/Audio/SoundSource.pkg

@@ -12,7 +12,7 @@ class SoundSource : public Component
     void SetGain(float gain);
     void SetAttenuation(float attenuation);
     void SetPanning(float panning);
-    void SetAutoRemove(bool enable);
+    void SetAutoRemoveMode(AutoRemoveMode mode);
 
     Sound* GetSound() const;
     String GetSoundType() const;
@@ -21,7 +21,7 @@ class SoundSource : public Component
     float GetGain() const;
     float GetAttenuation() const;
     float GetPanning() const;
-    bool GetAutoRemove() const;
+    AutoRemoveMode GetAutoRemoveMode() const;
     bool IsPlaying() const;
     
     tolua_readonly tolua_property__get_set Sound* sound;
@@ -31,6 +31,6 @@ class SoundSource : public Component
     tolua_property__get_set float gain;
     tolua_property__get_set float attenuation;
     tolua_property__get_set float panning;
-    tolua_property__get_set bool autoRemove;
+    tolua_property__get_set AutoRemoveMode autoRemoveMode;
     tolua_readonly tolua_property__is_set bool playing;
 };

+ 3 - 0
Source/Urho3D/LuaScript/pkgs/Graphics/ParticleEmitter.pkg

@@ -6,6 +6,7 @@ class ParticleEmitter : public BillboardSet
     void SetNumParticles(unsigned num);
     void SetEmitting(bool enable);
     void SetSerializeParticles(bool enable);
+    void SetAutoRemoveMode(AutoRemoveMode mode);
     void ResetEmissionTimer();
     void RemoveAllParticles();
     void Reset();
@@ -15,11 +16,13 @@ class ParticleEmitter : public BillboardSet
     unsigned GetNumParticles() const;
     bool IsEmitting() const;
     bool GetSerializeParticles() const;
+    AutoRemoveMode GetAutoRemoveMode() const;
 
     tolua_property__get_set ParticleEffect* effect;
     tolua_property__get_set unsigned numParticles;
     tolua_property__is_set bool emitting;
     tolua_property__get_set bool serializeParticles;
+    tolua_property__get_set AutoRemoveMode autoRemoveMode;
 };
 
 ${

+ 7 - 0
Source/Urho3D/LuaScript/pkgs/Scene/Component.pkg

@@ -1,5 +1,12 @@
 $#include "Scene/Component.h"
 
+enum AutoRemoveMode
+{
+    REMOVE_DISABLED = 0,
+    REMOVE_COMPONENT,
+    REMOVE_NODE
+};
+
 class Component : public Animatable
 {
     void SetEnabled(bool enable);

+ 25 - 0
Source/Urho3D/Scene/Component.cpp

@@ -44,6 +44,13 @@
 namespace Urho3D
 {
 
+const char* autoRemoveModeNames[] = {
+    "Disabled",
+    "Component",
+    "Node",
+    0
+};
+
 Component::Component(Context* context) :
     Animatable(context),
     node_(0),
@@ -296,4 +303,22 @@ Component* Component::GetFixedUpdateSource()
     return ret;
 }
 
+void Component::DoAutoRemove(AutoRemoveMode mode)
+{
+    switch (mode)
+    {
+    case REMOVE_COMPONENT:
+        Remove();
+        return;
+
+    case REMOVE_NODE:
+        if (node_)
+            node_->Remove();
+        return;
+
+    default:
+        return;
+    }
+}
+
 }

+ 10 - 0
Source/Urho3D/Scene/Component.h

@@ -33,6 +33,14 @@ class Scene;
 
 struct ComponentReplicationState;
 
+/// Autoremove is used by some components for automatic removal from the scene hierarchy upon completion of an action, for example sound or particle effect.
+enum AutoRemoveMode
+{
+    REMOVE_DISABLED = 0,
+    REMOVE_COMPONENT,
+    REMOVE_NODE
+};
+
 /// Base class for components. Components can be created to scene nodes.
 class URHO3D_API Component : public Animatable
 {
@@ -119,6 +127,8 @@ protected:
     void HandleAttributeAnimationUpdate(StringHash eventType, VariantMap& eventData);
     /// Return a component from the scene root that sends out fixed update events (either PhysicsWorld or PhysicsWorld2D). Return null if neither exists.
     Component* GetFixedUpdateSource();
+    /// Perform autoremove. Called by subclasses. Caller should keep a weak pointer to itself to check whether was actually removed, and return immediately without further member operations in that case.
+    void DoAutoRemove(AutoRemoveMode mode);
 
     /// Scene node.
     Node* node_;