Skip to content

Commit

Permalink
+ Fix sprite missing in some case.
Browse files Browse the repository at this point in the history
  • Loading branch information
LancerComet committed Jun 18, 2019
1 parent 383dc6b commit 1adb941
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 63 deletions.
5 changes: 4 additions & 1 deletion Svga/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -40,7 +41,9 @@ private async void SelectFileAndInitPlayer () {
player.LoadSvgaFileData(stream.AsStream());
player.InitStage();
player.Play();
player.OnLoopFinish += () => { this.IsShowDoneText = true; };
player.OnLoopFinish += () => {
this.IsShowDoneText = true;
};
}
}
}
Expand Down
44 changes: 25 additions & 19 deletions Svga/SvgaPlayer/Controls/SvgaPlayer.Draw.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System;
using System.Numerics;
using Windows.Foundation;
using Microsoft.Graphics.Canvas;
using Svga.SvgaPlayer.Models;
Expand All @@ -12,30 +13,35 @@ public partial class SvgaPlayer {
/// <param name="sprite"></param>
private void DrawSingleSprite (CanvasDrawingSession session, SvgaSprite sprite) {
var currentFrame = sprite.Frames[this.CurrentFrame];
if (currentFrame != null) {
var perspective = new Matrix4x4(); // Sprite 透视参数.
var transform = currentFrame.Transform;
if (transform != null) {
perspective = new Matrix4x4(new Matrix3x2(
transform.A, transform.B, transform.C, transform.D, transform.Tx, transform.Ty
));
}
if (currentFrame == null) {
return;
}

var width = 0f;
var height = 0f;
var alpha = currentFrame.Alpha;
var layout = currentFrame.Layout;
if (layout != null) {
width = layout.Width;
height = layout.Height;
}

var width = 0f;
var height = 0f;
var layout = currentFrame.Layout;
if (layout != null) {
width = layout.Width;
height = layout.Height;
}
var transform = currentFrame.Transform;
if (transform != null) {
// Sprite 透视参数.
var perspective = new Matrix4x4(new Matrix3x2(
transform.A, transform.B, transform.C, transform.D, transform.Tx, transform.Ty
));

var alpha = currentFrame.Alpha;

session.DrawImage(
sprite.CanvasBitmap, 0, 0, new Rect(0, 0, width, height), alpha,
CanvasImageInterpolation.Linear, perspective
);
sprite.CurrentFrame++;
} else {
session.DrawImage(
sprite.CanvasBitmap, 0, 0, new Rect(0, 0, width, height), alpha,
CanvasImageInterpolation.Linear
);
}
}
}
Expand Down
36 changes: 13 additions & 23 deletions Svga/SvgaPlayer/Controls/SvgaPlayer.Stage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public int Fps {
/// <summary>
/// 动画总帧数.
/// </summary>
private int TotalFrame => this.MovieParams.Frames;
private int TotalFrame => this.MovieParams?.Frames ?? 0;

/// <summary>
/// 当前播放帧.
/// </summary>
private int CurrentFrame { get; set; }
public int CurrentFrame { get; set; }

/// <summary>
/// CanvasControl 对象.
Expand All @@ -63,12 +63,7 @@ public int Fps {
/// </summary>
private StageResource StageResource { get; set; }

/// <summary>
/// Stage OnCreateResource 事件.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private async void StageOnCreateResources (CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) {
private async void InitStageResource () {
if (this.IsResourceReady) {
return;
}
Expand All @@ -91,6 +86,14 @@ private async void StageOnCreateResources (CanvasAnimatedControl sender, CanvasC
this.IsResourceReady = true;
}

/// <summary>
/// Stage OnCreateResource 事件.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void StageOnCreateResources (CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) {
}

/// <summary>
/// Stage OnUpdate 事件.
/// 用于更新舞台数据, 一般在运行一次 Update 后运行一次 Draw 事件.
Expand All @@ -107,7 +110,7 @@ private void StageOnUpdate (ICanvasAnimatedControl sender, CanvasAnimatedUpdateE
/// <param name="sender"></param>
/// <param name="args"></param>
private void StageOnDraw (ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) {
if (!this.IsInPlay || !this.IsResourceReady) {
if (!this.IsInPlay || !this.IsResourceReady || !this.IsStageInited) {
return;
}

Expand Down Expand Up @@ -166,10 +169,7 @@ public void InitStage () {
}
stage.TargetElapsedTime = TimeSpan.FromMilliseconds(1000d / fps);

stage.CreateResources += this.StageOnCreateResources;
stage.Update += this.StageOnUpdate;
stage.Draw += this.StageOnDraw;

this.InitStageResource();
this.IsStageInited = true;
}

Expand Down Expand Up @@ -199,16 +199,6 @@ public void UnloadStage () {
this.StageResource = null;
this.InflatedBytes = null;
this.MovieEntity = null;
this.UnloadStageEvents();
}

/// <summary>
/// 卸载舞台事件.
/// </summary>
private void UnloadStageEvents () {
this.Stage.CreateResources -= this.StageOnCreateResources;
this.Stage.Update -= this.StageOnUpdate;
this.Stage.Draw -= this.StageOnDraw;
}
}
}
10 changes: 9 additions & 1 deletion Svga/SvgaPlayer/Controls/SvgaPlayer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
d:DesignHeight="300"
d:DesignWidth="400"
xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml"
Loaded="OnLoaded"
Unloaded="OnUnloaded"
>
<win2d:CanvasAnimatedControl x:Name="Canvas" Paused="True"></win2d:CanvasAnimatedControl>
<win2d:CanvasAnimatedControl
x:Name="Canvas"
Paused="True"
Update="StageOnUpdate"
Draw="StageOnDraw"
CreateResources="StageOnCreateResources"
></win2d:CanvasAnimatedControl>
</UserControl>
7 changes: 2 additions & 5 deletions Svga/SvgaPlayer/Controls/SvgaPlayer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class SvgaPlayer : UserControl {
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnLoaded (object sender, RoutedEventArgs e) {
// ...
}

/// <summary>
Expand All @@ -24,15 +25,11 @@ private void OnLoaded (object sender, RoutedEventArgs e) {
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnUnloaded (object sender, RoutedEventArgs e) {
this.UnloadStageEvents();
this.Loaded -= this.OnLoaded;
this.Unloaded -= this.OnUnloaded;
// ...
}

public SvgaPlayer () {
this.InitializeComponent();
this.Loaded += this.OnLoaded;
this.Unloaded += this.OnUnloaded;
}
}
}
14 changes: 0 additions & 14 deletions Svga/SvgaPlayer/Models/SvgaPlayer.Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@ public class SvgaSprite {
/// </summary>
public int TotalFrames => this.Frames.Count;

/// <summary>
/// 当前绘制帧下标.
/// </summary>
private int _currentFrame;
public int CurrentFrame {
get => this._currentFrame;
set {
if (value > this.TotalFrames - 1) {
value = 0;
}
this._currentFrame = value;
}
}

/// <summary>
/// 初始化 CanvasBitmap.
/// </summary>
Expand Down

0 comments on commit 1adb941

Please sign in to comment.