using System; using System.Collections.Generic; using System.Drawing; using System.Text.Json; using System.Xml; namespace CoreUnit.Control.UserControl.Canvas { public class DrawingLayer : ICanvasLayer, ISerialize { int m_no; string m_name = ""; Color m_color; float m_width = 0.001f; bool m_enabled = true; bool m_visible = true; public Color Color { get { return m_color; } set { m_color = value; } } public float Width { get { return m_width; } set { m_width = value; } } public string Name { get { return m_name; } set { m_name = value; } } public DrawingLayer(int no, string name, Color color, float width) { m_no = no; m_name = name; m_color = color; m_width = width; } List m_objects = new List(); // Dictionary m_objectMap = new Dictionary(); public void AddObject(IDrawObject drawobject) { if (drawobject is DrawObjectBase) { ((DrawObjectBase)drawobject).Layer = this; } m_objects.Add(drawobject); /* if (m_objectMap.ContainsKey(drawobject)) return; // this should never happen if (drawobject is DrawObjectBase) { ((DrawObjectBase)drawobject).Layer = this; } m_objects.Add(drawobject); m_objectMap[drawobject] = true; * */ } public List DeleteObjects(List objects) { if (Enabled == false) { return null; } List removedobjects = new List(); foreach (IDrawObject obj in objects) { removedobjects.Add(obj); } foreach (IDrawObject obj in removedobjects) { m_objects.Remove(obj); } return removedobjects; /* * if (Enabled == false) { return null; } List removedobjects = new List(); // first remove from map only foreach (IDrawObject obj in objects) { if (m_objectMap.ContainsKey(obj)) { m_objectMap.Remove(obj); removedobjects.Add(obj); } } // need some smart algorithm here to either remove from existing list or build a new list // for now I will just ise the removed count; if (removedobjects.Count == 0) return null; if (removedobjects.Count < 10) // remove from existing list { foreach (IDrawObject obj in removedobjects) m_objects.Remove(obj); } else // else build new list; { List newlist = new List(); foreach (IDrawObject obj in m_objects) { if (m_objectMap.ContainsKey(obj)) newlist.Add(obj); } m_objects.Clear(); m_objects = newlist; } return removedobjects; * */ } public int Count { get { return m_objects.Count; } } public void Copy(DrawingLayer acopy, bool includeDrawObjects) { if (includeDrawObjects) { Objects.Clear(); foreach (IDrawObject obj in acopy.Objects) { Objects.Add(obj.Clone()); } } m_name = acopy.m_name; m_color = acopy.m_color; m_width = acopy.m_width; m_enabled = acopy.m_enabled; m_visible = acopy.m_visible; } #region ICanvasLayer Members public void Draw(ICanvas canvas, RectangleF unitrect) { int cnt = 0; foreach (IDrawObject drawobject in m_objects) { DrawObjectBase obj = drawobject as DrawObjectBase; //Allows drawing into invisible areas //if (obj is IDrawObject && ((IDrawObject)obj).ObjectInRectangle(canvas, unitrect, true) == false) // continue; if (!(obj is IDrawObject)) continue; bool sel = obj.Selected; bool high = obj.Highlighted; obj.Selected = false; drawobject.Draw(canvas, unitrect); obj.Selected = sel; obj.Highlighted = high; cnt++; } } public PointF SnapPoint(PointF unitmousepoint) { return PointF.Empty; } public int No { get { return m_no; } set { m_no = value; } } public ISnapPoint SnapPoint(ICanvas canvas, UnitPoint point, List otherobj) { foreach (IDrawObject obj in m_objects) { ISnapPoint sp = obj.SnapPoint(canvas, point, otherobj, null, null); if (sp != null) return sp; } return null; } public List Objects { get { return m_objects; } } public bool Enabled { get { return m_enabled && m_visible; } set { m_enabled = value; } } public bool Visible { get { return m_visible; } set { m_visible = value; } } public SizeF Spacing { get { return new SizeF(10, 10); } set { } } SolidBrush m_backgroundBrush; SolidBrush m_fieldBrush; Color m_backgroundColor = Color.Black; public Color BackgroundColor { get { return m_backgroundColor; } set { m_color = value; m_backgroundColor = value; m_backgroundBrush = new SolidBrush(m_backgroundColor); } } Color m_fieldColor = Color.White; public Color FieldColor { get { return m_fieldColor; } set { m_fieldColor = value; m_fieldBrush = new SolidBrush(m_fieldColor); } } #endregion #region ISerialize Interface public void GetObjectData(XmlWriter wr) { wr.WriteStartElement("layer"); wr.WriteAttributeString("No", m_no.ToString()); Common.XmlUtil.WriteProperties(this, wr); wr.WriteStartElement("items"); foreach (IDrawObject drawobj in m_objects) { if (drawobj is ISerialize) ((ISerialize)drawobj).GetObjectData(wr); } wr.WriteEndElement(); wr.WriteEndElement(); } public virtual string GetObjectData() { string jsonString = string.Empty; var options = new JsonSerializerOptions { WriteIndented = true }; try { //jsonString += JsonSerializer.Serialize(this, options); foreach (IDrawObject drawobj in m_objects) { if (drawobj is ISerialize) jsonString += ((ISerialize)drawobj).GetObjectData(); } } catch { } return jsonString; } public void GetObjectsData(List objects, XmlWriter wr) { foreach (IDrawObject obj in objects) { if (obj is ISerialize) { ((ISerialize)obj).GetObjectData(wr); } if (obj is Group) { GetObjectsData(((Group)obj).SubObjects, wr); } } } public void AfterSerializedIn() { } public static DrawingLayer NewLayer(XmlElement xmlelement) { int no = Convert.ToInt16(xmlelement.GetAttribute("No")); /* if (id.Length == 0) { id = Guid.NewGuid().ToString(); } */ DrawingLayer layer = new DrawingLayer(no, string.Empty, Color.Black, 0.01f); foreach (XmlElement node in xmlelement.ChildNodes) { Common.XmlUtil.ParseProperty(node, layer); if (node.Name == "items") { foreach (XmlElement itemnode in node.ChildNodes) { object item = DataModel.NewDrawObject(itemnode.Name); if (item == null) { continue; } if (item != null) { Common.XmlUtil.ParseProperties(itemnode, item); if (itemnode.Name == Group.ObjectType) //그룹인 경우에는 그룹에 포함된 객체를 서브객체로 포함시킴 { NewGroup(item, itemnode); } } if (item is ISerialize) { ((ISerialize)item).AfterSerializedIn(); } if (item is IDrawObject) { layer.AddObject(item as IDrawObject); } } } } return layer; } public static void NewGroup(object obj, XmlElement node) { foreach (XmlElement subNode in node.ChildNodes) { if (DataModel.NewDrawObject(subNode.Name) is object subItem) { Common.XmlUtil.ParseProperties(subNode, subItem); ((Group)obj).SubObjects.Add((IDrawObject)subItem); if (subNode.Name == Group.ObjectType) NewGroup(subItem, subNode); } } } #endregion } }