Skip to content

Commit 27d5ec8

Browse files
authored
Merge pull request #7 from anno-mods/devel/add-islands
add/remove islands
2 parents 1029517 + 7ac33ae commit 27d5ec8

18 files changed

+451
-162
lines changed

AnnoMapEditor/AnnoMapEditor.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@
6161
</ItemGroup>
6262

6363
<ItemGroup>
64-
<Compile Update="UI\Controls\SessionProperties.xaml.cs">
65-
<SubType>Code</SubType>
66-
</Compile>
6764
<Compile Update="UserSettings.Designer.cs">
6865
<DesignTimeSharedInput>True</DesignTimeSharedInput>
6966
<AutoGen>True</AutoGen>

AnnoMapEditor/MapTemplates/Island.cs

+29-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public class Island : ObservableBase
3232
public string? MapPath { get; set; }
3333
public int MapSizeInTiles { get; private set; }
3434
public string? AssumedMapPath { get; private set; }
35-
public bool IsPool => string.IsNullOrEmpty(MapPath);
35+
public bool IsPool => ElementType != 2 && string.IsNullOrEmpty(MapPath);
3636
public string? Label { get; set; }
3737

38+
public bool IsNew => template is null;
39+
3840
private static readonly Random rnd = new((int)DateTime.Now.Ticks);
3941

40-
private Serializing.A7tinfo.TemplateElement template;
42+
private Serializing.A7tinfo.TemplateElement? template;
4143

4244
private Region Region { get; }
4345

@@ -47,7 +49,6 @@ public class Island : ObservableBase
4749
private Island(Region region)
4850
{
4951
Region = region;
50-
template = new Serializing.A7tinfo.TemplateElement();
5152
}
5253

5354
public async Task<Island> CloneAsync()
@@ -87,11 +88,33 @@ public static async Task<Island> FromSerialized(Serializing.A7tinfo.TemplateElem
8788
return island;
8889
}
8990

