Skip to content

Commit

Permalink
+ Add more info to interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
LancerComet committed Jun 26, 2019
1 parent 1adb941 commit f1efa82
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 214 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 若要了解有关 .editorconfig 的详细信息,请参阅 https://aka.ms/editorconfigdocs

# All files
[*]
indent_style = space
indent_size = 2
9 changes: 7 additions & 2 deletions Svga.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
# Visual Studio Version 16
VisualStudioVersion = 16.0.29001.49
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Svga", "Svga\Svga.csproj", "{F0F7B6EC-3DAF-4D81-AA3C-D4EEB2508943}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{90C0113A-9866-4EEF-AB49-2C146A0B57C6}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Expand Down
49 changes: 45 additions & 4 deletions Svga/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
>
<Grid>
<Viewbox>
<controls:SvgaPlayer x:Name="Player"></controls:SvgaPlayer>
</Viewbox>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<StackPanel
Grid.Row="0"
Orientation="Horizontal"
>
<Button Content="Select Svga File" Click="OnSelectFileClick"></Button>
<Button Click="OnPlayClick">
<SymbolIcon Symbol="Play"></SymbolIcon>
Expand All @@ -28,5 +32,42 @@
Visibility="{x:Bind IsShowDoneText, Mode=OneWay}"
></TextBlock>
</StackPanel>

<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

<ScrollViewer Grid.Column="0">
<StackPanel Padding="10">
<StackPanel Orientation="Horizontal">
<TextBlock Text="尺寸:"></TextBlock>
<TextBlock Text="{x:Bind Player.StageWidth, Mode=OneWay}">--</TextBlock>
<TextBlock Text="x"></TextBlock>
<TextBlock Text="{x:Bind Player.StageHeight, Mode=OneWay}">--</TextBlock>
</StackPanel>

<StackPanel Orientation="Horizontal">
<TextBlock Text="Sprite 数量:"></TextBlock>
<TextBlock Text="{x:Bind Player.SpriteCount, Mode=OneWay}">--</TextBlock>
</StackPanel>

<StackPanel Orientation="Horizontal">
<TextBlock Text="总帧数:"></TextBlock>
<TextBlock Text="{x:Bind TotalFrame, Mode=OneWay}">--</TextBlock>
</StackPanel>

<StackPanel Orientation="Horizontal">
<TextBlock Text="当前帧:"></TextBlock>
<TextBlock Text="{x:Bind Player.CurrentFrame, Mode=OneWay}">--</TextBlock>
</StackPanel>
</StackPanel>
</ScrollViewer>

<Viewbox Grid.Column="1">
<controls:SvgaPlayer x:Name="Player"></controls:SvgaPlayer>
</Viewbox>
</Grid>
</Grid>
</Page>
34 changes: 20 additions & 14 deletions Svga/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
Expand All @@ -11,6 +10,9 @@

