aboutsummaryrefslogtreecommitdiff
path: root/source/ui/elements/game/CraftingRecipeSlot.cs
blob: 72101d8cbdea337dd93d9bbba9ee23d6dde50fce (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using Celesteia.Game.Components.Items;
using Celesteia.Resources.Types;
using Celesteia.UI.Properties;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Input;
using MonoGame.Extended.TextureAtlases;

namespace Celesteia.UI.Elements.Game {
    public class CraftingRecipeSlot : Clickable {
        public const float SLOT_SIZE = 64f;
        public const float SLOT_SPACING = 16f;

        public Inventory referenceInv;

        public CraftingRecipeSlot(Rect rect) {
            SetRect(rect);
        }
        
        public CraftingRecipeSlot SetNewRect(Rect rect) {
            SetRect(rect);
            return this;
        }

        // RECIPE REFERENCE PROPERTIES
        private Recipe _recipe;
        public CraftingRecipeSlot SetRecipe(Recipe recipe) {
            _recipe = recipe;
            return this;
        }

        // DRAWING PROPERTIES

        private Texture2D _texture;
        private TextureAtlas _patches;
        private int _patchSize;

        public CraftingRecipeSlot SetTexture(Texture2D texture) {
            _texture = texture;
            return this;
        }

        public CraftingRecipeSlot SetPatches(TextureAtlas patches, int size) {
            if (_texture != null) {
                _patchSize = size;
                _patches = patches;
            }
            return this;
        }

        // TEXT PROPERTIES

        private TextProperties _text;

        public CraftingRecipeSlot SetTextProperties(TextProperties text) {
            _text = text;
            return this;
        }

        public CraftingRecipeSlot SetText(string text) {
            _text.SetText(text);
            return this;
        }

        // CLICKING PROPERTIES

        private ClickEvent _onMouseDown = null;
        private ClickEvent _onMouseUp = null;
        public delegate void CraftHoverEvent(Recipe recipe);
        private CraftHoverEvent _onMouseIn = null;
        private HoverEvent _onMouseOut = null;

        public CraftingRecipeSlot SetOnMouseDown(ClickEvent func) {
            _onMouseDown = func;
            return this;
        }

        public CraftingRecipeSlot SetOnMouseUp(ClickEvent func) {
            _onMouseUp = func;
            return this;
        }

        public CraftingRecipeSlot SetOnMouseIn(CraftHoverEvent func) {
            _onMouseIn = func;
            return this;
        }

        public CraftingRecipeSlot SetOnMouseOut(HoverEvent func) {
            _onMouseOut = func;
            return this;
        }
        
        public override void OnMouseDown(MouseButton button, Point position) {
            base.OnMouseDown(button, position);
            _onMouseDown?.Invoke(button, position);
        }

        public override void OnMouseUp(MouseButton button, Point position) {
            base.OnMouseUp(button, position);
            _onMouseUp?.Invoke(button, position);
        }
        
        public override void OnMouseIn() {
            base.OnMouseIn();
            if (_recipe != null) _onMouseIn?.Invoke(_recipe);
        }

        public override void OnMouseOut() {
            base.OnMouseOut();
            _onMouseOut?.Invoke();
        }

        private Rectangle GetScaledTriangle(Rectangle r, float scale) {
            int newWidth = (int)Math.Round(r.Width * scale);
            int newHeight = (int)Math.Round(r.Height * scale);
            return new Rectangle(
                (int)Math.Round(r.X + ((r.Width - newWidth) / 2f)),
                (int)Math.Round(r.Y + ((r.Height - newHeight) / 2f)),
                newWidth,
                newHeight
            );
        }
        
        Color color;
        public override void Update(GameTime gameTime, out bool clickedAnything) {
            base.Update(gameTime, out clickedAnything);

            if (!this.GetEnabled()) return;
            color = _recipe.Craftable(referenceInv) ? Color.White : Color.Gray;
        }

        Rectangle rectangle;
        Rectangle itemRectangle;
        Rectangle textRectangle;
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (_recipe == null) return;

            rectangle = GetRectangle();
            itemRectangle = GetScaledTriangle(rectangle, .6f);
            textRectangle = GetScaledTriangle(rectangle, .4f);

            // Draw the slot's texture.
            if (_patches != null) ImageUtilities.DrawPatched(spriteBatch, rectangle, _patches, _patchSize, color);
            else spriteBatch.Draw(GetTexture(spriteBatch), rectangle, null, color);

            spriteBatch.Draw(_recipe.Result.GetItemType().Sprite, itemRectangle, color);
            TextUtilities.DrawAlignedText(spriteBatch, textRectangle, _text.GetFont(), _recipe.Result.Amount + "", _text.GetColor(), _text.GetAlignment(), _text.GetFontSize());
        }

        public Texture2D GetTexture(SpriteBatch spriteBatch) {
            if (_texture == null) {
                _texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
                _texture.SetData(new[] { Color.Gray });
            }

            return _texture;
        }

        public CraftingRecipeSlot Clone() {
            return new CraftingRecipeSlot(GetRect())
                .SetRecipe(_recipe)
                .SetOnMouseDown(_onMouseDown)
                .SetOnMouseUp(_onMouseUp)
                .SetOnMouseIn(_onMouseIn)
                .SetOnMouseOut(_onMouseOut)
                .SetTextProperties(_text)
                .SetTexture(_texture)
                .SetPatches(_patches, _patchSize);
        }
    }
}