aboutsummaryrefslogtreecommitdiff
path: root/source/ui/elements/game/CraftingWindow.cs
blob: e5bcc71431ac26e275428ee71d617916cfeae672 (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
using System;
using System.Collections.Generic;
using Celesteia.Game.Components.Items;
using Celesteia.GUIs.Game;
using Celesteia.Resources;
using Celesteia.Resources.Management;
using Celesteia.Resources.Types;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Celesteia.UI.Elements.Game {
    public class CraftingWindow : Container {
        private Inventory _referenceInventory;
        private Image background;
        private GameGUI _gameGui;

        public CraftingWindow(GameGUI gameGui, Rect rect, Texture2D backgroundImage, Inventory inventory, CraftingRecipeSlot template) : base(rect) {
            _gameGui = gameGui;

            _referenceInventory = inventory;

            background = new Image(Rect.RelativeFull(rect)).SetTexture(backgroundImage).MakePatches(4).SetColor(Color.White);
            AddChild(background);

            AddRecipes(27, template);
        }

        int columns = 9;
        private void AddRecipes(int amountPerPage, CraftingRecipeSlot template) {
            int rows = (int)Math.Ceiling(amountPerPage / (double)columns);

            float o = CraftingRecipeSlot.SLOT_SPACING;
            int index = 0;
            int i = 0;
            for (int row = 0; row < rows; row++)
                for (int column = 0; column < columns; column++) {
                    if (i >= ResourceManager.Recipes.Recipes.Count) break;

                    int slotNumber = i;
                    Recipe recipe = ResourceManager.Recipes.Recipes[index];
                    CraftingRecipeSlot slot = template.Clone()
                        .SetNewRect(template.GetRect()
                            .SetX(AbsoluteUnit.WithValue(column * CraftingRecipeSlot.SLOT_SIZE + (column * CraftingRecipeSlot.SLOT_SPACING) + o))
                            .SetY(AbsoluteUnit.WithValue(row * CraftingRecipeSlot.SLOT_SIZE + (row * CraftingRecipeSlot.SLOT_SPACING) + o))
                        )
                        .SetRecipe(recipe)
                        .SetOnMouseUp((button, point) => {
                            if (button == MonoGame.Extended.Input.MouseButton.Left) {
                                recipe.Craft(_referenceInventory);
                            }
                        });
                    slot.referenceInv = _referenceInventory;
                    slot.SetPivot(new Vector2(0f, 0f));

                    index++;
                    i++;

                    AddChild(slot);
                }
        }
    }
}