Exemplo n.º 1
0
        public static ILocationF GetNudgeLocation(this SpacialElement el, IRectangularF desiredLocation = null, float optimalAngle = 0, int?z = null)
        {
            desiredLocation = desiredLocation ?? el.EffectiveBounds();
            var obstacles = el.GetObstacles(z: z);

            if (obstacles.Where(o => o.Touches(desiredLocation)).Any() || SpaceTime.CurrentSpaceTime.Bounds.Contains(desiredLocation) == false)
            {
                foreach (var angle in Enumerate360Angles(optimalAngle))
                {
                    for (var d = .1f; d < 15f; d += .1f)
                    {
                        var effectiveAngle = angle % 360;
                        var testLoc        = desiredLocation.MoveTowards(effectiveAngle, d);
                        var testArea       = RectangularF.Create(testLoc.Left, testLoc.Top, desiredLocation.Width, desiredLocation.Height);
                        if (obstacles.Where(o => o.Touches(testArea)).None() && SpaceTime.CurrentSpaceTime.Bounds.Contains(testArea))
                        {
                            return(testLoc.TopLeft());
                        }
                    }
                }
                return(null);
            }
            else
            {
                return(el.TopLeft());
            }
        }
Exemplo n.º 2
0
        public static NudgeEvent NudgeFree(this SpacialElement el, IRectangularF desiredLocation = null, float optimalAngle = 0, int?z = null)
        {
            var loc = GetNudgeLocation(el, desiredLocation, optimalAngle, z);

            if (loc != null)
            {
                if (el is IHaveMassBounds == false)
                {
                    el.MoveTo(loc.Left, loc.Top, z);
                }
                else
                {
                    var elBounds = el.EffectiveBounds();
                    var dx       = el.Left - elBounds.Left;
                    var dy       = el.Top - elBounds.Top;
                    el.MoveTo(loc.Left + dx, loc.Top + dy, z);
                }
                var ev = new NudgeEvent()
                {
                    Element = el, Success = true
                };
                OnNudge.Fire(ev);
                return(ev);
            }
            else
            {
                var ev = new NudgeEvent()
                {
                    Element = el, Success = false
                };
                OnNudge.Fire(ev);
                return(ev);
            }
        }
Exemplo n.º 3
0
        public static ILocationF GetNudgeLocation(this SpacialElement el, IRectangularF desiredLocation = null, float initialAngle = 0)
        {
            desiredLocation = desiredLocation ?? el.EffectiveBounds();
            var obstacles = el.GetObstacles();

            if (obstacles.Where(o => o.Touches(desiredLocation)).Any())
            {
                for (var d = 1f; d < 15; d++)
                {
                    for (var angle = initialAngle; angle < initialAngle + 360; angle += 20)
                    {
                        var effectiveAngle = angle % 360;
                        var testLoc        = desiredLocation.MoveTowards(effectiveAngle, d);
                        var testArea       = RectangularF.Create(testLoc.Left, testLoc.Top, desiredLocation.Width, desiredLocation.Height);
                        if (obstacles.Where(o => o.Touches(testArea)).None() && SpaceTime.CurrentSpaceTime.Bounds.Contains(testArea))
                        {
                            return(testLoc.TopLeft());
                        }
                    }
                }
            }
            return(null);
        }