From 150d6f771a9f2e7e02e269995145b6151a5c53c5 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 12 Sep 2019 18:44:24 +0200 Subject: [PATCH] upgrade images a bit --- MLEM.Ui/Elements/Image.cs | 9 ++++++-- MLEM.Ui/Elements/SpriteAnimationImage.cs | 27 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 MLEM.Ui/Elements/SpriteAnimationImage.cs diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index 95549f4..38ce989 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -30,6 +30,9 @@ namespace MLEM.Ui.Elements { } } public bool MaintainImageAspect = true; + public SpriteEffects ImageEffects = SpriteEffects.None; + public Vector2 ImageScale = Vector2.One; + public float ImageRotation; public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) { this.texture = texture; @@ -43,12 +46,14 @@ namespace MLEM.Ui.Elements { } public override void Draw(GameTime time, SpriteBatch batch, float alpha) { + var center = new Vector2(this.texture.Width / 2F, this.texture.Height / 2F); 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.ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0); + batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale + imageOffset, this.Color * alpha, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); } else { - batch.Draw(this.texture, this.DisplayArea, this.Color * alpha); + var scale = new Vector2(1F / this.texture.Width, 1F / this.texture.Height) * this.DisplayArea.Size.ToVector2(); + batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale, this.Color * alpha, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); } base.Draw(time, batch, alpha); } diff --git a/MLEM.Ui/Elements/SpriteAnimationImage.cs b/MLEM.Ui/Elements/SpriteAnimationImage.cs new file mode 100644 index 0000000..ed847fc --- /dev/null +++ b/MLEM.Ui/Elements/SpriteAnimationImage.cs @@ -0,0 +1,27 @@ +using Microsoft.Xna.Framework; +using MLEM.Animations; +using MLEM.Textures; + +namespace MLEM.Ui.Elements { + public class SpriteAnimationImage : Image { + + public SpriteAnimationGroup Group; + + public SpriteAnimationImage(Anchor anchor, Vector2 size, TextureRegion texture, SpriteAnimationGroup group, bool scaleToImage = false) : + base(anchor, size, texture, scaleToImage) { + this.Group = group; + } + + public SpriteAnimationImage(Anchor anchor, Vector2 size, TextureRegion texture, SpriteAnimation animation, bool scaleToImage = false) : + this(anchor, size, texture, new SpriteAnimationGroup().Add(animation, () => true), scaleToImage) { + } + + public override void Update(GameTime time) { + base.Update(time); + this.Group.Update(time); + this.Texture = this.Group.CurrentRegion; + } + + } + +} \ No newline at end of file