Esempio n. 1
0
        private void RenderCurrentAnimationSettings()
        {
            if (AnimController == null)
            {
                if (ImGui.Button("Create Controller"))
                {
                    AnimController = new AnimationController(Animation);
                    AnimController.AddAnimation(new AnimationNode("Default")
                    {
                        EndingFrame = Animation.EndingFrame,
                        StartingFrame = Animation.StartingFrame,
                        LoopType = Animation.LoopType
                    });
                }

                int frameTime = Animation.TimeBetweenFrames;
                if (ImGui.InputInt("MS Between Frames", ref frameTime))
                {
                    Animation.TimeBetweenFrames = frameTime;
                    Animation.Reset();
                }

                ImGui.Text("Starting and ending frames are 0 indexed and inclusive.");

                int startingFrame = Animation.StartingFrame;
                if (ImGui.InputInt("Starting Frame", ref startingFrame))
                {
                    Animation.StartingFrame = startingFrame;
                    Animation.Reset();
                }

                int endingFrame = Animation.EndingFrame;
                if (ImGui.InputInt("Ending Frame", ref endingFrame))
                {
                    Animation.EndingFrame = endingFrame;
                    Animation.Reset();
                }

                var loopType = (int) Animation.LoopType;
                if (ImGui.Combo("Loop Type", ref loopType, string.Join('\0', Enum.GetNames(typeof(AnimationLoopType)))))
                {
                    Animation.LoopType = (AnimationLoopType) loopType;
                    Animation.Reset();
                }
            }
            else
            {
                ImGui.Text("Animations");
                ImGui.PushID("animList");
                foreach (AnimationNode n in AnimController.Animations.Values)
                {
                    bool activeOverlay = _overlayAnimation == n;
                    ImGui.PushID(n.Name);
                    if (n == AnimController.CurrentAnimation)
                    {
                        ImGui.Text(n.Name);
                    }
                    else
                    {
                        if (ImGui.Button(n.Name)) AnimController.SetAnimation(n.Name);
                    }

                    ImGui.SameLine();
                    ImGui.Text($"({n.StartingFrame}-{n.EndingFrame})");
                    ImGui.SameLine();
                    if (ImGui.Checkbox("Overlay", ref activeOverlay)) _overlayAnimation = activeOverlay ? n : null;
                    ImGui.PopID();
                }

                bool enableShortcuts = !ImGui.IsAnyItemActive();
                if (ImGui.Button("(C)reate") || enableShortcuts && Engine.Host.IsKeyDown(Key.C) && AnimController.Animations.All(x => x.Key != "NewAnim"))
                {
                    var newNode = new AnimationNode("NewAnim");
                    AnimController.AddAnimation(newNode);
                    AnimController.SetAnimation(newNode.Name);
                }

                ImGui.SameLine();
                if (AnimController.CurrentAnimation == null)
                {
                    ImGui.PopID();
                    return;
                }

                if (ImGui.Button("Remove")) AnimController.RemoveAnimation(AnimController.CurrentAnimation.Name);
                ImGui.SameLine();
                if (ImGui.Button("(R)ename") || enableShortcuts && Engine.Host.IsKeyDown(Key.R))
                {
                    var newName = new StringInputModal(s =>
                        {
                            AnimController.CurrentAnimation.Name = s;
                            AnimController.Reindex();
                        },
                        $"New name for {AnimController.CurrentAnimation.Name}");
                    Parent.AddWindow(newName);
                }

                ImGui.PopID();

                AnimationNode cur = AnimController.CurrentAnimation;
                var modified = false;

                int frameTime = cur.TimeBetweenFrames;
                if (ImGui.InputInt("MS Between Frames", ref frameTime))
                {
                    cur.TimeBetweenFrames = frameTime;
                    modified = true;
                }

                ImGui.Text("Starting and ending frames are 0 indexed and inclusive.");

                int startingFrame = cur.StartingFrame;
                if (ImGui.InputInt("Starting Frame", ref startingFrame))
                {
                    cur.StartingFrame = startingFrame;
                    modified = true;
                }

                int endingFrame = cur.EndingFrame;
                if (ImGui.InputInt("Ending Frame", ref endingFrame))
                {
                    cur.EndingFrame = endingFrame;
                    modified = true;
                }

                var loopType = (int) cur.LoopType;
                if (ImGui.Combo("Loop Type", ref loopType, string.Join('\0', Enum.GetNames(typeof(AnimationLoopType)))))
                {
                    cur.LoopType = (AnimationLoopType) loopType;
                    modified = true;
                }

                if (modified) AnimController.SetAnimation(cur.Name, true);
            }
        }
