Skip to content

Commit

Permalink
加载优化
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisYounger committed Feb 22, 2023
1 parent 232850b commit 112a633
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 92 deletions.
3 changes: 2 additions & 1 deletion VPet-Simulator.Core/Display/Main.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public partial class Main : ContentControlX, IDisposable
public event Action<Main> TimeUIHandle;
public Main(GameCore core, bool loadtouchevent = true)
{
Console.WriteLine(DateTime.Now.ToString("T:fff"));
InitializeComponent();
Core = core;

Expand All @@ -64,7 +65,7 @@ public Main(GameCore core, bool loadtouchevent = true)
var ig2 = Core.Graph.FindGraph(GraphCore.GraphType.Touch_Head_A_Start, core.Save.Mode);
PetGrid2.Child = ig2.This; //用于缓存
PetGrid2.Visibility = Visibility.Collapsed;
ig.Run(DisplayNomal);
ig.WaitForReadyRun(DisplayNomal);


EventTimer.Elapsed += EventTimer_Elapsed;
Expand Down
4 changes: 2 additions & 2 deletions VPet-Simulator.Core/Display/MainDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public void DisplayTouchBody()
ig2.IsContinue = true;
return;
}

Core.Graph.RndGraph.Clear();
Display(GraphCore.GraphType.Touch_Body_A_Start, () =>
Display(GraphCore.GraphType.Touch_Body_B_Loop, () =>
Display(GraphCore.GraphType.Touch_Body_C_End, DisplayNomal
)));
, true), true), true);
}
/// <summary>
/// 显示待机(模式1)情况
Expand Down
6 changes: 5 additions & 1 deletion VPet-Simulator.Core/Graph/IGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ public interface IGraph
/// 是否继续播放
/// </summary>
bool IsContinue { get; set; }
/// <summary>
/// 从0开始运行该动画, 等待部署完成后执行
/// </summary>
void WaitForReadyRun(Action EndAction = null);
///// <summary>//经过测试,储存到内存好处多多,不储存也要占用很多内存,干脆存了吧
///// 是否储存到内存以支持快速显示
///// </summary>
//bool StoreMemory { get; }
/// <summary>
/// 该动画UI状态
/// </summary>
Save.ModeType ModeType { get;}
Save.ModeType ModeType { get; }
/// <summary>
/// 该动画UI状态
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions VPet-Simulator.Core/Graph/PNGAnimation.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Grid x:Class="VPet_Simulator.Core.PNGAnimation"
<Image x:Class="VPet_Simulator.Core.PNGAnimation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:VPet_Simulator.Core"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500">
</Grid>
</Image>
116 changes: 62 additions & 54 deletions VPet-Simulator.Core/Graph/PNGAnimation.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
using LinePutScript;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView;
using Panuon.WPF.UI;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

