Skip to content

Commit 1a0f1fd

Browse files
converter
1 parent 5fa409f commit 1a0f1fd

File tree

3 files changed

+33
-49
lines changed

3 files changed

+33
-49
lines changed

ASECII/STypeConverter.cs

+28-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
using SadRogue.Primitives;
1010
using static SadConsole.ColoredString;
1111
using System.IO;
12+
using System.Diagnostics.CodeAnalysis;
1213

1314
namespace ASECII {
1415
//https://stackoverflow.com/a/57319194
1516
public static class STypeConverter {
1617
public static void PrepareConvert() {
1718
//https://stackoverflow.com/a/57319194
1819
TypeDescriptor.AddAttributes(typeof((int, int)), new TypeConverterAttribute(typeof(Int2Converter)));
19-
20-
TypeDescriptor.AddAttributes(typeof((uint, uint)), new TypeConverterAttribute(typeof(UInt2Converter)));
21-
//TypeDescriptor.AddAttributes(typeof(Color), new TypeConverterAttribute(typeof(ColorConverter)));
22-
}
20+
21+
TypeDescriptor.AddAttributes(typeof((uint, uint)), new TypeConverterAttribute(typeof(UInt2Converter)));
22+
TypeDescriptor.AddAttributes(typeof((uint, uint, int)), new TypeConverterAttribute(typeof(TileDataConverter)));
23+
//TypeDescriptor.AddAttributes(typeof(Color), new TypeConverterAttribute(typeof(ColorConverter)));
24+
}
2325
}
2426
public static class ASECIILoader {
2527
public static Dictionary<(int, int), TileValue> LoadCG(string path) =>
@@ -52,12 +54,33 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
5254
}
5355
}
5456
public class UInt2Converter : TypeConverter {
55-
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
57+
58+
public override bool CanConvertTo (ITypeDescriptorContext context, [NotNullWhen(true)] Type destinationType) {
59+
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
60+
}
61+
62+
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
63+
var v = ((uint Front, uint Back, int Glyph)) value;
64+
return $"({v.Front}, {v.Back}, {v.Glyph})";
65+
}
66+
67+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
5668
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
5769
}
5870
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
5971
var elements = Convert.ToString(value).Trim('(').Trim(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
6072
return (uint.Parse(elements.First()), uint.Parse(elements.Last()));
6173
}
6274
}
75+
76+
77+
public class TileDataConverter : TypeConverter {
78+
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType) {
79+
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
80+
}
81+
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) {
82+
var elements = Convert.ToString(value).Trim('(', ')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
83+
return (uint.Parse(elements[0]), uint.Parse(elements[1]), int.Parse(elements[2]));
84+
}
85+
}
6386
}

ASECII/Sprite.cs

