From d826efa14ae931ee4c04d4458f53422f1ce8de9d Mon Sep 17 00:00:00 2001 From: nickk888samp Date: Sat, 13 Jul 2024 15:41:52 +0200 Subject: [PATCH] Added Direct Drag movement --- .vscode/settings.json | 2 +- .../RTSCameraController_OldInputSystem.prefab | 3 +- ...ontroller_NewInputSystem.unitypackage.meta | 7 -- .../Scenes/ExampleScene.unity | 4 +- .../Scripts/RTSCameraTargetController.cs | 93 +++++++++++++------ .../Scripts/RTSCanvasController.cs | 18 ++-- 6 files changed, 83 insertions(+), 44 deletions(-) delete mode 100644 Assets/Nickk888/RTSCameraController/RTSCameraController_NewInputSystem.unitypackage.meta diff --git a/.vscode/settings.json b/.vscode/settings.json index 1ab3a32b..1389e46a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -52,5 +52,5 @@ "Temp/": true }, "git.ignoreLimitWarning": true, - "dotnet.defaultSolution": "RTSCameraController.sln" + "dotnet.defaultSolution": "RTSCameraController-Cinemachine.sln" } \ No newline at end of file diff --git a/Assets/Nickk888/RTSCameraController/Prefabs/RTSCameraController_OldInputSystem.prefab b/Assets/Nickk888/RTSCameraController/Prefabs/RTSCameraController_OldInputSystem.prefab index 0bfcabb6..0679e073 100644 --- a/Assets/Nickk888/RTSCameraController/Prefabs/RTSCameraController_OldInputSystem.prefab +++ b/Assets/Nickk888/RTSCameraController/Prefabs/RTSCameraController_OldInputSystem.prefab @@ -1260,13 +1260,14 @@ MonoBehaviour: AllowTiltRotate: 1 AllowZoom: 1 AllowDragMove: 1 + mouseDragStyle: 0 AllowKeysMove: 1 AllowScreenSideMove: 1 AllowHeightOffsetChange: 1 MouseLockOnRotate: 1 InvertMouseVertical: 0 InvertMouseHorizontal: 0 - CameraMouseSpeed: 2 + CameraMouseSpeed: 8 CameraRotateSpeed: 2 CameraKeysRotateSpeedMultiplier: 50 CameraKeysSpeed: 6 diff --git a/Assets/Nickk888/RTSCameraController/RTSCameraController_NewInputSystem.unitypackage.meta b/Assets/Nickk888/RTSCameraController/RTSCameraController_NewInputSystem.unitypackage.meta deleted file mode 100644 index 6e17c279..00000000 --- a/Assets/Nickk888/RTSCameraController/RTSCameraController_NewInputSystem.unitypackage.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 4ede2cbe826235f45beb829dc066ce50 -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Nickk888/RTSCameraController/Scenes/ExampleScene.unity b/Assets/Nickk888/RTSCameraController/Scenes/ExampleScene.unity index 2e4e07c4..00847b3f 100644 --- a/Assets/Nickk888/RTSCameraController/Scenes/ExampleScene.unity +++ b/Assets/Nickk888/RTSCameraController/Scenes/ExampleScene.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cfc7ad9ba962e199b39e3bc59a4bec43d5618c9983fce6bd5b00ebdaacc85f56 -size 74814141 +oid sha256:a1c33d23d3f397268d61d3d6215c9a49f5eb339c6edb9da565e17c32133c2968 +size 74814319 diff --git a/Assets/Nickk888/RTSCameraController/Scripts/RTSCameraTargetController.cs b/Assets/Nickk888/RTSCameraController/Scripts/RTSCameraTargetController.cs index 7ca13bac..7d40e56f 100644 --- a/Assets/Nickk888/RTSCameraController/Scripts/RTSCameraTargetController.cs +++ b/Assets/Nickk888/RTSCameraController/Scripts/RTSCameraTargetController.cs @@ -22,14 +22,17 @@ public class OnRotateHandledEventArgs : EventArgs public event EventHandler OnMouseDragStarted; public class OnMouseDragStartedEventArgs : EventArgs { + public MouseDragStyle mouseDragStyle; public Vector2 mouseLockPosition; } public event EventHandler OnMouseDragStopped; public event EventHandler OnMouseDragHandled; public class OnMouseDragHandledEventArgs : EventArgs { + public MouseDragStyle mouseDragStyle; public bool isMoving; public Vector2 mousePosition; + public Vector2 vectorChange; } public event EventHandler OnZoomHandled; @@ -81,6 +84,10 @@ [SerializeField] [Tooltip("Allows or Disallows Zooming.")] [SerializeField] [Tooltip("Allows or Disallows mouse drag movement.")] public bool AllowDragMove = true; + public enum MouseDragStyle { MouseDirection, Direct } + [SerializeField] [Tooltip("The style of mouse Drag.")] + private MouseDragStyle mouseDragStyle = MouseDragStyle.MouseDirection; + [SerializeField] [Tooltip("Allows or Disallows camera movement with keys/gamepad input.")] public bool AllowKeysMove = true; @@ -99,9 +106,11 @@ [SerializeField] [Tooltip("Invert the vertical mouse input?")] [SerializeField] [Tooltip("Invert the horizontal mouse input?")] public bool InvertMouseHorizontal = false; + + [Space] [Header("Speed")] [SerializeField, Min(0)] - public float CameraMouseSpeed = 2.0f; + public float CameraMouseSpeed = 8.0f; [SerializeField, Min(0)] public float CameraRotateSpeed = 2.0f; @@ -219,8 +228,8 @@ private void Awake() _cinemachineBrain = _cam.gameObject.GetComponent(); else Debug.LogError("Main Camera wasn't found. Can't get the Cinemachine Brain."); - if(Instance is not null) - Destroy(Instance); + // if(Instance is not null) + // Destroy(Instance); Instance = this; } @@ -359,32 +368,62 @@ private void HandleMouseDrag(Vector3 mousePos) { if (!_isRotating && !_isSideZoneMoving) { - if (_inputProvider.DragButtonInput() && AllowDragMove && !_isDragging) - { - _mouseLockPos = mousePos; - _isDragging = true; - CancelTargetLock(); - OnMouseDragStarted?.Invoke(this, new OnMouseDragStartedEventArgs { mouseLockPosition = mousePos }); - } - if ((_isDragging && !_inputProvider.DragButtonInput()) || (_isDragging && !AllowDragMove)) + switch(mouseDragStyle) { - Cursor.visible = true; - _isDragging = false; - OnMouseDragStopped?.Invoke(this, EventArgs.Empty); - } - if (_inputProvider.DragButtonInput() && _isDragging && AllowDragMove) - { - Vector3 vectorChange = new Vector3(_mouseLockPos.x - mousePos.x, 0, _mouseLockPos.y - mousePos.y) * -1; - float distance = vectorChange.sqrMagnitude; - bool canMove = distance > (CameraDragDeadZone * CameraDragDeadZone); - Cursor.visible = !canMove; - - // Move target relative to Camera - if (canMove) - MoveTargetRelativeToCamera(vectorChange, CameraMouseSpeed / 100); - - OnMouseDragHandled?.Invoke(this, new OnMouseDragHandledEventArgs { isMoving = canMove, mousePosition = mousePos }); + case MouseDragStyle.MouseDirection: + if (_inputProvider.DragButtonInput() && AllowDragMove && !_isDragging) + { + _mouseLockPos = mousePos; + _isDragging = true; + CancelTargetLock(); + OnMouseDragStarted?.Invoke(this, new OnMouseDragStartedEventArgs { mouseLockPosition = mousePos, mouseDragStyle = MouseDragStyle.MouseDirection }); + } + if ((_isDragging && !_inputProvider.DragButtonInput()) || (_isDragging && !AllowDragMove)) + { + Cursor.visible = true; + _isDragging = false; + OnMouseDragStopped?.Invoke(this, EventArgs.Empty); + } + if (_inputProvider.DragButtonInput() && _isDragging && AllowDragMove) + { + Vector3 vectorChange = new Vector3(_mouseLockPos.x - mousePos.x, 0, _mouseLockPos.y - mousePos.y) * -1; + float distance = vectorChange.sqrMagnitude; + bool canMove = distance > (CameraDragDeadZone * CameraDragDeadZone); + Cursor.visible = !canMove; + + // Move target relative to Camera + if (canMove) + MoveTargetRelativeToCamera(vectorChange, CameraMouseSpeed / 100); + + OnMouseDragHandled?.Invoke(this, new OnMouseDragHandledEventArgs { isMoving = canMove, mousePosition = mousePos }); + } + break; + case MouseDragStyle.Direct: + if(_inputProvider.DragButtonInput() && AllowDragMove && !_isDragging) + { + Cursor.visible = false; + Cursor.lockState = CursorLockMode.Locked; + _isDragging = true; + CancelTargetLock(); + OnMouseDragStarted?.Invoke(this, new OnMouseDragStartedEventArgs { mouseLockPosition = mousePos, mouseDragStyle = MouseDragStyle.Direct }); + } + if ((_isDragging && !_inputProvider.DragButtonInput()) || (_isDragging && !AllowDragMove)) + { + Cursor.visible = true; + Cursor.lockState = CursorLockMode.None; + _isDragging = false; + OnMouseDragStopped?.Invoke(this, EventArgs.Empty); + } + if (_inputProvider.DragButtonInput() && _isDragging && AllowDragMove) + { + Vector3 vectorChange = _inputProvider.MouseInput(); + vectorChange.z = vectorChange.y; + vectorChange.y = 0; + MoveTargetRelativeToCamera(vectorChange, CameraMouseSpeed); + } + break; } + } } diff --git a/Assets/Nickk888/RTSCameraController/Scripts/RTSCanvasController.cs b/Assets/Nickk888/RTSCameraController/Scripts/RTSCanvasController.cs index df9bd7d9..ee4d928c 100644 --- a/Assets/Nickk888/RTSCameraController/Scripts/RTSCanvasController.cs +++ b/Assets/Nickk888/RTSCameraController/Scripts/RTSCanvasController.cs @@ -91,8 +91,11 @@ private void RTSCameraTargetController_OnRotateStarted(object sender, EventArgs private void RTSCameraTargetController_OnMouseDragStarted(object sender, RTSCameraTargetController.OnMouseDragStartedEventArgs e) { - mouseDragStartPoint.transform.position = new Vector2(e.mouseLockPosition.x, e.mouseLockPosition.y); - mouseDragCanvasGameObject.SetActive(true); + if(e.mouseDragStyle == RTSCameraTargetController.MouseDragStyle.MouseDirection) + { + mouseDragStartPoint.transform.position = new Vector2(e.mouseLockPosition.x, e.mouseLockPosition.y); + mouseDragCanvasGameObject.SetActive(true); + } } private void RTSCameraTargetController_OnMouseDragStopped(object sender, EventArgs e) @@ -100,10 +103,13 @@ private void RTSCameraTargetController_OnMouseDragStopped(object sender, EventAr private void RTSCameraTargetController_OnMouseDragHandled(object sender, RTSCameraTargetController.OnMouseDragHandledEventArgs e) { - mouseDragEndPoint.transform.position = e.mousePosition; - Vector3 dir = (Vector3)e.mousePosition - mouseDragStartPoint.transform.position; - mouseDragEndPoint.transform.right = dir; - mouseDragEndPoint.SetActive(e.isMoving); + if (e.mouseDragStyle == RTSCameraTargetController.MouseDragStyle.MouseDirection) + { + mouseDragEndPoint.transform.position = e.mousePosition; + Vector3 dir = (Vector3)e.mousePosition - mouseDragStartPoint.transform.position; + mouseDragEndPoint.transform.right = dir; + mouseDragEndPoint.SetActive(e.isMoving); + } } private void RTSCameraTargetController_OnZoomHandled(object sender, RTSCameraTargetController.OnZoomHandledEventArgs e)