Browse Source

Make XAnimatedSprite2D derived from Drawable, export attributes.

aster2013 11 years ago
parent
commit
002292d01f

+ 1 - 1
Source/Engine/Urho2D/Drawable2D.h

@@ -55,7 +55,7 @@ public:
     virtual void OnSetEnabled();
 
     /// Set layer.
-    virtual void SetLayer(int layer);
+    void SetLayer(int layer);
     /// Set order in layer.
     void SetOrderInLayer(int orderInLayer);
     /// Set sprite.

+ 116 - 58
Source/Engine/Urho2D/XAnimatedSprite2D.cpp

@@ -22,6 +22,7 @@
 
 #include "Precompiled.h"
 #include "Context.h"
+#include "ResourceCache.h"
 #include "Scene.h"
 #include "SceneEvents.h"
 #include "Sprite2D.h"
@@ -36,9 +37,13 @@ namespace Urho3D
 {
 
 extern const char* URHO2D_CATEGORY;
+extern const char* blendModeNames[];
 
 XAnimatedSprite2D::XAnimatedSprite2D(Context* context) :
-    Drawable2D(context),
+    Drawable(context, DRAWABLE_GEOMETRY),
+    layer_(0),
+    orderInLayer_(0),
+    blendMode_(BLEND_ALPHA),
     speed_(1.0f),
     animationTime_(0.0f)
 {
@@ -51,12 +56,18 @@ XAnimatedSprite2D::~XAnimatedSprite2D()
 void XAnimatedSprite2D::RegisterObject(Context* context)
 {
     context->RegisterFactory<XAnimatedSprite2D>(URHO2D_CATEGORY);
-    ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_FLOAT, "Speed", GetSpeed, SetSpeed, float, 1.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_INT, "Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_INT, "Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
+    ENUM_ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, "Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_FLOAT, "Speed", GetSpeed, SetSpeed, float, 1.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_RESOURCEREF, "Animation Set", GetAnimationSetAttr, SetAnimationSetAttr, ResourceRef, ResourceRef(XAnimatedSprite2D::GetTypeStatic()), AM_DEFAULT);
+    REF_ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_STRING, "Animation", GetAnimation, SetAnimation, String, String::EMPTY, AM_DEFAULT);
+    COPY_BASE_ATTRIBUTES(Drawable2D, Drawable);
 }
 
 void XAnimatedSprite2D::OnSetEnabled()
 {
-    Drawable2D::OnSetEnabled();
+    Drawable::OnSetEnabled();
 
     Scene* scene = GetScene();
     if (scene)
@@ -70,11 +81,34 @@ void XAnimatedSprite2D::OnSetEnabled()
 
 void XAnimatedSprite2D::SetLayer(int layer)
 {
-    Drawable2D::SetLayer(layer);
+    if (layer == layer_)
+        return;
+
+    layer_ = layer;
+
+    for (unsigned i = 0; i < objectNodes_.Size(); ++i)
+    {
+        StaticSprite2D* objectSprite = objectNodes_[i]->GetComponent<StaticSprite2D>();
+        objectSprite->SetLayer(layer_);
+    }
+}
+
+void XAnimatedSprite2D::SetOrderInLayer(int orderInLayer)
+{
+    orderInLayer_ = orderInLayer;
+}
+
+void XAnimatedSprite2D::SetBlendMode(BlendMode blendMode)
+{
+    if (blendMode == blendMode_)
+        return;
+
+    blendMode_ = blendMode;
+
     for (unsigned i = 0; i < objectNodes_.Size(); ++i)
     {
         StaticSprite2D* objectSprite = objectNodes_[i]->GetComponent<StaticSprite2D>();
-        objectSprite->SetLayer(layer);
+        objectSprite->SetBlendMode(blendMode_);
     }
 }
 
@@ -86,71 +120,56 @@ void XAnimatedSprite2D::SetSpeed(float speed)
 
 void XAnimatedSprite2D::SetAnimation(XAnimationSet2D* animationSet, const String& name)
 {
-    SetAnimation(animationSet->GetAnimation(name));
+    animationSet_ = animationSet;
+    animationName_ = name;
+    
+    if (animationSet)
+        SetAnimation(animationSet->GetAnimation(name));
+    else
+        SetAnimation(0);
 }
 
-void XAnimatedSprite2D::SetAnimation(XAnimation2D* animation)
+void XAnimatedSprite2D::SetAnimationSet(XAnimationSet2D* animationSet)
 {
-    if (animation == animation_)
-    {
-        // Reset time
-        animationTime_ = 0.0f;
+    if (animationSet == animationSet_)
         return;
-    }
 
-    if (animation_)
-    {
-        // Remove all object nodes
-        for (unsigned i = 0; i < objectNodes_.Size(); ++i)
-            objectNodes_[i]->Remove();
-        objectNodes_.Clear();
-    }
-
-    animation_ = animation;
-    
-    if (!animation_)
-        return;
-
-    animationTime_ = 0.0f;
-    
-    for (unsigned i = 0; i < animation_->GetTimelines().Size(); ++i)
-    {
-        SharedPtr<Node> objectNode(GetNode()->CreateChild(animation_->GetTimelines()[i].name_));
+    animationSet_ = animationSet;
 
-        StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
-        staticSprite->SetLayer(GetLayer());
-        staticSprite->SetUseHotSpot(true);
-        staticSprite->SetBlendMode(BLEND_ALPHA);
-
-        objectNodes_.Push(objectNode);        
-    }
+    if (animationSet_)
+        SetAnimation(animationSet_->GetAnimation(animationName_));
+    else
+        SetAnimation(0);
 
-    UpdateAnimation(0.0f);
+}
+void XAnimatedSprite2D::SetAnimation(const String& name)
+{
+    animationName_ = name;
 
-    UpdateDefaultMaterial();
-    MarkNetworkUpdate();
+    if (animationSet_)
+        SetAnimation(animationSet_->GetAnimation(animationName_));
 }
 
-void XAnimatedSprite2D::SetAnimation(const String& name)
+XAnimationSet2D* XAnimatedSprite2D::GetAnimationSet() const
 {
-    if (!animation_)
-        return;
+    return animationSet_;
+}
 
-    XAnimationSet2D* animationSet = animation_->GetAnimationSet();
-    if (!animationSet)
-        return;
 
-    SetAnimation(animationSet->GetAnimation(name));
+void XAnimatedSprite2D::SetAnimationSetAttr(ResourceRef value)
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    SetAnimationSet(cache->GetResource<XAnimationSet2D>(value.name_));
 }
 
-XAnimation2D* XAnimatedSprite2D::GetAnimation() const
+Urho3D::ResourceRef XAnimatedSprite2D::GetAnimationSetAttr() const
 {
-    return animation_;
+    return GetResourceRef(animationSet_, XAnimationSet2D::GetTypeStatic());
 }
 
 void XAnimatedSprite2D::OnNodeSet(Node* node)
 {
-    Drawable2D::OnNodeSet(node);
+    Drawable::OnNodeSet(node);
 
     if (node)
     {
@@ -174,15 +193,47 @@ void XAnimatedSprite2D::OnWorldBoundingBoxUpdate()
     boundingBox_ = worldBoundingBox_.Transformed(node_->GetWorldTransform().Inverse());
 }
 
-void XAnimatedSprite2D::UpdateVertices()
-{
-}
 
-void XAnimatedSprite2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
+
+void XAnimatedSprite2D::SetAnimation(XAnimation2D* animation)
 {
-    using namespace ScenePostUpdate;
-    float timeStep = eventData[P_TIMESTEP].GetFloat();
-    UpdateAnimation(timeStep);
+    if (animation == animation_)
+    {
+        // Reset time
+        animationTime_ = 0.0f;
+        return;
+    }
+
+    if (animation_)
+    {
+        // Remove all object nodes
+        for (unsigned i = 0; i < objectNodes_.Size(); ++i)
+            objectNodes_[i]->Remove();
+        objectNodes_.Clear();
+    }
+
+    animation_ = animation;
+
+    if (!animation_)
+        return;
+
+    animationTime_ = 0.0f;
+
+    for (unsigned i = 0; i < animation_->GetTimelines().Size(); ++i)
+    {
+        SharedPtr<Node> objectNode(GetNode()->CreateChild(animation_->GetTimelines()[i].name_));
+
+        StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
+        staticSprite->SetLayer(layer_);
+        staticSprite->SetBlendMode(blendMode_);
+        staticSprite->SetUseHotSpot(true);
+
+        objectNodes_.Push(objectNode);        
+    }
+
+    UpdateAnimation(0.0f);
+
+    MarkNetworkUpdate();
 }
 
 void XAnimatedSprite2D::UpdateAnimation(float timeStep)
@@ -231,7 +282,7 @@ void XAnimatedSprite2D::UpdateAnimation(float timeStep)
             objectNode->SetEnabled(true);
 
             StaticSprite2D* staticSprite = objectNode->GetComponent<StaticSprite2D>();
-            staticSprite->SetOrderInLayer(objectRef->zIndex_);
+            staticSprite->SetOrderInLayer(orderInLayer_ + objectRef->zIndex_);
 
             const Vector<ObjectKey>& objectKeys = timelines[i].objectKeys_;
             for (unsigned j = 0; j < objectKeys.Size() - 1; ++j)
@@ -273,5 +324,12 @@ void XAnimatedSprite2D::UpdateAnimation(float timeStep)
     MarkForUpdate();
 }
 
+void XAnimatedSprite2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
+{
+    using namespace ScenePostUpdate;
+    float timeStep = eventData[P_TIMESTEP].GetFloat();
+    UpdateAnimation(timeStep);
+}
+
 }
 

