Browse Source

Tweaked scene selection process

marco.bellan 9 years ago
parent
commit
01052c3813

+ 2 - 0
Source/MBansheeEditor/Windows/Scene/SceneSelection.cs

@@ -2,6 +2,7 @@
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 using System;
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 using BansheeEngine;
 
 namespace BansheeEditor
@@ -13,6 +14,7 @@ namespace BansheeEditor
     /// <summary>
     /// Contains information regarding object snapping.
     /// </summary>
+    [StructLayout(LayoutKind.Sequential)]
     struct SnapData
     {
         /// <summary>

+ 68 - 36
Source/MBansheeEditor/Windows/Scene/SceneWindow.cs

@@ -90,6 +90,8 @@ namespace BansheeEditor
         private bool isDraggingSelection;
         private Vector2I dragSelectionStart;
         private Vector2I dragSelectionEnd;
+        private Vector2I mouseDownPosition;
+        private GUIPanel selectionPanel;
 
         /// <summary>
         /// Returns the scene camera.
@@ -270,6 +272,8 @@ namespace BansheeEditor
             GUIPanel mainPanel = mainLayout.AddPanel();
             rtPanel = mainPanel.AddPanel();
 
+            selectionPanel = mainPanel.AddPanel(-1);
+
             GUIPanel sceneAxesPanel = mainPanel.AddPanel(-1);
             sceneAxesGUI = new SceneAxesGUI(this, sceneAxesPanel, HandleAxesGUISize, HandleAxesGUISize, ProjectionType.Perspective);
 
@@ -484,10 +488,13 @@ namespace BansheeEditor
             // Update scene view handles and selection
             sceneGizmos.Draw();
             sceneGrid.Draw();
-
+            bool dragResult = false;
             bool handleActive = false;
+            Vector2I scenePos;
+            bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
             if (Input.IsPointerButtonUp(PointerButton.Left))
             {
+                dragResult = EndDragSelection();
                 if (sceneHandles.IsActive())
                 {
                     sceneHandles.ClearSelection();
@@ -499,36 +506,22 @@ namespace BansheeEditor
                     sceneAxesGUI.ClearSelection();
                     handleActive = true;
                 }
+            } 
+            else if (Input.IsPointerButtonDown(PointerButton.Left))
+            {
+                mouseDownPosition = scenePos;
             }
 
-            Vector2I scenePos;
-            bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
-
             bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
             draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;
 
-            if (inBounds)
-            {
-                if (Input.IsPointerButtonDown(PointerButton.Left))
-                {
-                    StartDragSelection(scenePos);
-                }
-                else if (Input.IsPointerButtonHeld(PointerButton.Left))
-                {
-                    UpdateDragSelection(scenePos);
-                }
-                else if (Input.IsPointerButtonUp(PointerButton.Left))
-                {
-                    EndDragSelection();
-                }
-            }
-
             if (draggedOver)
             {
                 if (DragDrop.DropInProgress)
                 {
+                    #region Drop and select
                     dragActive = false;
-
+                    Debug.Log("hey");
                     if (draggedSO != null)
                     {
                         Selection.SceneObject = draggedSO;
@@ -536,6 +529,7 @@ namespace BansheeEditor
                     }
                     
                     draggedSO = null;
+                    #endregion
                 }
                 else
                 {
@@ -594,19 +588,22 @@ namespace BansheeEditor
 
                     if (draggedSO != null)
                     {
+                        #region Move dragged object
                         if (Input.IsButtonHeld(ButtonCode.Space))
                         {
                             SnapData snapData = sceneSelection.Snap(scenePos, new SceneObject[] {draggedSO});
                             Debug.Log(snapData.normal);
                             Debug.Log(snapData.position);
+                            Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
                             draggedSO.Position = snapData.position;
-                            draggedSO.Rotation = Quaternion.FromAxisAngle(snapData.normal, new Degree(0));
+                            draggedSO.Rotation = q;
                         }
                         else
                         {
                             Ray worldRay = camera.ViewportToWorldRay(scenePos);
                             draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
                         }
+                        #endregion
                     }
                 }
 
@@ -632,6 +629,10 @@ namespace BansheeEditor
 
                 if (inBounds)
                 {
+                    bool newHandle = false;
+                    SceneHandles.BeginInput();
+                    newHandle = sceneHandles.IsActive();
+                    SceneHandles.EndInput();  
                     if (Input.IsPointerButtonDown(PointerButton.Left))
                     {
                         Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX, 
@@ -642,21 +643,29 @@ namespace BansheeEditor
                         else
                             sceneHandles.TrySelect(scenePos);
                     }
+                    else if (Input.IsPointerButtonHeld(PointerButton.Left) && !newHandle && !dragActive &&
+                             draggedSO == null && scenePos != mouseDownPosition)
+                    {
+                        if (isDraggingSelection)
+                            UpdateDragSelection(scenePos);
+                        else
+                            StartDragSelection(scenePos);
+                    }
                     else if (Input.IsPointerButtonUp(PointerButton.Left))
                     {
-                        if (!handleActive && !dragActive)
+                        if (!handleActive && !dragActive && !dragResult)
                         {
                             bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                             Input.IsButtonHeld(ButtonCode.RightControl);
 
-                            sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO });
+                            sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] {draggedSO});
                         }
                     }
                 }
             }
             else
                 cameraController.EnableInput(false);
-
+            
             SceneHandles.BeginInput();
             sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
             sceneHandles.Draw();
