Beispiel #1
0
        public void GrowNode(ImagePair imagePair)
        {
            if (IsEmptyLeaf())
            {
                throw new Exception("Attempted to grow an empty leaf");
            }

            var imageW   = imagePair.Image.Width;
            var imageH   = imagePair.Image.Height;
            var newChild = Clone();

            Children.Clear();
            Children.Add(newChild);
            Image     = null;
            ImageName = "";
            if (Rect.ShouldGrowVertically(imageW, imageH))
            {
                Children.Add(new Sprite(null, new Rect(Rect.X, Rect.Y + Rect.H, Rect.W, imageH)));
                Rect.H += imageH;
            }
            else
            {
                Children.Add(new Sprite(null, new Rect(Rect.X + Rect.W, Rect.Y, imageW, Rect.H)));
                Rect.W += imageW;
            }
            Children[1].SplitNode(imagePair);
        }
Beispiel #2
0
        public void SplitNode(ImagePair imagePair)
        {
            if (!IsLeaf())
            {
                throw new Exception("Attempted to split non-leaf");
            }

            var imageW = imagePair.Image.Width;
            var imageH = imagePair.Image.Height;

            if (!Rect.CanContain(imageW, imageH))
            {
                throw new Exception("Attempted to place an img in a node it doesn't fit");
            }

            //if it fits exactly then we are done
            if (Rect.IsCongruentWith(imageW, imageH))
            {
                ImageName = imagePair.Path;
                Image     = imagePair.Image;
            }
            else
            {
                if (Rect.ShouldSplitVertically(imageW, imageH))
                {
                    var vertRects = Rect.SplitVertical(imageH);
                    var top       = new Sprite(null, vertRects[0]);
                    var bottom    = new Sprite(null, vertRects[1]);
                    Children.Add(top);
                    Children.Add(bottom);
                }
                else
                {
                    var horzRects = Rect.SplitHorizontal(imageW);
                    var left      = new Sprite(null, horzRects[0]);
                    var right     = new Sprite(null, horzRects[1]);
                    Children.Add(left);
                    Children.Add(right);
                }
                Children[0].SplitNode(imagePair);
            }
        }
Beispiel #3
0
 public Sprite(ImagePair imagePair, Rect rect, List <Sprite> children = null)
 {
     if (null != imagePair)
     {
         ImageName = imagePair.Path;
         Image     = imagePair.Image;
     }
     else
     {
         ImageName = "";
         Image     = null;
     }
     Rect   = rect;
     Offset = new Offset(0, 0);
     if (null != children)
     {
         Children = children;
     }
     else
     {
         Children = new List <Sprite>();
     }
 }