public void PlaceInWorkspace(Moveable moveable) { try { try { Canvas.SetZIndex(moveable, ++zIndex); } catch (ArithmeticException) { foreach (UIElement element in mainCanvas.Children) { Canvas.SetZIndex(element, 0); zIndex = 0; } } if (!(moveable.Parent == mainCanvas)) { moveable.Remove(); mainCanvas.Children.Add(moveable); } mainCanvas.InvalidateMeasure(); } catch (Exception x) { MessageBox.Show("Something went wrong when placing a moveable on the canvas.\n\n" + x); } }
/// <summary> /// Accepts dropped Moveable objects, if they fit this slot. /// </summary> protected virtual void AcceptDrop(object sender, DragEventArgs e) { if (!e.Handled) { if (e.Data.GetDataPresent(typeof(Moveable))) { Moveable moveable = e.Data.GetData(typeof(Moveable)) as Moveable; if (moveable != null && moveable != Contents && Fits(moveable)) { if (e.AllowedEffects == DragDropEffects.Copy) { Contents = moveable.DeepCopy(); } else if (e.AllowedEffects == DragDropEffects.Move) { moveable.Remove(); Contents = moveable; } //ActivityLog.Write(new Activity("PlacedBlock","Block",Contents.GetLogText(),"PlacedOn",this.GetLogText())); Log.WriteAction(LogAction.placed, "block", Contents.GetLogText() + " on " + this.GetLogText()); } e.Handled = true; } } SetDefaultAppearance(); }
protected void ReturnMoveableToBox(object sender, DragEventArgs e) { try { if (!e.Handled) { if (e.Data.GetDataPresent(typeof(Moveable))) { Moveable moveable = (Moveable)e.Data.GetData(typeof(Moveable)); if (!blockBox.HasMoveable(moveable)) { moveable.Remove(); //ActivityLog.Write(new Activity("PlacedBlock","Block",moveable.GetLogText(),"PlacedOn","BackInBox")); Log.WriteAction(LogAction.placed, "block", moveable.GetLogText() + " back in box"); } e.Handled = true; } } } catch (Exception x) { MessageBox.Show("Something went wrong when handling a drop on the block box.\n\n" + x); } }
/// <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; }