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