protected void DroppedOnCanvas(object sender, DragEventArgs e) { try { if (!e.Handled) { Moveable moveable = null; Size size = new Size(); if (e.Data.GetDataPresent(typeof(Moveable))) { moveable = (Moveable)e.Data.GetData(typeof(Moveable)); size = moveable.RenderSize; // use the original's size as the clone has not been drawn yet if (e.AllowedEffects == DragDropEffects.Copy) { moveable = moveable.DeepCopy(); } } // // HACK: // else if (e.Data.GetDataPresent(typeof(NWN2InstanceCollection))) { // NWN2InstanceCollection instances = (NWN2InstanceCollection)e.Data.GetData(typeof(NWN2InstanceCollection)); // if (instances.Count > 0) { // moveable = factory.CreateInstanceBlock(instances[0]); // size = ObjectBlock.DefaultSize; // } // } // else if (e.Data.GetDataPresent(typeof(NWN2BlueprintCollection))) { // try { // NWN2BlueprintCollection blueprints = (NWN2BlueprintCollection)e.Data.GetData(typeof(NWN2BlueprintCollection)); // if (blueprints.Count > 0) { // moveable = factory.CreateBlueprintBlock(blueprints[0]); // size = ObjectBlock.DefaultSize; // } // } // catch (System.Runtime.InteropServices.COMException x) { // // MessageBox.Show(x.ToString()); // /* // * Weird error occurs here - even though GetDataPresent() returns true, // * actually trying to retrieve the data raises this nasty exception. // * TODO: // * Look for the blueprints directly in the toolset instead. // */ // } // } if (moveable != null) { bool movingWithinCanvas = moveable.Parent == mainCanvas; PlaceInWorkspace(moveable); Point position = e.GetPosition(mainCanvas); position.X -= (size.Width / 2); position.Y -= (size.Height / 2); moveable.MoveTo(position); if (movingWithinCanvas) { //ActivityLog.Write(new Activity("MovedWithinCanvas","Block",moveable.GetLogText())); Log.WriteAction(LogAction.moved, "block", moveable.GetLogText() + " to different spot on canvas"); } else { //ActivityLog.Write(new Activity("PlacedBlock","Block",moveable.GetLogText(),"PlacedOn","Canvas")); Log.WriteAction(LogAction.placed, "block", moveable.GetLogText() + " on canvas"); } } } } catch (Exception x) { MessageBox.Show("Something went wrong when handling a drop on the canvas.\n\n" + x); } }
public FlipWindow(MoveableProvider provider, ImageProvider imageProvider, OpenDeleteScriptDelegate openDeleteScriptDelegate, SaveScriptDelegate saveScriptDelegate, DeserialisationHelper deserialisationHelper) { if (provider == null) { throw new ArgumentNullException("provider"); } if (imageProvider == null) { throw new ArgumentNullException("imageProvider"); } if (openDeleteScriptDelegate == null) { throw new ArgumentNullException("openDeleteScriptDelegate"); } if (saveScriptDelegate == null) { throw new ArgumentNullException("saveScriptDelegate"); } if (deserialisationHelper == null) { throw new ArgumentNullException("deserialisationHelper"); } this.provider = provider; this.imageProvider = imageProvider; this.openDeleteScriptDelegate = openDeleteScriptDelegate; this.saveScriptDelegate = saveScriptDelegate; ChosenDeserialisationHelper = deserialisationHelper; InitializeComponent(); /* * New SizedCanvas which unlike Canvas reports a desired size * (by overriding MeasureOverride) based on the positions of * its children, and hence can be used with a ScrollViewer. */ mainCanvas = new SizedCanvas(); mainCanvas.Name = "mainCanvas"; mainCanvas.Background = Brushes.Transparent; mainCanvas.AllowDrop = true; scrollViewer.Content = mainCanvas; blockBox = new MoveablesPanel(); Grid.SetRow(blockBox, 1); Grid.SetColumn(blockBox, 1); Grid.SetRowSpan(blockBox, 2); blockBox.PreviewDrop += ReturnMoveableToBox; provider.Populate(blockBox); mainGrid.Children.Add(blockBox); /* * Was MouseDown but ScrollViewer suppresses this event * so now using PreviewMouseDown. GetDragSource only * gets a reference to what was clicked on and where, * so there's no reason to believe this change would * affect any other program logic, and it doesn't seem to. */ PreviewMouseDown += GetDragSource; MouseMove += StartDrag; mainCanvas.Drop += DroppedOnCanvas; PreviewDragEnter += CreateAdorner; PreviewDragOver += UpdateAdorner; PreviewDragLeave += DestroyAdorner; PreviewDrop += DestroyAdorner; // Set up trigger bar: triggerBar = new TriggerBar(new SpineFitter()); triggerBar.SaveButton.Click += SaveScript; Canvas.SetTop(triggerBar, 30); Canvas.SetLeft(triggerBar, 30); mainCanvas.Children.Add(triggerBar); triggerBar.Changed += ScriptChanged; UpdateNaturalLanguageView(triggerBar); // Set up conditional frame: conditionalFrame = new ConditionalFrame(); conditionalFrame.SaveButton.Click += SaveScript; //conditionalFrame.FinishButton.Click += LeaveConditionModeWithDialog; Canvas.SetTop(conditionalFrame, 30); Canvas.SetLeft(conditionalFrame, 30); mainCanvas.Children.Add(conditionalFrame); conditionalFrame.Changed += ScriptChanged; UpdateNaturalLanguageView(conditionalFrame); if (mainCanvas.ContextMenu == null) { mainCanvas.ContextMenu = new ContextMenu(); } MenuItem paste = new MenuItem(); paste.Header = "Paste"; paste.Click += delegate { try { if (Moveable.CopiedToClipboard != null) { Moveable copied = Moveable.CopiedToClipboard.DeepCopy(); Point position = Mouse.GetPosition(this); copied.MoveTo(position); PlaceInWorkspace(copied); //ActivityLog.Write(new Activity("PlacedBlock","Block",copied.GetLogText(),"PlacedOn","Canvas")); Log.WriteAction(LogAction.placed, "block", copied.GetLogText() + " on canvas"); } } catch (Exception x) { MessageBox.Show("Something went wrong when copy-pasting.\n\n" + x); } }; mainCanvas.ContextMenu.Items.Add(paste); mainCanvas.ContextMenuOpening += delegate { paste.IsEnabled = (Moveable.CopiedToClipboard != null); }; LeaveConditionMode(); }