+3-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class Sprite {
1111
public Point origin;
1212
public Point end;
1313
public Dictionary<(int, int), TileValue> preview = new();
14-
public Dictionary<(int, int), TileData> exportData = new();
14+
[IgnoreDataMember]
15+
public Dictionary<(int, int), (uint Foreground, uint Background, int Glyph)> exportData = new();
1516

1617

1718
public static TileValue empty => new TileValue(Color.Transparent, Color.Transparent, 0);
@@ -61,7 +62,7 @@ public void UpdatePreview() {
6162

6263
exportData.Clear();
6364
foreach(var(pos, tile) in preview) {
64-
exportData[pos] = tile;
65+
exportData[pos] = (tile.Foreground.PackedValue, tile.Background.PackedValue, tile.Glyph);
6566
}
6667
if (preview.Any()) {
6768
origin = new Point(preview.Keys.Min(k => k.Item1), preview.Keys.Min(k => k.Item2));
@@ -394,22 +395,6 @@ public TileValue(Color Foreground, Color Background, int Glyph) {
394395
this.Glyph = Glyph;
395396
}
396397
public static implicit operator ColoredGlyph(TileValue tv) => new ColoredGlyph(tv.Foreground, tv.Background, tv.Glyph);
397-
398-
public static implicit operator TileData (TileValue tv) => new() {
399-
Background = tv.Background.PackedValue,
400-
Foreground = tv.Foreground.PackedValue,
401-
Glyph = tv.Glyph
402-
};
403-
}
404-
[DataContract]
405-
public class TileData {
406-
[DataMember]
407-
public uint Foreground;
408-
[DataMember]
409-
public uint Background;
410-
[DataMember]
411-
public int Glyph;
412-
413398
}
414399

415400
public class NotepadTile : TileRef {

ASECII/SpriteMenu.cs

+2-26
Original file line numberDiff line numberDiff line change
@@ -497,23 +497,17 @@ void AddPickerMenu() {
497497
layerCutButton.UpdateActive();
498498

499499
UpdateChannels();
500-
501500
}
502501
public void InitUI() {
503502
InitControls();
504-
505503
historyMenu = new HistoryMenu(16, Height, model);
506504
historyMenu.UpdateListing();
507505
model.historyChanged += historyMenu.HistoryChanged;
508-
509506
spriteMenu = new SpriteMenu(Width - controlsMenu.Width, Height, model) {
510507
FocusOnMouseClick = true,
511508
UseMouse = true
512509
};
513510
spriteMenu.OnKeyboard += OnKeyboard;
514-
515-
516-
517511
void OnKeyboard(Keyboard info) {
518512

519513
if (info.IsKeyPressed(Tab)) {
@@ -729,7 +723,6 @@ public override void Render(TimeSpan timeElapsed) {
729723
y += 1;
730724
this.Print(x, y, new ColoredString($"{r.Width,4} {r.Height,4}", f, b));
731725
}
732-
733726
bool GetRectDisplay(out Rectangle r) => model.selectRect.GetAdjustedRect(out r) ||
734727
!((r = model.selection.rects.FirstOrDefault(r => r.Contains(model.cursor))).IsEmpty);
735728
bool GetEllipseDisplay(out Ellipse e) => model.selectRect.GetAdjustedEllipse(out e);
@@ -738,11 +731,9 @@ bool GetRectDisplay(out Rectangle r) => model.selectRect.GetAdjustedRect(out r)
738731
case Mode.SelectOutline: {
739732
int x = model.cursorScreen.X + 2;
740733
int y = model.cursorScreen.Y;
741-
742734
var f = model.brush.foreground;
743735
var b = model.brush.background;
744736
this.Print(x, y, new ColoredString($"{model.cursorScreen.X,4} {model.cursorScreen.Y,4}", f, b));
745-
746737
if (model.ticksSelect % 30 < 15) {
747738
IEnumerable<Point> points = model.selectOutline.outline;
748739
if(points.Any()) {
@@ -761,11 +752,9 @@ bool GetRectDisplay(out Rectangle r) => model.selectRect.GetAdjustedRect(out r)
761752
case Mode.SelectWand: {
762753
int x = model.cursorScreen.X + 2;
763754
int y = model.cursorScreen.Y;
764-
765755
var f = model.brush.foreground;
766756
var b = model.brush.background;
767757
this.Print(x, y, new ColoredString($"{model.cursorScreen.X,4} {model.cursorScreen.Y,4}", f, b));
768-
769758
if (model.ticksSelect % 30 < 15) {
770759
DrawSelection();
771760
}
@@ -774,11 +763,9 @@ bool GetRectDisplay(out Rectangle r) => model.selectRect.GetAdjustedRect(out r)
774763
case Mode.Move:
775764
DrawSelection();
776765
//Draw offset
777-
778766
//TO DO: Fix
779767
if (model.ticksSelect % 10 < 5 && model.move.current.HasValue) {
780768
var offset = model.move.end - model.move.start.Value;
781-
782769
int x = model.move.start.Value.X - camera.X;
783770
int y = model.move.start.Value.Y - camera.Y;
784771
bool first = true;
@@ -901,15 +888,11 @@ void DrawVertical() {
901888
break;
902889
}
903890
}
904-
905-
906-
907891
base.Render(timeElapsed);
908892
void DrawSelection() => DrawPoints(model.selection.GetAll());
909893
void DrawSelectionWith(IEnumerable<Point> points) => DrawPoints(model.selection.GetAll().Union(points));
910894
void DrawPoints(IEnumerable<Point> all) {
911895
foreach(var point in all) {
912-
913896
bool n = Contains(new Point(0, -1)), e = Contains(new Point(1, 0)), s = Contains(new Point(0, +1)), w = Contains(new Point(-1, 0)), ne = Contains(new Point(1, -1)), se = Contains(new Point(1, 1)), sw = Contains(new Point(-1, 1)), nw = Contains(new Point(-1, -1));
914897
bool Contains(Point offset) => all.Contains(point + offset);
915898
var p = point - camera;
@@ -979,8 +962,6 @@ void DrawPoints(IEnumerable<Point> all) {
979962
}
980963
void DrawRect(Rectangle r) {
981964
var (left, top) = (r.X, r.Y);
982-
983-
984965
if (r.Width > 1 && r.Height > 1) {
985966
DrawSidesX(Line.Single);
986967
DrawSidesY(Line.Single);
@@ -999,7 +980,6 @@ void DrawRect(Rectangle r) {
999980
} else {
1000981
DrawBox(left, top, new BoxGlyph { n = Line.Single, e = Line.Single, s = Line.Single, w = Line.Single });
1001982
}
1002-
1003983
void DrawCornerNW() {
1004984
//Left top
1005985
DrawBox(left, top, new BoxGlyph { e = Line.Single, s = Line.Single });
@@ -1052,7 +1032,6 @@ public override bool ProcessKeyboard(Keyboard info) {
10521032
}
10531033
if (info.IsKeyPressed(S)) {
10541034
//File.WriteAllText(Path.Combine(Environment.CurrentDirectory, Path.GetFileName(Path.GetTempFileName())), JsonConvert.SerializeObject(model));
1055-
10561035
if (model.filepath == null || info.IsKeyDown(LeftShift)) {
10571036
var p = Game.Instance.Screen as Console;
10581037
Game.Instance.Screen = new FileMenu(p.Width, p.Height, new SaveMode(model, this));
@@ -1061,23 +1040,20 @@ public override bool ProcessKeyboard(Keyboard info) {
10611040
}
10621041
}
10631042
}
1064-
10651043
model.ProcessKeyboard(info);
10661044
}
10671045
return base.ProcessKeyboard(info);
10681046
}
10691047
public override bool ProcessMouse(MouseScreenObjectState state) {
10701048
IsFocused = true;
1071-
var delta = state.Mouse.ScrollWheelValueChange / 120;
1049+
var delta = state.Mouse.ScrollWheelValueChange;
10721050
if (model.ctrl) {
10731051
if(delta < 0) {
10741052
FontSize += new Point(1, 1);
1075-
10761053
(int w, int h) = Program.calculate(FontSize.X);
10771054
Resize(w, h, w, h, false);
10781055
} else if(delta > 0) {
10791056
FontSize -= new Point(1, 1);
1080-
10811057
(int w, int h) = Program.calculate(FontSize.X);
10821058
Resize(w, h, w, h, false);
10831059
}
@@ -1194,7 +1170,7 @@ public SpriteModel(int width, int height) {
11941170
public void Save(Console renderer) {
11951171
var fileName = Path.GetDirectoryName(filepath) + "/" + Path.GetFileNameWithoutExtension(filepath);
11961172
File.WriteAllText($"{fileName}.asc", ASECIILoader.SerializeObject(this));
1197-
File.WriteAllText($"{fileName}.dat", JsonConvert.SerializeObject(sprite.exportData, SFileMode.settings));
1173+
File.WriteAllText($"{fileName}.dat", ASECIILoader.SerializeObject(sprite.exportData));
11981174
var preview = sprite.preview;
11991175
StringBuilder str = new StringBuilder();
12001176
for (int y = sprite.origin.Y; y <= sprite.end.Y; y++) {

0 commit comments

Comments
 (0)