I need the rendered dimensions of a sprite, I'm calling Draw in the class below:
public class Ship
{
public Ship(Texture2D texture)
{
Width = texture.Width;
Height = texture.Height;
Texture = texture;
}
public int Width { get; set; }
public int Height { get; set; }
public float Scale { get; set; }
public Vector2 Position { get; set; }
public Texture2D Texture { get; }
public Rectangle Rectangle => new Rectangle((int)Position.X, (int) Position.Y, Width, Height);
public void Draw(SpriteBatch batch)
{
var origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
var scale = new Vector2(Scale, Scale);
var rotation = GetRotationInRadians();
batch.Draw(Texture, Position, null, Color.White,
rotation,
origin,
scale,
SpriteEffects.None, 0f);
}
}
The problem is Width and Height are based off the source (see the constructor) Texture size, so after drawing they are not actual dimensions. Is there away to determine the scaled sprite size?
Side note: I'm using these dimension to detect if an sprite was clicked on by the mouse. Maybe there is a better way to detect clicks?
Update: I'm using the Camera.ScreentoWorld method, which is working for mouse clicks:
var camPoint = _camera.ScreenToWorld(mouseVector);
if (entity.GetBoundingRectangle().Contains(camPoint))
{
entity.Highlight = true;
}
However, for drag and drop selection, its not working I think because the selection box size does not translate to a camera adjusted size:
var selectionVector = new Vector2(selectionBox.X, selectionBox.Y);
var camPoint = _camera.ScreenToWorld(selectionVector);
var camRect = new RectangleF(camPoint.X, camPoint.Y, selectionBox.Width, selectionBox.Height);
Frustratingly it works fine if my camera is not zoomed in.