+ 39 - 13
Source/Engine/Urho2D/XAnimatedSprite2D.h

@@ -22,7 +22,7 @@
 
 #pragma once
 
-#include "Drawable2D.h"
+#include "Drawable.h"
 
 namespace Urho3D
 {
@@ -31,7 +31,7 @@ class XAnimationSet2D;
 class XAnimation2D;
 
 /// Spriter animation component.
-class URHO3D_API XAnimatedSprite2D : public Drawable2D
+class URHO3D_API XAnimatedSprite2D : public Drawable
 {
     OBJECT(XAnimatedSprite2D);
 
@@ -45,38 +45,64 @@ public:
 
     /// Handle enabled/disabled state change.
     virtual void OnSetEnabled();
-    /// Set layer.
-    virtual void SetLayer(int layer);
-
+    
+    /// Set layer.
+    void SetLayer(int layer);
+    /// Set order in layer.
+    void SetOrderInLayer(int orderInLayer);
+    /// Set blend mode.
+    void SetBlendMode(BlendMode mode);
     /// Set speed.
     void SetSpeed(float speed);
-
     /// Set animation by animation set and name.
     void SetAnimation(XAnimationSet2D* animationSet, const String& name);
-    /// Set animation.
-    void SetAnimation(XAnimation2D* animation);
+    /// Set animation set.
+    void SetAnimationSet(XAnimationSet2D* animationSet);
     /// Set animation by name.
     void SetAnimation(const String& name);
 
+    /// Return layer.
+    int GetLayer() const { return layer_; }
+    /// Return order in layer.
+    int GetOrderInLayer() const { return orderInLayer_; }
+    /// Return blend mode.
+    BlendMode GetBlendMode() const { return blendMode_; }
     /// Return speed.
     float GetSpeed() const { return speed_; }
     /// Return animation.
-    XAnimation2D* GetAnimation() const;
+    XAnimationSet2D* GetAnimationSet() const;
+    /// Return animation name.
+    const String& GetAnimation() const { return animationName_; }
+
+    /// Set animation set attribute.
+    void SetAnimationSetAttr(ResourceRef value);
+    /// Return animtion set attribute.
+    ResourceRef GetAnimationSetAttr() const;
 
 protected:
     /// Handle node being assigned.
     virtual void OnNodeSet(Node* node);
     /// Recalculate the world-space bounding box.
     virtual void OnWorldBoundingBoxUpdate();
-    /// Update vertices.
-    virtual void UpdateVertices();
-    /// Handle scene post update.
-    void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
+    /// Set animation.
+    void SetAnimation(XAnimation2D* animation);
     /// Update.
     void UpdateAnimation(float timeStep);
+    /// Handle scene post update.
+    void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
 
+    /// Layer.
+    int layer_;
+    /// Order in layer.
+    int orderInLayer_;
+    /// Blend mode.
+    BlendMode blendMode_;
     /// Speed.
     float speed_;
+    /// Animation set.
+    SharedPtr<XAnimationSet2D> animationSet_;
+    /// Animation name.
+    String animationName_;
     /// Animation.
     SharedPtr<XAnimation2D> animation_;
     /// Animation time.

+ 2 - 2
Source/Engine/Urho2D/XAnimation2D.h

@@ -111,7 +111,7 @@ public:
     void SetName(const String& name);
     /// Set length.
     void SetLength(float length);
-    /// Set loop.
+    /// Set looped.
     void SetLooped(bool looped);
     /// Add mainline key.
     void AddMainlineKey(const MainlineKey& mainlineKey);
@@ -124,7 +124,7 @@ public:
     const String& GetName() const { return name_; }
     /// Return length.
     float GetLength() const { return length_; }
-    /// Return looping.
+    /// Return looped.
     bool IsLooped() const { return looped_; }
     /// Return all mainline keys.
     const Vector<MainlineKey>& GetMainlineKeys() const { return mainlineKeys_; }

+ 1 - 1
Source/Engine/Urho2D/XAnimationSet2D.cpp

@@ -116,7 +116,7 @@ Sprite2D* XAnimationSet2D::GetSprite(unsigned folderId, unsigned fileId) const
     return 0;
 }
 
-bool XAnimationSet2D::LoadFolders(const XMLElement &rootElem)
+bool XAnimationSet2D::LoadFolders(const XMLElement& rootElem)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     String parentPath = GetParentPath(GetName());

+ 1 - 0
Source/Samples/33_Urho2DSpriterAnimation/Urho2DSpriterAnimation.cpp

@@ -22,6 +22,7 @@
 
 #include "Camera.h"
 #include "CoreEvents.h"
+#include "Drawable2D.h"
 #include "Engine.h"
 #include "Font.h"
 #include "Graphics.h"