Ver código fonte

Add spriter animation AS binding and sample.

aster2013 11 anos atrás
pai
commit
22d6ede8e6

+ 171 - 0
Bin/Data/Scripts/33_Urho2DSpriterAnimation.as

@@ -0,0 +1,171 @@
+// Urho2D sprite example.
+// This sample demonstrates:
+//     - Creating a 2D scene with spriter animation
+//     - Displaying the scene using the Renderer subsystem
+//     - Handling keyboard to move and zoom 2D camera
+
+#include "Scripts/Utilities/Sample.as"
+
+Node@ spriteNode;
+int animationIndex = 0;
+
+Array<String> animationNames = 
+{
+    "idle",
+    "run",
+    "attack",
+    "hit",
+    "dead",
+    "dead2",
+    "dead3",
+};
+
+void Start()
+{
+    // Execute the common startup for samples
+    SampleStart();
+
+    // Create the scene content
+    CreateScene();
+
+    // Create the UI content
+    CreateInstructions();
+
+    // Setup the viewport for displaying the scene
+    SetupViewport();
+
+    // Hook up to the frame update events
+    SubscribeToEvents();
+}
+
+void CreateScene()
+{
+    scene_ = Scene();
+
+    // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
+    // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
+    // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
+    // optimizing manner
+    scene_.CreateComponent("Octree");
+
+    // Create a scene node for the camera, which we will move around
+    // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
+    cameraNode = scene_.CreateChild("Camera");
+    // Set an initial position for the camera scene node above the plane
+    cameraNode.position = Vector3(0.0f, 0.0f, -10.0f);
+
+    Camera@ camera = cameraNode.CreateComponent("Camera");
+    camera.orthographic = true;
+    camera.orthoSize = graphics.height * PIXEL_SIZE;
+
+    XAnimationSet2D@ animationSet = cache.GetResource("XAnimationSet2D", "Urho2D/imp/imp.scml");
+    if (animationSet is null)
+        return;
+
+    spriteNode = scene_.CreateChild("SpriterAnimation");
+    spriteNode.position = Vector3(-1.4f, 2.0f, -0.0f);
+
+    XAnimatedSprite2D@ animatedSprite = spriteNode.CreateComponent("XAnimatedSprite2D");
+    animatedSprite.SetAnimation(animationSet, animationNames[animationIndex]);
+}
+
+void CreateInstructions()
+{
+    // Construct new Text object, set string to display and font to use
+    Text@ instructionText = ui.root.CreateChild("Text");
+    instructionText.text = "Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.";
+    instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
+
+    // Position the text relative to the screen center
+    instructionText.horizontalAlignment = HA_CENTER;
+    instructionText.verticalAlignment = VA_CENTER;
+    instructionText.SetPosition(0, ui.root.height / 4);
+}
+
+void SetupViewport()
+{
+    // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
+    // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
+    // use, but now we just use full screen and default render path configured in the engine command line options
+    Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
+    renderer.viewports[0] = viewport;
+}
+
+void MoveCamera(float timeStep)
+{
+    // Do not move if the UI has a focused element (the console)
+    if (ui.focusElement !is null)
+        return;
+
+    // Movement speed as world units per second
+    const float MOVE_SPEED = 4.0f;
+
+    // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
+    if (input.keyDown['W'])
+        cameraNode.Translate(Vector3(0.0f, 1.0f, 0.0f) * MOVE_SPEED * timeStep);
+    if (input.keyDown['S'])
+        cameraNode.Translate(Vector3(0.0f, -1.0f, 0.0f) * MOVE_SPEED * timeStep);
+    if (input.keyDown['A'])
+        cameraNode.Translate(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
+    if (input.keyDown['D'])
+        cameraNode.Translate(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
+
+    if (input.keyDown[KEY_PAGEUP])
+    {
+        Camera@ camera = cameraNode.GetComponent("Camera");
+        camera.zoom = camera.zoom * 1.01f;
+    }
+
+    if (input.keyDown[KEY_PAGEDOWN])
+    {
+        Camera@ camera = cameraNode.GetComponent("Camera");
+        camera.zoom = camera.zoom * 0.99f;
+    }
+}
+
+void SubscribeToEvents()
+{
+    // Subscribe HandleUpdate() function for processing update events
+    SubscribeToEvent("Update", "HandleUpdate");
+    SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
+
+    // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
+    UnsubscribeFromEvent("SceneUpdate");
+}
+
+void HandleUpdate(StringHash eventType, VariantMap& eventData)
+{
+    // Take the frame time step, which is stored as a float
+    float timeStep = eventData["TimeStep"].GetFloat();
+
+    // Move the camera, scale movement with time step
+    MoveCamera(timeStep);
+}
+
+void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
+{
+    XAnimatedSprite2D@ animatedSprite = spriteNode.GetComponent("XAnimatedSprite2D");
+    animationIndex = (animationIndex + 1) % 7;
+    animatedSprite.animation = animationNames[animationIndex];
+}
+
+// Create XML patch instructions for screen joystick layout specific to this sample app
+String patchInstructions =
+        "<patch>" +
+        "    <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
+        "    <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" +
+        "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
+        "        <element type=\"Text\">" +
+        "            <attribute name=\"Name\" value=\"KeyBinding\" />" +
+        "            <attribute name=\"Text\" value=\"PAGEUP\" />" +
+        "        </element>" +
+        "    </add>" +
+        "    <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
+        "    <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" +
+        "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
+        "        <element type=\"Text\">" +
+        "            <attribute name=\"Name\" value=\"KeyBinding\" />" +
+        "            <attribute name=\"Text\" value=\"PAGEDOWN\" />" +
+        "        </element>" +
+        "    </add>" +
+        "</patch>";

+ 40 - 0
Source/Engine/Script/Urho2DAPI.cpp

@@ -51,6 +51,9 @@
 #include "Sprite2D.h"
 #include "SpriteSheet2D.h"
 #include "StaticSprite2D.h"
+#include "XAnimation2D.h"
+#include "XAnimationSet2D.h"
+#include "XAnimatedSprite2D.h"
 
 #ifdef _MSC_VER
 #pragma warning(disable:4345)
@@ -163,6 +166,40 @@ static void RegisterParticleEmitter2D(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ParticleEmitter2D", "ParticleEffect2D@+ get_effect() const", asMETHOD(ParticleEmitter2D, GetEffect), asCALL_THISCALL);
 }
 
+static void RegisterXAnimation2D(asIScriptEngine* engine)
+{
+    RegisterRefCounted<XAnimation2D>(engine, "XAnimation2D");
+    engine->RegisterObjectMethod("XAnimation2D", "const String& get_name() const", asMETHOD(XAnimation2D, GetName), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimation2D", "float get_length() const", asMETHOD(XAnimation2D, GetLength), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimation2D", "bool get_looped() const", asMETHOD(XAnimation2D, IsLooped), asCALL_THISCALL);
+
+}
+static void RegisterXAnimationSet2D(asIScriptEngine* engine)
+{
+    RegisterResource<XAnimationSet2D>(engine, "XAnimationSet2D");
+    engine->RegisterObjectMethod("XAnimationSet2D", "uint get_numAnimations() const", asMETHOD(XAnimationSet2D, GetNumAnimations), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimationSet2D", "XAnimation2D@+ GetAnimation(uint) const", asMETHODPR(XAnimationSet2D, GetAnimation, (unsigned) const, XAnimation2D*), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimationSet2D", "XAnimation2D@+ GetAnimation(const String&) const", asMETHODPR(XAnimationSet2D, GetAnimation, (const String&) const, XAnimation2D*), asCALL_THISCALL);
+}
+
+static void RegisterXAnimatedSprite2D(asIScriptEngine* engine)
+{
+    RegisterDrawable<XAnimatedSprite2D>(engine, "XAnimatedSprite2D");
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void set_layer(int)", asMETHOD(XAnimatedSprite2D, SetLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "int get_layer() const", asMETHOD(XAnimatedSprite2D, GetLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void set_orderInLayer(int)", asMETHOD(XAnimatedSprite2D, SetOrderInLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "int get_orderInLayer() const", asMETHOD(XAnimatedSprite2D, GetOrderInLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void set_blendMode(BlendMode)", asMETHOD(XAnimatedSprite2D, SetBlendMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "BlendMode get_blendMode() const", asMETHOD(XAnimatedSprite2D, GetBlendMode), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void set_speed(float)", asMETHOD(XAnimatedSprite2D, SetSpeed), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "float get_speed() const", asMETHOD(XAnimatedSprite2D, GetSpeed), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void SetAnimation(XAnimationSet2D@+, const String&)", asMETHODPR(XAnimatedSprite2D, SetAnimation, (XAnimationSet2D*, const String&), void), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void set_animationSet(XAnimationSet2D@+)", asMETHOD(XAnimatedSprite2D, SetAnimationSet), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "XAnimationSet2D@+ get_animationSet() const", asMETHOD(XAnimatedSprite2D, GetAnimationSet), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "void set_animation(const String&)", asMETHODPR(XAnimatedSprite2D, SetAnimation, (const String&), void), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XAnimatedSprite2D", "const String& get_animation() const", asMETHOD(XAnimatedSprite2D, GetAnimation), asCALL_THISCALL);
+}
+
 static void RegisterRigidBody2D(asIScriptEngine* engine)
 {
     engine->RegisterEnum("BodyType2D");
@@ -596,6 +633,9 @@ void RegisterUrho2DAPI(asIScriptEngine* engine)
     RegisterAnimatedSprite2D(engine);
     RegisterParticleEffect2D(engine);
     RegisterParticleEmitter2D(engine);
+    RegisterXAnimation2D(engine);
+    RegisterXAnimationSet2D(engine);
+    RegisterXAnimatedSprite2D(engine);
 
     RegisterRigidBody2D(engine);
     RegisterPhysicsWorld2D(engine);

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

@@ -112,7 +112,7 @@ void Urho2DSpriterAnimation::CreateInstructions()
 
     // Construct new Text object, set string to display and font to use
     Text* instructionText = ui->GetRoot()->CreateChild<Text>();
-    instructionText->SetText("Use mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.");
+    instructionText->SetText("Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.");
     instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
 
     // Position the text relative to the screen center