Example #1
0
        public override bool HitTest(Point2F point)
        {
            EllipseGeometry g   = d2DFactory.CreateEllipseGeometry(ellipse);
            bool            ret = g.FillContainsPoint(point, 1);

            g.Dispose();
            return(ret);
        }
Example #2
0
 public override bool HitTest(Point2F point)
 {
     foreach (var ellipse in ellipses)
     {
         EllipseGeometry g = d2DFactory.CreateEllipseGeometry(new Ellipse(
                                                                  new Point2F(
                                                                      (ellipse.rect.Left + ellipse.rect.Right) / 2,
                                                                      (ellipse.rect.Top + ellipse.rect.Bottom) / 2),
                                                                  ellipse.rect.Width / 2,
                                                                  ellipse.rect.Height / 2));
         bool ret = g.FillContainsPoint(point, 1);
         g.Dispose();
         if (ret)
         {
             return(true);
         }
     }
     return(false);
 }
        /// <summary>
        /// The main rendering method.
        /// </summary>
        private void Render()
        {
            this.CreateDeviceResource();
            this._renderTarget.BeginDraw();

            // Do some clearing.
            this._renderTarget.Transform = Matrix3x2F.Identity;
            this._renderTarget.Clear(new ColorF(Colors.Black));
            SizeF size           = this._renderTarget.Size;
            RectF rectBackground = new RectF(0f, 0f, size.Width, size.Height);

            // Get the size of the RenderTarget.
            float rtWidth  = this._renderTarget.Size.Width;
            float rtHeight = this._renderTarget.Size.Height;

            // Draw some small stars
            for (int i = 0; i < 300; i++)
            {
                float   x         = (float)(this._random.NextDouble()) * rtWidth;
                float   y         = (float)(this._random.NextDouble()) * rtHeight;
                Ellipse smallStar = new Ellipse(new Point2F(x, y), 1f, 1f);
                this._renderTarget.FillEllipse(smallStar, this._smallStarBrush);
            }

            Ellipse planet = new Ellipse(new Point2F(100f, 100f), 100f, 100f);

            // When animating from right to left, draw the planet afte the star so it has a smaller z-index, and will be covered by the star.
            if (!this._animateToRight)
            {
                this.DrawPlanet(planet);
            }

            // Draw the star.
            Ellipse star = new Ellipse(new Point2F(95f, 95f), 75f, 75f);
            // Scale the star, and translate it to the center of the screen. Note if translation is performed before scaling, you'll get different result.
            Matrix3x2F scaleMatrix       = Matrix3x2F.Scale(2f, 2f, new Point2F(95f, 95f));
            Matrix3x2F translationMatrix = Matrix3x2F.Translation(rtWidth / 2 - 95, rtHeight / 2 - 95);

            // Since the managed counter part of Matrix3x2F does not expose the multiply operaion, let's convert them to WPF matrixes to do the multiplication.
            System.Windows.Media.Matrix wpfScaleMatrix     = new System.Windows.Media.Matrix(scaleMatrix.M11, scaleMatrix.M12, scaleMatrix.M21, scaleMatrix.M22, scaleMatrix.M31, scaleMatrix.M32);
            System.Windows.Media.Matrix wpfTranslateMatrix = new System.Windows.Media.Matrix(translationMatrix.M11, translationMatrix.M12, translationMatrix.M21, translationMatrix.M22, translationMatrix.M31, translationMatrix.M32);
            System.Windows.Media.Matrix wpfResultMatrix    = wpfScaleMatrix * wpfTranslateMatrix;
            this._renderTarget.Transform = new Matrix3x2F((float)wpfResultMatrix.M11, (float)wpfResultMatrix.M12, (float)wpfResultMatrix.M21, (float)wpfResultMatrix.M22, (float)wpfResultMatrix.OffsetX, (float)wpfResultMatrix.OffsetY);
            this._renderTarget.FillGeometry(this._starOutline, this._starOutlineBrush);
            // The transform matrix will be apllied to all rendered elements, until it is reset. So we don't need to set the matrix for the ellipse again.
            this._renderTarget.FillEllipse(star, this._starBrush);

            // By default, or when animating from left to right, draw the planet afte the star so it has a larger z-index.
            if (this._animateToRight)
            {
                this.DrawPlanet(planet);
            }

            if (this._animate)
            {
                // Perform a hit test. If the user clicked the planet, let's animate it to make it move around the star.
                EllipseGeometry hitTestEllipse = this._d2DFactory.CreateEllipseGeometry(planet);
                Point2F         point          = new Point2F(this._clickedPointX, this._clickedPointY);
                Matrix3x2F      matrix         = Matrix3x2F.Translation(10f, rtHeight / 2 - 100);
                bool            hit            = hitTestEllipse.FillContainsPoint(point, 0f, matrix);
                if (!hit)
                {
                    this._animate = false;
                }
                else
                {
                    // When moving from left to right, translate transform becomes larger and lager.
                    if (this._animateToRight)
                    {
                        this._animateTranslateX++;
                        if (this._animateTranslateX > rtWidth - 220)
                        {
                            this._animateToRight = false;
                        }
                    }
                    else
                    {
                        // When moving from right to left, translate transform becomes smaller and smaller.
                        this._animateTranslateX--;
                        if (this._animateTranslateX <= 0)
                        {
                            this._animateToRight    = true;
                            this._animateTranslateX = 0;
                            this._animate           = false;
                        }
                    }
                }
            }

            // Finish drawing.
            this._renderTarget.EndDraw();
        }