Skip to content

Commit 4b5584b

Browse files
committed
Add support for CollectionDefinition contents from a ReferenceDef, or by a collection of keys.
Allow more wildcards in DataTransformer.
1 parent 7258ed6 commit 4b5584b

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

StructuredXmlEditor/Core.xmldef

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@
196196
<String Name="Seperator" Default="," ToolTip="The string to be used when in collapse mode." VisibleIf="Collapse==true" />
197197
<Number Name="MinCount" Min="0" ToolTip="The minimum number of child elements this can have." />
198198
<Number Name="MaxCount" Min="1" Default="99999999" ToolTip="The maximum number of child elements this can have." />
199+
<String Name="Keys" ToolTip="The names of the objects to use in this collection." />
200+
<String Name="DefKey" ToolTip="The name of the ReferenceDef to use in this collection." />
199201
<Boolean Name="SkipIfDefault" ToolTip="Don't write out this element if it is at the default value." Default="true" />
200202
<String Name="VisibleIf" ToolTip="The expression to use to determine whether this element is visible. Leave blank to have always visible." />
201203
<Colour Name="TextColour" ToolTip="The colour on the element name in the ui." Default="Collection" HasAlpha="false" />
@@ -216,6 +218,8 @@
216218
<String Name="Seperator" Default="," ToolTip="The string to be used when in collapse mode." VisibleIf="Collapse==true" />
217219
<Number Name="MinCount" Min="0" ToolTip="The minimum number of child elements this can have." />
218220
<Number Name="MaxCount" Min="1" Default="99999999" ToolTip="The maximum number of child elements this can have." />
221+
<String Name="Keys" ToolTip="The names of the objects to use in this collection." />
222+
<String Name="DefKey" ToolTip="The name of the ReferenceDef to use in this collection." />
219223
<Boolean Name="SkipIfDefault" ToolTip="Don't write out this element if it is at the default value." Default="true" />
220224
<String Name="VisibleIf" ToolTip="The expression to use to determine whether this element is visible. Leave blank to have always visible." />
221225
<Colour Name="TextColour" ToolTip="The colour on the element name in the ui." Default="Collection" HasAlpha="false" />

StructuredXmlEditor/Data/DataTransformer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,16 @@ public List<XElement> GetElements(XElement el, string path)
201201
var nextEls = new List<XElement>();
202202
foreach (var currentEl in els)
203203
{
204-
foreach (var nextEl in currentEl.Elements(part))
204+
if (part == "*")
205205
{
206-
nextEls.Add(nextEl);
206+
nextEls.AddRange(currentEl.Elements());
207+
}
208+
else
209+
{
210+
foreach (var nextEl in currentEl.Elements(part))
211+
{
212+
nextEls.Add(nextEl);
213+
}
207214
}
208215
}
209216

StructuredXmlEditor/Definition/CollectionDefinition.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Windows.Data;
77
using System.Xml.Linq;
88
using StructuredXmlEditor.Data;
9+
using StructuredXmlEditor.View;
910

1011
namespace StructuredXmlEditor.Definition
1112
{
@@ -19,6 +20,8 @@ public class CollectionDefinition : ComplexDataDefinition
1920
public List<DataDefinition> AdditionalDefs { get; } = new List<DataDefinition>();
2021
public int MinCount { get; set; } = 0;
2122
public int MaxCount { get; set; } = int.MaxValue;
23+
public List<Tuple<string, string>> DefKeys { get; set; } = new List<Tuple<string, string>>();
24+
public string DefKey { get; set; }
2225

2326
public CollectionDefinition()
2427
{
@@ -155,6 +158,27 @@ public override void Parse(XElement definition)
155158
MinCount = TryParseInt(definition, "MinCount", 0);
156159
MaxCount = TryParseInt(definition, "MaxCount", int.MaxValue);
157160

161+
DefKey = definition.Attribute("DefKey")?.Value?.ToString();
162+
var keyString = definition.Attribute("Keys")?.Value?.ToString();
163+
if (!string.IsNullOrWhiteSpace(keyString))
164+
{
165+
if (!keyString.Contains('('))
166+
{
167+
DefKeys.AddRange(keyString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => new Tuple<string, string>(e.Trim(), "Type")));
168+
}
169+
else
170+
{
171+
var categories = keyString.Split(new char[] { ')' }, StringSplitOptions.RemoveEmptyEntries);
172+
foreach (var categoryString in categories)
173+
{
174+
var split = categoryString.Split('(');
175+
var category = split[0].Trim();
176+
if (category.StartsWith(",")) category = category.Substring(1);
177+
DefKeys.AddRange(split[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => new Tuple<string, string>(e.Trim(), category)));
178+
}
179+
}
180+
}
181+
158182
var currentGroup = "Items";
159183

160184
var childDefs = definition.Nodes();
@@ -180,7 +204,7 @@ public override void Parse(XElement definition)
180204
}
181205
}
182206

183-
if (ChildDefinitions.Count == 0)
207+
if (ChildDefinitions.Count == 0 && DefKey == null && DefKeys.Count == 0)
184208
{
185209
throw new Exception("No child definitions in collection '" + Name + "'!");
186210
}
@@ -289,6 +313,46 @@ public override void DoSaveData(XElement parent, DataItem item)
289313

290314
public override void RecursivelyResolve(Dictionary<string, DataDefinition> local, Dictionary<string, DataDefinition> global, Dictionary<string, Dictionary<string, DataDefinition>> referenceableDefinitions)
291315
{
316+
if (DefKey != null)
317+
{
318+
var key = DefKey.ToLower();
319+
320+
Dictionary<string, DataDefinition> defs = null;
321+
if (local.ContainsKey(key)) defs = local;
322+
else if (global.ContainsKey(key)) defs = global;
323+
324+
if (defs != null)
325+
{
326+
var def = defs[key] as ReferenceDefinition;
327+
DefKeys = def.Keys;
328+
}
329+
else
330+
{
331+
Message.Show("Failed to find key " + DefKey + "!", "Collection Resolve Failed", "Ok");
332+
}
333+
}
334+
335+
foreach (var key in DefKeys)
336+
{
337+
Dictionary<string, DataDefinition> defs = null;
338+
if (local.ContainsKey(key.Item1.ToLower())) defs = local;
339+
else if (global.ContainsKey(key.Item1.ToLower())) defs = global;
340+
341+
if (defs != null)
342+
{
343+
var childDef = defs[key.Item1.ToLower()];
344+
var childWrapperDef = new CollectionChildDefinition();
345+
childWrapperDef.WrappedDefinition = childDef;
346+
347+
ChildDefinitions.Add(childWrapperDef);
348+
Keys.Add(new Tuple<CollectionChildDefinition, string>(childWrapperDef, key.Item2));
349+
}
350+
else if (key.Item1 != "---")
351+
{
352+
Message.Show("Failed to find key " + key.Item1 + "!", "Collection Resolve Failed", "Ok");
353+
}
354+
}
355+
292356
foreach (var def in ChildDefinitions) def.WrappedDefinition.RecursivelyResolve(local, global, referenceableDefinitions);
293357
foreach (var def in AdditionalDefs) def.RecursivelyResolve(local, global, referenceableDefinitions);
294358
}

0 commit comments

Comments
 (0)