public void Utilities_CanEraseInNativeArrayWithCapacity()
    {
        var array1 = new NativeArray <int>(new[] { 1, 2, 3, 4, 5, 0, 0, 0 }, Allocator.Temp);
        var array2 = new NativeArray <int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }, Allocator.Temp);
        var array3 = new NativeArray <int>(new[] { 1, 2, 3, 4, 0, 0, 0, 0 }, Allocator.Temp);

        var array1Length = 5;
        var array2Length = 8;
        var array3Length = 4;

        try
        {
            ArrayHelpers.EraseAtWithCapacity(array1, ref array1Length, 2);
            ArrayHelpers.EraseAtWithCapacity(array2, ref array2Length, 7);
            ArrayHelpers.EraseAtWithCapacity(array3, ref array3Length, 0);

            // For NativeArray, we don't clear memory.
            Assert.That(array1, Is.EquivalentTo(new[] { 1, 2, 4, 5, 5, 0, 0, 0 }));
            Assert.That(array2, Is.EquivalentTo(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }));
            Assert.That(array3, Is.EquivalentTo(new[] { 2, 3, 4, 4, 0, 0, 0, 0 }));

            Assert.That(array1Length, Is.EqualTo(4));
            Assert.That(array2Length, Is.EqualTo(7));
            Assert.That(array3Length, Is.EqualTo(3));
        }
        finally
        {
            array1.Dispose();
            array2.Dispose();
            array3.Dispose();
        }
    }
Exemple #2
0
        /// <summary>
        /// Remove an active user.
        /// </summary>
        /// <param name="user">An active user.</param>
        /// <exception cref="ArgumentNullException"><paramref name="user"/> is <c>null</c>.</exception>
        /// <remarks>
        /// Removing a user also unassigns all currently assigned devices from the user. On completion of this
        /// method, <see cref="devices"/> of <paramref name="user"/> will be empty.
        /// </remarks>
        public static void Remove(IInputUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var index = FindUserIndex(user);

            if (index == -1)
            {
                return;
            }

            // Remove devices.
            var userData = s_AllUserData[index];

            if (userData.deviceCount > 0)
            {
                ArrayHelpers.EraseSliceWithCapacity(ref s_AllDevices, ref s_AllDeviceCount, userData.deviceStartIndex,
                                                    userData.deviceCount);
            }

            // Remove.
            var userCount = s_AllUserCount;

            ArrayHelpers.EraseAtWithCapacity(ref s_AllUsers, ref userCount, index);
            ArrayHelpers.EraseAtWithCapacity(ref s_AllUserData, ref s_AllUserCount, index);

            // Send notification.
            Notify(user, InputUserChange.Removed);
        }
Exemple #3
0
        /// <summary>
        /// Pop a specific action located anywhere in the stack off the stack.
        /// </summary>
        /// <param name="action"></param>
        /// <remarks>
        /// Does nothing if the action is not on the stack.
        /// </remarks>
        public void Pop(InputAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (m_ActionCount == 0)
            {
                return;
            }

            // Find the action on the stack.
            var index = ArrayHelpers.IndexOfReference(m_Actions, m_ActionCount, action);

            if (index == -1)
            {
                return;
            }

            // If we don't own the action array, duplicate.
            if ((m_Flags & Flags.UsingActionArrayOfMap) != 0)
            {
                ArrayHelpers.DuplicateWithCapacity(ref m_Actions, m_ActionCount, 10);
            }

            // Remove the action.
            ArrayHelpers.EraseAtWithCapacity(ref m_Actions, ref m_ActionCount, index);

            // Finally, disable action if necessary.
            if (enabled)
            {
                action.Disable();
            }
        }
        protected void RemovePointer(Pointer pointer)
        {
            if (pointer == null)
            {
                throw new ArgumentNullException(nameof(pointer));
            }

            // Ignore if not added.
            var index = ArrayHelpers.IndexOfReference(m_Sources, pointer, m_NumSources);

            if (index == -1)
            {
                return;
            }

            // Removing the pointer will shift indices of all pointers coming after it. So we uninstall all
            // monitors starting with the device we're about to remove and then re-install whatever is left
            // starting at the same index.
            UninstallStateChangeMonitors(index);

            // Cancel all ongoing touches from the pointer.
            for (var i = 0; i < m_Touches.Length; ++i)
            {
                if (m_Touches[i].touchId == 0 || m_Touches[i].sourceIndex != index)
                {
                    continue;
                }

                var isPrimary = m_PrimaryTouchIndex == i;
                var touch     = new TouchState
                {
                    phase    = TouchPhase.Canceled,
                    position = m_CurrentPositions[index],
                    touchId  = m_Touches[i].touchId,
                };

                if (isPrimary)
                {
                    InputState.Change(simulatedTouchscreen.primaryTouch, touch);
                    m_PrimaryTouchIndex = -1;
                }

                InputState.Change(simulatedTouchscreen.touches[i], touch);

                m_Touches[i].touchId     = 0;
                m_Touches[i].sourceIndex = 0;
            }

            // Remove from arrays.
            var numPositions = m_NumSources;

            ArrayHelpers.EraseAtWithCapacity(m_CurrentPositions, ref numPositions, index);
            ArrayHelpers.EraseAtWithCapacity(m_Sources, ref m_NumSources, index);

            if (index != m_NumSources)
            {
                InstallStateChangeMonitors(index);
            }
        }