@@ -950,6 +959,7 @@ namespace BansheeEditor
         /// <param name="scenePos">Coordinates relative to the scene where the drag originated.</param>
         private void StartDragSelection(Vector2I scenePos)
         {
+            Debug.Log("Started");
             isDraggingSelection = true;
             dragSelectionStart = scenePos;
             dragSelectionEnd = dragSelectionStart;
@@ -965,11 +975,23 @@ namespace BansheeEditor
         {
             if (!isDraggingSelection)
                 return false;
-
+            Debug.Log("Updated");
             if (dragSelection == null)
             {
-                dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
-                rtPanel.AddElement(dragSelection);
+                Debug.Log("Created texture");
+                try
+                {
+                    dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
+                    selectionPanel.AddElement(dragSelection);
+                }
+                catch (Exception e)
+                {
+                    Debug.Log(e.ToString());   
+                }
+            }
+            else
+            {
+                Debug.Log("Texture is here");
             }
 
             
@@ -981,8 +1003,8 @@ namespace BansheeEditor
             Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x), Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
             selectionArea.x = min.x;
             selectionArea.y = min.y;
-            selectionArea.width = max.x - min.x;
-            selectionArea.height = max.y - min.y;
+            selectionArea.width = Math.Max(max.x - min.x, 1);
+            selectionArea.height = Math.Max(max.y - min.y, 1);
 
             dragSelection.Bounds = selectionArea;
 
@@ -997,19 +1019,29 @@ namespace BansheeEditor
         {
             if (!isDraggingSelection)
                 return false;
-
+            Debug.Log("Ended");
             if (dragSelection != null)
             {
                 dragSelection.Destroy();
                 dragSelection = null;
             }
-            Debug.Log("heya");
-            Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x), Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
-            Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x), Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
-            sceneSelection.PickObjects(min, max - min, Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl));
 
+            if ((dragSelectionEnd - dragSelectionStart).Length < 1)
+            {
+                isDraggingSelection = false;
+                return false;
+            }
+
+            Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x),
+                    Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
+            Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x),
+                Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
+            Debug.Log(min);
+            Debug.Log(max);
+            sceneSelection.PickObjects(min, max - min,
+                Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl));
             isDraggingSelection = false;
-            return false;
+            return true;
         }
     }
 

+ 10 - 14
Source/SBansheeEditor/Source/BsScriptSceneSelection.cpp

@@ -42,9 +42,6 @@ namespace BansheeEngine
 
 	void ScriptSceneSelection::internal_PickObject(ScriptSceneSelection* thisPtr, Vector2I* inputPos, bool additive, MonoArray* ignoreRenderables)
 	{
-		assert(_CrtCheckMemory() == 1);
-		SnapData data;
-
 		Vector<HSceneObject> ignoredSceneObjects;
 
 		if (ignoreRenderables != nullptr)
@@ -65,8 +62,7 @@ namespace BansheeEngine
 			}
 		}
 
-		HSceneObject pickedObject = ScenePicking::instance().pickClosestObject(thisPtr->mCamera, *inputPos, Vector2I(1, 1), ignoredSceneObjects);
-		assert(_CrtCheckMemory() == 1);
+		HSceneObject pickedObject = ScenePicking::instance().pickClosestObject(thisPtr->mCamera, *inputPos, Vector2I(1, 1), ignoredSceneObjects); //TODO REMOVE DATA
 		if (pickedObject)
 		{
 			if (additive) // Append to existing selection
@@ -89,15 +85,15 @@ namespace BansheeEngine
 				Selection::instance().setSceneObjects(selectedSOs);
 			}
 		}
-		else //TODO: Should we clear when it is additive?
+		else if (!additive)
+		{
 			Selection::instance().clearSceneSelection();
-		assert(_CrtCheckMemory() == 1);
+		}
+			
 	}
 
 	void ScriptSceneSelection::internal_PickObjects(ScriptSceneSelection* thisPtr, Vector2I* inputPos, Vector2I* area, bool additive, MonoArray* ignoreRenderables)
 	{
-		SnapData data;
-		assert(_CrtCheckMemory() == 1);
 		Vector<HSceneObject> ignoredSceneObjects;
 
 		if (ignoreRenderables != nullptr)
@@ -117,9 +113,8 @@ namespace BansheeEngine
 				ignoredSceneObjects.push_back(so);
 			}
 		}
-		assert(_CrtCheckMemory() == 1);
-		Vector<HSceneObject> pickedObjects = ScenePicking::instance().pickObjects(thisPtr->mCamera, *inputPos, *area, ignoredSceneObjects);
-		assert(_CrtCheckMemory() == 1);
+		Vector<HSceneObject> pickedObjects = ScenePicking::instance().pickObjects(thisPtr->mCamera, *inputPos, *area, ignoredSceneObjects); //TODO: REMOVE DATA
+
 		if (pickedObjects.size() != 0)
 		{
 			if (additive) // Append to existing selection
@@ -144,9 +139,10 @@ namespace BansheeEngine
 			else
 				Selection::instance().setSceneObjects(pickedObjects);
 		}
-		else //TODO: Should we clear when it is additive?
+		else if (!additive)
+		{
 			Selection::instance().clearSceneSelection();
-		assert(_CrtCheckMemory() == 1);
+		}
 	}
 
 	void ScriptSceneSelection::internal_Snap(ScriptSceneSelection* thisPtr, Vector2I* inputPos, SnapData* data, MonoArray* ignoreRenderables)