namespace VPet_Simulator.Core
{
/// <summary>
/// PNGAnimation.xaml 的交互逻辑
/// </summary>
public partial class PNGAnimation : Grid, IGraph
public partial class PNGAnimation : System.Windows.Controls.Image, IGraph
{
/// <summary>
/// 所有动画帧
Expand Down Expand Up @@ -61,6 +62,10 @@ public partial class PNGAnimation : Grid, IGraph

public GraphCore.GraphType GraphType { get; private set; }
/// <summary>
/// 是否准备完成
/// </summary>
public bool IsReady { get; private set; } = false;
/// <summary>
/// 动画停止时运行的方法
/// </summary>
private Action StopAction;
Expand All @@ -79,59 +84,7 @@ public PNGAnimation(string path, FileInfo[] paths, Save.ModeType modetype, Graph
//StoreMemory = storemem;
GraphType = graphtype;
ModeType = modetype;
//新方法:加载大图片
//生成大文件加载非常慢,先看看有没有缓存能用
string cp = GraphCore.CachePath + $"\\{Sub.GetHashCode(path)}_{paths.Length}.png";
if (File.Exists(cp))
{
var img = new System.Windows.Controls.Image()
{
HorizontalAlignment = HorizontalAlignment.Left,
Width = 500 * (paths.Length + 1),
Height = 500
};
Children.Add(img);
for (int i = 0; i < paths.Length; i++)
{
FileInfo file = paths[i];
int time = int.Parse(file.Name.Split('.').Reverse().ToArray()[1].Split('_').Last());
int wxi = -500 * i;
Animations.Add(new Animation(this, time, () => img.Margin = new Thickness(wxi, 0, 0, 0)));
}
img.Source = new BitmapImage(new Uri(cp));
}
else
{
List<System.Drawing.Image> imgs = new List<System.Drawing.Image>();
foreach (var file in paths)
imgs.Add(System.Drawing.Image.FromFile(file.FullName));
int w = imgs[0].Width;
int h = imgs[0].Height;
Bitmap joinedBitmap = new Bitmap(w * paths.Length, h);
var graph = Graphics.FromImage(joinedBitmap);
var img = new System.Windows.Controls.Image()
{
HorizontalAlignment = HorizontalAlignment.Left,
Width = 500 * (paths.Length + 1),
Height = 500
};
Children.Add(img);
for (int i = 0; i < paths.Length; i++)
{
FileInfo file = paths[i];
int time = int.Parse(file.Name.Split('.').Reverse().ToArray()[1].Split('_').Last());
graph.DrawImage(imgs[i], w * i, 0, w, h);
int wxi = -500 * i;
Animations.Add(new Animation(this, time, () => img.Margin = new Thickness(wxi, 0, 0, 0)));
}
joinedBitmap.Save(cp);
graph.Dispose();
joinedBitmap.Dispose();
imgs.ForEach(x => x.Dispose());
img.Source = new BitmapImage(new Uri(cp));
}


Task.Run(() => startup(path, paths));
//if (storemem)
//foreach (var file in paths)
//{
Expand Down Expand Up @@ -196,6 +149,50 @@ public PNGAnimation(string path, FileInfo[] paths, Save.ModeType modetype, Graph
// , () => imgs[last % 3].Visibility = Visibility.Hidden));
//}
}
private void startup(string path, FileInfo[] paths)
{
//新方法:加载大图片
//生成大文件加载非常慢,先看看有没有缓存能用
string cp = GraphCore.CachePath + $"\\{Sub.GetHashCode(path)}_{paths.Length}.png";
Dispatcher.Invoke(() => Width = 500 * (paths.Length + 1));
if (File.Exists(cp))
{
for (int i = 0; i < paths.Length; i++)
{
FileInfo file = paths[i];
int time = int.Parse(file.Name.Split('.').Reverse().ToArray()[1].Split('_').Last());
int wxi = -500 * i;
Animations.Add(new Animation(this, time, () => Margin = new Thickness(wxi, 0, 0, 0)));
}
Dispatcher.Invoke(() => Source = new BitmapImage(new Uri(cp)));
IsReady = true;
}
else
{
List<System.Drawing.Image> imgs = new List<System.Drawing.Image>();
foreach (var file in paths)
imgs.Add(System.Drawing.Image.FromFile(file.FullName));
int w = imgs[0].Width;
int h = imgs[0].Height;
Bitmap joinedBitmap = new Bitmap(w * paths.Length, h);
var graph = Graphics.FromImage(joinedBitmap);
for (int i = 0; i < paths.Length; i++)
{
FileInfo file = paths[i];
int time = int.Parse(file.Name.Split('.').Reverse().ToArray()[1].Split('_').Last());
graph.DrawImage(imgs[i], w * i, 0, w, h);
int wxi = -500 * i;
Animations.Add(new Animation(this, time, () => Margin = new Thickness(wxi, 0, 0, 0)));
}
joinedBitmap.Save(cp);
graph.Dispose();
joinedBitmap.Dispose();
imgs.ForEach(x => x.Dispose());
Dispatcher.Invoke(() => Source = new BitmapImage(new Uri(cp)));
IsReady = true;
}
}

/// <summary>
/// 单帧动画
/// </summary>
Expand Down Expand Up @@ -310,5 +307,16 @@ public void Stop(bool StopEndAction = false)
//IsResetPlay = false;
}

public void WaitForReadyRun(Action EndAction = null)
{
Task.Run(() =>
{
while (!IsReady)
{
Thread.Sleep(100);
}
Run(EndAction);
});
}
}
}
2 changes: 2 additions & 0 deletions VPet-Simulator.Core/Graph/Picture.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@ public void Stop(bool StopEndAction = false)
PlayState = false;
this.StopEndAction = StopEndAction;
}

