加载优化

This commit is contained in:
ZouJin 2023-02-22 16:37:23 +11:00
parent 232850b535
commit 112a633e8c
9 changed files with 109 additions and 92 deletions

View File

@ -44,6 +44,7 @@ namespace VPet_Simulator.Core
public event Action<Main> TimeUIHandle;
public Main(GameCore core, bool loadtouchevent = true)
{
Console.WriteLine(DateTime.Now.ToString("T:fff"));
InitializeComponent();
Core = core;
@ -64,7 +65,7 @@ namespace VPet_Simulator.Core
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;

View File

@ -137,11 +137,11 @@ namespace VPet_Simulator.Core
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)情况

View File

@ -29,6 +29,10 @@ namespace VPet_Simulator.Core
/// 是否继续播放
/// </summary>
bool IsContinue { get; set; }
/// <summary>
/// 从0开始运行该动画, 等待部署完成后执行
/// </summary>
void WaitForReadyRun(Action EndAction = null);
///// <summary>//经过测试,储存到内存好处多多,不储存也要占用很多内存,干脆存了吧
///// 是否储存到内存以支持快速显示
///// </summary>
@ -36,7 +40,7 @@ namespace VPet_Simulator.Core
/// <summary>
/// 该动画UI状态
/// </summary>
Save.ModeType ModeType { get;}
Save.ModeType ModeType { get; }
/// <summary>
/// 该动画UI状态
/// </summary>

View File

@ -1,4 +1,4 @@
<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"
@ -6,4 +6,4 @@
xmlns:local="clr-namespace:VPet_Simulator.Core"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500">
</Grid>
</Image>

View File

@ -19,13 +19,14 @@ using System.Drawing;
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>
/// 所有动画帧
@ -61,6 +62,10 @@ namespace VPet_Simulator.Core
public GraphCore.GraphType GraphType { get; private set; }
/// <summary>
/// 是否准备完成
/// </summary>
public bool IsReady { get; private set; } = false;
/// <summary>
/// 动画停止时运行的方法
/// </summary>
private Action StopAction;
@ -79,59 +84,7 @@ namespace VPet_Simulator.Core
//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)
//{
@ -196,6 +149,50 @@ namespace VPet_Simulator.Core
// , () => 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>
@ -310,5 +307,16 @@ namespace VPet_Simulator.Core
//IsResetPlay = false;
}
public void WaitForReadyRun(Action EndAction = null)
{
Task.Run(() =>
{
while (!IsReady)
{
Thread.Sleep(100);
}
Run(EndAction);
});
}
}
}

View File

@ -72,5 +72,7 @@ namespace VPet_Simulator.Core
PlayState = false;
this.StopEndAction = StopEndAction;
}
public void WaitForReadyRun(Action EndAction = null) => Run(EndAction);
}
}

View File

@ -52,41 +52,38 @@ namespace VPet_Simulator.Core
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)

View File

@ -36,7 +36,10 @@ namespace VPet_Simulator.Tool
{
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;

View File

@ -144,15 +144,16 @@ namespace VPet_Simulator.Windows
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)
@ -188,6 +189,7 @@ namespace VPet_Simulator.Windows
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) =>
{