/// <summary>
        /// Triggered when the user double clicks the left mouse button.
        /// </summary>
        /// <param name="ev">Information about the mouse event.</param>
        private void OnPointerDoubleClicked(PointerEvent ev)
        {
            if (ev.IsUsed)
            {
                return;
            }

            Vector2I panelPos          = ScreenToKeyEditorPos(ev.ScreenPos);
            Rect2I   guiGradientBounds = guiGradientTexture.Bounds;

            if (guiGradientBounds.Contains(panelPos))
            {
                Vector2I canvasPos = panelPos - new Vector2I(guiGradientBounds.x, guiGradientBounds.y);

                float time = canvasPos.x / (float)Math.Max(guiGradientBounds.width - 1, 1);
                if (time >= 0.0f && time <= 1.0f)
                {
                    List <ColorGradientKey> keys = new List <ColorGradientKey>(gradient.GetKeys());

                    keys.Add(new ColorGradientKey(Color.Black, time));
                    keys.Sort((lhs, rhs) => lhs.time.CompareTo(rhs.time));

                    gradient = new ColorGradient(keys.ToArray());

                    UpdateTexture();
                    editor.Rebuild(keys);
                    UpdateKeyLines();
                }
            }
            else
            {
                editor.OnPointerDoubleClicked(panelPos, ev.Button);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Selects a handle under the pointer position.
        /// </summary>
        /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
        public void TrySelect(Vector2I pointerPos)
        {
            if (!bounds.Contains(pointerPos))
            {
                return;
            }

            pointerPos.x -= bounds.x;
            pointerPos.y -= bounds.y;
            sceneHandles.TrySelect(pointerPos);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Selects a handle under the pointer position.
        /// </summary>
        /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
        public void TrySelect(Vector2I pointerPos)
        {
            if (!bounds.Contains(pointerPos))
            {
                return;
            }

            pointerPos.x -= bounds.x;
            pointerPos.y -= bounds.y;
            if (sceneHandles.TrySelect(pointerPos))
            {
                NotifyNeedsRedraw();
            }
        }
Exemplo n.º 4
0
            /// <summary>
            /// Handles input over the color box, moving the handle as needed.
            /// </summary>
            /// <param name="windowPos">Position of the pointer relative to the color picker window.</param>
            public void UpdateInput(Vector2I windowPos)
            {
                if (Input.IsPointerButtonHeld(PointerButton.Left))
                {
                    Rect2I bounds = guiTexture.Bounds;

                    if (bounds.Contains(windowPos))
                    {
                        Vector2 newValue = Vector2.Zero;
                        newValue.x = (windowPos.x - bounds.x) / (float)bounds.width;
                        newValue.y = 1.0f - (windowPos.y - bounds.y) / (float)bounds.height;

                        SetValue(newValue);
                    }
                }
            }
Exemplo n.º 5
0
        /// <summary>
        /// Converts screen coordinates into coordinates relative to the scene view render texture.
        /// </summary>
        /// <param name="screenPos">Coordinates relative to the screen.</param>
        /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
        /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
        private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
        {
            scenePos = screenPos;
            Vector2I windowPos = ScreenToWindowPos(screenPos);

            Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI, GUI);
            if (bounds.Contains(windowPos))
            {
                scenePos.x = windowPos.x - bounds.x;
                scenePos.y = windowPos.y - bounds.y;

                return true;
            }

            return false;
        }
            /// <summary>
            /// Handles input. Should be called by the owning window whenever a pointer is double-clicked.
            /// </summary>
            /// <param name="panelPos">Position of the pointer relative to the panel parent to this element.</param>
            /// <param name="button">Pointer button involved in the event.</param>
            internal void OnPointerDoubleClicked(Vector2I panelPos, PointerButton button)
            {
                Rect2I canvasBounds = canvas.Bounds;

                if (!canvasBounds.Contains(panelPos))
                {
                    return;
                }

                Vector2I canvasPos = panelPos - new Vector2I(canvasBounds.x, canvasBounds.y);

                int keyIdx;

                if (FindKey(canvasPos, out keyIdx))
                {
                    ColorPicker.Show(keys[keyIdx].color, false,
                                     (b, color) =>
                    {
                        if (b)
                        {
                            ColorGradientKey key = keys[keyIdx];
                            key.color            = color;
                            keys[keyIdx]         = key;

                            OnGradientModified?.Invoke(new ColorGradient(keys.ToArray()));
                            Rebuild();
                        }
                    });
                }
                else
                {
                    float time = PixelToTime(canvasPos.x);
                    if (time >= 0.0f && time <= 1.0f)
                    {
                        keys.Add(new ColorGradientKey(Color.Black, time));
                        keys.Sort((lhs, rhs) => lhs.time.CompareTo(rhs.time));

                        OnGradientModified?.Invoke(new ColorGradient(keys.ToArray()));
                    }

                    Rebuild();
                }
            }
Exemplo n.º 7
0
        private void OnEditorUpdate()
        {
            UpdateLoadingProgress();

            if (HasFocus)
            {
                if (!Input.IsPointerButtonHeld(PointerButton.Right))
                {
                    if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
                    {
                        DuplicateSelection();
                    }
                    else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
                    {
                        DeleteSelection();
                    }
                    else if (VirtualInput.IsButtonDown(viewToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.View;
                    }
                    else if (VirtualInput.IsButtonDown(moveToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Move;
                    }
                    else if (VirtualInput.IsButtonDown(rotateToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
                    }
                    else if (VirtualInput.IsButtonDown(scaleToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
                    }
                }
            }

            // Refresh GUI buttons if needed (in case someones changes the values from script)
            if (editorSettingsHash != EditorSettings.Hash)
            {
                UpdateButtonStates();
                editorSettingsHash = EditorSettings.Hash;
            }

            // Update scene view handles and selection
            sceneGrid.Draw();

            bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();

            Vector2I scenePos;
            bool     inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);

            bool clearSelection = false;

            if (AllowViewportInput)
            {
                if (Input.IsPointerButtonUp(PointerButton.Left))
                {
                    clearSelection = true;
                }
                else if (Input.IsPointerButtonDown(PointerButton.Left))
                {
                    mouseDownPosition = scenePos;
                }
            }
            else
            {
                clearSelection = true;
                inBounds       = false;
            }

            bool dragResult = false;

            if (clearSelection)
            {
                dragResult = EndDragSelection();
                if (sceneHandles.IsActive())
                {
                    sceneHandles.ClearSelection();
                }

                if (sceneAxesGUI.IsActive())
                {
                    sceneAxesGUI.ClearSelection();
                }
            }

            bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;

            draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;

            if (draggedOver)
            {
                if (DragDrop.DropInProgress)
                {
                    dragActive = false;
                    if (draggedSO != null)
                    {
                        Selection.SceneObject = draggedSO;
                        EditorApplication.SetSceneDirty();
                    }

                    draggedSO = null;
                }
                else
                {
                    if (!dragActive)
                    {
                        dragActive = true;

                        ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;

                        string[] draggedPaths = dragData.Paths;

                        for (int i = 0; i < draggedPaths.Length; i++)
                        {
                            ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
                            if (meta != null)
                            {
                                if (meta.ResType == ResourceType.Mesh)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
                                        draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                                        Mesh mesh = ProjectLibrary.Load <Mesh>(draggedPaths[i]);

                                        Renderable renderable = draggedSO.AddComponent <Renderable>();
                                        renderable.Mesh = mesh;
                                        if (mesh != null)
                                        {
                                            draggedSOOffset = mesh.Bounds.Box.Center;
                                        }
                                        else
                                        {
                                            draggedSOOffset = Vector3.Zero;
                                        }
                                    }

                                    break;
                                }
                                else if (meta.ResType == ResourceType.Prefab)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        Prefab prefab = ProjectLibrary.Load <Prefab>(draggedPaths[i]);
                                        draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);

                                        if (draggedSO != null)
                                        {
                                            AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
                                            draggedSOOffset = draggedObjBounds.Center;
                                        }
                                        else
                                        {
                                            draggedSOOffset = Vector3.Zero;
                                        }
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    if (draggedSO != null)
                    {
                        if (Input.IsButtonHeld(ButtonCode.Space))
                        {
                            SnapData snapData;
                            sceneSelection.Snap(scenePos, out snapData, new SceneObject[] { draggedSO });

                            Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
                            draggedSO.Position = snapData.position;
                            draggedSO.Rotation = q;
                        }
                        else
                        {
                            Ray worldRay = camera.ScreenPointToRay(scenePos);
                            draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
                        }
                    }
                }

                return;
            }
            else
            {
                if (dragActive)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        draggedSO.Destroy();
                        draggedSO = null;
                    }
                }
            }

            if ((HasContentFocus || IsPointerHovering) && AllowViewportInput)
            {
                cameraController.EnableInput(true);

                if (inBounds && HasContentFocus)
                {
                    if (Input.IsPointerButtonDown(PointerButton.Left))
                    {
                        Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
                                                               HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);

                        if (sceneAxesGUIBounds.Contains(scenePos))
                        {
                            sceneAxesGUI.TrySelect(scenePos);
                        }
                        else
                        {
                            sceneHandles.TrySelect(scenePos);
                        }
                    }
                    else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
                             draggedSO == null && scenePos != mouseDownPosition)
                    {
                        if (isDraggingSelection)
                        {
                            UpdateDragSelection(scenePos);
                        }
                        else
                        {
                            StartDragSelection(scenePos);
                        }
                    }
                    else if (Input.IsPointerButtonUp(PointerButton.Left))
                    {
                        if (!handleActive && !dragActive && !dragResult)
                        {
                            bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                            Input.IsButtonHeld(ButtonCode.RightControl);

                            sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO });
                        }
                    }
                }
            }
            else
            {
                cameraController.EnableInput(false);
            }

            if (AllowViewportInput)
            {
                SceneHandles.BeginInput();
                sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
                sceneAxesGUI.UpdateInput(scenePos);
                SceneHandles.EndInput();
            }

            sceneHandles.Draw();
            sceneAxesGUI.Draw();

            // Must be done after handle input is processed, in order to reflect most recent transform
            sceneGizmos.Draw();
            sceneSelection.Draw();

            UpdateGridMode();

            if (VirtualInput.IsButtonDown(frameKey))
            {
                cameraController.FrameSelected();
            }
        }
