2019-08-27 21:44:02 +02:00
|
|
|
using System;
|
2019-08-09 22:23:16 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Extensions;
|
|
|
|
using MLEM.Textures;
|
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class Image : Element {
|
|
|
|
|
|
|
|
public Color Color = Color.White;
|
2019-08-14 22:06:06 +02:00
|
|
|
public TextureRegion Texture;
|
2019-08-27 21:44:02 +02:00
|
|
|
public bool ScaleToImage;
|
|
|
|
public bool MaintainImageAspect = true;
|
2019-08-09 22:23:16 +02:00
|
|
|
|
|
|
|
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
2019-08-14 22:06:06 +02:00
|
|
|
this.Texture = texture;
|
2019-08-27 21:44:02 +02:00
|
|
|
this.ScaleToImage = scaleToImage;
|
2019-08-28 18:27:17 +02:00
|
|
|
this.CanBeSelected = false;
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override Point CalcActualSize(Rectangle parentArea) {
|
2019-08-27 21:44:02 +02:00
|
|
|
return this.ScaleToImage ? this.Texture.Size : base.CalcActualSize(parentArea);
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-12 19:44:16 +02:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
|
2019-08-27 21:44:02 +02:00
|
|
|
if (this.MaintainImageAspect) {
|
|
|
|
var scale = Math.Min(this.DisplayArea.Width / (float) this.Texture.Width, this.DisplayArea.Height / (float) this.Texture.Height);
|
|
|
|
var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.Texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.Texture.Height * scale / 2);
|
|
|
|
batch.Draw(this.Texture, (this.DisplayArea.Location + offset).ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
|
|
|
|
} else {
|
|
|
|
batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), this.Color * alpha);
|
|
|
|
}
|
2019-08-12 19:44:16 +02:00
|
|
|
base.Draw(time, batch, alpha, offset);
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|