Esempio n. 1
0
        public override void Update(GameTime gameTime)
        {
            if (BoundingRect.Contains(InputManager.MousePosition))
            {
                if (InputManager.IsMouseButtonDown(MouseButton.Left))
                {
                    State = ButtonState.Clicking;
                }
                else if (InputManager.WasMouseButtonReleased(MouseButton.Left))
                {
                    if (State == ButtonState.Clicking)
                    {
                        State = ButtonState.Pressed;

                        Clicked?.Invoke(this, EventArgs.Empty);
                    }
                }
                else
                {
                    State = ButtonState.Hovered;
                }
            }
            else
            {
                State = ButtonState.None;
            }

            base.Update(gameTime);
        }
        /// <summary>
        /// True if the point is in the shape.
        /// </summary>
        /// <param name="pt">The point to test set.</param>
        public bool Contains(C2DPoint pt)
        {
            if (!BoundingRect.Contains(pt))
            {
                return(false);
            }

            var IntersectedPts = new C2DPointSet();

            var Ray = new C2DLine(pt, new C2DVector(BoundingRect.Width(), 0.000001));     // Make sure to leave

            if (!this.Crosses(Ray, IntersectedPts))
            {
                return(false);
            }
            else
            {
                IntersectedPts.SortByDistance(Ray.point);
                if (IntersectedPts[0].PointEqualTo(pt))
                {
                    // For integers, the pt can start On a line, meaning it's INSIDE, but the ray could cross again
                    // so just return true. Because the equality test is really a test for proximity, this leads to the
                    // possibility that a point could lie just outside the shape but be considered to be inside. This would
                    // only be a problem with very small shapes that are a very long way from the origin. E.g. a 1m2 object
                    // 1 million metres from the origin and a point 0.1mm away from the edge would give rise to a relative
                    // difference of 0.0001 / 1000000 = 0.000000001 which would just be consider to be inside.
                    return(true);
                }
                else
                {
                    // Return true if the ray
                    return((IntersectedPts.Count & (int)1) > 0);
                }
            }
        }
Esempio n. 3
0
            /// <summary>
            /// Called at each touch down event
            /// </summary>
            /// <param name="sender">Object</param>
            /// <param name="e">EventArgs</param>
            public virtual bool TouchedDown(object sender, EventArgs e)
            {
                if (!_touchable)
                {
                    return(false);
                }
                TouchEventArgs args  = (TouchEventArgs)e;
                TouchPoint     touch = args.TouchPoint;

                if (BoundingRect.Contains((int)touch.CenterX, (int)touch.CenterY))
                {
                    _touchId = touch.Id;
                    _delta   = new Vector2(touch.CenterX - Position.X, touch.CenterY - Position.Y);
                    _asMove  = false;

                    if (_menu != null)
                    {
                        if (!_menu.displayed())
                        {
                            _timer          = new System.Timers.Timer(1000);
                            _timer.Enabled  = true;
                            _timer.Elapsed += OnTimedEvent;
                        }
                        else
                        {
                            this._menu.Hide();
                        }
                    }
                    return(true);
                }
                return(false);
            }
        /// <summary>
        /// True if it entirely contains the other.
        /// </summary>
        /// <param name="Other">The other polygon.</param>
        public bool Contains(C2DPolyBase Other)
        {
            if (Other.Lines.Count == 0)
            {
                return(false);
            }

            if (!BoundingRect.Contains(Other.BoundingRect))
            {
                return(false);
            }

            if (!Contains(Other.Lines[0].GetPointFrom()))
            {
                return(false);
            }

            return(!this.Crosses(Other));
        }
Esempio n. 5
0
 public override void Update(GameTime gameTime)
 {
     if (!Active)
     {
         return;
     }
     // If we click on the enemy object play the assosiated sound effect
     // and hide it
     if (InputEngine.IsMouseLeftClick())
     {
         if (BoundingRect.Contains(InputEngine.MousePosition))
         {
             SoundEffect sndFx;
             if (AudioManager.SoundEffects.TryGetValue(ImageName, out sndFx))
             {
                 sndFx.CreateInstance().Play();
             }
             Active = false;
         }
     }
     base.Update(gameTime);
 }
Esempio n. 6
0
        public bool Contains(float x, float y)
        {
            if (!BoundingRect.Contains(x, y))
            {
                return(false);
            }

            int numPoints = points.Length;

            bool contains = false;

            for (int i = 0; i < numPoints; i++)
            {
                Vector2 p  = points[i];
                Vector2 p1 = points[(i + 1) % numPoints];

                if ((p.Y <= y && y < p1.Y || p1.Y <= y && y < p.Y) && x < (p1.X - p.X) / (p1.Y - p.Y) * (y - p.Y) + p.X)
                {
                    contains = !contains;
                }
            }

            return(contains);
        }
