diff options
| author | hazel <hazel@hazelthats.me> | 2026-01-26 22:04:39 +0100 |
|---|---|---|
| committer | hazel <hazel@hazelthats.me> | 2026-01-26 22:04:39 +0100 |
| commit | 567c422f8cd42eba2437f9a8c2522716a1649be7 (patch) | |
| tree | 93c5b296f3b7c14b626d0aadf5cad37764c41c74 /source/ui/elements/Image.cs | |
| download | celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.tar.gz celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.tar.bz2 celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.zip | |
celesteia archive, last updated april 9th 2024
Signed-off-by: hazel <hazel@hazelthats.me>
Diffstat (limited to 'source/ui/elements/Image.cs')
| -rw-r--r-- | source/ui/elements/Image.cs | 59 |
1 files changed, 59 insertions, 0 deletions
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 |
