mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 22:18:34 +01:00
made image automatically set its area dirty when necessary
This commit is contained in:
parent
3b76ff1f2a
commit
3f66430d90
1 changed files with 28 additions and 9 deletions
|
@ -8,28 +8,47 @@ namespace MLEM.Ui.Elements {
|
||||||
public class Image : Element {
|
public class Image : Element {
|
||||||
|
|
||||||
public Color Color = Color.White;
|
public Color Color = Color.White;
|
||||||
public TextureRegion Texture;
|
private TextureRegion texture;
|
||||||
public bool ScaleToImage;
|
public TextureRegion Texture {
|
||||||
|
get => this.texture;
|
||||||
|
set {
|
||||||
|
if (this.texture != value) {
|
||||||
|
this.texture = value;
|
||||||
|
if (this.scaleToImage)
|
||||||
|
this.SetAreaDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private bool scaleToImage;
|
||||||
|
public bool ScaleToImage {
|
||||||
|
get => this.scaleToImage;
|
||||||
|
set {
|
||||||
|
if (this.scaleToImage != value) {
|
||||||
|
this.scaleToImage = value;
|
||||||
|
this.SetAreaDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public bool MaintainImageAspect = true;
|
public bool MaintainImageAspect = true;
|
||||||
|
|
||||||
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
||||||
this.Texture = texture;
|
this.texture = texture;
|
||||||
this.ScaleToImage = scaleToImage;
|
this.scaleToImage = scaleToImage;
|
||||||
this.CanBeSelected = false;
|
this.CanBeSelected = false;
|
||||||
this.CanBeMoused = false;
|
this.CanBeMoused = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Point CalcActualSize(Rectangle parentArea) {
|
protected override Point CalcActualSize(Rectangle parentArea) {
|
||||||
return this.ScaleToImage ? this.Texture.Size : base.CalcActualSize(parentArea);
|
return this.scaleToImage ? this.texture.Size : base.CalcActualSize(parentArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
|
||||||
if (this.MaintainImageAspect) {
|
if (this.MaintainImageAspect) {
|
||||||
var scale = Math.Min(this.DisplayArea.Width / (float) this.Texture.Width, this.DisplayArea.Height / (float) this.Texture.Height);
|
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);
|
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.ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
|
batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
|
||||||
} else {
|
} else {
|
||||||
batch.Draw(this.Texture, this.DisplayArea, this.Color * alpha);
|
batch.Draw(this.texture, this.DisplayArea, this.Color * alpha);
|
||||||
}
|
}
|
||||||
base.Draw(time, batch, alpha);
|
base.Draw(time, batch, alpha);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue