mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
functional card/row edit and drag
This commit is contained in:
29
Serialization/SerialCard.cs
Normal file
29
Serialization/SerialCard.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class SerialCard
|
||||
{
|
||||
[JsonInclude]
|
||||
public string Text { get; set; }
|
||||
[JsonInclude]
|
||||
public SerialImage Image { get; set; }
|
||||
public card ToCard(SceneTree tree)
|
||||
{
|
||||
var c = card.MakeCard(tree);
|
||||
c.CardName = Text;
|
||||
if (Image.DataWebp.Any())
|
||||
{
|
||||
var iwm = Image.ToImageWithMetadata();
|
||||
c.SetTexture(ImageTexture.CreateFromImage(iwm.Image));
|
||||
c.SetStretchMode(iwm.StretchMode);
|
||||
}
|
||||
else
|
||||
{
|
||||
c.SetStretchMode(Image.StretchMode);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
42
Serialization/SerialColor.cs
Normal file
42
Serialization/SerialColor.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Godot;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class SerialColor
|
||||
{
|
||||
[JsonInclude]
|
||||
public float R { get; set; }
|
||||
[JsonInclude]
|
||||
public float G { get; set; }
|
||||
[JsonInclude]
|
||||
public float B { get; set; }
|
||||
public void Deconstruct(out float r, out float g, out float b)
|
||||
{
|
||||
r = R;
|
||||
g = G;
|
||||
b = B;
|
||||
}
|
||||
public SerialColor()
|
||||
{
|
||||
R = 0;
|
||||
G = 0;
|
||||
B = 0;
|
||||
}
|
||||
public SerialColor(float r, float g, float b)
|
||||
{
|
||||
R = r;
|
||||
G = g;
|
||||
B = b;
|
||||
}
|
||||
public SerialColor(Color c)
|
||||
{
|
||||
R = c.R;
|
||||
G = c.G;
|
||||
B = c.B;
|
||||
}
|
||||
public static implicit operator Color(SerialColor sc)
|
||||
=> new(
|
||||
sc.R,
|
||||
sc.G,
|
||||
sc.B
|
||||
);
|
||||
}
|
||||
9
Serialization/SerialGame.cs
Normal file
9
Serialization/SerialGame.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class SerialGame
|
||||
{
|
||||
[JsonInclude]
|
||||
public SerialRow[] Rows { get; set; }
|
||||
[JsonInclude]
|
||||
public SerialCard[] UnassignedCards { get; set; }
|
||||
}
|
||||
17
Serialization/SerialImage.cs
Normal file
17
Serialization/SerialImage.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Godot;
|
||||
|
||||
public class SerialImage
|
||||
{
|
||||
[JsonInclude]
|
||||
public byte[] DataWebp { get; set; }
|
||||
[JsonInclude]
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public StretchMode StretchMode { get; set; }
|
||||
public ImageWithMetadata ToImageWithMetadata()
|
||||
{
|
||||
var im = new Image();
|
||||
im.LoadWebpFromBuffer(DataWebp);
|
||||
return new(im, StretchMode);
|
||||
}
|
||||
}
|
||||
24
Serialization/SerialRow.cs
Normal file
24
Serialization/SerialRow.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Godot;
|
||||
|
||||
public class SerialRow
|
||||
{
|
||||
[JsonInclude]
|
||||
public string Text { get; set; }
|
||||
[JsonInclude]
|
||||
public SerialColor Color { get; set; }
|
||||
[JsonInclude]
|
||||
public SerialCard[] Cards { get; set; }
|
||||
public row ToRow(SceneTree tree)
|
||||
{
|
||||
var r = row.MakeRow(tree);
|
||||
r.RowText = Text;
|
||||
r.RowColor = Color;
|
||||
foreach (var c in Cards)
|
||||
r.AddCard(c.ToCard(tree));
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
65
Serialization/Serializer.cs
Normal file
65
Serialization/Serializer.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
public static class Serializer
|
||||
{
|
||||
public static string CreateGameJson(game g, ExportSettings es)
|
||||
=> JsonSerializer.Serialize(CreateGameObject(g, es));
|
||||
private static SerialGame CreateGameObject(game g, ExportSettings es)
|
||||
=> new()
|
||||
{
|
||||
Rows = g.GetNode("%RowContainer").GetChildren().OfType<row>()
|
||||
.Select(r => RowToSerial(r, es)).ToArray(),
|
||||
UnassignedCards = g.GetNode("%UnassignedCardContainer").GetChildren()
|
||||
.OfType<card>().Select(c => CardToSerial(c, es)).ToArray(),
|
||||
};
|
||||
private static SerialRow RowToSerial(row r, ExportSettings es)
|
||||
=> new()
|
||||
{
|
||||
Color = new(r.RowColor),
|
||||
Cards = r.Cards.Select(c => CardToSerial(c, es)).ToArray(),
|
||||
Text = r.RowText
|
||||
};
|
||||
private static SerialCard CardToSerial(card c, ExportSettings es)
|
||||
{
|
||||
byte[] imData;
|
||||
var i = c.GetTexture()?.GetImage();
|
||||
if (i is Image img)
|
||||
{
|
||||
if (es.ScaleImages)
|
||||
{
|
||||
Image copy = new();
|
||||
copy.CopyFrom(img);
|
||||
copy.Resize(500, 500);
|
||||
imData = copy.SaveWebpToBuffer(true);
|
||||
}
|
||||
else
|
||||
imData = img.SaveWebpToBuffer(false);
|
||||
}
|
||||
else
|
||||
imData = Array.Empty<byte>();
|
||||
return new()
|
||||
{
|
||||
Text = c.CardName,
|
||||
Image = new()
|
||||
{
|
||||
DataWebp = imData,
|
||||
StretchMode = c.GetStretchMode(),
|
||||
},
|
||||
};
|
||||
}
|
||||
public static void LoadFromSerial(game g, string jsonString)
|
||||
{
|
||||
var sg = JsonSerializer.Deserialize<SerialGame>(jsonString)
|
||||
?? throw new Exception("Failed to deserialize game");
|
||||
g.Clear();
|
||||
using var context = card.MakeContext(g.GetTree());
|
||||
foreach (var r in sg.Rows)
|
||||
g.AddRow(r.ToRow(g.GetTree()));
|
||||
foreach (var c in sg.UnassignedCards)
|
||||
g.AddUnassignedCard(c.ToCard(g.GetTree()));
|
||||
g.PropogateCardSize();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user