VPet/VPet-Simulator.Core/Graph/Picture.cs

198 lines
6.9 KiB
C#
Raw Normal View History

2023-03-26 22:35:19 +00:00
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Controls;
2023-05-31 17:47:23 +00:00
using LinePutScript;
using static VPet_Simulator.Core.GraphCore;
using static VPet_Simulator.Core.Picture;
2023-03-26 22:35:19 +00:00
2023-07-16 23:58:09 +00:00
2023-03-26 22:35:19 +00:00
namespace VPet_Simulator.Core
{
/// <summary>
/// Picture.xaml 的交互逻辑
/// </summary>
2023-07-16 23:58:09 +00:00
public class Picture : IImageRun
2023-03-26 22:35:19 +00:00
{
/// <summary>
/// 新建新静态图像
/// </summary>
/// <param name="path">图片路径</param>
2023-07-16 23:58:09 +00:00
public Picture(GraphCore graphCore, string path, GraphInfo graphinfo, int length = 1000, bool isloop = false)
2023-03-26 22:35:19 +00:00
{
2023-07-16 23:58:09 +00:00
GraphInfo = graphinfo;
2023-03-26 22:35:19 +00:00
IsLoop = isloop;
Length = length;
GraphCore = graphCore;
2023-03-26 22:46:23 +00:00
Path = path;
2023-03-26 22:35:19 +00:00
if (!GraphCore.CommConfig.ContainsKey("PIC_Setup"))
{
GraphCore.CommConfig["PIC_Setup"] = true;
2023-05-19 08:17:51 +00:00
GraphCore.CommUIElements["Image1.Picture"] = new Image() { Width = 500, Height = 500 };
GraphCore.CommUIElements["Image2.Picture"] = new Image() { Width = 500, Height = 500 };
2023-03-26 22:35:19 +00:00
}
}
2023-05-31 17:47:23 +00:00
public static void LoadGraph(GraphCore graph, FileSystemInfo path, ILine info)
{
if (!(path is FileInfo))
{
PNGAnimation.LoadGraph(graph, path, info);
return;
}
int length = info.GetInt("length");
if (length == 0)
{
if (!int.TryParse(path.Name.Split('.').Reverse().ToArray()[1].Split('_').Last(), out length))
length = 1000;
}
bool isLoop = info[(gbol)"loop"];
2023-07-24 01:53:18 +00:00
Picture pa = new Picture(graph, path.FullName, new GraphInfo(path, info), length, isLoop);
2023-07-16 23:58:09 +00:00
graph.AddGraph(pa);
2023-05-31 17:47:23 +00:00
}
2023-03-26 22:35:19 +00:00
/// <summary>
/// 图片资源
/// </summary>
2023-03-26 22:46:23 +00:00
public string Path;
2023-03-26 22:35:19 +00:00
private GraphCore GraphCore;
public bool PlayState { get; set; }
public bool IsLoop { get; set; }
public int Length { get; set; }
//public bool StoreMemory => true;//经过测试,储存到内存好处多多,不储存也要占用很多内存,干脆存了吧
public bool IsContinue { get; set; }
2023-07-16 23:58:09 +00:00
/// <summary>
/// 动画信息
/// </summary>
public GraphInfo GraphInfo { get; private set; }
2023-03-26 22:35:19 +00:00
2023-05-31 17:47:23 +00:00
public bool IsReady => true;
2023-03-26 22:35:19 +00:00
public void Run(Border parant, Action EndAction = null)
{
2023-05-26 16:05:59 +00:00
if (PlayState)
{
2023-06-01 08:16:45 +00:00
IsContinue = true;
2023-05-26 16:05:59 +00:00
return;
}
2023-03-26 22:35:19 +00:00
PlayState = true;
2023-05-26 16:05:59 +00:00
DoEndAction = true;
2023-03-28 13:18:14 +00:00
parant.Dispatcher.Invoke(() =>
2023-03-26 22:35:19 +00:00
{
2023-03-28 13:18:14 +00:00
if (parant.Tag != this)
2023-03-26 22:35:19 +00:00
{
2023-05-19 08:17:51 +00:00
Image img;
2023-03-28 13:18:14 +00:00
if (parant.Child == GraphCore.CommUIElements["Image1.Picture"])
2023-03-26 22:35:19 +00:00
{
2023-05-19 08:17:51 +00:00
img = (Image)GraphCore.CommUIElements["Image1.Picture"];
2023-03-28 13:18:14 +00:00
}
else
{
2023-05-19 08:17:51 +00:00
img = (Image)GraphCore.CommUIElements["Image2.Picture"];
2023-05-26 16:05:59 +00:00
if (parant.Child != img)
2023-03-26 22:35:19 +00:00
{
2023-03-28 13:18:14 +00:00
if (img.Parent == null)
{
2023-05-26 16:05:59 +00:00
parant.Child = null;
2023-03-28 13:18:14 +00:00
parant.Child = img;
}
else
{
2023-08-12 05:44:17 +00:00
img = (Image)GraphCore.CommUIElements["Image1.Picture"];
2023-08-13 07:28:08 +00:00
if (img.Parent != null)
((Decorator)img.Parent).Child = null;
2023-03-28 13:18:14 +00:00
parant.Child = img;
}
2023-03-26 22:35:19 +00:00
}
}
2023-03-28 13:18:14 +00:00
//var bitmap = new BitmapImage();
//bitmap.BeginInit();
//stream.Seek(0, SeekOrigin.Begin);
//bitmap.StreamSource = stream;
//bitmap.CacheOption = BitmapCacheOption.OnLoad;
//bitmap.EndInit();
2023-05-31 17:47:23 +00:00
img.Width = 500;
img.Source = new BitmapImage(new Uri(Path));
parant.Tag = this;
2023-03-26 22:35:19 +00:00
}
2023-03-28 13:18:14 +00:00
Task.Run(() =>
2023-03-26 22:35:19 +00:00
{
2023-03-28 13:18:14 +00:00
Thread.Sleep(Length);
if (IsLoop && PlayState)
{
Run(parant, EndAction);
}
else
{
PlayState = false;
2023-05-26 16:05:59 +00:00
if (DoEndAction)
2023-03-28 13:18:14 +00:00
EndAction?.Invoke();//运行结束动画时事件
}
});
2023-03-26 22:35:19 +00:00
});
}
2023-05-26 16:05:59 +00:00
bool DoEndAction = true;
2023-03-26 22:35:19 +00:00
public void Stop(bool StopEndAction = false)
{
PlayState = false;
2023-05-26 16:05:59 +00:00
this.DoEndAction = !StopEndAction;
2023-03-26 22:35:19 +00:00
}
2023-08-16 03:09:32 +00:00
public Task Run(System.Windows.Controls.Image img, Action EndAction = null)
2023-05-31 17:47:23 +00:00
{
PlayState = true;
DoEndAction = true;
return img.Dispatcher.Invoke(() =>
{
if (img.Tag == this)
{
2023-08-16 03:09:32 +00:00
return new Task(() =>
2023-05-31 17:47:23 +00:00
{
Thread.Sleep(Length);
if (IsLoop && PlayState)
{
Run(img, EndAction);
}
else
{
PlayState = false;
if (DoEndAction)
EndAction?.Invoke();//运行结束动画时事件
}
});
}
img.Tag = this;
img.Source = new BitmapImage(new Uri(Path));
img.Width = 500;
2023-08-16 03:09:32 +00:00
return new Task(() =>
2023-05-31 17:47:23 +00:00
{
Thread.Sleep(Length);
if (IsLoop && PlayState)
{
Run(img, EndAction);
}
else
{
PlayState = false;
if (DoEndAction)
EndAction?.Invoke();//运行结束动画时事件
}
});
});
}
2023-06-01 08:16:45 +00:00
public interface IImageRun : IGraph
2023-05-31 17:47:23 +00:00
{
/// <summary>
/// 指定图像图像控件准备运行该动画
/// </summary>
/// <param name="img">用于显示的Image</param>
/// <param name="EndAction">结束动画</param>
/// <returns>准备好的线程</returns>
2023-08-16 03:09:32 +00:00
Task Run(System.Windows.Controls.Image img, Action EndAction = null);
2023-05-31 17:47:23 +00:00
}
2023-03-26 22:35:19 +00:00
}
2023-05-31 17:47:23 +00:00
2023-03-26 22:35:19 +00:00
}