Esempio n. 2
0
        public void AnimatedTextureClassDrawing()
        {
            var normalLoop = new AnimatedTexture(Engine.AssetLoader.Get <TextureAsset>("Images/spritesheetAnimation.png"), new Vector2(50, 50), AnimationLoopType.Normal, 500, 0, 3);
            // Test constructor without starting-ending frame as well. It should set the starting and ending frames to 0-3 as well.
            var noLoop            = new AnimatedTexture(Engine.AssetLoader.Get <TextureAsset>("Images/spritesheetAnimation.png"), 2, 2, AnimationLoopType.None, 500);
            var normalThenReverse = new AnimatedTexture(Engine.AssetLoader.Get <TextureAsset>("Images/spritesheetAnimation.png"), new Vector2(50, 50), AnimationLoopType.NormalThenReverse, 500,
                                                        0, 3);
            var noLoopReverse = new AnimatedTexture(Engine.AssetLoader.Get <TextureAsset>("Images/spritesheetAnimation.png"), new Vector2(50, 50), AnimationLoopType.NoneReverse, 500, 0, 3);
            var reverseLoop   = new AnimatedTexture(Engine.AssetLoader.Get <TextureAsset>("Images/spritesheetAnimation.png"), new Vector2(50, 50), AnimationLoopType.Reverse, 500);

            void DrawFrame(Action end)
            {
                // ReSharper disable AccessToModifiedClosure
                Runner.ExecuteAsLoop(_ =>
                {
                    RenderComposer composer = Engine.Renderer.StartFrame();

                    composer.RenderSprite(new Vector3(10, 10, 0), new Vector2(100, 100), Color.White, normalLoop.Texture, normalLoop.CurrentFrame);
                    composer.RenderSprite(new Vector3(115, 10, 0), new Vector2(100, 100), Color.White, noLoop.Texture, noLoop.CurrentFrame);
                    composer.RenderSprite(new Vector3(220, 10, 0), new Vector2(100, 100), Color.White, normalThenReverse.Texture, normalThenReverse.CurrentFrame);
                    composer.RenderSprite(new Vector3(325, 10, 0), new Vector2(100, 100), Color.White, noLoopReverse.Texture, noLoopReverse.CurrentFrame);
                    composer.RenderSprite(new Vector3(430, 10, 0), new Vector2(100, 100), Color.White, reverseLoop.Texture, reverseLoop.CurrentFrame);

                    Engine.Renderer.EndFrame();

                    end();
                }).WaitOne();
                // ReSharper enable AccessToModifiedClosure
            }

            // The function which advances time for the animation.
            void AdvanceAnimation(float time)
            {
                noLoop.Update(time);
                normalLoop.Update(time);
                normalThenReverse.Update(time);
                noLoopReverse.Update(time);
                reverseLoop.Update(time);
            }

            // Assert the objects are as expected.
            Assert.Equal(3, normalLoop.AnimationFrames);
            Assert.Equal(0, normalLoop.StartingFrame);
            Assert.Equal(3, normalLoop.EndingFrame);
            Assert.Equal(500, normalLoop.TimeBetweenFrames);

            Assert.Equal(3, noLoop.AnimationFrames);
            Assert.Equal(0, noLoop.StartingFrame);
            Assert.Equal(3, noLoop.EndingFrame);
            Assert.Equal(500, noLoop.TimeBetweenFrames);

            Assert.Equal(3, normalThenReverse.AnimationFrames);
            Assert.Equal(0, normalThenReverse.StartingFrame);
            Assert.Equal(3, normalThenReverse.EndingFrame);
            Assert.Equal(500, normalThenReverse.TimeBetweenFrames);

            Assert.Equal(3, noLoopReverse.AnimationFrames);
            Assert.Equal(0, noLoopReverse.StartingFrame);
            Assert.Equal(3, noLoopReverse.EndingFrame);
            Assert.Equal(500, noLoopReverse.TimeBetweenFrames);

            Assert.Equal(3, reverseLoop.AnimationFrames);
            Assert.Equal(0, reverseLoop.StartingFrame);
            Assert.Equal(3, reverseLoop.EndingFrame);
            Assert.Equal(500, reverseLoop.TimeBetweenFrames);

            // Capture starting frames.
            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest1); });
            Assert.Equal(0, normalLoop.CurrentFrameIndex);
            Assert.Equal(0, noLoop.CurrentFrameIndex);
            Assert.Equal(0, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(3, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(3, reverseLoop.CurrentFrameIndex);

            // Move 500 ms into the future, as the frames are specified to change every 500ms.
            AdvanceAnimation(500);

            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest2); });
            Assert.Equal(1, normalLoop.CurrentFrameIndex);
            Assert.Equal(1, noLoop.CurrentFrameIndex);
            Assert.Equal(1, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(2, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(2, reverseLoop.CurrentFrameIndex);

            // Move 500 ms into the future, as the frames are specified to change every 500ms.
            AdvanceAnimation(500);

            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest3); });
            Assert.Equal(2, normalLoop.CurrentFrameIndex);
            Assert.Equal(2, noLoop.CurrentFrameIndex);
            Assert.Equal(2, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(1, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(1, reverseLoop.CurrentFrameIndex);

            // Move 500 ms into the future, as the frames are specified to change every 500ms.
            AdvanceAnimation(500);

            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest4); });
            Assert.Equal(3, normalLoop.CurrentFrameIndex);
            Assert.Equal(3, noLoop.CurrentFrameIndex);
            Assert.Equal(3, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(0, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(0, reverseLoop.CurrentFrameIndex);

            // Move 500 ms into the future, as the frames are specified to change every 500ms.
            AdvanceAnimation(500);

            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest5); });
            Assert.Equal(0, normalLoop.CurrentFrameIndex);
            Assert.Equal(3, noLoop.CurrentFrameIndex);
            Assert.Equal(2, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(0, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(3, reverseLoop.CurrentFrameIndex);

            // Move 500 ms into the future, as the frames are specified to change every 500ms.
            AdvanceAnimation(500);

            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest6); });
            Assert.Equal(1, normalLoop.CurrentFrameIndex);
            Assert.Equal(3, noLoop.CurrentFrameIndex);
            Assert.Equal(1, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(0, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(2, reverseLoop.CurrentFrameIndex);

            AdvanceAnimation(500);
            Assert.Equal(0, normalThenReverse.CurrentFrameIndex);

            // Ensure objects are as expected.
            Assert.Equal(3, normalLoop.AnimationFrames);
            Assert.Equal(0, normalLoop.StartingFrame);
            Assert.Equal(3, normalLoop.EndingFrame);
            Assert.Equal(500, normalLoop.TimeBetweenFrames);
            Assert.Equal(1, normalLoop.LoopCount);

            Assert.Equal(3, noLoop.AnimationFrames);
            Assert.Equal(0, noLoop.StartingFrame);
            Assert.Equal(3, noLoop.EndingFrame);
            Assert.Equal(500, noLoop.TimeBetweenFrames);
            Assert.Equal(1, noLoop.LoopCount);

            Assert.Equal(3, normalThenReverse.AnimationFrames);
            Assert.Equal(0, normalThenReverse.StartingFrame);
            Assert.Equal(3, normalThenReverse.EndingFrame);
            Assert.Equal(500, normalThenReverse.TimeBetweenFrames);
            Assert.Equal(1, normalThenReverse.LoopCount);

            Assert.Equal(3, noLoopReverse.AnimationFrames);
            Assert.Equal(0, noLoopReverse.StartingFrame);
            Assert.Equal(3, noLoopReverse.EndingFrame);
            Assert.Equal(500, noLoopReverse.TimeBetweenFrames);
            Assert.Equal(1, noLoopReverse.LoopCount);

            Assert.Equal(3, reverseLoop.AnimationFrames);
            Assert.Equal(0, reverseLoop.StartingFrame);
            Assert.Equal(3, reverseLoop.EndingFrame);
            Assert.Equal(500, reverseLoop.TimeBetweenFrames);
            Assert.Equal(1, reverseLoop.LoopCount);

            // Reset
            normalLoop.Reset();
            noLoop.Reset();
            normalThenReverse.Reset();
            noLoopReverse.Reset();
            reverseLoop.Reset();

            // Ensure starting frame is reset.
            Assert.Equal(0, normalLoop.CurrentFrameIndex);
            Assert.Equal(0, noLoop.CurrentFrameIndex);
            Assert.Equal(0, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(3, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(3, reverseLoop.CurrentFrameIndex);

            // Ensure objects are as expected.
            Assert.Equal(3, normalLoop.AnimationFrames);
            Assert.Equal(0, normalLoop.StartingFrame);
            Assert.Equal(3, normalLoop.EndingFrame);
            Assert.Equal(500, normalLoop.TimeBetweenFrames);
            Assert.Equal(0, normalLoop.LoopCount);

            Assert.Equal(3, noLoop.AnimationFrames);
            Assert.Equal(0, noLoop.StartingFrame);
            Assert.Equal(3, noLoop.EndingFrame);
            Assert.Equal(500, noLoop.TimeBetweenFrames);
            Assert.Equal(0, noLoop.LoopCount);

            Assert.Equal(3, normalThenReverse.AnimationFrames);
            Assert.Equal(0, normalThenReverse.StartingFrame);
            Assert.Equal(3, normalThenReverse.EndingFrame);
            Assert.Equal(500, normalThenReverse.TimeBetweenFrames);
            Assert.Equal(0, normalThenReverse.LoopCount);

            Assert.Equal(3, noLoopReverse.AnimationFrames);
            Assert.Equal(0, noLoopReverse.StartingFrame);
            Assert.Equal(3, noLoopReverse.EndingFrame);
            Assert.Equal(500, noLoopReverse.TimeBetweenFrames);
            Assert.Equal(0, noLoopReverse.LoopCount);

            Assert.Equal(3, reverseLoop.AnimationFrames);
            Assert.Equal(0, reverseLoop.StartingFrame);
            Assert.Equal(3, reverseLoop.EndingFrame);
            Assert.Equal(500, reverseLoop.TimeBetweenFrames);
            Assert.Equal(0, reverseLoop.LoopCount);

            // Check if matching starting capture.
            DrawFrame(() => { Runner.VerifyScreenshot(ResultDb.AnimatedTextureTest1); });
            Assert.Equal(0, normalLoop.CurrentFrameIndex);
            Assert.Equal(0, noLoop.CurrentFrameIndex);
            Assert.Equal(0, normalThenReverse.CurrentFrameIndex);
            Assert.Equal(3, noLoopReverse.CurrentFrameIndex);
            Assert.Equal(3, reverseLoop.CurrentFrameIndex);

            normalLoop        = null;
            noLoop            = null;
            normalThenReverse = null;
            noLoopReverse     = null;
            reverseLoop       = null;
            Engine.AssetLoader.Destroy("Images/spritesheetAnimation.png");
        }
Esempio n. 3
0
 public override void Reset()
 {
     base.Reset();
     player.Velocity = new Vec2(player.Velocity.X, player.JumpForce);
     tex.Reset();
 }
Esempio n. 4
0
        private void RenderCurrentAnimationSettings()
        {
            if (_animController == null)
            {
                if (ImGui.Button("Create Controller"))
                {
                    _animController = new AnimationController(_animation);
                    _animController.AddAnimation(new AnimationNode("Default")
                    {
                        EndingFrame   = _animation.EndingFrame,
                        StartingFrame = _animation.StartingFrame,
                        LoopType      = _animation.LoopType
                    });
                }

                int frameTime = _animation.TimeBetweenFrames;
                ImGui.InputInt("MS Between Frames", ref frameTime);
                if (frameTime != _animation.TimeBetweenFrames)
                {
                    _animation.TimeBetweenFrames = frameTime;
                }

                int startingFrame = _animation.StartingFrame;
                ImGui.InputInt("Starting Frame", ref startingFrame);
                if (startingFrame != _animation.StartingFrame)
                {
                    _animation.StartingFrame = startingFrame;
                }

                int endingFrame = _animation.EndingFrame;
                ImGui.InputInt("Ending Frame", ref endingFrame);
                if (endingFrame != _animation.EndingFrame)
                {
                    _animation.EndingFrame = endingFrame;
                }

                var loopType = (int)_animation.LoopType;
                ImGui.Combo("Loop Type", ref loopType, string.Join('\0', Enum.GetNames(typeof(AnimationLoopType))));
                if ((AnimationLoopType)loopType != _animation.LoopType)
                {
                    _animation.LoopType = (AnimationLoopType)loopType;
                    _animation.Reset();
                }
            }
            else
            {
                ImGui.Text("Animations");
                ImGui.PushID("animList");
                foreach (AnimationNode n in _animController.Animations.Values)
                {
                    if (n == _animController.CurrentAnimation)
                    {
                        ImGui.Text(n.Name);
                    }
                    else
                    {
                        if (ImGui.Button(n.Name))
                        {
                            _animController.SetAnimation(n.Name);
                        }
                    }
                }

                if (ImGui.Button("Create") && _animController.Animations.All(x => x.Key != "NewAnim"))
                {
                    var newNode = new AnimationNode("NewAnim");
                    _animController.AddAnimation(newNode);
                }

                ImGui.SameLine();
                if (_animController.CurrentAnimation == null)
                {
                    ImGui.PopID();
                    return;
                }

                if (ImGui.Button("Remove"))
                {
                    _animController.RemoveAnimation(_animController.CurrentAnimation.Name);
                }
                ImGui.SameLine();
                if (ImGui.Button("Rename"))
                {
                    var newName = new StringInputModal(s =>
                    {
                        _animController.CurrentAnimation.Name = s;
                        _animController.Reindex();
                    },
                                                       $"New name for {_animController.CurrentAnimation.Name}");
                    Parent.AddWindow(newName);
                }

                ImGui.PopID();

                AnimationNode cur      = _animController.CurrentAnimation;
                var           modified = false;

                int frameTime = cur.TimeBetweenFrames;
                ImGui.InputInt("MS Between Frames", ref frameTime);
                if (frameTime != cur.TimeBetweenFrames)
                {
                    cur.TimeBetweenFrames = frameTime;
                    modified = true;
                }

                int startingFrame = cur.StartingFrame;
                ImGui.InputInt("Starting Frame", ref startingFrame);
                if (startingFrame != cur.StartingFrame)
                {
                    cur.StartingFrame = startingFrame;
                    modified          = true;
                }

                int endingFrame = cur.EndingFrame;
                ImGui.InputInt("Ending Frame", ref endingFrame);
                if (endingFrame != cur.EndingFrame)
                {
                    cur.EndingFrame = endingFrame;
                    modified        = true;
                }

                var loopType = (int)cur.LoopType;
                ImGui.Combo("Loop Type", ref loopType, string.Join('\0', Enum.GetNames(typeof(AnimationLoopType))));
                if ((AnimationLoopType)loopType != cur.LoopType)
                {
                    cur.LoopType = (AnimationLoopType)loopType;
                    modified     = true;
                }

                if (modified)
                {
                    _animController.SetAnimation(cur.Name);
                }
            }
        }