< Summary

Class:Builder.DCLBuilderObjectDragger
Assembly:Builder
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/DCLBuilderObjectDragger.cs
Covered lines:15
Uncovered lines:36
Coverable lines:51
Total lines:123
Line coverage:29.4% (15 of 51)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OnEnable()0%220100%
OnDisable()0%110100%
OnSelectedObjectListChanged(...)0%6200%
OnEntityPressed(...)0%20400%
OnMouseUp(...)0%30500%
OnMouseDrag(...)0%30500%
DragTargetEntity(...)0%12300%
OnSetGridResolution(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/DCLBuilderObjectDragger.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5namespace Builder
 6{
 7    public class DCLBuilderObjectDragger : MonoBehaviour
 8    {
 9        public DCLBuilderRaycast builderRaycast;
 10
 11        public static event Action OnDraggingObjectStart;
 12        public static event Action OnDraggingObject;
 13        public static event Action OnDraggingObjectEnd;
 14
 15        private List<EditableEntity> selectedEntities;
 16        private Vector3 targetOffset;
 17        private Vector3 initialHitPoint;
 18        private Transform selectedEntitiesParent;
 19        private bool isDragging = false;
 20
 21        private float snapFactorPosition = 0;
 22
 23        private bool isGameObjectActive = false;
 24
 25        private void OnEnable()
 26        {
 127            if (!isGameObjectActive)
 28            {
 129                DCLBuilderObjectSelector.OnSelectedObjectListChanged += OnSelectedObjectListChanged;
 130                DCLBuilderObjectSelector.OnEntityPressed += OnEntityPressed;
 131                DCLBuilderInput.OnMouseDrag += OnMouseDrag;
 132                DCLBuilderInput.OnMouseUp += OnMouseUp;
 133                DCLBuilderBridge.OnSetGridResolution += OnSetGridResolution;
 34            }
 135            isGameObjectActive = true;
 136        }
 37
 38        private void OnDisable()
 39        {
 140            isGameObjectActive = false;
 141            DCLBuilderObjectSelector.OnSelectedObjectListChanged -= OnSelectedObjectListChanged;
 142            DCLBuilderObjectSelector.OnEntityPressed -= OnEntityPressed;
 143            DCLBuilderInput.OnMouseDrag -= OnMouseDrag;
 144            DCLBuilderInput.OnMouseUp -= OnMouseUp;
 145            DCLBuilderBridge.OnSetGridResolution -= OnSetGridResolution;
 146        }
 47
 48        private void OnSelectedObjectListChanged(Transform selectionParent, List<EditableEntity> selectedEntities)
 49        {
 050            this.selectedEntitiesParent = selectionParent;
 051            this.selectedEntities = selectedEntities;
 052            if (isDragging)
 53            {
 054                targetOffset = selectedEntitiesParent.position - initialHitPoint;
 55            }
 056        }
 57
 58        private void OnEntityPressed(DCLBuilderEntity entity, Vector3 hitPoint)
 59        {
 060            if (selectedEntities == null || !selectedEntities.Contains(entity))
 61            {
 062                return;
 63            }
 64
 065            OnDraggingObjectStart?.Invoke();
 66
 067            initialHitPoint = hitPoint;
 068            targetOffset = selectedEntitiesParent.position - hitPoint;
 069            builderRaycast.SetEntityHitPlane(hitPoint.y);
 070            isDragging = true;
 071        }
 72
 73        private void OnMouseUp(int buttonId, Vector3 mousePosition)
 74        {
 75            // NOTE: only process mouse's left button press
 076            if (buttonId != 0)
 77            {
 078                return;
 79            }
 80
 081            if (isDragging && selectedEntities != null)
 82            {
 083                OnDraggingObjectEnd?.Invoke();
 84            }
 085            isDragging = false;
 086        }
 87
 88        private void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 89        {
 90            // NOTE: only process if mouse's left button is pressed and if there is any entity selected
 091            if (buttonId != 0 || selectedEntities == null)
 92            {
 093                return;
 94            }
 95
 096            bool hasMouseMoved = (axisX != 0 || axisY != 0);
 097            if (isDragging && hasMouseMoved)
 98            {
 099                DragTargetEntity(mousePosition);
 100            }
 0101        }
 102
 103        private void DragTargetEntity(Vector3 mousePosition)
 104        {
 0105            Vector3 hitPosition = builderRaycast.RaycastToEntityHitPlane(mousePosition);
 0106            Vector3 targetPosition = hitPosition + targetOffset;
 0107            targetPosition.y = selectedEntitiesParent.position.y;
 108
 0109            if (snapFactorPosition > 0)
 110            {
 0111                targetPosition.x = targetPosition.x - (targetPosition.x % snapFactorPosition);
 0112                targetPosition.z = targetPosition.z - (targetPosition.z % snapFactorPosition);
 113            }
 114
 0115            Vector3 moveAmount = targetPosition - selectedEntitiesParent.transform.position;
 0116            selectedEntitiesParent.transform.position = targetPosition;
 117
 0118            OnDraggingObject?.Invoke();
 0119        }
 120
 0121        private void OnSetGridResolution(float position, float rotation, float scale) { snapFactorPosition = position; }
 122    }
 123}