90-
public Serializing.A7tinfo.TemplateElement ToTemplate()
91+
public static Island Create(IslandSize size, IslandType type, Vector2 position)
92+
{
93+
var island = new Island(Region.Moderate)
94+
{
95+
ElementType = 1,
96+
Position = position,
97+
Size = size,
98+
Type = type
99+
};
100+
return island;
101+
}
102+
103+
public void CreateTemplate()
104+
{
105+
template = new Serializing.A7tinfo.TemplateElement
106+
{
107+
Element = new Serializing.A7tinfo.Element()
108+
};
109+
IslandChanged?.Invoke();
110+
}
111+
112+
public Serializing.A7tinfo.TemplateElement? ToTemplate()
91113
{
92-
if (template.Element is null)
114+
if (template?.Element is null)
93115
return template;
94116

117+
template.ElementType = ElementType;
95118
template.Element.Position = new int[] { Position.X, Position.Y };
96119
template.Element.Size = Size.ElementValue;
97120
if (IsPool)
@@ -138,7 +161,7 @@ public async Task InitAsync(Region region)
138161
{
139162
if (ElementType == 2)
140163
{
141-
MapSizeInTiles = 1;
164+
MapSizeInTiles = Vector2.Tile.X;
142165
return;
143166
}
144167

AnnoMapEditor/MapTemplates/Math.cs

+76-21
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.Windows;
67

78
namespace AnnoMapEditor.MapTemplates
89
{
910
public record Vector2
1011
{
11-
private int Normalize(int x) => (x + 4) / 8 * 8;
12+
public static readonly Vector2 Zero = new(0, 0);
13+
public static readonly Vector2 Tile = new(8, 8);
1214

1315
public int X
1416
{
@@ -24,6 +26,8 @@ public int Y
2426
}
2527
private int _y = 0;
2628

29+
public double Length => Math.Sqrt(X * X + Y * Y);
30+
2731
public Vector2(int x, int y)
2832
{
2933
X = x;
@@ -51,14 +55,61 @@ public Vector2(int[]? numbers)
5155
Y = numbers[1];
5256
}
5357
}
58+
59+
public Vector2(Point point)
60+
{
61+
X = (int)point.X;
62+
Y = (int)point.Y;
63+
}
64+
65+
public static Vector2 operator + (Vector2 a, Vector2 b)
66+
{
67+
return new Vector2(a.X + b.X, a.Y + b.Y);
68+
}
69+
70+
public static Vector2 operator -(Vector2 a, Vector2 b)
71+
{
72+
return new Vector2(a.X - b.X, a.Y - b.Y);
73+
}
74+
75+
public static Vector2 operator -(Vector2 a, int b)
76+
{
77+
return new Vector2(a.X - b, a.Y - b);
78+
}
79+
80+
public Vector2 Clamp(Rect2 area)
81+
{
82+
return new Vector2(Math.Clamp(X, area.X, area.Max.X), Math.Clamp(Y, area.Y, area.Max.Y));
83+
}
84+
85+
public Vector2 Clamp(Vector2 min, Vector2 max)
86+
{
87+
return new Vector2(Math.Clamp(X, min.X, max.X), Math.Clamp(Y, min.Y, max.Y));
88+
}
89+
90+
public Vector2 FlipY(int sessionSize)
91+
{
92+
return new Vector2(X, sessionSize - Y - 8);
93+
}
94+
95+
public bool Within(Rect2 area)
96+
{
97+
return X >= area.X && Y >= area.Y && X < area.X + area.Width && Y < area.Y + area.Height;
98+
}
99+
100+
private static int Normalize(int x) => (x + 4) / 8 * 8;
54101
}
55102

56103
public struct Rect2
57104
{
58-
public int X;
59-
public int Y;
60-
public int Width;
61-
public int Height;
105+
public Vector2 Position;
106+
public Vector2 Size;
107+
public Vector2 Max => Position + Size - Vector2.Tile;
108+
109+
public int X => Position.X;
110+
public int Y => Position.Y;
111+
public int Width => Size.X;
112+
public int Height => Size.Y;
62113

63114
public Rect2(string? area)
64115
{
@@ -67,35 +118,39 @@ public Rect2(string? area)
67118
string[] parts = area.Split(' ');
68119
if (parts.Length == 4)
69120
{
70-
X = int.Parse(parts[0]);
71-
Y = int.Parse(parts[1]);
72-
Width = int.Parse(parts[2]) - X;
73-
Height = int.Parse(parts[3]) - Y;
121+
Position = new Vector2(int.Parse(parts[0]), int.Parse(parts[1]));
122+
Size = new Vector2(int.Parse(parts[2]), int.Parse(parts[3])) - Position;
74123
return;
75124
}
76125
}
77126

78-
X = 0;
79-
Y = 0;
80-
Width = 0;
81-
Height = 0;
127+
Position = Vector2.Zero;
128+
Size = Vector2.Zero;
82129
}
83130

84131
public Rect2(int[]? numbers)
85132
{
86133
if (numbers?.Length == 4)
87134
{
88-
X = numbers[0];
89-
Y = numbers[1];
90-
Width = numbers[2] - X;
91-
Height = numbers[3] - Y;
135+
Position = new Vector2(numbers);
136+
Size = new Vector2(numbers[2], numbers[3]) - Position;
92137
return;
93138
}
94139

95-
X = 0;
96-
Y = 0;
97-
Width = 0;
98-
Height = 0;
140+
Position = Vector2.Zero;
141+
Size = Vector2.Zero;
142+
}
143+
144+
public Rect2(Vector2 max)
145+
{
146+
Position = Vector2.Zero;
147+
Size = max - Position;
148+
}
149+
150+
public Rect2(Vector2 min, Vector2 max)
151+
{
152+
Position = min;
153+
Size = max - Position;
99154
}
100155
}
101156
}

AnnoMapEditor/MapTemplates/Session.cs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.Specialized;
23
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -10,19 +11,23 @@ namespace AnnoMapEditor.MapTemplates
1011
{
1112
public class Session
1213
{
13-
public List<Island> Islands { get; private set; }
14+
public IReadOnlyList<Island> Islands => _islands;
15+
private List<Island> _islands;
16+
1417
public Vector2 Size { get; private set; }
1518
public Rect2 PlayableArea { get; private set; }
1619
public Region Region { get; set; }
1720

1821
private Serializing.A7tinfo.MapTemplateDocument template;
1922

23+
public event NotifyCollectionChangedEventHandler? IslandCollectionChanged;
24+
2025
public string MapSizeText => $"Size: {Size.X}, Playable: {PlayableArea.Width}";
2126

2227
public Session()
2328
{
2429
Size = new Vector2(0, 0);
25-
Islands = new List<Island>();
30+
_islands = new List<Island>();
2631
template = new Serializing.A7tinfo.MapTemplateDocument();
2732
}
2833

@@ -77,7 +82,7 @@ where element?.Element is not null
7782
Session session = new()
7883
{
7984
Region = region,
80-
Islands = new List<Island>(await Task.WhenAll(islands)),
85+
_islands = new List<Island>(await Task.WhenAll(islands)),
8186
Size = new Vector2(document.MapTemplate?.Size),
8287
PlayableArea = new Rect2(document.MapTemplate?.PlayableArea),
8388
template = document
@@ -112,7 +117,7 @@ public async Task UpdateAsync()
112117

113118
return new Serializing.A7tinfo.MapTemplateDocumentExport()
114119
{
115-
MapTemplate = new Serializing.A7tinfo.MapTemplateExport(template.MapTemplate, Islands.Select(x => x.ToTemplate()))
120+
MapTemplate = new Serializing.A7tinfo.MapTemplateExport(template.MapTemplate, Islands.Select(x => x.ToTemplate()).Where(x => x is not null)!)
116121
};
117122
}
118123

@@ -140,5 +145,19 @@ public async Task SaveAsync(string filePath)
140145
file.SetLength(0); // clear
141146
await Serializer.WriteAsync(export, file);
142147
}
148+
149+
public void AddIsland(Island island)
150+
{
151+
island.CreateTemplate();
152+
_islands.Add(island);
153+
Task.Run(async () => await island.InitAsync(Region));
154+
IslandCollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
155+
}
156+
157+
public void RemoveIsland(Island island)
158+
{
159+
_islands.Remove(island);
160+
IslandCollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
161+
}
143162
}
144163
}

