Saving to DB, need to render

This commit is contained in:
Lani Aung
2021-11-02 18:33:07 -06:00
parent d52cc4b206
commit 16dfc040a4
18 changed files with 318 additions and 212 deletions
+2 -52
View File
@@ -1,70 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using fxl.codes.kisekae.Services;
using Microsoft.Extensions.Logging;
namespace fxl.codes.kisekae.Models
{
public class CelModel
{
private const string Regex = @"#(?<Id>\d*)\.?(?<Fix>\d*)\s*(?<FileName>[\w\d\-]*\.[cCeElL]*)\s*\*?(?<PaletteId>\d*)?\s*\:?(?<Sets>[\d\s]*)?;?(?<Comment>[\w\d\-\s\%]*)";
public Dictionary<int, string> ImageByPalette = new();
internal CelModel(ILogger<ConfigurationReaderService> logger, string line)
{
logger.LogTrace($"Parsing cel line {line}");
if (line.Contains("%t"))
{
var opacity = line[line.IndexOf("%t")..].Trim();
opacity = opacity.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[0];
opacity = opacity.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[0];
opacity = opacity.Replace("%t", "");
Opacity = 1.0 - double.Parse(opacity) / 255;
}
var matcher = new Regex(Regex);
var match = matcher.Match(line);
foreach (var groupName in matcher.GetGroupNames())
{
var property = typeof(CelModel).GetProperty(groupName);
if (property == null) continue;
match.Groups.TryGetValue(groupName, out var group);
if (string.IsNullOrEmpty(group?.Value))
{
if (string.Equals(groupName, "Sets"))
for (var index = 0; index < Sets.Length; index++)
Sets[index] = true;
logger.LogWarning($"Unable to find regex group {groupName} in {line}");
continue;
}
var value = group.Value.Trim();
if (property.PropertyType == typeof(string)) property.SetValue(this, value);
if (property.PropertyType == typeof(int)) property.SetValue(this, int.Parse(value));
if (property.PropertyType != Sets.GetType()) continue;
foreach (var id in value.Split(' ', StringSplitOptions.RemoveEmptyEntries))
{
var success = int.TryParse(id, out var result);
if (success) Sets[result] = true;
else logger.LogWarning($"Unable to parse {id} as cel sets");
}
}
}
public int Id { get; init; }
public int Fix { get; init; }
public string FileName { get; init; }
public int PaletteId { get; init; }
public bool[] Sets { get; } = new bool[10];
public string Comment { get; init; }
public double Opacity { get; } = 1.0;
[JsonIgnore] public int Transparency { get; set; }
public double Opacity { get; internal set; } = 1.0;
public Coordinate[] InitialPositions { get; } = new Coordinate[10];
[JsonIgnore] public string DefaultImage => ImageByPalette.ContainsKey(PaletteId) ? ImageByPalette[PaletteId] : null;
public Coordinate Offset { get; set; }
+3 -15
View File
@@ -1,24 +1,12 @@
using System;
using fxl.codes.kisekae.Services;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
namespace fxl.codes.kisekae.Models
{
public class PaletteModel
{
internal PaletteModel(ILogger<ConfigurationReaderService> logger, string line)
{
logger.LogTrace($"Parsing palette line {line}");
var parts = line.Split(';');
FileName = parts[0].Replace("%", "").Trim();
if (parts.Length > 1) Comment = parts[1].Trim();
}
public string FileName { get; }
public string Comment { get; }
public Color[] Colors { get; set; } = Array.Empty<Color>();
public string FileName { get; internal set; }
public string Comment { get; internal set; }
public Color[] Colors { get; internal set; } = Array.Empty<Color>();
}
}
+2 -15
View File
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SixLabors.ImageSharp;
@@ -7,24 +6,12 @@ namespace fxl.codes.kisekae.Models
{
public class PlaysetModel
{
[JsonIgnore] public Color BorderColor = Color.Black;
public string Name { get; set; }
public int Height { get; set; }
public int Width { get; set; }
[JsonIgnore] public int BorderColorIndex { get; set; }
[JsonIgnore] public Color BorderColor { get; set; } = Color.Black;
[JsonIgnore] public List<PaletteModel> Palettes { get; } = new();
public List<CelModel> Cels { get; } = new();
public bool[] EnabledSets
{
get
{
var enabled = new bool[10];
for (var index = 0; index < enabled.Length; index++) enabled[index] = Cels.Any(x => x.Sets[index]);
return enabled;
}
}
public bool[] EnabledSets { get; } = new bool[10];
}
}