A DisplayObjectContainer represents a collection of display objects. It is the base class of all display objects that act as a container for other objects. By maintaining an ordered list of children, it defines the back-to-front positioning of the children within the display tree. A container does not have size in itself. The width and height properties represent the extents of its children. Changing those properties will scale all children accordingly. As this is an abstract class, you can't instantiate it directly, but have to use a subclass instead. The most lightweight container class is Sprite. **Adding and removing children** The class defines methods that allow you to add or remove children. When you add a child, it will be added at the foremost position, possibly occluding a child that was added before. You can access the children via an index. The first child will have index 0, the second child index 1, etc. Adding and removing objects from a container triggers events. - 'Added': the object was added to a DisplayObjectContainer. - 'AddedToStage': the object was added to a DisplayObjectContainer that is connected to the stage. - 'Removed': the object was removed from a DisplayObjectContainer. - 'RemovedFromStage': the object was removed from a DisplayObjectContainer that is connected to the stage. Especially the AddedToStage event is very helpful, as it allows you to automatically execute some logic (e.g. start an animation) when an object is rendered the first time. **Sorting children** The 'sortChildren' method allows you to sort the children of a container by a custom criteria. Below is an example how to depth-sort children by their y-coordinate; this will put objects that are lower on the screen in front of those higher on the screen. public class CompareExample : IComparator { public int Compare(DisplayObject child1, DisplayObject child2) { if (child1.Y < child2.Y) return -1; else if (child1.Y > child2.Y) return 1; else return 0; } }
Inheritance: DisplayObject
Esempio n. 1
0
        internal void BroadcastRemovedFromStageEvent(DisplayObjectContainer currentTarget)
        {
            RemovedFromStage?.Invoke(this, currentTarget);
            var displayObjectContainer = this as DisplayObjectContainer;

            if (displayObjectContainer != null)
            {
                // We need to make a copy here because the Children list might be modified in an RemovedFromStage event handler
                List <DisplayObject> copy = new List <DisplayObject>(displayObjectContainer.Children);
                foreach (var child in copy)
                {
                    child.BroadcastRemovedFromStageEvent(currentTarget);
                }
            }
        }
Esempio n. 2
0
 internal void BroadcastRemovedFromStageEvent(DisplayObjectContainer currentTarget)
 {
     if (RemovedFromStage != null)
     {
         RemovedFromStage(this, currentTarget);
     }
     var displayObjectContainer = this as DisplayObjectContainer;
     if (displayObjectContainer != null)
     {
         // We need to make a copy here because the Children list might be modified in an RemovedFromStage event handler
         List<DisplayObject> copy = new List<DisplayObject>(displayObjectContainer.Children);
         foreach (var child in copy)
         {
             child.BroadcastRemovedFromStageEvent(currentTarget);
         }
     }
 }
Esempio n. 3
0
        public static int Compile(DisplayObject displayObject, List <QuadBatch> quadBatches, int quadBatchID,
                                  Matrix transformationMatrix, float alpha, uint blendMode)
        {
            bool  isRootObject = false;
            float objectAlpha  = displayObject.Alpha;

            Quad      quad  = displayObject is Quad ? (Quad)displayObject : null;
            QuadBatch batch = displayObject is QuadBatch ? (QuadBatch)displayObject : null;
            DisplayObjectContainer container = displayObject is DisplayObjectContainer ? (DisplayObjectContainer)displayObject : null;

            if (quadBatchID == -1)
            {
                isRootObject = true;
                quadBatchID  = 0;
                objectAlpha  = 1.0f;
                blendMode    = displayObject.BlendMode;

                if (quadBatches.Count == 0)
                {
                    quadBatches.Add(new QuadBatch());
                }
                else
                {
                    quadBatches[0].Reset();
                }
            }

            if (container != null)
            {
                Matrix childMatrix = Matrix.Create();
                childMatrix.Identity();
                int numChildren = container.NumChildren;
                for (int i = 0; i < numChildren; i++)
                {
                    DisplayObject child = container.GetChild(i);
                    if (child.HasVisibleArea)
                    {
                        uint childBlendMode = child.BlendMode;
                        if (childBlendMode == Sparrow.Display.BlendMode.AUTO)
                        {
                            childBlendMode = blendMode;
                        }

                        childMatrix.CopyFromMatrix(transformationMatrix);
                        childMatrix.PrependMatrix(child.TransformationMatrix);

                        quadBatchID = Compile(child, quadBatches, quadBatchID, childMatrix, alpha * objectAlpha, childBlendMode);
                    }
                }
            }
            else if (quad != null)
            {
                Texture texture = quad.Texture;
                bool    tinted  = quad.Tinted;
                bool    pma     = quad.PremultipliedAlpha;

                QuadBatch currentBatch = quadBatches[quadBatchID];
                if (currentBatch.IsStateChange(tinted, texture, alpha * objectAlpha, pma, blendMode, 1))
                {
                    quadBatchID++;

                    if (quadBatches.Count <= quadBatchID)
                    {
                        quadBatches.Add(new QuadBatch());
                    }

                    currentBatch = quadBatches[quadBatchID];
                    currentBatch.Reset();
                }

                currentBatch.AddQuad(quad, alpha * objectAlpha, blendMode, transformationMatrix);
            }
            else if (batch != null)
            {
                Texture texture  = quad.Texture;
                bool    tinted   = quad.Tinted;
                bool    pma      = quad.PremultipliedAlpha;
                int     numQuads = batch.NumQuads;

                QuadBatch currentBatch = quadBatches[quadBatchID];
                if (currentBatch.IsStateChange(tinted, texture, alpha * objectAlpha, pma, blendMode, numQuads))
                {
                    quadBatchID++;

                    if (quadBatches.Count <= quadBatchID)
                    {
                        quadBatches.Add(new QuadBatch());
                    }

                    currentBatch = quadBatches[quadBatchID];
                    currentBatch.Reset();
                }

                currentBatch.AddQuadBatch(batch, alpha * objectAlpha, blendMode, transformationMatrix);
            }
            else
            {
                throw new InvalidOperationException("Unsupported display object");
            }

            if (!isRootObject)
            {
                return(quadBatchID);
            }

            // remove unused batches
            for (int i = quadBatches.Count - 1; i > quadBatchID; --i)
            {
                quadBatches.RemoveAt(quadBatches.Count - 1);
            }
            return(quadBatchID);
        }