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;
|
2019-11-02 14:53:59 +01:00
|
|
|
using MLEM.Misc;
|
2019-08-09 22:23:16 +02:00
|
|
|
using MLEM.Textures;
|
2019-10-14 21:28:12 +02:00
|
|
|
using MLEM.Ui.Style;
|
2019-08-09 22:23:16 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class Image : Element {
|
|
|
|
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<Color> Color;
|
2019-09-11 21:01:08 +02:00
|
|
|
private TextureRegion texture;
|
2019-09-26 17:39:38 +02:00
|
|
|
public TextureCallback GetTextureCallback;
|
2019-09-11 21:01:08 +02:00
|
|
|
public TextureRegion Texture {
|
|
|
|
get => this.texture;
|
|
|
|
set {
|
|
|
|
if (this.texture != value) {
|
|
|
|
this.texture = value;
|
2019-09-13 11:53:28 +02:00
|
|
|
this.IsHidden = this.texture == null;
|
2019-09-11 21:01:08 +02:00
|
|
|
if (this.scaleToImage)
|
|
|
|
this.SetAreaDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private bool scaleToImage;
|
|
|
|
public bool ScaleToImage {
|
|
|
|
get => this.scaleToImage;
|
|
|
|
set {
|
|
|
|
if (this.scaleToImage != value) {
|
|
|
|
this.scaleToImage = value;
|
|
|
|
this.SetAreaDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 21:44:02 +02:00
|
|
|
public bool MaintainImageAspect = true;
|
2019-09-12 18:44:24 +02:00
|
|
|
public SpriteEffects ImageEffects = SpriteEffects.None;
|
|
|
|
public Vector2 ImageScale = Vector2.One;
|
|
|
|
public float ImageRotation;
|
2019-08-09 22:23:16 +02:00
|
|
|
|
|
|
|
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
2019-09-26 17:39:38 +02:00
|
|
|
this.Texture = texture;
|
|
|
|
this.scaleToImage = scaleToImage;
|
|
|
|
this.CanBeSelected = false;
|
|
|
|
this.CanBeMoused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Image(Anchor anchor, Vector2 size, TextureCallback getTextureCallback, bool scaleToImage = false) : base(anchor, size) {
|
|
|
|
this.GetTextureCallback = getTextureCallback;
|
|
|
|
this.Texture = getTextureCallback(this);
|
2019-09-11 21:01:08 +02:00
|
|
|
this.scaleToImage = scaleToImage;
|
2019-08-28 18:27:17 +02:00
|
|
|
this.CanBeSelected = false;
|
2019-09-11 20:10:28 +02:00
|
|
|
this.CanBeMoused = false;
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:53:59 +01:00
|
|
|
protected override Vector2 CalcActualSize(RectangleF parentArea) {
|
|
|
|
return this.texture != null && this.scaleToImage ? this.texture.Size.ToVector2() : base.CalcActualSize(parentArea);
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-12-19 14:04:50 +01:00
|
|
|
public override void ForceUpdateArea() {
|
|
|
|
if (this.GetTextureCallback != null)
|
|
|
|
this.Texture = this.GetTextureCallback(this);
|
|
|
|
base.ForceUpdateArea();
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:39:38 +02:00
|
|
|
public override void Update(GameTime time) {
|
|
|
|
base.Update(time);
|
|
|
|
if (this.GetTextureCallback != null)
|
|
|
|
this.Texture = this.GetTextureCallback(this);
|
|
|
|
}
|
|
|
|
|
2019-09-20 13:22:05 +02:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
|
2019-09-26 17:39:38 +02:00
|
|
|
if (this.texture == null)
|
|
|
|
return;
|
2019-09-12 18:44:24 +02:00
|
|
|
var center = new Vector2(this.texture.Width / 2F, this.texture.Height / 2F);
|
2019-11-05 13:28:41 +01:00
|
|
|
var color = this.Color.OrDefault(Microsoft.Xna.Framework.Color.White) * alpha;
|
2019-08-27 21:44:02 +02:00
|
|
|
if (this.MaintainImageAspect) {
|
2019-11-05 13:28:41 +01:00
|
|
|
var scale = Math.Min(this.DisplayArea.Width / this.texture.Width, this.DisplayArea.Height / this.texture.Height);
|
2019-09-11 21:01:08 +02:00
|
|
|
var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.texture.Height * scale / 2);
|
2019-11-02 14:53:59 +01:00
|
|
|
batch.Draw(this.texture, this.DisplayArea.Location + center * scale + imageOffset, color, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0);
|
2019-08-27 21:44:02 +02:00
|
|
|
} else {
|
2019-11-02 14:53:59 +01:00
|
|
|
var scale = new Vector2(1F / this.texture.Width, 1F / this.texture.Height) * this.DisplayArea.Size;
|
|
|
|
batch.Draw(this.texture, this.DisplayArea.Location + center * scale, color, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0);
|
2019-08-27 21:44:02 +02:00
|
|
|
}
|
2019-09-20 13:22:05 +02:00
|
|
|
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 17:39:38 +02:00
|
|
|
public delegate TextureRegion TextureCallback(Image image);
|
|
|
|
|
2019-08-09 22:23:16 +02:00
|
|
|
}
|
|
|
|
}
|