private void SnapToNearestHex()
        {
            // If the space has no hexes, then just leave
            if (_space.Count == 0)
            {
                return;
            }

            // First, work out which Hex is now in the center. It might be an occupied Hex or a free one.
            var centerHex = _hex2Pix.ToHex(-_xTranslation, -_yTranslation, Size);

            // If occupied Hex: just center it.
            if (_space.Contains(centerHex))
            {
                SnapToHex(centerHex);
            }
            else // If free Hex: find the nearest occupied Hex and move it to the center
            {
                var nearestHexes = _space.GetNearestHexes(centerHex);

                // From the equidistant Hexes, determine the one where the View is closest to the center and snap to that Hex
                var nearestHex = nearestHexes.OrderBy(hex => GetViewForHex(hex).GetDistanceToCenter()).FirstOrDefault();
                SnapToHex(nearestHex);
            }

            void SnapToHex(Hex hex)
            {
                var centerChild = GetViewForHex(hex);

                SnapChildren(
                    _content.Width / 2.0d - (centerChild.X + centerChild.Width / 2.0d + _xTranslation),
                    _content.Height / 2.0d - (centerChild.Y + centerChild.Height / 2.0d + _yTranslation)
                    );
            }
        }
Exemple #2
0
        private void SnapToNearestHex()
        {
            // If the space has no hexes, then just leave
            if (_space.Count == 0)
            {
                return;
            }

            // First, work out which Hex is now in the center. It might be an occupied Hex or a free one.
            var centerHex = _hex2Pix.ToHex(-_xTranslation, -_yTranslation, Size);

            // If occupied Hex: just center it.
            if (_space.Contains(centerHex))
            {
                SnapToHex(centerHex);
            }
            else // If free Hex: find the nearest occupied Hex and move it to the center
            {
                var nearestHex = _space.GetNearestHexes(centerHex).First();
                SnapToHex(nearestHex);
            }

            void SnapToHex(Hex hex)
            {
                var centerPayload = _space.GetPayload(hex);
                var centerChild   = _content.Children.First(c => c.BindingContext == centerPayload);

                SnapChildren(
                    _content.Width / 2.0d - (centerChild.X + centerChild.Width / 2.0d + _xTranslation),
                    _content.Height / 2.0d - (centerChild.Y + centerChild.Height / 2.0d + _yTranslation)
                    );
            }
        }