Skip to content

Commit

Permalink
Custom Attributes initial implementation (JasonLautzenheiser#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
MidiHax committed Feb 23, 2021
1 parent e66738a commit e3f2ff7
Show file tree
Hide file tree
Showing 13 changed files with 2,567 additions and 1,905 deletions.
9 changes: 8 additions & 1 deletion Domain/Elements/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ public object BeginLoad(XmlElementReader element) {
VertexList.Add(vertex);
}

LoadCustomAttributes(element["customAttributes"]);

return vertexElementList;
}

Expand Down Expand Up @@ -325,7 +327,7 @@ public override void Draw(XGraphics graphics, Palette palette, DrawingContext co
}

public void EndLoad(object state) {
var elements = (List<XmlElementReader>) state;
var elements = ((List<XmlElementReader>) state).Where((w) => w.HasName("dock")).ToList();
for (var index = 0; index < elements.Count; ++index) {
var element = elements[index];
if (element.HasName("dock"))
Expand Down Expand Up @@ -559,6 +561,8 @@ public void Save(XmlScribe scribe) {

++index;
}

SaveCustomAttributes(scribe);
}

public void SetText(string start, string mid, string end) {
Expand All @@ -583,6 +587,7 @@ public override void ShowDialog() {
dialog.EndText = EndText;
dialog.ConnectionColor = ConnectionColor;
dialog.Door = Door;
dialog.LoadCustomAttributes(CustomAttributes);
if (dialog.ShowDialog(Project.Canvas) == DialogResult.OK) {
Name = dialog.ConnectionName;
Description = dialog.ConnectionDescription;
Expand All @@ -593,6 +598,8 @@ public override void ShowDialog() {
MidText = dialog.MidText;
EndText = dialog.EndText;
Door = dialog.Door;
CustomAttributes.Clear();
CustomAttributes.AddRange(dialog.GetCustomAttributes());
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions Domain/Elements/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ THE SOFTWARE.
using PdfSharp.Drawing;
using Trizbort.Domain.Application;
using Trizbort.Domain.Misc;
using Trizbort.Util;

namespace Trizbort.Domain.Elements {
public class Element : IComparable<Element> {
Expand Down Expand Up @@ -91,6 +92,8 @@ public int ID {
[JsonIgnore]
public List<Port> PortList { get; set; } = new List<Port>();

public virtual List<CustomAttribute> CustomAttributes { get; set; } = new List<CustomAttribute>();

public virtual Vector Position { get; set; }

[JsonIgnore]
Expand Down Expand Up @@ -251,6 +254,43 @@ public virtual Rect UnionBoundsWith(Rect rect, bool includeMargins) {
return new Rect();
}

public virtual void LoadCustomAttributes(XmlElementReader element)
{
if (element == null)
{
return;
}

CustomAttributes.Clear();

foreach (var childElement in element.Children)
{
CustomAttributes.Add(new CustomAttribute
{
Name = childElement.Attribute("name").Text,
DataType = childElement.Attribute("dataType").Text,
Value = childElement.Attribute("value").Text,
});
}
}

public virtual void SaveCustomAttributes(XmlScribe scribe)
{
if (CustomAttributes == null)
{
return;
}

scribe.StartElement("customAttributes");

CustomAttributes.ForEach((ca) =>
{
ca.Save(scribe);
});

scribe.EndElement();
}

/// <summary>
/// Raise the Changed event.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions Domain/Elements/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,8 @@ public void Load(XmlElementReader element) {
ObjectsCustomPosition = element["objects"].Attribute("custom").ToBool();
ObjectsCustomPositionRight = element["objects"].Attribute("customRight").ToInt();
ObjectsCustomPositionDown = element["objects"].Attribute("customDown").ToInt();

LoadCustomAttributes(element["customAttributes"]);
}

public bool MatchDescription(string description) {
Expand Down Expand Up @@ -1333,6 +1335,8 @@ public void Save(XmlScribe scribe) {
scribe.Value(Objects.Replace("\r", string.Empty).Replace("|", "\\|").Replace("\n", "|"));
scribe.EndElement();
}

SaveCustomAttributes(scribe);
}

public void ShowDialog(PropertiesStartType startPoint) {
Expand Down Expand Up @@ -1453,6 +1457,7 @@ private void showRoomDialog(PropertiesStartType start = PropertiesStartType.Room
dialog.StraightEdges = StraightEdges;
dialog.AllCornersEqual = AllCornersEqual;
dialog.Shape = Shape;
dialog.LoadCustomAttributes(CustomAttributes);

if (dialog.ShowDialog(Project.Canvas) == DialogResult.OK) {
Name = dialog.RoomName;
Expand Down Expand Up @@ -1492,6 +1497,9 @@ private void showRoomDialog(PropertiesStartType start = PropertiesStartType.Room
Ellipse = dialog.Ellipse;
StraightEdges = dialog.StraightEdges;
AllCornersEqual = dialog.AllCornersEqual;

CustomAttributes.Clear();
CustomAttributes.AddRange(dialog.GetCustomAttributes());
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions Domain/Misc/CustomAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright (c) 2010-2018 by Genstein and Jason Lautzenheiser.
This file is (or was originally) part of Trizbort, the Interactive Fiction Mapper.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using Trizbort.Util;

namespace Trizbort.Domain.Misc
{
public class CustomAttribute : CustomAttributeDefinition
{
public string Value { get; set; }

public virtual void Save(XmlScribe scribe)
{
scribe.StartElement("customAttribute");

scribe.Attribute("name", Name);
scribe.Attribute("dataType", DataType);
scribe.Attribute("value", Value);

scribe.EndElement();
}

public virtual void Load(XmlElementReader element)
{
Name = element.Attribute("name").Text;
DataType = element.Attribute("dataType").Text;
Value = element.Attribute("value").Text;
}
}
}
104 changes: 104 additions & 0 deletions Domain/Misc/CustomAttributeDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright (c) 2010-2018 by Genstein and Jason Lautzenheiser.
This file is (or was originally) part of Trizbort, the Interactive Fiction Mapper.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using Trizbort.Util;

namespace Trizbort.Domain.Misc
{
public class CustomAttributeDefinition : IEquatable<CustomAttributeDefinition>
{
public virtual string Name { get; set; }

public virtual string DataType { get; set; } = "String"; // Future proofing, not actively used currently

public virtual string ObjectType { get; set; } // "room" or "connection"

public virtual void Save(XmlScribe scribe)
{
scribe.StartElement("attributeDefinition");

scribe.Attribute("name", Name);
scribe.Attribute("dataType", DataType);
scribe.Attribute("objectType", ObjectType);

scribe.EndElement();
}

public virtual void Load(XmlElementReader element)
{
Name = element.Attribute("name").Text;
DataType = element.Attribute("dataType").Text;
ObjectType = element.Attribute("objectType").Text;
}

public virtual string ToExportFileLine()
{
return $"{Name}|{DataType}|{ObjectType}";
}

public int GetHashCode(object obj)
{
var hash = 17;
hash *= 23 + Name.GetHashCode();
hash *= 23 + DataType.GetHashCode();
hash *= 23 + ObjectType.GetHashCode();
return hash;
}

public bool Equals(CustomAttributeDefinition other)
{
return Name == other.Name && DataType == other.DataType && ObjectType == other.ObjectType;
}

public static CustomAttributeDefinition FromImportFileLine(string line)
{
var fields = line.Split('|');

if (fields.Length != 3)
{
return null;
}

var newDef = new CustomAttributeDefinition
{
Name = fields[0],
DataType = fields[1],
ObjectType = fields[2],
};

if (newDef.DataType != "String")
{
return null;
}

if (newDef.ObjectType != "room" && newDef.ObjectType != "connection")
{
return null;
}

return newDef;
}
}
}
26 changes: 26 additions & 0 deletions Setup/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ public static float TextOffsetFromConnection {
}
}

public static List<CustomAttributeDefinition> CustomAttributeDefinitions { get; set; } = new List<CustomAttributeDefinition>();

public static event EventHandler Changed;

public static void Load(XmlElementReader element) {
Expand Down Expand Up @@ -418,6 +420,19 @@ public static void Load(XmlElementReader element) {

KeypadNavigationCreationModifier = stringToModifierKeys(element["keypadNavigation"]["creationModifier"].Text, sKeypadNavigationCreationModifier);
KeypadNavigationUnexploredModifier = stringToModifierKeys(element["keypadNavigation"]["unexploredModifier"].Text, sKeypadNavigationUnexploredModifier);

var attributeDefinitions = element["customAttributeDefinitions"];
if (attributeDefinitions != null)
{
CustomAttributeDefinitions.Clear();

foreach (var childElement in attributeDefinitions.Children)
{
var definition = new CustomAttributeDefinition();
definition.Load(childElement);
CustomAttributeDefinitions.Add(definition);
}
}
}


Expand Down Expand Up @@ -548,6 +563,12 @@ public static void Save(XmlScribe scribe) {
scribe.Element("creationModifier", modifierKeysToString(sKeypadNavigationCreationModifier));
scribe.Element("unexploredModifier", modifierKeysToString(sKeypadNavigationUnexploredModifier));
scribe.EndElement();

scribe.StartElement("customAttributeDefinitions");
foreach (var cad in CustomAttributeDefinitions) {
cad.Save(scribe);
}
scribe.EndElement();
}

public static void ShowMapDialog() {
Expand Down Expand Up @@ -591,6 +612,7 @@ public static void ShowMapDialog() {
dialog.WrapTextAtDashes = WrapTextAtDashes;
dialog.ConnectionArrowSize = ConnectionArrowSize;
dialog.DefaultRoomShape = DefaultRoomShape;
dialog.CustomAttributeDefinitions = CustomAttributeDefinitions;
if (dialog.ShowDialog() == DialogResult.OK) {
for (var index = 0; index < Colors.Count; ++index) {
if (Color[index] != dialog.ElementColors[index]) Project.Current.IsDirty = true;
Expand Down Expand Up @@ -669,6 +691,10 @@ public static void ShowMapDialog() {
if (regNameList[index] != newReg[index].RegionName)
Project.Current.IsDirty = true;
}

// Custom Attribute Definitions take effect even if we Cancel out since elements are updated immediately.
if (!Enumerable.SequenceEqual(CustomAttributeDefinitions, dialog.CustomAttributeDefinitions)) Project.Current.IsDirty = true;
CustomAttributeDefinitions = dialog.CustomAttributeDefinitions;
}
}

Expand Down
2 changes: 2 additions & 0 deletions Trizbort.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
<Compile Include="Domain\Misc\ConnectionIconBlock.cs" />
<Compile Include="Domain\Elements\Door.cs" />
<Compile Include="Domain\Enums\ValidationType.cs" />
<Compile Include="Domain\Misc\CustomAttribute.cs" />
<Compile Include="Domain\Misc\CustomAttributeDefinition.cs" />
<Compile Include="Domain\RoomValidationState.cs" />
<Compile Include="Domain\SerializeHelpers\ElementConverter.cs" />
<Compile Include="Domain\SerializeHelpers\PortConverter.cs" />
Expand Down
Loading

0 comments on commit e3f2ff7

Please sign in to comment.