summaryrefslogtreecommitdiff
path: root/source/ui/elements/Image.cs
diff options
context:
space:
mode:
Diffstat (limited to 'source/ui/elements/Image.cs')
-rw-r--r--source/ui/elements/Image.cs59
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