Exemple #5
0
        /// <summary>
        /// Remove the control at the given index.
        /// </summary>
        /// <param name="index">Index of control to remove.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is negative or equal
        /// or greater than <see cref="Count"/>.</exception>
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= m_Count)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(index), $"Index {index} is out of range in list with {m_Count} elements");
            }

            ArrayHelpers.EraseAtWithCapacity(m_Indices, ref m_Count, index);
        }
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= m_Count)
            {
                throw new ArgumentException(
                          string.Format("Index {0} is out of range in list with {1} elements", index, m_Count), "index");
            }

            ArrayHelpers.EraseAtWithCapacity(ref m_Indices, ref m_Count, index);
        }
Exemple #7
0
        public void Remove(TControl control)
        {
            if (m_Count == 0)
            {
                return;
            }

            var index = ToIndex(control);

            for (var i = 0; i < m_Count; ++i)
            {
                if (m_Indices[i] == index)
                {
                    ArrayHelpers.EraseAtWithCapacity(ref m_Indices, ref m_Count, i);
                    break;
                }
            }
        }
            public void RemoveCandidate(InputControl control)
            {
                if (control == null)
                {
                    throw new ArgumentNullException(nameof(control));
                }

                var index = m_Candidates.IndexOf(control);

                if (index == -1)
                {
                    return;
                }

                var candidateCount = m_Candidates.Count;

                m_Candidates.RemoveAt(index);
                ArrayHelpers.EraseAtWithCapacity(m_Scores, ref candidateCount, index);
            }
Exemple #9
0
        /// <summary>
        /// Called when the gamepad is removed from the system.
        /// </summary>
        protected override void OnRemoved()
        {
            if (current == this)
            {
                current = null;
            }

            // Remove from `all`.
            var index = ArrayHelpers.IndexOfReference(s_Gamepads, this, s_GamepadCount);

            if (index != -1)
            {
                ArrayHelpers.EraseAtWithCapacity(s_Gamepads, ref s_GamepadCount, index);
            }
            else
            {
                Debug.Assert(false,
                             $"Gamepad {this} seems to not have been added but is being removed (gamepad list: {string.Join(", ", all)})"); // Put in else to not allocate on normal path.
            }
        }
Exemple #10
0
        /// <summary>
        /// Remove a control from the list.
        /// </summary>
        /// <param name="item">Control to remove. Can be null.</param>
        /// <returns>True if the control was found in the list and removed, false otherwise.</returns>
        /// <seealso cref="Add"/>
        public bool Remove(TControl item)
        {
            if (m_Count == 0)
            {
                return(false);
            }

            var index = ToIndex(item);

            for (var i = 0; i < m_Count; ++i)
            {
                if (m_Indices[i] == index)
                {
                    ArrayHelpers.EraseAtWithCapacity(m_Indices, ref m_Count, i);
                    return(true);
                }
            }

            return(false);
        }
Exemple #11
0
        protected void RemovePointer(Pointer pointer)
        {
            if (pointer == null)
            {
                throw new ArgumentNullException(nameof(pointer));
            }

            // Ignore if not added.
            var pointerIndex = m_Pointers.IndexOfReference(pointer, m_NumPointers);

            if (pointerIndex == -1)
            {
                return;
            }

            // Cancel all ongoing touches from the pointer.
            for (var i = 0; i < m_Touches.Length; ++i)
            {
                var button = m_Touches[i];
                if (button != null && button.device != pointer)
                {
                    continue;
                }

                UpdateTouch(i, pointerIndex, TouchPhase.Canceled);
            }

            // Remove from list.
            var numPointers = m_NumPointers;

            ArrayHelpers.EraseAtWithCapacity(m_Pointers, ref m_NumPointers, pointerIndex);
            ArrayHelpers.EraseAtWithCapacity(m_CurrentPositions, ref numPointers, pointerIndex);

            // Re-enable the device (only in case it's still added to the system).
            if (pointer.added)
            {
                InputSystem.EnableDevice(pointer);
            }
        }
    public void Utilities_CanEraseInArrayWithCapacity()
    {
        var array1 = new[] { 1, 2, 3, 4, 5, 0, 0, 0 };
        var array2 = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        var array3 = new[] { 1, 2, 3, 4, 0, 0, 0, 0 };

        var array1Length = 5;
        var array2Length = 8;
        var array3Length = 4;

        ArrayHelpers.EraseAtWithCapacity(array1, ref array1Length, 2);
        ArrayHelpers.EraseAtWithCapacity(array2, ref array2Length, 7);
        ArrayHelpers.EraseAtWithCapacity(array3, ref array3Length, 0);

        Assert.That(array1, Is.EquivalentTo(new[] { 1, 2, 4, 5, 0, 0, 0, 0 }));
        Assert.That(array2, Is.EquivalentTo(new[] { 1, 2, 3, 4, 5, 6, 7, 0 }));
        Assert.That(array3, Is.EquivalentTo(new[] { 2, 3, 4, 0, 0, 0, 0, 0 }));

        Assert.That(array1Length, Is.EqualTo(4));
        Assert.That(array2Length, Is.EqualTo(7));
        Assert.That(array3Length, Is.EqualTo(3));
    }