/// <inheritdoc/>
        void IAddElementsHandler.AddElement(Vector3 addPosition)
        {
            var previousWaypoint = waypointInstance;

            waypointInstance = ScenarioManager.Instance.GetExtension <ScenarioWaypointsManager>()
                               .GetWaypointInstance <ScenarioDestinationPointWaypoint>();
            waypointInstance.transform.position = addPosition;
            var mapManager = ScenarioManager.Instance.GetExtension <ScenarioMapManager>();

            switch (selectedDestinationPoint.ParentAgent.Type)
            {
            case AgentType.Ego:
                mapManager.LaneSnapping.SnapToLane(LaneSnappingHandler.LaneType.Traffic,
                                                   waypointInstance.transform);
                break;

            case AgentType.Npc:
                mapManager.LaneSnapping.SnapToLane(LaneSnappingHandler.LaneType.Traffic,
                                                   waypointInstance.transform);
                break;

            case AgentType.Pedestrian:
                mapManager.LaneSnapping.SnapToLane(LaneSnappingHandler.LaneType.Pedestrian,
                                                   waypointInstance.transform);
                break;
            }

            selectedWaypointsPath.AddWaypoint(waypointInstance, previousWaypoint);
            ScenarioManager.Instance.IsScenarioDirty = true;
            ScenarioManager.Instance.GetExtension <ScenarioUndoManager>()
            .RegisterRecord(new UndoAddElement(previousWaypoint));
        }
        /// <inheritdoc/>
        void IAddElementsHandler.AddingCancelled(Vector3 addPosition)
        {
            if (waypointInstance != null)
            {
                waypointInstance.RemoveFromMap();
                waypointInstance.Dispose();
            }

            waypointInstance  = null;
            isAddingWaypoints = false;
        }
        /// <inheritdoc/>
        void IAddElementsHandler.AddingStarted(Vector3 addPosition)
        {
            if (selectedDestinationPoint == null)
            {
                Debug.LogWarning("Cannot add waypoints if no agent or waypoint is selected.");
                ScenarioManager.Instance.GetExtension <InputManager>().CancelAddingElements(this);
                return;
            }

            isAddingWaypoints = true;

            waypointInstance = ScenarioManager.Instance.GetExtension <ScenarioWaypointsManager>()
                               .GetWaypointInstance <ScenarioDestinationPointWaypoint>();
            if (waypointInstance == null)
            {
                Debug.LogWarning("Cannot add waypoints. Add waypoint component to the prefab.");
                ScenarioManager.Instance.GetExtension <InputManager>().CancelAddingElements(this);
                ScenarioManager.Instance.prefabsPools.ReturnInstance(waypointInstance.gameObject);
                return;
            }

            waypointInstance.transform.position = addPosition;
            var mapManager = ScenarioManager.Instance.GetExtension <ScenarioMapManager>();

            switch (selectedDestinationPoint.ParentAgent.Type)
            {
            case AgentType.Ego:
                mapManager.LaneSnapping.SnapToLane(LaneSnappingHandler.LaneType.Traffic,
                                                   waypointInstance.transform);
                break;

            case AgentType.Npc:
                mapManager.LaneSnapping.SnapToLane(LaneSnappingHandler.LaneType.Traffic,
                                                   waypointInstance.transform);
                break;

            case AgentType.Pedestrian:
                mapManager.LaneSnapping.SnapToLane(LaneSnappingHandler.LaneType.Pedestrian,
                                                   waypointInstance.transform);
                break;
            }

            selectedWaypointsPath.AddWaypoint(waypointInstance, selectedWaypoint);
        }
        /// <summary>
        /// Method called when another scenario element has been selected
        /// </summary>
        /// <param name="previousElement">Scenario element that has been deselected</param>
        /// <param name="selectedElement">Scenario element that has been selected</param>
        private void OnSelectedOtherElement(ScenarioElement previousElement, ScenarioElement selectedElement)
        {
            if (isAddingWaypoints)
            {
                ScenarioManager.Instance.GetExtension <InputManager>().CancelAddingElements(this);
            }

            // Force input apply on deselect
            if (selectedWaypoint != null)
            {
                SubmitChangedInputs();
            }
            selectedWaypoint = selectedElement as ScenarioDestinationPointWaypoint;
            var parentAgent = selectedWaypoint != null ? (ScenarioAgent)selectedWaypoint.ParentElement : null;

            selectedDestinationPoint = parentAgent != null
                ? parentAgent.GetExtension <AgentDestinationPoint>()?.DestinationPoint
                : null;

            selectedWaypointsPath = selectedDestinationPoint != null ? selectedDestinationPoint.PlaybackPath : null;

            // Disable panel if it is not supported
            if (selectedDestinationPoint == null || selectedWaypointsPath == null)
            {
                gameObject.SetActive(false);
            }
            else
            {
                gameObject.SetActive(true);
                speedPanel.SetActive(selectedWaypoint != null);
                if (selectedWaypoint != null)
                {
                    speedInput.CurrentContext = selectedWaypoint;
                    speedInput.ExternalValueChange(selectedWaypoint.DestinationSpeed, selectedWaypoint, false);
                }
                UnityUtilities.LayoutRebuild(transform as RectTransform);
            }
        }