Exemplo n.º 8
0
        private void OnUpdate()
        {
            bool isOrtographic = camera.ProjectionType == ProjectionType.Orthographic;

            if (inputEnabled)
            {
                bool goingForward = VirtualInput.IsButtonHeld(moveForwardBtn);
                bool goingBack    = VirtualInput.IsButtonHeld(moveBackwardBtn);
                bool goingLeft    = VirtualInput.IsButtonHeld(moveLeftBtn);
                bool goingRight   = VirtualInput.IsButtonHeld(moveRightBtn);
                bool goingUp      = VirtualInput.IsButtonHeld(moveUpBtn);
                bool goingDown    = VirtualInput.IsButtonHeld(moveDownBtn);
                bool fastMove     = VirtualInput.IsButtonHeld(fastMoveBtn);
                bool camActive    = VirtualInput.IsButtonHeld(activeBtn);
                bool isPanning    = VirtualInput.IsButtonHeld(panBtn);

                bool hideCursor = camActive || isPanning;
                if (hideCursor != lastButtonState)
                {
                    if (hideCursor)
                    {
                        Cursor.Hide();

                        Rect2I clipRect;
                        clipRect.x      = Input.PointerPosition.x - 2;
                        clipRect.y      = Input.PointerPosition.y - 2;
                        clipRect.width  = 4;
                        clipRect.height = 4;

                        Cursor.ClipToRect(clipRect);
                    }
                    else
                    {
                        Cursor.Show();
                        Cursor.ClipDisable();
                    }

                    lastButtonState = hideCursor;
                }

                float frameDelta = Time.FrameDelta;
                if (camActive)
                {
                    float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
                    float vertValue = VirtualInput.GetAxisValue(verticalAxis);

                    float rotationAmount = RotationalSpeed * EditorSettings.MouseSensitivity * frameDelta;

                    yaw   += new Degree(horzValue * rotationAmount);
                    pitch += new Degree(vertValue * rotationAmount);

                    yaw   = MathEx.WrapAngle(yaw);
                    pitch = MathEx.WrapAngle(pitch);

                    Quaternion yRot = Quaternion.FromAxisAngle(Vector3.YAxis, yaw);
                    Quaternion xRot = Quaternion.FromAxisAngle(Vector3.XAxis, pitch);

                    Quaternion camRot = yRot * xRot;
                    camRot.Normalize();

                    SceneObject.Rotation = camRot;

                    // Handle movement using movement keys
                    Vector3 direction = Vector3.Zero;

                    if (goingForward)
                    {
                        direction += SceneObject.Forward;
                    }
                    if (goingBack)
                    {
                        direction -= SceneObject.Forward;
                    }
                    if (goingRight)
                    {
                        direction += SceneObject.Right;
                    }
                    if (goingLeft)
                    {
                        direction -= SceneObject.Right;
                    }
                    if (goingUp)
                    {
                        direction += SceneObject.Up;
                    }
                    if (goingDown)
                    {
                        direction -= SceneObject.Up;
                    }

                    if (direction.SqrdLength != 0)
                    {
                        direction.Normalize();

                        float multiplier = 1.0f;
                        if (fastMove)
                        {
                            multiplier = FastModeMultiplier;
                        }

                        currentSpeed  = MathEx.Clamp(currentSpeed + Acceleration * frameDelta, StartSpeed, TopSpeed);
                        currentSpeed *= multiplier;
                    }
                    else
                    {
                        currentSpeed = 0.0f;
                    }

                    const float tooSmall = 0.0001f;
                    if (currentSpeed > tooSmall)
                    {
                        Vector3 velocity = direction * currentSpeed;
                        SceneObject.Move(velocity * frameDelta);
                    }
                }

                // Pan
                if (isPanning)
                {
                    float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
                    float vertValue = VirtualInput.GetAxisValue(verticalAxis);

                    Vector3 direction = new Vector3(horzValue, -vertValue, 0.0f);
                    direction = camera.SceneObject.Rotation.Rotate(direction);

                    SceneObject.Move(direction * PanSpeed * frameDelta);
                }
            }
            else
            {
                Cursor.Show();
                Cursor.ClipDisable();
            }

            SceneWindow sceneWindow = EditorWindow.GetWindow <SceneWindow>();

            if (sceneWindow.Active)
            {
                Rect2I bounds = sceneWindow.Bounds;

                // Move using scroll wheel
                if (bounds.Contains(Input.PointerPosition))
                {
                    float scrollAmount = VirtualInput.GetAxisValue(scrollAxis);
                    if (!isOrtographic)
                    {
                        SceneObject.Move(SceneObject.Forward * scrollAmount * ScrollSpeed);
                    }
                    else
                    {
                        float orthoHeight = MathEx.Max(1.0f, camera.OrthoHeight - scrollAmount);
                        camera.OrthoHeight = orthoHeight;
                    }
                }
            }

            UpdateAnim();
        }
