Exemplo n.º 1
0
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);

            if (Texture != null)
            {
                int texX      = SourceRect == null ? 0 : SourceRect.Value.X;
                int texY      = SourceRect == null ? 0 : SourceRect.Value.Y;
                int texWidth  = SourceRect == null ? Texture.Width : SourceRect.Value.Width;
                int texHeight = SourceRect == null ? Texture.Height : SourceRect.Value.Height;

                RectangleF rect         = DestinationRect;
                float      rectRatio    = rect.Width / (float)rect.Height;
                float      textureRatio = texWidth / (float)texHeight;
                if (rectRatio < textureRatio)
                {
                    float ratio = rectRatio / textureRatio;
                    rect.Height = rect.Height * ratio;
                }
                if (rectRatio > textureRatio)
                {
                    float ratio = textureRatio / rectRatio;
                    rect.Width = rect.Width * ratio;
                }
                rect.Width  = rect.Width / Zoom;
                rect.Height = rect.Height / Zoom;

                RectangleF final = rect;
                final.Width  = Math.Min(DestinationRect.Width, rect.Width);
                final.Height = Math.Min(DestinationRect.Height, rect.Height);

                double wRatio = final.Width / (double)rect.Width;
                double hRatio = final.Height / (double)rect.Height;

                Rectangle sourceRect = new Rectangle((int)Position.X + texX, (int)Position.Y + texY, (int)(texWidth * wRatio), (int)(texHeight * hRatio));

                ratioWPixelScreenPicture = sourceRect.Width / final.Width;
                ratioHPixelScreenPicture = sourceRect.Height / final.Height;

                spriteBatch.Draw(Texture, new Rectangle((int)final.X, (int)final.Y, (int)final.Width, (int)final.Height), sourceRect, Color.White, 0, Vector2.Zero, Effect, 0.5f);

                if (SelectionMode)
                {
                    Vector2 crossPos = new Vector2(Input.X, Input.Y);
                    if (DestinationRect.Contains(crossPos))
                    {
                        DrawHelper.DrawCross(spriteBatch, crossPos, DestinationRect, Color.DeepPink);
                    }
                }

                if (isSelecting)
                {
                    Rectangle selectionRect = new Rectangle();
                    Vector2   selectPoint   = ConvertPictureToScreen(new Vector2(selectionStartPoint.X + 0.4f, selectionStartPoint.Y + 0.4f));
                    selectionRect.Left   = Math.Min((int)selectPoint.X, Input.X);
                    selectionRect.Right  = Math.Max((int)selectPoint.X, Input.X);
                    selectionRect.Top    = Math.Min((int)selectPoint.Y, Input.Y);
                    selectionRect.Bottom = Math.Max((int)selectPoint.Y, Input.Y);
                    DrawHelper.DrawRectangle(spriteBatch, selectionRect, Color.Blue);

                    Color greenA = Color.Green; greenA.A = 100;
                    DrawHelper.DrawFilledRectangle(spriteBatch, DrawHelper.ClampRectangle(MagnetToPicture(selectionRect), DestinationRect), greenA);
                }
                else if (SelectedRectangle != null)
                {
                    Color     greenA   = Color.Green; greenA.A = 100;
                    Rectangle selected = (Rectangle)SelectedRectangle;
                    selected.Right -= 1; selected.Bottom -= 1;
                    DrawHelper.DrawFilledRectangle(spriteBatch, DrawHelper.ClampRectangle(ConvertPictureToScreen(selected), DestinationRect), greenA);
                }

                if (SelectPointMode && SelectedPoint != null)
                {
                    Color     redA  = Color.Red; redA.A = 100;
                    Rectangle point = new Rectangle((int)SelectedPoint.Value.X, (int)SelectedPoint.Value.Y, 0, 0);
                    DrawHelper.DrawFilledRectangle(spriteBatch, DrawHelper.ClampRectangle(ConvertPictureToScreen(point), DestinationRect), redA);
                }
            }
        }