public void Init()
 {
     // Create 2 things and a basic MultipleParentsBehavior for testing.
     this.parent1 = new Thing() { Name = "Thing1", ID = TestThingID.Generate("testthing") };
     this.parent2 = new Thing() { Name = "Thing2", ID = TestThingID.Generate("testthing") };
     this.child = new Thing() { Name = "Child1", ID = TestThingID.Generate("testthing") };
     this.multipleParentsBehavior = new MultipleParentsBehavior();
 }
Ejemplo n.º 2
0
        private bool PerformAdd(Thing thingToAdd, AddChildEvent addEvent, MultipleParentsBehavior multipleParentsBehavior)
        {
            if (addEvent.IsCancelled)
            {
                return(false);
            }

            // If an existing thing is stackable with the added thing, combine the new
            // thing into the existing thing instead of simply adding it.
            foreach (Thing currentThing in this.Children)
            {
                if (thingToAdd.CanStack(currentThing))
                {
                    currentThing.Combine(thingToAdd);
                    return(true);
                }
            }

            // The item cannot be stacked so add the item to the specified parent.
            if (!this.Children.Contains(thingToAdd))
            {
                this.Children.Add(thingToAdd);
            }

            // If we don't have a MultipleParentsBehavior, directly set the one-allowed
            // parent ourselves, else use the behavior's logic for setting parents.
            if (multipleParentsBehavior == null)
            {
                thingToAdd.Parent = this;
            }
            else
            {
                multipleParentsBehavior.AddParent(this);
            }

            this.Eventing.OnMovementEvent(addEvent, EventScope.SelfDown);
            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>Perform removal of the specified thing from our Children.</summary>
        /// <param name="thingToRemove">The thing to remove from our Children.</param>
        /// <param name="removalEvent">The removal event to work with; must have previously been sent as the request.</param>
        /// <param name="multipleParentsBehavior">The multipleParentsBehavior, if applicable.</param>
        /// <returns>True if the thing has been successfully removed, else false.</returns>
        private bool PerformRemoval(Thing thingToRemove, RemoveChildEvent removalEvent, MultipleParentsBehavior multipleParentsBehavior)
        {
            if (removalEvent.IsCancelled)
            {
                return(false);
            }

            // Send the removal event.
            this.Eventing.OnMovementEvent(removalEvent, EventScope.SelfDown);

            // If the thing to remove was in our Children collection, remove it.
            if (this.Children.Contains(thingToRemove))
            {
                this.Children.Remove(thingToRemove);
            }

            // If we don't have a MultipleParentsBehavior, directly remove the one-allowed
            // parent ourselves, else use the behavior's logic for adjusting the parents.
            if (multipleParentsBehavior == null)
            {
                thingToRemove.Parent = null;
            }
            else
            {
                multipleParentsBehavior.RemoveParent(this);
            }

            return(true);
        }