#region usings using System.Collections.Generic; using System.Collections.ObjectModel; using SM.Base.Contexts; using SM.Base.Drawing; #endregion namespace SM.Base.Scene { /// /// Contains a list of show items. /// /// The type of show items. public abstract class GenericItemCollection : List, IShowItem, IShowCollection, IScriptable where TItem : IShowItem { private List _scriptableObjects = new List(); /// /// Currently active script objects. /// public ReadOnlyCollection ScriptableObjects => new ReadOnlyCollection(_scriptableObjects); /// public List Objects => this; /// public object Parent { get; set; } /// public string Name { get; set; } = "Unnamed Item Collection"; /// public ICollection Flags { get; set; } = new List() {"collection"}; /// public virtual void Update(UpdateContext context) { for (var i = 0; i < _scriptableObjects.Count; i++) _scriptableObjects[i].Update(context); } /// public virtual void Draw(DrawContext context) { context.LastPassthough = this; for (var i = 0; i < Objects.Count; i++) this[i].Draw(context); } /// public virtual void OnAdded(object sender) { } /// public virtual void OnRemoved(object sender) { } /// /// Adds a item to the draw and the script collection, when applicable. /// public new void Add(TItem item) { AddObject(item); if (item is IScriptable scriptable) AddScript(scriptable); } /// /// Adds the object to the collection. /// /// public void AddObject(TItem item) { base.Add(item); item.Parent = this; item.OnAdded(this); } /// /// Adds the script to the collection. /// /// public void AddScript(IScriptable item) { _scriptableObjects.Add(item); } /// /// Removes a item from the draw and script collection, when applicable. /// /// public new void Remove(TItem item) { RemoveObject(item); if (item.GetType().IsAssignableFrom(typeof(IScriptable))) RemoveScript((IScriptable)item); } /// /// Remove the object from the draw collection. /// /// public void RemoveObject(TItem item) { base.Remove(item); item.Parent = null; item.OnRemoved(this); } /// /// Remove the object from the script collection. /// /// public void RemoveScript(IScriptable item) { _scriptableObjects.Remove(item); } /// /// Returns a object with this name or the default, if not available. /// Not reclusive. /// /// /// public TItem GetItemByName(string name) { TItem obj = default; for (var i = 0; i < Count; i++) if (this[i].Name == name) { obj = this[i]; break; } return obj; } /// /// Returns a object with this name or the default if not available. /// Not reclusive. /// /// Type of return public TGetItem GetItemByName(string name) where TGetItem : TItem { return (TGetItem) GetItemByName(name); } /// /// Returns all object that have this flag. /// Only in this list. /// public ICollection GetItemsWithFlag(string flag) { var list = new List(); for (var i = 0; i < Count; i++) { var obj = this[i]; if (obj.Flags == null) continue; if (obj.Flags.Contains(flag)) list.Add(obj); } return list; } } /// /// Contains a list of show items with transformation. /// /// The type of show items. /// The type of transformation. public abstract class GenericItemCollection : GenericItemCollection where TItem : IShowItem where TTransformation : GenericTransformation, new() { /// /// Transformation of the collection /// public TTransformation Transform = new TTransformation(); /// public override void Draw(DrawContext context) { context.ModelMaster = Transform.GetMatrix() * context.ModelMaster; base.Draw(context); } } }