< Summary

Class:Builder.Gizmos.DCLBuilderGizmoManager
Assembly:Builder
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/Gizmos/DCLBuilderGizmoManager.cs
Covered lines:72
Uncovered lines:53
Coverable lines:125
Total lines:297
Line coverage:57.6% (72 of 125)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLBuilderGizmoManager()0%110100%
GetSelectedGizmo()0%220100%
SetSnapFactor(...)0%220100%
OnBeginDrag(...)0%6200%
OnDrag(...)0%12300%
OnEndDrag()0%6200%
HasAxisHover()0%2100%
SetAxisHover(...)0%5.264057.14%
ForceRelativeScaleRatio()0%220100%
ShowGizmo()0%6200%
HideGizmo(...)0%330100%
IsGizmoActive()0%110100%
RaycastHit(...)0%6200%
OnEnable()0%220100%
OnDisable()0%110100%
Update()0%220100%
SetGizmoType(...)0%6.056088.89%
InitializeGizmos(...)0%2100%
InitializeGizmos(...)0%440100%
OnCameraZoomChanged(...)0%2100%
OnSetGridResolution(...)0%2100%
SetSelectedEntities(...)0%2100%
OnGizmosAxisPressed(...)0%2100%
OnMouseUp(...)0%12300%
OnMouseDrag(...)0%30500%
CheckGizmoHover(...)0%2.52050%
GizmoStatusUpdate()0%4.594066.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/Gizmos/DCLBuilderGizmoManager.cs

