Exemplo n.º 1
0
        public Peg(Fitter fitter)
        {
            InitializeComponent();

            dropZone = new DropZone(fitter);
            Grid.SetRow(dropZone, 2);
            Grid.SetColumn(dropZone, 0);
            Grid.SetColumnSpan(dropZone, 2);
            mainGrid.Children.Add(dropZone);

            slot           = new PegSlot(fitter);
            slot.MinHeight = 70;
            slot.MinWidth  = 190;
            Grid.SetRow(slot, 0);
            Grid.SetRowSpan(slot, 2);
            Grid.SetColumn(slot, 1);
            mainGrid.Children.Add(slot);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attach a Moveable to the spine, either onto a free adjacent peg,
        /// or onto a newly-created peg.
        /// </summary>
        protected void AttachToSpine(object sender, DragEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }
            if (!e.Data.GetDataPresent(typeof(Moveable)))
            {
                return;
            }
            if (!(e.AllowedEffects == DragDropEffects.Copy || e.AllowedEffects == DragDropEffects.Move))
            {
                return;
            }

            Moveable moveable = e.Data.GetData(typeof(Moveable)) as Moveable;

            if (fitter.Fits(moveable))
            {
                DropZone dropZone = sender as DropZone;
                if (dropZone == null)
                {
                    return;
                }

                Peg above = UIHelper.TryFindParent <Peg>(dropZone);
                if (above == null)
                {
                    return;
                }

                int index = Pegs.IndexOf(above);
                if (index == -1)
                {
                    throw new InvalidOperationException("Peg not found on spine.");
                }
                index++;

                Peg below;
                if (index < Pegs.Count)
                {
                    below = (Peg)Pegs[index];
                }
                else
                {
                    below = null;
                }

                Peg target = null;

                // If a moveable has been dropped just above or below
                // itself, do nothing. Otherwise, try to use an empty
                // peg, or create a new peg if there isn't one:

                bool pointlessMove =
                    e.AllowedEffects == DragDropEffects.Move &&
                    ((above != null && above.Slot.Contents == moveable) || (below != null && below.Slot.Contents == moveable));

                if (pointlessMove)
                {
                    e.Handled = true;
                    return;
                }
                else if (below != null && below.Slot.Contents == null)
                {
                    target = below;
                }
                else if (above != null && above.Slot.Contents == null)
                {
                    target = above;
                }
                else
                {
                    target = AddPeg(false, index);
                }

                if (e.AllowedEffects == DragDropEffects.Copy)
                {
                    target.Slot.Contents = moveable.DeepCopy();
                }
                else if (e.AllowedEffects == DragDropEffects.Move)
                {
                    moveable.Remove();
                    target.Slot.Contents = moveable;
                }

                try {
                    Log.WriteAction(LogAction.placed, "block", target.Slot.Contents.GetLogText() + " on " + target.Slot.GetLogText());
                    //ActivityLog.Write(new Activity("PlacedBlock","Block",target.Slot.Contents.GetLogText(),"PlacedOn",target.Slot.GetLogText()));
                }
                catch (Exception) {}

                OnChanged(new EventArgs());
            }

            e.Handled = true;
        }