timelapsenow/TimelapseExtensions/ClockExtension/ClockData.cs

75 lines
2.2 KiB
C#
Raw Normal View History

2022-05-21 15:37:43 +00:00
using Newtonsoft.Json;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
namespace ClockExtension
{
public class ClockData
{
public static ClockData Load(string extDir,FontCollection coll)
{
string fontDir = Path.Combine(extDir,"Fonts");
if(Directory.Exists(fontDir)){
foreach(var ttf in Directory.GetFiles(fontDir,"*.ttf"))
{
coll.Add(ttf);
}
}
string extInfoFile=Path.Combine(extDir,"ext_info.json");
if(File.Exists(extInfoFile))
{
string extInfoFileString = File.ReadAllText(extInfoFile);
var cd=JsonConvert.DeserializeObject<ClockData>(extInfoFileString);
if(cd != null)
{
cd.FilePath = extInfoFile;
FontFamily[] fam = coll.Families.ToArray();
for(int i = 0;i<fam.Length;i++)
{
if(fam[i].Name.Equals(cd.FontName,StringComparison.Ordinal))
{
cd.FontIndex=i;
break;
}
}
return cd;
}
}
return new ClockData {FilePath=extInfoFile};
}
public ClockData()
{
EnableClock=true;
DateFormat = "yyyy/MM/dd HH:mm:ss";
FontIndex=0;
FilePath="";
FontSize = 24;
ForeColor=Eto.Drawing.Colors.White;
X=16;
Y=16;
}
[JsonIgnore]
public int FontIndex {get;set;}
[JsonIgnore]
public string FilePath {get;private set;}
public bool EnableClock {get;set;}
public int X {get;set;}
public int Y {get;set;}
public string? FontName {get;set;}
public float FontSize {get;set;}
public string DateFormat {get;set;}
public Rgb ForeColor {get;set;}
public void Save()
{
string data=JsonConvert.SerializeObject(this);
File.WriteAllText(FilePath,data);
}
}
}