public void WaitForReadyRun(Action EndAction = null) => Run(EndAction);
}
}
57 changes: 27 additions & 30 deletions VPet-Simulator.Core/Handle/PetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,38 @@ public static void LoadGraph(GraphCore graph, DirectoryInfo di, string path_name
var list = di.EnumerateDirectories();
if (list.Count() == 0)
{
if (File.Exists(di.FullName + @"\info.lps"))
{//如果自带描述信息,则手动加载
//TODO:
}
else
{//自动加载 PNG ANMIN
path_name = path_name.Trim('_').ToLower();
for (int i = 0; i < GraphTypeValue.Length; i++)
//自动加载 PNG ANMIN
path_name = path_name.Trim('_').ToLower();
for (int i = 0; i < GraphTypeValue.Length; i++)
{
if (path_name.StartsWith(GraphTypeValue[i]))
{
if (path_name.StartsWith(GraphTypeValue[i]))
if (path_name.Contains("happy"))
{

if (path_name.Contains("happy"))
{
graph.AddGraph(di.FullName, Save.ModeType.Happy, (GraphType)i);
}
if (path_name.Contains("nomal"))
{
graph.AddGraph(di.FullName, Save.ModeType.Nomal, (GraphType)i);
}
if (path_name.Contains("poorcondition"))
{
graph.AddGraph(di.FullName, Save.ModeType.PoorCondition, (GraphType)i);
}
if (path_name.Contains("ill"))
{
graph.AddGraph(di.FullName, Save.ModeType.Ill, (GraphType)i);
}
return;
graph.AddGraph(di.FullName, Save.ModeType.Happy, (GraphType)i);
}
if (path_name.Contains("nomal"))
{
graph.AddGraph(di.FullName, Save.ModeType.Nomal, (GraphType)i);
}
if (path_name.Contains("poorcondition"))
{
graph.AddGraph(di.FullName, Save.ModeType.PoorCondition, (GraphType)i);
}
if (path_name.Contains("ill"))
{
graph.AddGraph(di.FullName, Save.ModeType.Ill, (GraphType)i);
}
return;
}
#if DEMO
throw new Exception("未知的图像类型: " + path_name);
#endif
}
#if DEBUG
throw new Exception("未知的图像类型: " + path_name);
#endif
}
else if (File.Exists(di.FullName + @"\info.lps"))
{//如果自带描述信息,则手动加载
//TODO:
}
else
foreach (var p in list)
Expand Down
5 changes: 4 additions & 1 deletion VPet-Simulator.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ static void Animation()
{
Console.WriteLine("请输入每张图片的持续时间 (单位: 毫秒)");
string timestr = Console.ReadLine();
int time = timestr == "" ? 125 : int.Parse(timestr);
if(!int.TryParse(timestr,out int time))
{
time = 125;
}
Console.WriteLine("请输入图片位置");
DirectoryInfo directoryInfo = new DirectoryInfo(Console.ReadLine());
int id = 0;
Expand Down
4 changes: 3 additions & 1 deletion VPet-Simulator.Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ public void GameLoad()
AutoSaveTimer.Start();
}


Dispatcher.Invoke(new Action(() =>
{
LoadingText.Content = "尝试加载动画和生成缓存";
var pl = Pets.Find(x => x.Name == Set.PetGraph);
Core.Graph = pl == null ? Pets[0].Graph() : pl.Graph();
LoadingText.Content = "正在加载游戏";

winSetting = new winGameSetting(this);
Main = new Main(Core) { };

Main.DefaultClickAction = () =>
{
if (new TimeSpan(DateTime.Now.Ticks - lastclicktime).TotalSeconds > 20)
Expand Down Expand Up @@ -188,6 +189,7 @@ public void GameLoad()
Top = (SystemParameters.PrimaryScreenHeight - Height) / 2;
}));
m_menu.MenuItems.Add(new MenuItem("反馈中心", (x, y) => { new winReport(this).Show(); }));
m_menu.MenuItems.Add(new MenuItem("开发控制台", (x, y) => { new winConsole(this).Show(); }));

m_menu.MenuItems.Add(new MenuItem("设置面板", (x, y) =>
{
Expand Down

0 comments on commit 112a633

Please sign in to comment.