示例#1
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;
        }
示例#2
0
 public bool Fits(Moveable moveable)
 {
     return(moveableFitter.Fits(moveable));
 }
示例#3
0
 /// <summary>
 /// Checks whether a given Moveable can be placed
 /// into this slot.
 /// </summary>
 /// <param name="moveable">The Moveable to check.</param>
 /// <returns>True if the given Moveable can fit
 /// in this slot; false otherwise.</returns>
 public virtual bool Fits(Moveable moveable)
 {
     return(fitter.Fits(moveable));
 }