aboutsummaryrefslogtreecommitdiff
path: root/source/ui/elements/Image.cs
blob: de661e8621ca69ff476712107c9f0b8228260fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
        }
    }
}