AnnoMapEditor/Mods/Mod.cs

+10-13
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public static bool CanSave(Session? session)
4646
return true;
4747
}
4848

49-
public async Task<bool> Save(string modsFolderPath, string modName, string? modID)
49+
public async Task<bool> Save(string modPath, string modName, string? modID)
5050
{
5151
string fullModName = "[Map] " + modName;
52-
string modPath = Path.Combine(modsFolderPath, fullModName);
5352

5453
try
5554
{
@@ -198,26 +197,24 @@ private class MapTemplateInfo
198197
{
199198
[2160] = new[] { "moderate_atoll_ll_01", "moderate_atoll_lm_01", "moderate_atoll_ls_01",
200199
"moderate_islandarc_ll_01", "moderate_islandarc_lm_01", "moderate_islandarc_ls_01" },
201-
202-
[1650] = new[] { "moderate_archipel_ll_01", "moderate_archipel_lm_01", "moderate_archipel_ls_01",
203-
"moderate_corners_ll_02", "moderate_corners_lm_01", "moderate_corners_ls_01",
200+
[1656] = new[] { "moderate_archipel_ll_01", "moderate_archipel_lm_01", "moderate_archipel_ls_01",
201+
"moderate_corners_lm_01", "moderate_corners_ll_02", "moderate_corners_ls_01",
204202
"moderate_snowflake_ll_02", "moderate_snowflake_lm_01", "moderate_snowflake_ls_01" },
205203
[1648] = new[] { "moderate_atoll_ml_01", "moderate_atoll_mm_01", "moderate_atoll_ms_01",
206204
"moderate_islandarc_ml_01", "moderate_islandarc_mm_01", "moderate_islandarc_ms_01" },
207-
[1436] = new[] { "moderate_corners_ml_01" },
205+
[1440] = new[] { "moderate_corners_ml_01" },
208206
[1392] = new[] { "moderate_archipel_ml_01" },
209-
[1366] = new[] { "moderate_snowflake_mm_01" },
210-
[1358] = new[] { "moderate_corners_sl_01" },
211-
[1356] = new[] { "moderate_snowflake_ml_01" },
212-
[1348] = new[] { "moderate_snowflake_sm_01" },
213-
[1338] = new[] { "moderate_corners_sm_01" },
207+
[1368] = new[] { "moderate_snowflake_mm_01" },
208+
[1360] = new[] { "moderate_corners_sl_01", "moderate_snowflake_ml_01" },
209+
[1352] = new[] { "moderate_snowflake_sm_01" },
210+
[1344] = new[] { "moderate_corners_sm_01" },
214211
[1336] = new[] { "moderate_atoll_sl_01", "moderate_atoll_sm_01", "moderate_atoll_ss_01",
215212
"moderate_corners_mm_01", "moderate_corners_ms_01",
216213
"moderate_islandarc_sl_01", "moderate_islandarc_sm_01", "moderate_islandarc_ss_01",
217214
"moderate_snowflake_ms_01" },
218-
[1327] = new[] { "moderate_snowflake_sl_01" },
215+
[1328] = new[] { "moderate_snowflake_sl_01" },
219216
[1312] = new[] { "moderate_archipel_mm_01", "moderate_archipel_ms_01" },
220-
[1220] = new[] { "moderate_archipel_sm_01" },
217+
[1224] = new[] { "moderate_archipel_sm_01" },
221218
[1208] = new[] { "moderate_corners_ss_01", "moderate_snowflake_ss_01" },
222219
[1200] = new[] { "moderate_archipel_sl_01", "moderate_archipel_ss_01" },
223220
};

AnnoMapEditor/Settings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private set
1919
if (_dataArchive is System.IDisposable disposable)
2020
disposable.Dispose();
2121
SetProperty(ref _dataArchive, value);
22+
OnPropertyChanged(nameof(DataPath));
2223
}
2324
}
2425
private IDataArchive _dataArchive = DataArchives.DataArchive.Default;

0 commit comments

Comments
 (0)