| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Builder; |
| | 6 | | using DCL.Camera; |
| | 7 | | using UnityEngine; |
| | 8 | | using CameraController = DCL.Camera.CameraController; |
| | 9 | |
|
| | 10 | | public class BIWGizmosController : BIWController, IBIWGizmosController |
| | 11 | | { |
| | 12 | | public event IBIWGizmosController.GizmoTransformDelegate OnGizmoTransformObjectStart; |
| | 13 | | public event IBIWGizmosController.GizmoTransformDelegate OnGizmoTransformObject; |
| | 14 | | public event IBIWGizmosController.GizmoTransformDelegate OnGizmoTransformObjectEnd; |
| | 15 | |
|
| | 16 | | public event Action<Vector3> OnChangeTransformValue; |
| | 17 | |
|
| | 18 | | private IBIWRaycastController raycastController; |
| | 19 | |
|
| | 20 | | private IBIWGizmos[] gizmos; |
| | 21 | |
|
| | 22 | | internal bool isTransformingObject; |
| 0 | 23 | | public IBIWGizmos activeGizmo { get; set; } |
| | 24 | |
|
| 0 | 25 | | private SnapInfo snapInfo = new SnapInfo(); |
| | 26 | |
|
| | 27 | | private BIWGizmosAxis hoveredAxis = null; |
| | 28 | |
|
| | 29 | | private Transform selectedEntitiesParent; |
| | 30 | | private List<BIWEntity> selectedEntities; |
| | 31 | | private GameObject gizmosGO; |
| | 32 | | private FreeCameraMovement freeCameraMovement; |
| | 33 | |
|
| | 34 | | public override void Initialize(IContext context) |
| | 35 | | { |
| 43 | 36 | | base.Initialize(context); |
| 43 | 37 | | gizmosGO = GameObject.Instantiate(context.projectReferencesAsset.gizmosPrefab, context.projectReferencesAsset.gi |
| 43 | 38 | | gizmos = gizmosGO.GetComponentsInChildren<IBIWGizmos>(true); |
| | 39 | |
|
| 43 | 40 | | raycastController = context.editorContext.raycastController; |
| | 41 | |
|
| 43 | 42 | | raycastController.OnGizmosAxisPressed += OnGizmosAxisPressed; |
| 43 | 43 | | BIWInputWrapper.OnMouseUp += OnMouseUp; |
| 43 | 44 | | BIWInputWrapper.OnMouseDrag += OnMouseDrag; |
| | 45 | |
|
| 43 | 46 | | if (context.sceneReferences.cameraController.GetComponent<CameraController>().TryGetCameraStateByType<FreeCamera |
| 43 | 47 | | freeCameraMovement = (FreeCameraMovement)cameraState; |
| | 48 | |
|
| | 49 | | // NOTE(Adrian): Take into account that right now to get the relative scale of the gizmos, we set the gizmos in |
| 43 | 50 | | InitializeGizmos(context.sceneReferences.mainCamera, freeCameraMovement.transform); |
| 43 | 51 | | ForceRelativeScaleRatio(); |
| 43 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void Dispose() |
| | 55 | | { |
| 43 | 56 | | base.Dispose(); |
| 43 | 57 | | UnityEngine.Object.Destroy(gizmosGO); |
| 43 | 58 | | raycastController.OnGizmosAxisPressed -= OnGizmosAxisPressed; |
| 43 | 59 | | BIWInputWrapper.OnMouseUp -= OnMouseUp; |
| 43 | 60 | | BIWInputWrapper.OnMouseDrag -= OnMouseDrag; |
| 43 | 61 | | } |
| | 62 | |
|
| | 63 | | public string GetSelectedGizmo() |
| | 64 | | { |
| 53 | 65 | | if (IsGizmoActive()) |
| 21 | 66 | | return activeGizmo.GetGizmoType(); |
| | 67 | |
|
| 32 | 68 | | return DCL.Components.DCLGizmos.Gizmo.NONE; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public void SetSnapFactor(float position, float rotation, float scale) |
| | 72 | | { |
| 37 | 73 | | snapInfo.position = position; |
| 37 | 74 | | snapInfo.rotation = rotation; |
| 37 | 75 | | snapInfo.scale = scale; |
| | 76 | |
|
| 37 | 77 | | if (activeGizmo != null) |
| 37 | 78 | | activeGizmo.SetSnapFactor(snapInfo); |
| 37 | 79 | | } |
| | 80 | |
|
| | 81 | | internal void OnBeginDrag(IBIWGizmosAxis hittedAxis) |
| | 82 | | { |
| 1 | 83 | | isTransformingObject = true; |
| 1 | 84 | | activeGizmo = hittedAxis.GetGizmo(); |
| 1 | 85 | | activeGizmo.OnBeginDrag(hittedAxis, selectedEntitiesParent); |
| 1 | 86 | | freeCameraMovement.SetCameraCanMove(false); |
| 1 | 87 | | OnGizmoTransformObjectStart?.Invoke(activeGizmo.GetGizmoType()); |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | internal void OnDrag(Vector3 hitPoint, Vector2 mousePosition) |
| | 91 | | { |
| 1 | 92 | | float value = activeGizmo.OnDrag(hitPoint, mousePosition); |
| 1 | 93 | | OnGizmoTransformObject?.Invoke(activeGizmo.GetGizmoType()); |
| 1 | 94 | | OnChangeTransformValue?.Invoke(value * activeGizmo.GetActiveAxisVector()); |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | internal void OnEndDrag() |
| | 98 | | { |
| 2 | 99 | | activeGizmo?.OnEndDrag(); |
| 2 | 100 | | freeCameraMovement.SetCameraCanMove(true); |
| 2 | 101 | | OnGizmoTransformObjectEnd?.Invoke(activeGizmo?.GetGizmoType()); |
| 2 | 102 | | isTransformingObject = false; |
| 2 | 103 | | } |
| | 104 | |
|
| 1 | 105 | | public bool HasAxisHover() { return hoveredAxis != null; } |
| | 106 | |
|
| | 107 | | private void SetAxisHover(BIWGizmosAxis axis) |
| | 108 | | { |
| 0 | 109 | | if (hoveredAxis != null && hoveredAxis != axis) |
| 0 | 110 | | hoveredAxis.SetColorDefault(); |
| 0 | 111 | | else if (axis != null) |
| 0 | 112 | | axis.SetColorHighlight(); |
| | 113 | |
|
| 0 | 114 | | hoveredAxis = axis; |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public void ForceRelativeScaleRatio() |
| | 118 | | { |
| 344 | 119 | | for (int i = 0; i < gizmos.Length; i++) |
| | 120 | | { |
| 129 | 121 | | gizmos[i].ForceRelativeScaleRatio(); |
| | 122 | | } |
| 43 | 123 | | } |
| | 124 | |
|
| | 125 | | public void ShowGizmo() |
| | 126 | | { |
| 1 | 127 | | gizmosGO.gameObject.SetActive(true); |
| 1 | 128 | | if (activeGizmo != null) |
| | 129 | | { |
| 1 | 130 | | activeGizmo.SetTargetTransform(selectedEntitiesParent); |
| 1 | 131 | | activeGizmo.currentGameObject.SetActive(true); |
| | 132 | | } |
| 1 | 133 | | } |
| | 134 | |
|
| | 135 | | public void HideGizmo(bool setInactiveGizmos = false) |
| | 136 | | { |
| 129 | 137 | | if (activeGizmo != null) |
| 89 | 138 | | activeGizmo.currentGameObject.SetActive(false); |
| | 139 | |
|
| 129 | 140 | | if (setInactiveGizmos) |
| 1 | 141 | | SetGizmoType(DCL.Components.DCLGizmos.Gizmo.NONE); |
| | 142 | |
|
| 129 | 143 | | gizmosGO.gameObject.SetActive(false); |
| 129 | 144 | | } |
| | 145 | |
|
| 1 | 146 | | public bool IsGizmoActive() { return activeGizmo != null; } |
| | 147 | |
|
| | 148 | | internal bool RaycastHit(Ray ray, out Vector3 hitPoint) |
| | 149 | | { |
| 0 | 150 | | if (activeGizmo != null) |
| 0 | 151 | | return activeGizmo.RaycastHit(ray, out hitPoint); |
| | 152 | |
|
| 0 | 153 | | hitPoint = Vector3.zero; |
| 0 | 154 | | return false; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public override void Update() |
| | 158 | | { |
| 0 | 159 | | base.Update(); |
| 0 | 160 | | if (!isTransformingObject) |
| 0 | 161 | | CheckGizmoHover(Input.mousePosition); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | public void SetGizmoType(string gizmoType) |
| | 165 | | { |
| 46 | 166 | | HideGizmo(); |
| | 167 | |
|
| 46 | 168 | | if (gizmoType != DCL.Components.DCLGizmos.Gizmo.NONE) |
| | 169 | | { |
| 45 | 170 | | bool wasGizmoActive = IsGizmoActive(); |
| | 171 | |
|
| 118 | 172 | | for (int i = 0; i < gizmos.Length; i++) |
| | 173 | | { |
| 59 | 174 | | if (gizmos[i].GetGizmoType() == gizmoType) |
| | 175 | | { |
| 45 | 176 | | activeGizmo = gizmos[i]; |
| 45 | 177 | | activeGizmo.SetSnapFactor(snapInfo); |
| 45 | 178 | | break; |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| 45 | 182 | | bool areEntitiesSelected = selectedEntities != null && selectedEntities.Count > 0; |
| | 183 | |
|
| 45 | 184 | | if (wasGizmoActive && areEntitiesSelected) |
| 0 | 185 | | ShowGizmo(); |
| | 186 | | else |
| 45 | 187 | | GizmoStatusUpdate(); |
| 45 | 188 | | } |
| | 189 | | else |
| | 190 | | { |
| 1 | 191 | | activeGizmo = null; |
| | 192 | | } |
| 1 | 193 | | } |
| | 194 | |
|
| | 195 | | private void InitializeGizmos(Camera camera, Transform cameraTransform) |
| | 196 | | { |
| 344 | 197 | | for (int i = 0; i < gizmos.Length; i++) |
| | 198 | | { |
| 129 | 199 | | if (!gizmos[i].initialized) |
| 129 | 200 | | gizmos[i].Initialize(camera, cameraTransform); |
| | 201 | | } |
| 43 | 202 | | } |
| | 203 | |
|
| | 204 | | public void SetSelectedEntities(Transform selectionParent, List<BIWEntity> entities) |
| | 205 | | { |
| 2 | 206 | | selectedEntities = entities; |
| 2 | 207 | | selectedEntitiesParent = selectionParent; |
| 2 | 208 | | GizmoStatusUpdate(); |
| 2 | 209 | | } |
| | 210 | |
|
| 0 | 211 | | private void OnGizmosAxisPressed(IBIWGizmosAxis pressedAxis) { OnBeginDrag(pressedAxis); } |
| | 212 | |
|
| | 213 | | internal void OnMouseUp(int buttonId, Vector3 mousePosition) |
| | 214 | | { |
| 1 | 215 | | if (!isTransformingObject) |
| 0 | 216 | | return; |
| | 217 | |
|
| 1 | 218 | | if (buttonId == 0) |
| 1 | 219 | | OnEndDrag(); |
| 1 | 220 | | } |
| | 221 | |
|
| | 222 | | internal void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 223 | | { |
| 1 | 224 | | if (buttonId != 0) |
| 1 | 225 | | return; |
| | 226 | |
|
| 0 | 227 | | bool hasMouseMoved = (axisX != 0 || axisY != 0); |
| 0 | 228 | | if (!isTransformingObject || !hasMouseMoved) |
| 0 | 229 | | return; |
| | 230 | |
|
| | 231 | | Vector3 hit; |
| 0 | 232 | | if (RaycastHit(raycastController.GetMouseRay(mousePosition), out hit)) |
| 0 | 233 | | OnDrag(hit, mousePosition); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | private void CheckGizmoHover(Vector3 mousePosition) |
| | 237 | | { |
| | 238 | | RaycastHit hit; |
| 0 | 239 | | if (raycastController.RaycastToGizmos(mousePosition, out hit)) |
| | 240 | | { |
| 0 | 241 | | BIWGizmosAxis gizmoAxis = hit.collider.gameObject.GetComponent<BIWGizmosAxis>(); |
| 0 | 242 | | SetAxisHover(gizmoAxis); |
| 0 | 243 | | } |
| | 244 | | else |
| | 245 | | { |
| 0 | 246 | | SetAxisHover(null); |
| | 247 | | } |
| 0 | 248 | | } |
| | 249 | |
|
| | 250 | | private void GizmoStatusUpdate() |
| | 251 | | { |
| 47 | 252 | | if (!IsGizmoActive()) |
| 0 | 253 | | return; |
| | 254 | |
|
| 47 | 255 | | if (selectedEntities == null || selectedEntities.Count == 0) |
| 46 | 256 | | HideGizmo(); |
| | 257 | | else |
| 1 | 258 | | ShowGizmo(); |
| 1 | 259 | | } |
| | 260 | | } |