public void Add(Type type, MyComponentBase component) { //System.Diagnostics.Debug.Assert(component == null || component.ContainerBase == null, "Component needs to be removed from a container before adding to a new one!"); System.Diagnostics.Debug.Assert(typeof(MyComponentBase).IsAssignableFrom(type), "Unsupported type of component!"); if (!typeof(MyComponentBase).IsAssignableFrom(type)) { return; } if (component != null) { System.Diagnostics.Debug.Assert(type.IsAssignableFrom(component.GetType()), "Component added with wrong type!"); if (!type.IsAssignableFrom(component.GetType())) { return; } } { //TODO: componentTypeFromAttribute cannot be null when all components has [MyComponentType(typeof(...))] attribute. var componentTypeFromAttribute = MyComponentTypeFactory.GetComponentType(type); if (componentTypeFromAttribute != null && componentTypeFromAttribute != type) { // Failed when component type from attribute is not the same as the given type. Means that [MyComponentType(typeof(...))] // should be specified for the component class (it is probably used for base class now). System.Diagnostics.Debug.Fail("Component: " + component.GetType() + " is set to container as type: " + type + " but type from attribute is: " + componentTypeFromAttribute); } } MyComponentBase containedComponent; if (m_components.TryGetValue(type, out containedComponent)) { //System.Diagnostics.Debug.Assert(containedComponent != component, "Adding a component to a container twice!"); if (containedComponent is IMyComponentAggregate) { (containedComponent as IMyComponentAggregate).AddComponent(component); return; } else if (component is IMyComponentAggregate) { Remove(type); (component as IMyComponentAggregate).AddComponent(containedComponent); m_components[type] = component; component.SetContainer(this); OnComponentAdded(type, component); return; } } Remove(type); if (component != null) { m_components[type] = component; component.SetContainer(this); OnComponentAdded(type, component); } }
public static bool RemoveComponent(this IMyComponentAggregate aggregate, MyComponentBase component) { int index = aggregate.ChildList.GetComponentIndex(component); if (index != -1) { aggregate.BeforeComponentRemove(component); component.SetContainer(null); aggregate.ChildList.RemoveComponentAt(index); return(true); } foreach (var child in aggregate.ChildList.Reader) { var childAggregate = child as IMyComponentAggregate; if (childAggregate == null) { continue; } bool removed = childAggregate.RemoveComponent(component); if (removed) { return(true); } } return(false); }
public static void AddComponent(this IMyComponentAggregate aggregate, MyComponentBase component) { Debug.Assert(aggregate != component, "Can not add to itself!"); if (component.ContainerBase != null) { component.OnBeforeRemovedFromContainer(); } aggregate.ChildList.AddComponent(component); component.SetContainer(aggregate.ContainerBase); aggregate.AfterComponentAdd(component); }
public static void RemoveComponent(this IMyComponentAggregate aggregate, MyComponentBase component) { int index = aggregate.ChildList.GetComponentIndex(component); if (index != -1) { aggregate.BeforeComponentRemove(component); component.SetContainer(null); aggregate.ChildList.RemoveComponentAt(index); } }
private void RemoveComponentInternal(Type t, MyComponentBase c) { c.SetContainer(null); m_components.Remove(t); OnComponentRemoved(t, c); }