/// <summary>
            /// Gets the index of the nearest item.
            /// </summary>
            /// <returns>The nearest item index.</returns>
            /// <param name="point">Point.</param>
            /// <param name="type">Preferable nearest index.</param>
            public override int GetNearestIndex(Vector2 point, NearestType type)
            {
                if (Owner.IsSortEnabled())
                {
                    return(-1);
                }

                var pos = Owner.IsHorizontal() ? point.x : -point.y;

                int index;

                switch (type)
                {
                case NearestType.Auto:
                    index = Mathf.RoundToInt(pos / GetItemSize());
                    break;

                case NearestType.Before:
                    index = Mathf.FloorToInt(pos / GetItemSize());
                    break;

                case NearestType.After:
                    index = Mathf.CeilToInt(pos / GetItemSize());
                    break;

                default:
                    throw new NotSupportedException("Unsupported NearestType: " + type);
                }

                return(Mathf.Min(index, Owner.DataSource.Count));
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Gets the index of the nearest item.
            /// </summary>
            /// <returns>The nearest item index.</returns>
            /// <param name="point">Point.</param>
            /// <param name="type">Preferable nearest index.</param>
            public override int GetNearestIndex(Vector2 point, NearestType type)
            {
                if (Owner.IsSortEnabled())
                {
                    return(-1);
                }

                var pos_block = Owner.IsHorizontal() ? point.x : -point.y;
                var start     = GetIndexAtPosition(pos_block);

                var position = Owner.IsHorizontal() ? -point.y : point.x;
                var spacing  = Owner.LayoutBridge.GetSpacing();
                var end      = Mathf.Min(Owner.DataSource.Count, start + GetItemsPerBlock());

                var index = 0;

                for (int i = start; i < end; i++)
                {
                    index = i;

                    var item_size = GetItemSize(i);
                    if (i > 0)
                    {
                        item_size += spacing;
                    }

                    if (position < item_size)
                    {
                        break;
                    }

                    position -= item_size;
                }

                switch (type)
                {
                case NearestType.Auto:
                    if (position >= (GetItemSize(index) / 2f))
                    {
                        index += 1;
                    }

                    break;

                case NearestType.Before:
                    break;

                case NearestType.After:
                    index += 1;
                    break;

                default:
                    throw new NotSupportedException("Unsupported NearestType: " + type);
                }

                return(Mathf.Min(index, Owner.DataSource.Count, start));
            }
Ejemplo n.º 3
0
            /// <summary>
            /// Gets the index of the nearest item.
            /// </summary>
            /// <returns>The nearest item index.</returns>
            /// <param name="point">Point.</param>
            /// <param name="type">Preferable nearest index.</param>
            public override int GetNearestIndex(Vector2 point, NearestType type)
            {
                if (Owner.IsSortEnabled())
                {
                    return(-1);
                }

                var pos_block = Owner.IsHorizontal() ? point.x : -point.y;
                var index     = GetIndexAtPosition(pos_block, type);

                return(Mathf.Min(index, Owner.DataSource.Count));
            }
            int GetIndexAtBlock(float position, int blockIndex, NearestType type)
            {
                var spacing = Owner.LayoutBridge.GetSpacing();
                var block   = BlocksIndices[blockIndex];
                var index   = block[0];

                var item_size = GetItemSize(index);

                if (position > item_size)
                {
                    position -= item_size;

                    for (var i = 1; i < block.Count; i++)
                    {
                        index = block[i];

                        item_size = GetItemSize(block[i]) + spacing;

                        if (position < item_size)
                        {
                            break;
                        }

                        position -= item_size;
                    }
                }

                switch (type)
                {
                case NearestType.Auto:
                    if (position >= (GetItemSize(index) / 2f))
                    {
                        index += 1;
                    }

                    break;

                case NearestType.Before:
                    break;

                case NearestType.After:
                    index += 1;
                    break;

                default:
                    throw new NotSupportedException("Unsupported NearestType: " + type);
                }

                return(index);
            }
Ejemplo n.º 5
0
        public Tile FindNearest(NearestType type, int boardLocation)
        {
            switch (type)
            {
            case NearestType.Railroad:
                return(tiles.Where(tile => tile is RailroadTile).OrderBy(tile => Math.Abs(tile.BoardPosition - boardLocation)).First());

            case NearestType.Utility:
                return(tiles.Where(tile => tile is UtilityTile).OrderBy(tile => Math.Abs(tile.BoardPosition - boardLocation)).First());

            default:
                throw new Exception($"Unhandled nearest tile type: {type}");
            }
        }
            /// <summary>
            /// Gets the index of the nearest item.
            /// </summary>
            /// <returns>The nearest item index.</returns>
            /// <param name="point">Point.</param>
            /// <param name="type">Preferable nearest index.</param>
            public override int GetNearestIndex(Vector2 point, NearestType type)
            {
                if (Owner.IsSortEnabled())
                {
                    return(-1);
                }

                var spacing = Owner.IsHorizontal() ? Owner.LayoutBridge.GetSpacingY() : Owner.LayoutBridge.GetSpacingX();
                var size    = Owner.IsHorizontal() ? Owner.ItemSize.y : Owner.ItemSize.x;

                var block_pos   = Owner.IsHorizontal() ? -point.y : point.x;
                var item_pos    = Owner.IsHorizontal() ? point.x : -point.y;
                var block_index = (block_pos < size) ? 0 : Mathf.FloorToInt((block_pos - size) / (size + spacing)) + 1;

                return(GetIndexAtBlock(item_pos, block_index, type));
            }
Ejemplo n.º 7
0
            /// <summary>
            /// Gets the index of the item at he specified position.
            /// </summary>
            /// <returns>The index.</returns>
            /// <param name="position">Position.</param>
            /// <param name="type">Type.</param>
            int GetIndexAtPosition(float position, NearestType type)
            {
                var spacing = Owner.LayoutBridge.GetSpacing();
                var index   = 0;

                for (int i = 0; i < Owner.DataSource.Count; i++)
                {
                    index = i;

                    var item_size = GetItemSize(i);
                    if (i > 0)
                    {
                        item_size += spacing;
                    }

                    if (position < item_size)
                    {
                        break;
                    }

                    position -= item_size;
                }

                switch (type)
                {
                case NearestType.Auto:
                    var item_size = GetItemSize(index);
                    if (position >= (item_size / 2f))
                    {
                        index += 1;
                    }

                    break;

                case NearestType.Before:
                    break;

                case NearestType.After:
                    index += 1;
                    break;

                default:
                    throw new NotSupportedException("Unsupported NearestType: " + type);
                }

                return(index);
            }
Ejemplo n.º 8
0
            /// <summary>
            /// Gets the index of the nearest item.
            /// </summary>
            /// <returns>The nearest item index.</returns>
            /// <param name="point">Point.</param>
            /// <param name="type">Preferable nearest index.</param>
            public override int GetNearestIndex(Vector2 point, NearestType type)
            {
                // RoundToInt replaced with FloorToInt
                if (Owner.IsSortEnabled())
                {
                    return(-1);
                }

                // block index
                var pos_block = Owner.IsHorizontal() ? point.x : -point.y;

                var block = Mathf.FloorToInt(pos_block / GetItemSize());

                // item index in block
                var pos_elem = Owner.IsHorizontal() ? -point.y : point.x;
                var size     = Owner.IsHorizontal() ? Owner.ItemSize.y + Owner.GetItemSpacingY() : Owner.ItemSize.x + Owner.GetItemSpacingX();

                int k;

                switch (type)
                {
                case NearestType.Auto:
                    k = Mathf.RoundToInt(pos_elem / size);
                    break;

                case NearestType.Before:
                    k = Mathf.FloorToInt(pos_elem / size);
                    break;

                case NearestType.After:
                    k = Mathf.CeilToInt(pos_elem / size);
                    break;

                default:
                    throw new NotSupportedException("Unsupported NearestType: " + type);
                }

                return((block * GetItemsPerBlock()) + k);
            }
Ejemplo n.º 9
0
            /// <summary>
            /// Gets the index of the nearest item.
            /// </summary>
            /// <returns>The nearest item index.</returns>
            /// <param name="point">Point.</param>
            /// <param name="type">Preferable nearest index.</param>
            public override int GetNearestIndex(Vector2 point, NearestType type)
            {
                if (Owner.IsSortEnabled())
                {
                    return(0);
                }

                if (Owner.Components.Count == 0)
                {
                    return(0);
                }

                if (Owner.Components.Count == 1)
                {
                    switch (type)
                    {
                    case NearestType.Auto:
                        return(0);

                    case NearestType.Before:
                        return(0);

                    case NearestType.After:
                        return(1);

                    default:
                        throw new NotSupportedException("Unknown position: " + type);
                    }
                }

                int index;

                var nearest          = 0;
                var minimal_distance = GetDistance(nearest, point);
                var base_distance    = BaseDistance();

                switch (type)
                {
                case NearestType.Auto:
                    for (int i = 1; i < Owner.Components.Count; i++)
                    {
                        var new_distance = GetDistance(i, point);
                        if (new_distance < minimal_distance)
                        {
                            minimal_distance = new_distance;
                            nearest          = i;
                        }
                    }

                    index = Owner.Components[nearest].Index;
                    break;

                case NearestType.Before:
                    for (int i = 1; i < Owner.Components.Count; i++)
                    {
                        var new_distance = GetDistance(i, point);
                        if ((new_distance < minimal_distance) && (new_distance < (base_distance / 2f)))
                        {
                            minimal_distance = new_distance;
                            nearest          = i;
                        }
                    }

                    index = Owner.Components[nearest].Index;
                    break;

                case NearestType.After:
                    for (int i = 1; i < Owner.Components.Count; i++)
                    {
                        var new_distance = GetDistance(i, point);
                        if ((new_distance < minimal_distance) && (new_distance < (base_distance / 2f)))
                        {
                            minimal_distance = new_distance;
                            nearest          = i;
                        }
                    }

                    index = Owner.Components[nearest].Index + 1;
                    break;

                default:
                    throw new NotSupportedException("Unsupported NearestType: " + type);
                }

                return(index);
            }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets the index of the nearest item.
 /// </summary>
 /// <returns>The nearest item index.</returns>
 /// <param name="point">Point.</param>
 /// <param name="type">Preferable nearest index.</param>
 public abstract int GetNearestIndex(Vector2 point, NearestType type);
Ejemplo n.º 11
0
 /// <summary>
 /// Gets the index of the nearest item.
 /// </summary>
 /// <returns>The nearest item index.</returns>
 /// <param name="point">Point.</param>
 /// <param name="type">Preferable nearest index.</param>
 public virtual int GetNearestIndex(Vector2 point, NearestType type)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets the index of the nearest item.
 /// </summary>
 /// <returns>The nearest index.</returns>
 /// <param name="eventData">Event data.</param>
 /// <param name="type">Preferable nearest index.</param>
 public virtual int GetNearestIndex(PointerEventData eventData, NearestType type)
 {
     throw new NotSupportedException();
 }