Esempio n. 7
0
 public virtual bool ContainsPointOnScreen(Point point, Camera camera)
 {
     return(BoundingRect.Contains(point.Transform(camera.GetMatrix().Invert())));
 }
        /// <summary>
        /// Distance of the poly from the shape.
        /// </summary>
        /// <param name="Other">The other polygon test.</param>
        /// <param name="ptOnThis">Output. The closest point on this.</param>
        /// <param name="ptOnOther">The closest point on the other.</param>
        public double Distance(C2DPolyBase Other, C2DPoint ptOnThis, C2DPoint ptOnOther)
        {
            if (Lines.Count == 0)
            {
                return(0);
            }

            if (Other.Lines.Count == 0)
            {
                return(0);
            }

            if (Other.LineRects.Count != Other.Lines.Count)
            {
                return(0);
            }

            if (Lines.Count != LineRects.Count)
            {
                return(0);
            }

            // First we find the closest line rect to the other's bounding rectangle.
            var    usThisClosestLineGuess = 0;
            var    OtherBoundingRect      = Other.BoundingRect;
            double dClosestDist           = LineRects[0].Distance(OtherBoundingRect);

            for (var i = 1; i < LineRects.Count; i++)
            {
                double dDist = LineRects[i].Distance(OtherBoundingRect);
                if (dDist < dClosestDist)
                {
                    dClosestDist           = dDist;
                    usThisClosestLineGuess = i;
                }
            }
            // Now cycle through all the other poly's line rects to find the closest to the
            // guessed at closest line on this.
            var usOtherClosestLineGuess = 0;

            dClosestDist = Other.LineRects[0].Distance(LineRects[usThisClosestLineGuess]);
            for (var j = 1; j < Other.LineRects.Count; j++)
            {
                double dDist = Other.LineRects[j].Distance(LineRects[usThisClosestLineGuess]);
                if (dDist < dClosestDist)
                {
                    dClosestDist            = dDist;
                    usOtherClosestLineGuess = j;
                }
            }

            // Now we have a guess at the 2 closest lines.
            double dMinDistGuess = Lines[usThisClosestLineGuess].Distance(
                Other.Lines[usOtherClosestLineGuess],
                ptOnThis,
                ptOnOther);

            // If its 0 then return 0.
            if (dMinDistGuess == 0)
            {
                return(0);
            }

            var ptOnThisTemp  = new C2DPoint();
            var ptOnOtherTemp = new C2DPoint();

            // Now go through all of our line rects and only check further if they are closer
            // to the other's bounding rect than the min guess.
            for (var i = 0; i < Lines.Count; i++)
            {
                if (LineRects[i].Distance(OtherBoundingRect) < dMinDistGuess)
                {
                    for (var j = 0; j < Other.Lines.Count; j++)
                    {
                        double dDist = Lines[i].Distance(Other.Lines[j],
                                                         ptOnThisTemp,
                                                         ptOnOtherTemp);

                        if (dDist < dMinDistGuess)
                        {
                            ptOnThis.Set(ptOnThisTemp);
                            ptOnOther.Set(ptOnOtherTemp);

                            if (dDist == 0)
                            {
                                return(0);
                            }

                            dMinDistGuess = dDist;
                        }
                    }
                }
            }

            // if we are here, there is no intersection but the other could be inside this or vice-versa
            if (BoundingRect.Contains(Other.BoundingRect) &&
                Contains(ptOnOtherTemp))
            {
                dMinDistGuess *= -1.0;
            }
            else if (Other.BoundingRect.Contains(BoundingRect) &&
                     Other.Contains(ptOnThisTemp))
            {
                dMinDistGuess *= -1.0;
            }

            return(dMinDistGuess);
        }
Esempio n. 9
0
        public override int GetExtremePositions(BoundingRect thisBounds, ISurface other, BoundingRect otherBounds, out List <Tuple <double, double, double, double> > extremePositions)
        {
            switch (other)
            {
            case SphericalSurface ss:
            {
                GeoPoint2D fp = PositionOf(ss.Location);
                extremePositions = new List <Tuple <double, double, double, double> >();
                extremePositions.Add(new Tuple <double, double, double, double>(fp.x, fp.y, double.NaN, double.NaN));
                return(1);
            }

            case ToroidalSurface ts:
            {
                GeoVector dir = (ts.ZAxis ^ Normal) ^ ts.ZAxis;         // this is the direction of a line from the center of the torus where the u positions of the extereme position of the torus are
                if (dir.IsNullVector())
                {
                    break;
                }
                GeoPoint2D[] ip = ts.GetLineIntersection(ts.Location, dir);         // the result should be 4 points, but we are interested in u parameters only and this must be u and u+pi
                if (ip != null && ip.Length > 0)
                {
                    extremePositions = new List <Tuple <double, double, double, double> >();
                    double u = ip[0].x;
                    SurfaceHelper.AdjustUPeriodic(ts, otherBounds, ref u);
                    if (u >= otherBounds.Left && u <= otherBounds.Right)
                    {
                        extremePositions.Add(new Tuple <double, double, double, double>(double.NaN, double.NaN, u, double.NaN));
                    }
                    u += Math.PI;
                    SurfaceHelper.AdjustUPeriodic(ts, otherBounds, ref u);
                    if (u >= otherBounds.Left && u <= otherBounds.Right)
                    {
                        extremePositions.Add(new Tuple <double, double, double, double>(double.NaN, double.NaN, u, double.NaN));
                    }
                    return(extremePositions.Count);
                }
            }
            break;

            case PlaneSurface ps:
            case CylindricalSurface cys:
            case ConicalSurface cos:
                extremePositions = null;
                return(0);

            case ISurfaceImpl ns:
            {
                GeoPoint2D[] normals = ns.BoxedSurfaceEx.PositionOfNormal(Normal);
                extremePositions = new List <Tuple <double, double, double, double> >();
                for (int i = 0; i < normals.Length; i++)
                {
                    if (otherBounds.Contains(normals[i]))
                    {
                        extremePositions.Add(new Tuple <double, double, double, double>(double.NaN, double.NaN, normals[i].x, normals[i].y));
                    }
                }
            }
            break;
            }
            extremePositions = null;
            return(-1); // means: no implementation for this combination
        }
 private bool PointInRectangle(BoundingRect rect, Vector2 point)
 {
     return(rect.Contains(point));
 }