]> O.S.I.I.S - jp/crow.git/commitdiff
drag and drop operations
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 18 Feb 2018 22:26:23 +0000 (23:26 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 18 Feb 2018 22:26:23 +0000 (23:26 +0100)
Crow.csproj
Tests/Interfaces/DragAndDrop/0.crow
src/DragDropEventArgs.cs [new file with mode: 0644]
src/GraphicObjects/GraphicObject.cs
src/Input/MouseState.cs
src/Interface.cs

index b340d9e408dd1948a0f213ef79c51e6fc6484f84..bb4a9bbec2419c4165a262fc209fa7779dadeba4 100644 (file)
     <Compile Include="src\IListChanged.cs" />
     <Compile Include="src\ObservableList.cs" />
     <Compile Include="src\GraphicObjects\IMLContainer.cs" />
+    <Compile Include="src\DragDropEventArgs.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
index a9c113349a5f77ee1fc7a5b7d0ef8ef473085460..c36f840d2f523d5d0752b4446192c0222f2cc9f6 100755 (executable)
@@ -1,19 +1,20 @@
 <?xml version="1.0"?>
 <HorizontalStack Background="Onyx" Margin="10" Width="90%" Height="90%" Spacing="100" Focusable="true">
-       <Container StartDrag="{Background=Yellow}" EndDrag="{Background=Mantis}" Focusable="true" Fit="true"
-                       MouseEnter="{/txt.Foreground=Red;Background=Blue}" MouseLeave="{/txt.Foreground=White}"
+       <Container Name="source" StartDrag="{Background=Jet}" EndDrag="{Background=Mantis}" Focusable="true" Fit="true"
+                       Drop="{../target.Background=Green}"
+                       MouseEnter="{/txt.Foreground=Red}" MouseLeave="{/txt.Foreground=White}" MouseClick="{Background=Mantis}"
                        AllowDrag="true" Width="200" Height="200" Background="Mantis">
                <VerticalStack Margin="50">
                        <Label Name="txt" Text="Drag me" Foreground="Gray"/>
-                       <Label Text="Dragged" Visible="{/IsDragged}"/>
+                       <Label Text="Dragged" Visible="{../../IsDragged}" Foreground="Gray"/>
                </VerticalStack>
        </Container>
-       <Container StartDrag="{Background=Yellow}" EndDrag="{Background=Mantis}" Focusable="true" Fit="true"
+       <Container Name="target" DragEnter="{Background=Mantis}" DragLeave="{Background=Jet}" Focusable="true" Fit="true"
                        MouseEnter="{/txt.Foreground=Red}" MouseLeave="{/txt.Foreground=White}"
-                       AllowDrop="true"  Background="Mantis">
+                       AllowDrop="true"  Background="Jet">
                <VerticalStack Margin="50">
                        <Label Name="txt" Text="Drop here" Foreground="Gray"/>
-                       <Label Text="Dragged" Visible="{/IsDragged}"/>
+                       <Label Text="Dragged" Visible="{../../IsDragged}" Foreground="Gray"/>
                </VerticalStack>
        </Container>
 </HorizontalStack>
\ No newline at end of file
diff --git a/src/DragDropEventArgs.cs b/src/DragDropEventArgs.cs
new file mode 100644 (file)
index 0000000..428a736
--- /dev/null
@@ -0,0 +1,55 @@
+//
+// LayoutingEventArgs.cs
+//
+// Author:
+//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
+//
+// 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
+       {
+               /// <summary>
+               /// Source of the drag and drop operation
+               /// </summary>
+               public GraphicObject DragSource;
+               /// <summary>
+               /// Target of the drag and drop operation
+               /// </summary>
+               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);
+               }
+       }
+}
+
index ef2e019748a4d4ed3d6aa7fb12af8fb846e2d70c..65f8ed26fe18154de25887b54c54d8532e1249d1 100644 (file)
@@ -318,8 +318,10 @@ namespace Crow
                /// <summary>Occurs when the enabled state this object is set to false</summary>
                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;
                /// <summary>Occurs when one part of the rendering slot changed</summary>
                public event EventHandler<LayoutingEventArgs> LayoutChanged;
                /// <summary>Occurs when DataSource changed</summary>
@@ -1088,37 +1090,42 @@ namespace Crow
                                        return;
                                isDragged = value;
 
-                               if (isDragged) {
-                                       CurrentInterface.HoverWidget = null;
-                                       onStartDrag (this, null);
-                               }
-
-                               NotifyValueChanged ("IsDrag", IsDragged);
+                               NotifyValueChanged ("IsDragged", IsDragged);
                        }
                }
                /// <summary>
-               /// start dragging
+               /// fired when drag and drop operation start
                /// </summary>
-               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());
                }
                /// <summary>
                ///  Occured when dragging ends without dropping
                /// </summary>
-               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());
                }
-               /// <summary>
-               /// Dragging end with a dropping
-               /// </summary>
-               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
index 594619ac0feb7bc219a06c47942214f78fb8cbb9..f6db21eaf586c8311d85b0d3520fa313d6aa65c5 100644 (file)
@@ -46,6 +46,8 @@ namespace Crow
 
        #endregion
 
+
+
        #region Public Members
 
        /// <summary>
index e1bd7533b82147a58168cb8f79114bc97d798732..becd37ce356f7b35ea0c1d1f1bcdd236d50131c6 100644 (file)
@@ -206,6 +206,8 @@ namespace Crow
                /// </summary>
                public static Dictionary<String, Instantiator> Instantiators = new Dictionary<string, Instantiator>();
                public List<CrowThread> CrowThreads = new List<CrowThread>();//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) {