namespace Svga {
public sealed partial class MainPage : Page, INotifyPropertyChanged {
private int SpriteCount => 0;
private int TotalFrame => this.Player?.TotalFrame ?? 0;

public MainPage () {
this.InitializeComponent();
}
Expand All @@ -32,20 +34,24 @@ private async void SelectFileAndInitPlayer () {
picker.FileTypeFilter.Add(".svga");

var file = await picker.PickSingleFileAsync();
if (file != null) {
this.IsShowDoneText = false;
using (var stream = await file.OpenAsync(FileAccessMode.Read, StorageOpenOptions.AllowOnlyReaders)) {
var player = this.Player;
player.UnloadStage();
// player.LoopCount = 1;
player.LoadSvgaFileData(stream.AsStream());
player.InitStage();
player.Play();
player.OnLoopFinish += () => {
this.IsShowDoneText = true;
};
}
if (file == null) {
return;
}

this.IsShowDoneText = false;
using (var stream = await file.OpenAsync(FileAccessMode.Read, StorageOpenOptions.AllowOnlyReaders)) {
var player = this.Player;
player.UnloadStage();
player.LoadSvgaFileData(stream.AsStream());
player.InitStage();
player.Play();
player.OnLoopFinish += () => {
this.IsShowDoneText = true;
};
}

this.Notify(nameof(this.SpriteCount));
this.Notify(nameof(this.TotalFrame));
}

private void OnSelectFileClick (object sender, RoutedEventArgs e) {
Expand Down
11 changes: 9 additions & 2 deletions Svga/Svga.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WindowsXamlEnableOverview>true</WindowsXamlEnableOverview>
<PackageCertificateKeyFile>Svga_TemporaryKey.pfx</PackageCertificateKeyFile>
<PackageCertificateKeyFile>
</PackageCertificateKeyFile>
<GenerateAppInstallerFile>False</GenerateAppInstallerFile>
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
<AppxBundle>Always</AppxBundle>
<AppxBundlePlatforms>x64</AppxBundlePlatforms>
<PackageCertificateThumbprint>43B4EC126AF9DEE491B1B0BE9DEE1AE70E4FDA89</PackageCertificateThumbprint>
<AppInstallerUpdateFrequency>1</AppInstallerUpdateFrequency>
<AppInstallerCheckForUpdateFrequency>OnApplicationRun</AppInstallerCheckForUpdateFrequency>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -120,7 +128,6 @@
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\Annotations.cs" />
<Compile Include="SvgaPlayer\Controls\SvgaPlayer.Draw.cs" />
<Compile Include="SvgaPlayer\Controls\SvgaPlayer.xaml.cs">
<DependentUpon>SvgaPlayer.xaml</DependentUpon>
</Compile>
Expand Down
135 changes: 110 additions & 25 deletions Svga/SvgaPlayer/Controls/SvgaPlayer.Data.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,128 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Windows.UI.Core;
using Com.Opensource.Svga;
using Google.Protobuf;
using Google.Protobuf.Collections;

namespace Svga.SvgaPlayer.Controls {
public partial class SvgaPlayer {
/// <summary>
/// 当前已播放次数.
/// </summary>
private int PlayedCount { get; set; }

/// <summary>
/// SVGA 文件原始二进制.
/// </summary>
private byte[] InflatedBytes { get; set; }
private byte[] _inflatedBytes;

/// <summary>
/// MovieEntity 对象.
/// SVGA 的所有数据将从本对象中读取.
/// </summary>
private MovieEntity MovieEntity { get; set; }
private MovieEntity _movieEntity;

/// <summary>
/// 从 MovieEntity 中获取 SVGA 图片.
/// MovieEntity 的 Images 属性下保存了 SVGA 的图片文件, 为一个可枚举的 byteString 集合,
/// 键名为图片名称, 键值为 PNG 的二进制数据.
/// </summary>
private MapField<string, ByteString> Images => this.MovieEntity?.Images;
private MapField<string, ByteString> _images;

/// <summary>
/// SVGA 配置参数.
/// </summary>
private MovieParams MovieParams => this.MovieEntity?.Params;
private MovieParams _movieParams;

/// <summary>
/// SVGA Sprite Entity 列表.
/// </summary>
private List<SpriteEntity> _sprites;

/// <summary>
/// Sprite 数量.
/// </summary>
private int _spriteCount;
public int SpriteCount {
get => this._spriteCount;
set {
this._spriteCount = value;
this.Notify(nameof(this.SpriteCount));
}
}

/// <summary>
/// 播放循环次数, 默认为 0.
/// 当为 0 时代表无限循环播放.
/// </summary>
public int LoopCount { get; set; }

/// <summary>
/// 当前播放帧.
/// </summary>
private int _currentFrame;
public int CurrentFrame {
get => this._currentFrame;
private set {
this._currentFrame = value;
this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => {
this.Notify(nameof(this.CurrentFrame));
});
}
}

/// <summary>
/// 是否处于播放状态.
/// </summary>
public bool IsInPlay => this.Stage.Paused == false;

/// <summary>
/// SVGA Sprite 对象.
/// 动画总帧数.
/// </summary>
private RepeatedField<SpriteEntity> Sprites => this.MovieEntity?.Sprites;
private int _totalFrame;
public int TotalFrame {
get => this._totalFrame;
set {
this._totalFrame = value;
this.Notify(nameof(this.TotalFrame));
}
}

/// <summary>
/// 目标播放帧率.
/// 若不设置或设置为 0 时使用默认帧率, 设置后将使用自定义帧率.
/// </summary>
private int _fps;
public int Fps {
get => this._fps;
set {
if (value < 0) { value = 0; }
this._fps = value;
this.Notify(nameof(this.Fps));
}
}

/// <summary>
/// 画布宽度.
/// </summary>
private float _stageWidth;
public float StageWidth {
get => this._stageWidth;
set {
this._stageWidth = value;
this.Notify(nameof(this.StageWidth));
}
}

/// <summary>
/// 画布高度.
/// </summary>
private float _stageHeight;
public float StageHeight {
get => this._stageHeight;
set {
this._stageHeight = value;
this.Notify(nameof(this.StageHeight));
}
}

/// <summary>
/// Inflate SVGA 文件, 获取其原始数据.
Expand All @@ -59,26 +143,27 @@ private void InflateSvgaFile (Stream svgaFileBuffer) {
}
}

this.InflatedBytes = inflatedBytes;
this._inflatedBytes = inflatedBytes;
}

/// <summary>
/// 通过 Infalte 数据获取 SVGA 的 MovieEntity.
/// 通过 Inflate 数据获取 SVGA 的 MovieEntity.
/// </summary>
/// <param name="inflatedBytes"></param>
private void GetMovieEntity () {
if (this.InflatedBytes != null) {
this.MovieEntity = MovieEntity.Parser.ParseFrom(this.InflatedBytes);
private void InitMovieEntity () {
if (this._inflatedBytes == null) {
return;
}
}

/// <summary>
/// 载入 SVGA 文件数据.
/// </summary>
/// <param name="svgaFileBuffer">SVGA 文件二进制 Stream.</param>
public void LoadSvgaFileData (Stream svgaFileBuffer) {
this.InflateSvgaFile(svgaFileBuffer);
this.GetMovieEntity();
var moveEntity = MovieEntity.Parser.ParseFrom(this._inflatedBytes);
this._movieEntity = moveEntity;
this._movieParams = moveEntity.Params;
this._images = moveEntity.Images;
this._sprites = moveEntity.Sprites.ToList();
this.TotalFrame = moveEntity.Params.Frames;
this.SpriteCount = this._sprites.Count;
this.StageWidth = this._movieParams.ViewBoxWidth;
this.StageHeight = this._movieParams.ViewBoxHeight;
}
}
}
48 changes: 0 additions & 48 deletions Svga/SvgaPlayer/Controls/SvgaPlayer.Draw.cs

This file was deleted.

Loading

0 comments on commit f1efa82

Please sign in to comment.