From 567c422f8cd42eba2437f9a8c2522716a1649be7 Mon Sep 17 00:00:00 2001 From: hazel Date: Mon, 26 Jan 2026 22:04:39 +0100 Subject: celesteia archive, last updated april 9th 2024 Signed-off-by: hazel --- source/ui/elements/Image.cs | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 source/ui/elements/Image.cs (limited to 'source/ui/elements/Image.cs') diff --git a/source/ui/elements/Image.cs b/source/ui/elements/Image.cs new file mode 100644 index 0000000..de661e8 --- /dev/null +++ b/source/ui/elements/Image.cs @@ -0,0 +1,59 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.TextureAtlases; + +namespace Celesteia.UI.Elements { + public class Image : Element { + private Texture2D _texture; + public Color _color; + + public Image(Rect rect) { + SetRect(rect); + } + + public Image SetTexture(Texture2D texture) { + _texture = texture; + return this; + } + + public Image SetColor(Color color) { + _color = color; + return this; + } + + public Image SetPivotPoint(Vector2 pivot) { + SetPivot(pivot); + return this; + } + + private TextureAtlas _patches; + private int _patchSize; + public Image MakePatches(int size) { + if (_texture != null) { + _patchSize = size; + _patches = TextureAtlas.Create("patches", _texture, _patchSize, _patchSize); + } + return this; + } + + public override void Draw(SpriteBatch spriteBatch) + { + if (_patches != null) ImageUtilities.DrawPatched(spriteBatch, GetRectangle(), _patches, _patchSize, _color); + else spriteBatch.Draw(GetTexture(spriteBatch), GetRectangle(), null, _color); + } + + public Texture2D GetTexture(SpriteBatch spriteBatch) + { + if (_texture == null) { + // Make a new texture. + _texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1); + + // Set the default texture to one gray pixel. + _texture.SetData(new[] { Color.Gray }); + } + + return _texture; + } + } +} \ No newline at end of file -- cgit v1.2.3