22 public class CUIMapLink
24 internal static void InitStatic()
26 CUI.OnInit += () => Default =
new CUIMapLink(
null,
null);
27 CUI.OnDispose += () => Default =
null;
29 public static CUIMapLink Default;
35 public string StartAKA;
37 public float LineWidth;
38 public Color LineColor;
40 public XElement ToXML()
42 XElement connection =
new XElement(
"Connection");
43 if (LineWidth != Default.LineWidth)
45 connection.SetAttributeValue(
"LineWidth", LineWidth);
47 connection.SetAttributeValue(
"Start", StartAKA ??
"");
48 connection.SetAttributeValue(
"End", EndAKA ??
"");
55 LineColor = lineColor ??
new Color(128, 128, 128);
56 LineWidth = lineWidth;
60 StartAKA = start?.
AKA;
65 #region LinksContainer
69 public List<CUIMapLink> Connections =
new List<CUIMapLink>();
71 public override void Draw(SpriteBatch spriteBatch)
73 base.Draw(spriteBatch);
75 foreach (CUIMapLink link
in Connections)
77 Vector2 midPoint =
new Vector2(link.End.
Real.Center.X, link.Start.
Real.Center.Y);
79 GUI.DrawLine(spriteBatch,
80 link.Start.
Real.Center,
82 link.LineColor, width: link.LineWidth
85 GUI.DrawLine(spriteBatch,
88 link.LineColor, width: link.LineWidth
93 public LinksContainer()
97 Border.Color = Color.Transparent;
104 public LinksContainer linksContainer;
105 public List<CUIMapLink> Connections => linksContainer.Connections;
113 if (startComponent !=
null && endComponent !=
null)
115 if (color ==
null && (!startComponent.
Disabled || !endComponent.
Disabled)) color =
new Color(0, 0, 255);
116 linksContainer.Connections.Add(
new CUIMapLink(startComponent, endComponent, color));
118 return startComponent;
122 end = MathUtils.PositiveModulo(end, Children.Count);
123 CUIComponent endComponent = Children.ElementAtOrDefault(end);
124 return Connect(startComponent, endComponent, color);
128 public CUIComponent Connect(
string start,
string end, Color? color =
null)
133 if (startComponent !=
null && endComponent !=
null)
135 if (color ==
null && (!startComponent.
Disabled || !endComponent.
Disabled)) color =
new Color(0, 0, 255);
136 linksContainer.Connections.Add(
new CUIMapLink(startComponent, endComponent, color)
142 return startComponent;
144 public CUIComponent Connect(
int start,
int end, Color? color =
null)
146 start = MathUtils.PositiveModulo(start, Children.Count);
147 end = MathUtils.PositiveModulo(end, Children.Count);
149 CUIComponent startComponent = Children.ElementAtOrDefault(start);
150 CUIComponent endComponent = Children.ElementAtOrDefault(end);
151 return Connect(startComponent, endComponent, color);
156 foreach (
CUIComponent child
in children) { Connect(Host, child); }
161 public override XElement ToXML(CUIAttribute propAttribute = CUIAttribute.CUISerializable)
163 Type type = GetType();
165 XElement element =
new XElement(type.Name);
167 PackProps(element, propAttribute);
169 XElement connections =
new XElement(
"Connections");
170 element.Add(connections);
172 foreach (CUIMapLink link
in Connections)
174 connections.Add(link.ToXML());
177 XElement children =
new XElement(
"Children");
178 element.Add(children);
182 if (child == linksContainer)
continue;
183 children.Add(child.ToXML());
190 public override void FromXML(XElement element,
string baseFolder =
null)
192 foreach (XElement childElement
in element.Element(
"Children").Elements())
194 Type childType = CUIReflection.GetComponentTypeByName(childElement.Name.ToString());
195 if (childType ==
null)
continue;
198 child.FromXML(childElement);
203 foreach (XElement link
in element.Element(
"Connections").Elements())
205 CUIComponent startComponent =
this[link.Attribute(
"Start").Value];
206 CUIComponent endComponent =
this[link.Attribute(
"End").Value];
208 if (startComponent ==
null || endComponent ==
null)
210 CUIDebug.Error(
"startComponent == null || endComponent == null");
213 Connect(link.Attribute(
"Start").Value, link.Attribute(
"End").Value);
218 ExtractProps(element);
224 ConsumeMouseClicks =
true;
230 this[
"links"] = linksContainer =
new LinksContainer();
236 m.MousePosition -
Real.Position,