Browse Source

Update Urho2D API(Lua and AngelScript).

aster2013 11 years ago
parent
commit
9f80292978

+ 1 - 1
Source/Engine/LuaScript/pkgs/Urho2D/AnimatedSprite2D.pkg

@@ -11,7 +11,7 @@ class AnimatedSprite2D : public StaticSprite2D
 {
     void SetSpeed(float speed);
     void SetCycleMode(CycleMode cycleMode);
-    void SetAnimation(Animation2D* animation, float startTime = 0.0f);
+    void SetAnimation(Animation2D* animation);
 
     float GetSpeed() const;
     CycleMode GetCycleMode() const;

+ 1 - 1
Source/Engine/LuaScript/pkgs/Urho2DLuaAPI.pkg

@@ -1,6 +1,6 @@
-$pfile "Urho2D/Drawable2D.pkg"
 $pfile "Urho2D/Sprite2D.pkg"
 $pfile "Urho2D/SpriteSheet2D.pkg"
+$pfile "Urho2D/Drawable2D.pkg"
 $pfile "Urho2D/StaticSprite2D.pkg"
 $pfile "Urho2D/Animation2D.pkg"
 $pfile "Urho2D/AnimatedSprite2D.pkg"

+ 45 - 11
Source/Engine/Script/Urho2DAPI.cpp

@@ -21,13 +21,15 @@
 //
 
 #include "Precompiled.h"
+#include "AnimatedSprite2D.h"
+#include "Animation2D.h"
 #include "APITemplates.h"
 #include "Drawable2D.h"
+#include "ParticleEmitter2D.h"
+#include "ParticleModel2D.h"
 #include "Sprite2D.h"
 #include "SpriteSheet2D.h"
 #include "StaticSprite2D.h"
-#include "ParticleModel2D.h"
-#include "ParticleEmitter2D.h"
 
 #ifdef _MSC_VER
 #pragma warning(disable:4345)
@@ -60,7 +62,7 @@ static void RegisterSpriteSheet2D(asIScriptEngine* engine)
 template <class T> void RegisterDrawable2D(asIScriptEngine* engine, const char* className)
 {
     RegisterDrawable<T>(engine, className);
-    RegisterSubclass<Drawable2D, T>(engine, "Drawable2D", className);    
+    RegisterSubclass<Drawable2D, T>(engine, "Drawable2D", className);
     engine->RegisterObjectMethod(className, "void set_unitPerPixel(float)", asMETHOD(T, SetUnitPerPixel), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "float get_unitPerPixel() const", asMETHOD(T, GetUnitPerPixel), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_sprite(Sprite2D@+)", asMETHODPR(T, SetSprite, (Sprite2D*), void), asCALL_THISCALL);
@@ -78,16 +80,46 @@ static void RegisterDrawable2D(asIScriptEngine* engine)
     RegisterDrawable2D<Drawable2D>(engine, "Drawable2D");
 }
 
+/// Template function for registering a class derived from StaticSprite2D.
+template <class T> void RegisterStaticSprite2D(asIScriptEngine* engine, const char* className)
+{
+    RegisterDrawable2D<T>(engine, className);
+    RegisterSubclass<StaticSprite2D, T>(engine, "StaticSprite2D", className);
+    engine->RegisterObjectMethod(className, "void SetFlip(bool, bool)", asMETHODPR(T, SetFlip, (bool, bool), void), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_flipX(bool)", asMETHOD(T, SetFlipX), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "bool get_flipX() const", asMETHOD(T, GetFlipX), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_flipY(bool)", asMETHOD(T, SetFlipY), asCALL_THISCALL);    
+    engine->RegisterObjectMethod(className, "bool get_flipY() const", asMETHOD(T, GetFlipY), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_color(const Color&in)", asMETHOD(T, SetColor), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "const Color& get_color() const", asMETHOD(T, GetColor), asCALL_THISCALL);
+}
+
 static void RegisterStaticSprite2D(asIScriptEngine* engine)
 {
-    RegisterDrawable2D<StaticSprite2D>(engine, "StaticSprite2D");
-    engine->RegisterObjectMethod("StaticSprite2D", "void SetFlip(bool, bool)", asMETHODPR(StaticSprite2D, SetFlip, (bool, bool), void), asCALL_THISCALL);
-    engine->RegisterObjectMethod("StaticSprite2D", "void set_flipX(bool)", asMETHOD(StaticSprite2D, SetFlipX), asCALL_THISCALL);
-    engine->RegisterObjectMethod("StaticSprite2D", "bool get_flipX() const", asMETHOD(StaticSprite2D, GetFlipX), asCALL_THISCALL);
-    engine->RegisterObjectMethod("StaticSprite2D", "void set_flipY(bool)", asMETHOD(StaticSprite2D, SetFlipY), asCALL_THISCALL);    
-    engine->RegisterObjectMethod("StaticSprite2D", "bool get_flipY() const", asMETHOD(StaticSprite2D, GetFlipY), asCALL_THISCALL);
-    engine->RegisterObjectMethod("StaticSprite2D", "void set_color(const Color&in)", asMETHOD(StaticSprite2D, SetColor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("StaticSprite2D", "const Color& get_color() const", asMETHOD(StaticSprite2D, GetColor), asCALL_THISCALL);
+    RegisterStaticSprite2D<StaticSprite2D>(engine, "StaticSprite2D");
+}
+
+static void RegisterAnimation2D(asIScriptEngine* engine)
+{
+    RegisterResource<Animation2D>(engine, "Animation2D");
+    engine->RegisterObjectMethod("Animation2D", "float get_totalTime() const", asMETHOD(Animation2D, GetTotalTime), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Animation2D", "uint get_numKeyFrames() const", asMETHOD(Animation2D, GetNumKeyFrames), asCALL_THISCALL);
+}
+
+static void RegisterAnimatedSprite2D(asIScriptEngine* engine)
+{
+    engine->RegisterEnum("CycleMode");
+    engine->RegisterEnumValue("CycleMode", "CM_LOOP", CM_LOOP);
+    engine->RegisterEnumValue("CycleMode", "CM_CLAMP", CM_CLAMP);
+    engine->RegisterEnumValue("CycleMode", "CM_PINGPONG", CM_PINGPONG);
+
+    RegisterStaticSprite2D<AnimatedSprite2D>(engine, "AnimatedSprite2D");
+    engine->RegisterObjectMethod("AnimatedSprite2D", "void set_speed(float)", asMETHOD(AnimatedSprite2D, SetSpeed), asCALL_THISCALL);
+    engine->RegisterObjectMethod("AnimatedSprite2D", "float get_speed() const", asMETHOD(AnimatedSprite2D, GetSpeed), asCALL_THISCALL);
+    engine->RegisterObjectMethod("AnimatedSprite2D", "void set_cycleMode(CycleMode)", asMETHOD(AnimatedSprite2D, SetCycleMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("AnimatedSprite2D", "CycleMode get_cycleMode() const", asMETHOD(AnimatedSprite2D, GetCycleMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("AnimatedSprite2D", "void set_animation(Animation2D@+)", asMETHOD(AnimatedSprite2D, SetAnimation), asCALL_THISCALL);
+    engine->RegisterObjectMethod("AnimatedSprite2D", "Animation2D@+ get_animation() const", asMETHOD(AnimatedSprite2D, GetAnimation), asCALL_THISCALL);
 }
 
 static void RegisterParticleModel2D(asIScriptEngine* engine)
@@ -112,6 +144,8 @@ void RegisterUrho2DAPI(asIScriptEngine* engine)
     RegisterSpriteSheet2D(engine);
     RegisterDrawable2D(engine);
     RegisterStaticSprite2D(engine);
+    RegisterAnimation2D(engine);
+    RegisterAnimatedSprite2D(engine);
     RegisterParticleModel2D(engine);
     RegisterParticleEmitter2D(engine);
 }

+ 2 - 2
Source/Engine/Urho2D/AnimatedSprite2D.cpp

@@ -93,9 +93,9 @@ void AnimatedSprite2D::SetCycleMode(CycleMode cycleMode)
     cycleMode_ = cycleMode;
 }
 
-void AnimatedSprite2D::SetAnimation(Animation2D* animation, float startTime)
+void AnimatedSprite2D::SetAnimation(Animation2D* animation)
 {
-    animationTime_ = startTime;
+    animationTime_ = 0.0f;
 
     if (animation_ == animation)
         return;

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

@@ -40,7 +40,7 @@ enum CycleMode
     CM_PINGPONG,
 };
 
-/// Static sprite component.
+/// Animated sprite component.
 class URHO3D_API AnimatedSprite2D : public StaticSprite2D
 {
     OBJECT(AnimatedSprite2D);
@@ -61,7 +61,7 @@ public:
     /// Set cycle mode.
     void SetCycleMode(CycleMode cycleMode);
     /// Set animation.
-    void SetAnimation(Animation2D* animation, float startTime = 0.0f);
+    void SetAnimation(Animation2D* animation);
 
     /// Return speed.
     float GetSpeed() const { return speed_; }

+ 2 - 2
Source/Engine/Urho2D/Animation2D.cpp

@@ -100,7 +100,7 @@ bool Animation2D::Load(Deserializer& source)
 
             keyFrame.sprite_ = sprite;
         }
-        else
+        else if (names.Size() == 2)
         {
             SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(names[0]);
             if (!spriteSheet)
@@ -118,7 +118,7 @@ bool Animation2D::Load(Deserializer& source)
 
             keyFrame.sprite_ = sprite;
         }
-
+        
         keyFrames_.Push(keyFrame);
 
         totalTime += keyFrame.duration_;

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

@@ -55,7 +55,7 @@ public:
     virtual bool Load(Deserializer& source);
     /// Save resource. Return true if successful.
     virtual bool Save(Serializer& dest) const;
-
+    
     /// Return total time.
     float GetTotalTime() const;
     /// Return num key frame.