using System; using System.Collections.Generic; using Chernobyl.Update; namespace Chernobyl.Input.Controls { /// /// A helper class that implements a default control and makes creating /// IControls easier. This class also defines some static helper methods /// for working with s. /// public abstract class Control : Updateable, IControl { /// /// Constructor. /// protected Control() : this(new List()) { } /// /// Constructor. /// /// The list to use as a child control list. protected Control(ICollection controlChildrenList) { ControlChildren = controlChildrenList; } /// /// The name of this control. /// public string Name { get; set; } /// /// A helper method that takes one and makes it /// the parent of another. If a previous parent is attached to child, /// that parent will first be separated from the child passed in. This /// method also makes the parent and child into /// parent and child; i.e. it is the same as calling: /// /// Control.MakeParentChild(parent, child, true); /// /// /// The that is to become the /// parent of the . /// The that is to become the /// child of the . public static void MakeParentChild(IControl parent, IControl child) { MakeParentChild(parent, child, true); } /// /// A helper method that takes one and makes it /// the parent of another. If a previous parent is attached to child, /// that parent will first be separated from the child passed in. /// /// The that is to become the /// parent of the . /// The that is to become the /// child of the . /// True if the /// and should be made /// into parent and child also, false if /// otherwise. public static void MakeParentChild(IControl parent, IControl child, bool makeUpdateableParentChild) { parent.ControlChildren.Add(child); // if the child already has a parent, have them separate if (child.ControlParent != null) child.ControlParent.ControlChildren.Remove(child); child.ControlParent = parent; if(makeUpdateableParentChild == true) Updateable.MakeParentChild(parent, child); } /// /// A helper method that splits apart a parent/child relationship of two /// s. The child's parent is first checked to make /// sure it is the parent passed in. If it is, then the child's parent /// property is set to null. If not, the parent property on the child is /// left alone. If the parent and child are /// parent and child as well, then they will be separated. /// /// The parent that is to be /// separated from the . /// The child that is to be /// separated from the . public static void SeparateParentChild(IControl parent, IControl child) { // ensure these two are actually parented before separating them if (child.ControlParent == parent) { parent.ControlChildren.Remove(child); child.ControlParent = null; Updateable.SeparateParentChild(parent, child); } } /// /// Shallow copies the data from the /// to the /// . /// /// The to copy the /// data to. /// The to copy the data /// from. public static void ShallowCopy(IControl destination, IControl source) { Updateable.ShallowCopy(destination, source); destination.Name = source.Name; destination.ControlParent = source.ControlParent; foreach (IControl controlChild in source.ControlChildren) MakeParentChild(destination, controlChild); } /// /// The parent of this control. /// public IControl ControlParent { get; set; } /// /// The children of this control /// public ICollection ControlChildren { get; protected set; } /// /// An event that is raised when this is /// disconnected from the device it was previously connected to and is /// no longer available for use. Actions performed on the /// after it has been disconnected may result /// is an being thrown. /// public virtual event EventHandler Disconnected { add { DisconnectedMethod += value; } remove { DisconnectedMethod -= value; } } /// /// The method that acts as the backing property to /// . Use this method if you wish to raise the /// event. /// protected EventHandler DisconnectedMethod { get; private set; } } }