#LineLine coverage
 1using UnityEngine;
 2using System.Collections.Generic;
 3using System;
 4
 5namespace Builder.Gizmos
 6{
 7    public class DCLBuilderGizmoManager : MonoBehaviour
 8    {
 9        public delegate void GizmoTransformDelegate(string gizmoType);
 10
 11        public static event GizmoTransformDelegate OnGizmoTransformObjectStart;
 12        public static event GizmoTransformDelegate OnGizmoTransformObject;
 13        public static event GizmoTransformDelegate OnGizmoTransformObjectEnd;
 14
 15        public event Action<Vector3> OnChangeTransformValue;
 16
 17        public DCLBuilderRaycast builderRaycast;
 18
 19        [SerializeField] private DCLBuilderGizmo[] gizmos = null;
 20
 021        public bool isTransformingObject { private set; get; }
 022        public DCLBuilderGizmo activeGizmo { private set; get; }
 23
 19924        private SnapInfo snapInfo = new SnapInfo();
 25
 26        private bool isGameObjectActive = false;
 27        private bool isGizmosInitialized = false;
 28
 29        private DCLBuilderGizmoAxis hoveredAxis = null;
 30
 31        private Transform selectedEntitiesParent;
 32        private List<EditableEntity> selectedEntities;
 33
 34        public string GetSelectedGizmo()
 35        {
 836            if (IsGizmoActive())
 37            {
 638                return activeGizmo.GetGizmoType();
 39            }
 240            return DCL.Components.DCLGizmos.Gizmo.NONE;
 41        }
 42
 43        public void SetSnapFactor(float position, float rotation, float scale)
 44        {
 445            snapInfo.position = position;
 446            snapInfo.rotation = rotation;
 447            snapInfo.scale = scale;
 48
 449            if (activeGizmo != null)
 50            {
 451                activeGizmo.SetSnapFactor(snapInfo);
 52            }
 453        }
 54
 55        private void OnBeginDrag(DCLBuilderGizmoAxis hittedAxis)
 56        {
 057            isTransformingObject = true;
 058            activeGizmo = hittedAxis.GetGizmo();
 059            activeGizmo.OnBeginDrag(hittedAxis, selectedEntitiesParent);
 60
 061            OnGizmoTransformObjectStart?.Invoke(activeGizmo.GetGizmoType());
 062        }
 63
 64        private void OnDrag(Vector3 hitPoint, Vector2 mousePosition)
 65        {
 066            float value = activeGizmo.OnDrag(hitPoint, mousePosition);
 067            OnGizmoTransformObject?.Invoke(activeGizmo.GetGizmoType());
 068            OnChangeTransformValue?.Invoke(value * activeGizmo.GetActiveAxisVector());
 069        }
 70
 71        private void OnEndDrag()
 72        {
 073            activeGizmo.OnEndDrag();
 074            OnGizmoTransformObjectEnd?.Invoke(activeGizmo.GetGizmoType());
 075            isTransformingObject = false;
 076        }
 77
 078        public bool HasAxisHover() { return hoveredAxis != null; }
 79
 80        private void SetAxisHover(DCLBuilderGizmoAxis axis)
 81        {
 1182            if (hoveredAxis != null && hoveredAxis != axis)
 83            {
 084                hoveredAxis.SetColorDefault();
 085            }
 1186            else if (axis != null)
 87            {
 088                axis.SetColorHighlight();
 89            }
 1190            hoveredAxis = axis;
 1191        }
 92
 93        public void ForceRelativeScaleRatio()
 94        {
 3295            for (int i = 0; i < gizmos.Length; i++)
 96            {
 1297                gizmos[i].ForceRelativeScaleRatio();
 98            }
 499        }
 100
 101        public void ShowGizmo()
 102        {
 0103            if (activeGizmo != null)
 104            {
 0105                activeGizmo.SetTargetTransform(selectedEntitiesParent);
 0106                activeGizmo.gameObject.SetActive(true);
 107            }
 0108        }
 109
 110        public void HideGizmo(bool setInactiveGizmos = false)
 111        {
 29112            if (activeGizmo != null)
 113            {
 20114                activeGizmo.gameObject.SetActive(false);
 115            }
 29116            if (setInactiveGizmos)
 117            {
 1118                SetGizmoType(DCL.Components.DCLGizmos.Gizmo.NONE);
 119            }
 29120        }
 121
 22122        private bool IsGizmoActive() { return activeGizmo != null; }
 123
 124        private bool RaycastHit(Ray ray, out Vector3 hitPoint)
 125        {
 0126            if (activeGizmo != null)
 127            {
 0128                return activeGizmo.RaycastHit(ray, out hitPoint);
 129            }
 0130            hitPoint = Vector3.zero;
 0131            return false;
 132        }
 133
 134        private void OnEnable()
 135        {
 1136            if (!isGameObjectActive)
 137            {
 1138                DCLBuilderBridge.OnSelectGizmo += SetGizmoType;
 1139                DCLBuilderBridge.OnSetGridResolution += OnSetGridResolution;
 1140                DCLBuilderCamera.OnCameraZoomChanged += OnCameraZoomChanged;
 1141                DCLBuilderObjectSelector.OnSelectedObjectListChanged += SetSelectedEntities;
 1142                DCLBuilderObjectSelector.OnGizmosAxisPressed += OnGizmosAxisPressed;
 1143                DCLBuilderInput.OnMouseUp += OnMouseUp;
 1144                DCLBuilderInput.OnMouseDrag += OnMouseDrag;
 1145                isGameObjectActive = true;
 146            }
 1147        }
 148
 149        private void OnDisable()
 150        {
 1151            DCLBuilderBridge.OnSelectGizmo -= SetGizmoType;
 1152            DCLBuilderBridge.OnSetGridResolution -= OnSetGridResolution;
 1153            DCLBuilderCamera.OnCameraZoomChanged -= OnCameraZoomChanged;
 1154            DCLBuilderObjectSelector.OnSelectedObjectListChanged -= SetSelectedEntities;
 1155            DCLBuilderObjectSelector.OnGizmosAxisPressed -= OnGizmosAxisPressed;
 1156            DCLBuilderInput.OnMouseUp -= OnMouseUp;
 1157            DCLBuilderInput.OnMouseDrag -= OnMouseDrag;
 1158            isGameObjectActive = false;
 1159        }
 160
 161        private void Update()
 162        {
 11163            if (!isTransformingObject)
 164            {
 11165                CheckGizmoHover(Input.mousePosition);
 166            }
 11167        }
 168
 169        public void SetGizmoType(string gizmoType)
 170        {
 8171            HideGizmo();
 172
 8173            if (gizmoType != DCL.Components.DCLGizmos.Gizmo.NONE)
 174            {
 7175                bool wasGizmoActive = IsGizmoActive();
 176
 26177                for (int i = 0; i < gizmos.Length; i++)
 178                {
 13179                    if (gizmos[i].GetGizmoType() == gizmoType)
 180                    {
 7181                        activeGizmo = gizmos[i];
 7182                        activeGizmo.SetSnapFactor(snapInfo);
 7183                        break;
 184                    }
 185                }
 186
 7187                bool areEntitiesSelected = selectedEntities != null && selectedEntities.Count > 0;
 7188                if (wasGizmoActive && areEntitiesSelected)
 189                {
 0190                    ShowGizmo();
 0191                }
 192                else
 193                {
 7194                    GizmoStatusUpdate();
 195                }
 7196            }
 197            else
 198            {
 1199                activeGizmo = null;
 200            }
 1201        }
 202
 0203        public void InitializeGizmos(Camera camera) { InitializeGizmos(camera, camera.transform); }
 204
 205        public void InitializeGizmos(Camera camera, Transform cameraTransform)
 206        {
 4207            if (!isGizmosInitialized)
 208            {
 8209                for (int i = 0; i < gizmos.Length; i++)
 210                {
 3211                    if (!gizmos[i].initialized)
 212                    {
 3213                        gizmos[i].Initialize(camera, cameraTransform);
 214                    }
 215                }
 1216                isGizmosInitialized = true;
 217            }
 4218        }
 219
 0220        private void OnCameraZoomChanged(Camera camera, float zoom) { InitializeGizmos(camera); }
 221
 0222        private void OnSetGridResolution(float position, float rotation, float scale) { SetSnapFactor(position, rotation
 223
 224        public void SetSelectedEntities(Transform selectionParent, List<EditableEntity> entities)
 225        {
 0226            selectedEntities = entities;
 0227            selectedEntitiesParent = selectionParent;
 0228            GizmoStatusUpdate();
 0229        }
 0230        private void OnGizmosAxisPressed(DCLBuilderGizmoAxis pressedAxis) { OnBeginDrag(pressedAxis); }
 231
 232        private void OnMouseUp(int buttonId, Vector3 mousePosition)
 233        {
 0234            if (!isTransformingObject)
 235            {
 0236                return;
 237            }
 238
 0239            if (buttonId == 0)
 240            {
 0241                OnEndDrag();
 242            }
 0243        }
 244
 245        private void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 246        {
 0247            if (buttonId == 0)
 248            {
 0249                bool hasMouseMoved = (axisX != 0 || axisY != 0);
 0250                if (isTransformingObject && hasMouseMoved)
 251                {
 252                    Vector3 hit;
 0253                    if (RaycastHit(builderRaycast.GetMouseRay(mousePosition), out hit))
 254                    {
 0255                        OnDrag(hit, mousePosition);
 256                    }
 257                }
 258            }
 0259        }
 260
 261        private void CheckGizmoHover(Vector3 mousePosition)
 262        {
 263            RaycastHit hit;
 11264            if (builderRaycast.RaycastToGizmos(mousePosition, out hit))
 265            {
 0266                DCLBuilderGizmoAxis gizmoAxis = hit.collider.gameObject.GetComponent<DCLBuilderGizmoAxis>();
 0267                SetAxisHover(gizmoAxis);
 0268            }
 269            else
 270            {
 11271                SetAxisHover(null);
 272            }
 11273        }
 274
 275        private void GizmoStatusUpdate()
 276        {
 7277            if (IsGizmoActive())
 278            {
 7279                if (selectedEntities == null || selectedEntities.Count == 0)
 280                {
 7281                    HideGizmo();
 7282                }
 283                else
 284                {
 0285                    ShowGizmo();
 286                }
 287            }
 0288        }
 289
 290        public class SnapInfo
 291        {
 292            public float position = 0;
 293            public float rotation = 0;
 294            public float scale = 0;
 295        }
 296    }
 297}