From befe7d822ce7024b0557fdc3b92db2458b2fbac8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sun, 18 Feb 2018 23:26:23 +0100 Subject: [PATCH] drag and drop operations --- Crow.csproj | 1 + Tests/Interfaces/DragAndDrop/0.crow | 13 ++--- src/DragDropEventArgs.cs | 55 +++++++++++++++++++ src/GraphicObjects/GraphicObject.cs | 83 ++++++++++++++++++----------- src/Input/MouseState.cs | 2 + src/Interface.cs | 7 +-- 6 files changed, 121 insertions(+), 40 deletions(-) create mode 100644 src/DragDropEventArgs.cs diff --git a/Crow.csproj b/Crow.csproj index b340d9e4..bb4a9bbe 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -219,6 +219,7 @@ + diff --git a/Tests/Interfaces/DragAndDrop/0.crow b/Tests/Interfaces/DragAndDrop/0.crow index a9c11334..c36f840d 100755 --- a/Tests/Interfaces/DragAndDrop/0.crow +++ b/Tests/Interfaces/DragAndDrop/0.crow @@ -1,19 +1,20 @@ - - + AllowDrop="true" Background="Jet"> \ No newline at end of file diff --git a/src/DragDropEventArgs.cs b/src/DragDropEventArgs.cs new file mode 100644 index 00000000..428a7363 --- /dev/null +++ b/src/DragDropEventArgs.cs @@ -0,0 +1,55 @@ +// +// LayoutingEventArgs.cs +// +// Author: +// Jean-Philippe Bruyère +// +// Copyright (c) 2013-2017 Jean-Philippe Bruyère +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; + +namespace Crow +{ + public class DragDropEventArgs: EventArgs + { + /// + /// Source of the drag and drop operation + /// + public GraphicObject DragSource; + /// + /// Target of the drag and drop operation + /// + public GraphicObject DropTarget; + + //public DragDropEventArgs (GraphicObject source, GraphicObject target = null) : base() + public DragDropEventArgs (GraphicObject source = null, GraphicObject target = null) : base() + { + DragSource = source; + DropTarget = target; + } + + public override string ToString () + { + return string.Format ("{0} => {1}", DragSource,DropTarget); + } + } +} + diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index ef2e0197..65f8ed26 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -318,8 +318,10 @@ namespace Crow /// Occurs when the enabled state this object is set to false public event EventHandler Disabled; public event EventHandler StartDrag; + public event EventHandler DragEnter; + public event EventHandler DragLeave; public event EventHandler EndDrag; - public event EventHandler Dropped; + public event EventHandler Drop; /// Occurs when one part of the rendering slot changed public event EventHandler LayoutChanged; /// Occurs when DataSource changed @@ -1088,37 +1090,42 @@ namespace Crow return; isDragged = value; - if (isDragged) { - CurrentInterface.HoverWidget = null; - onStartDrag (this, null); - } - - NotifyValueChanged ("IsDrag", IsDragged); + NotifyValueChanged ("IsDragged", IsDragged); } } /// - /// start dragging + /// fired when drag and drop operation start /// - protected virtual void onStartDrag (object sender, EventArgs e){ - Debug.WriteLine("DRAG => " + this.ToString()); - StartDrag.Raise (this, null); + protected virtual void onStartDrag (object sender, DragDropEventArgs e){ + CurrentInterface.HoverWidget = this.focusParent; + IsDragged = true; + StartDrag.Raise (this, e); + Debug.WriteLine(this.ToString() + " : START DRAG => " + e.ToString()); } /// /// Occured when dragging ends without dropping /// - protected virtual void onEndDrag (object sender, EventArgs e){ + protected virtual void onEndDrag (object sender, DragDropEventArgs e){ IsDragged = false; - EndDrag.Raise (this, null); - Debug.WriteLine("END DRAG => " + this.ToString()); + EndDrag.Raise (this, e); + Debug.WriteLine(this.ToString() + " : END DRAG => " + e.ToString()); } - /// - /// Dragging end with a dropping - /// - protected virtual void onDrop (object sender, EventArgs e){ + protected virtual void onDragEnter (object sender, DragDropEventArgs e){ + e.DropTarget = this; + DragEnter.Raise (this, e); + Debug.WriteLine(this.ToString() + " : DRAG Enter => " + e.ToString()); + } + protected virtual void onDragLeave (object sender, DragDropEventArgs e){ + e.DropTarget = null; + DragLeave.Raise (this, e); + Debug.WriteLine(this.ToString() + " : DRAG Leave => " + e.ToString()); + } + protected virtual void onDrop (object sender, DragDropEventArgs e){ IsDragged = false; - Debug.WriteLine("DROPPED => " + this.ToString()); - Dropped.Raise (this, null); + Drop.Raise (this, e); + Debug.WriteLine(this.ToString() + " : DROP => " + e.ToString()); } + #endregion #region Queuing @@ -1544,8 +1551,12 @@ namespace Crow } public virtual void onMouseMove(object sender, MouseMoveEventArgs e) { - if (AllowDrag & !IsDragged & IsActive) - IsDragged = true; + if (AllowDrag & HasFocus & e.Mouse.LeftButton == ButtonState.Pressed) { + if (CurrentInterface.DragAndDropOperation == null) { + CurrentInterface.DragAndDropOperation = new DragDropEventArgs (this); + onStartDrag (this, CurrentInterface.DragAndDropOperation); + } + } //bubble event to the top GraphicObject p = focusParent; @@ -1582,16 +1593,14 @@ namespace Crow Debug.WriteLine("MOUSE UP => " + this.ToString()); #endif - if (IsDragged){ - bool dropOK = false; - if (CurrentInterface.HoverWidget!=null) { - if (CurrentInterface.HoverWidget.AllowDrop) - dropOK = true; + if (CurrentInterface.DragAndDropOperation != null){ + if (CurrentInterface.DragAndDropOperation.DragSource == this) { + if (CurrentInterface.DragAndDropOperation.DropTarget != null) + onDrop (this, CurrentInterface.DragAndDropOperation); + else + onEndDrag (this, CurrentInterface.DragAndDropOperation); + CurrentInterface.DragAndDropOperation = null; } - if (dropOK) - onDrop (this, null); - else - onEndDrag (this, null); } //bubble event to the top @@ -1631,6 +1640,14 @@ namespace Crow #if DEBUG_FOCUS Debug.WriteLine("MouseEnter => " + this.ToString()); #endif + + if (CurrentInterface.DragAndDropOperation != null) { + if (this.AllowDrop) { + if (CurrentInterface.DragAndDropOperation.DragSource != this && CurrentInterface.DragAndDropOperation.DropTarget != this) + onDragEnter (this, CurrentInterface.DragAndDropOperation); + } + } + MouseEnter.Raise (this, e); } public virtual void onMouseLeave(object sender, MouseMoveEventArgs e) @@ -1638,6 +1655,10 @@ namespace Crow #if DEBUG_FOCUS Debug.WriteLine("MouseLeave => " + this.ToString()); #endif + if (CurrentInterface.DragAndDropOperation != null) { + if (CurrentInterface.DragAndDropOperation.DropTarget == this) + onDragLeave (this, CurrentInterface.DragAndDropOperation); + } MouseLeave.Raise (this, e); } #endregion diff --git a/src/Input/MouseState.cs b/src/Input/MouseState.cs index 594619ac..f6db21ea 100644 --- a/src/Input/MouseState.cs +++ b/src/Input/MouseState.cs @@ -46,6 +46,8 @@ namespace Crow #endregion + + #region Public Members /// diff --git a/src/Interface.cs b/src/Interface.cs index e1bd7533..becd37ce 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -206,6 +206,8 @@ namespace Crow /// public static Dictionary Instantiators = new Dictionary(); public List CrowThreads = new List();//used to monitor thread finished + + public DragDropEventArgs DragAndDropOperation = null; #endregion #region Private Fields @@ -824,12 +826,11 @@ namespace Crow MouseMoveEventArgs e = new MouseMoveEventArgs (x, y, deltaX, deltaY); e.Mouse = Mouse; - if (ActiveWidget != null) { + if (ActiveWidget != null&& DragAndDropOperation == null) { //TODO, ensure object is still in the graphic tree //send move evt even if mouse move outside bounds ActiveWidget.onMouseMove (this, e); - if (!ActiveWidget.IsDragged)//if active is dragged, process mouse move as it was not visible. - return true; + return true; } if (HoverWidget != null) { -- 2.47.3