Exemplo n.º 9
0
        private void OnEditorUpdate()
        {
            if (HasFocus)
            {
                if (!Input.IsPointerButtonHeld(PointerButton.Right))
                {
                    if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
                        EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));

                    if (VirtualInput.IsButtonDown(viewToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.View;

                    if (VirtualInput.IsButtonDown(moveToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Move;

                    if (VirtualInput.IsButtonDown(rotateToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;

                    if (VirtualInput.IsButtonDown(scaleToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Scale;

                    if (VirtualInput.IsButtonDown(duplicateKey))
                    {
                        SceneObject[] selectedObjects = Selection.SceneObjects;
                        CleanDuplicates(ref selectedObjects);

                        if (selectedObjects.Length > 0)
                        {
                            String message;
                            if (selectedObjects.Length == 1)
                                message = "Duplicated " + selectedObjects[0].Name;
                            else
                                message = "Duplicated " + selectedObjects.Length + " elements";

                            UndoRedo.CloneSO(selectedObjects, message);
                            EditorApplication.SetSceneDirty();
                        }
                    }

                    if (VirtualInput.IsButtonDown(deleteKey))
                    {
                        SceneObject[] selectedObjects = Selection.SceneObjects;
                        CleanDuplicates(ref selectedObjects);

                        if (selectedObjects.Length > 0)
                        {
                            foreach (var so in selectedObjects)
                            {
                                string message = "Deleted " + so.Name;
                                UndoRedo.DeleteSO(so, message);
                            }

                            EditorApplication.SetSceneDirty();
                        }
                    }
                }
            }

            // Refresh GUI buttons if needed (in case someones changes the values from script)
            if (editorSettingsHash != EditorSettings.Hash)
            {
                UpdateButtonStates();
                UpdateProfilerOverlay();
                editorSettingsHash = EditorSettings.Hash;
            }

            // Update scene view handles and selection
            sceneGizmos.Draw();
            sceneGrid.Draw();

            bool handleActive = false;
            if (Input.IsPointerButtonUp(PointerButton.Left))
            {
                if (sceneHandles.IsActive())
                {
                    sceneHandles.ClearSelection();
                    handleActive = true;
                }

                if (sceneAxesGUI.IsActive())
                {
                    sceneAxesGUI.ClearSelection();
                    handleActive = true;
                }
            }

            Vector2I scenePos;
            bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);

            bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
            draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;

            if (draggedOver)
            {
                if (DragDrop.DropInProgress)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        Selection.SceneObject = draggedSO;
                        EditorApplication.SetSceneDirty();
                    }

                    draggedSO = null;
                }
                else
                {
                    if (!dragActive)
                    {
                        dragActive = true;

                        ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;

                        string[] draggedPaths = dragData.Paths;

                        for (int i = 0; i < draggedPaths.Length; i++)
                        {
                            LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
                            if (entry != null && entry.Type == LibraryEntryType.File)
                            {
                                FileEntry fileEntry = (FileEntry) entry;
                                if (fileEntry.ResType == ResourceType.Mesh)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
                                        draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                                        Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);

                                        Renderable renderable = draggedSO.AddComponent<Renderable>();
                                        renderable.Mesh = mesh;
                                    }

                                    break;
                                }
                                else if (fileEntry.ResType == ResourceType.Prefab)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
                                        draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    if (draggedSO != null)
                    {
                        Ray worldRay = camera.ScreenToWorldRay(scenePos);
                        draggedSO.Position = worldRay*DefaultPlacementDepth;
                    }
                }

                return;
            }
            else
            {
                if (dragActive)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        draggedSO.Destroy();
                        draggedSO = null;
                    }
                }
            }

            if (HasFocus)
            {
                cameraController.EnableInput(true);

                if (inBounds)
                {
                    if (Input.IsPointerButtonDown(PointerButton.Left))
                    {
                        Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX, 
                            HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);

                        if (sceneAxesGUIBounds.Contains(scenePos))
                            sceneAxesGUI.TrySelect(scenePos);
                        else
                            sceneHandles.TrySelect(scenePos);
                    }
                    else if (Input.IsPointerButtonUp(PointerButton.Left))
                    {
                        if (!handleActive)
                        {
                            bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                            Input.IsButtonHeld(ButtonCode.RightControl);

                            sceneSelection.PickObject(scenePos, ctrlHeld);
                        }
                    }
                }
            }
            else
                cameraController.EnableInput(false);

            SceneHandles.BeginInput();
            sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
            sceneHandles.Draw();

            sceneAxesGUI.UpdateInput(scenePos);
            sceneAxesGUI.Draw();
            SceneHandles.EndInput();

            sceneSelection.Draw();

            UpdateGridMode();

            if (VirtualInput.IsButtonDown(frameKey))
                cameraController.FrameSelected();
        }