forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UISlotZone.cs
108 lines (98 loc) · 3.13 KB
/
UISlotZone.cs
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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;
namespace MagicStorage
{
public class UISlotZone : UIElement
{
public delegate Item GetItem(int slot, ref int context);
public delegate void HoverItemSlot(int slot, ref int hoverSlot);
private const int Padding = 4;
private readonly GetItem getItem;
private readonly float inventoryScale;
private readonly HoverItemSlot onHover;
private int hoverSlot = -1;
private int numColumns = 10;
private int numRows = 4;
private int height;
public UISlotZone(HoverItemSlot onHover, GetItem getItem, float scale)
{
this.onHover = onHover;
this.getItem = getItem;
inventoryScale = scale;
}
public void SetDimensions(int columns, int rows)
{
numColumns = columns;
numRows = rows;
height = (int)(Main.inventoryBackTexture.Width * inventoryScale * Main.UIScale) * rows + Padding;
}
public override void Update(GameTime gameTime)
{
hoverSlot = -1;
Vector2 origin = InterfaceHelper.GetFullRectangle(this).TopLeft();
MouseState curMouse = StorageGUI.curMouse;
if (curMouse.X <= origin.X || curMouse.Y <= origin.Y)
{
return;
}
int slotWidth = (int)(Main.inventoryBackTexture.Width * inventoryScale * Main.UIScale);
int slotHeight = (int)(Main.inventoryBackTexture.Height * inventoryScale * Main.UIScale);
int slotX = (curMouse.X - (int)origin.X) / (slotWidth + Padding);
int slotY = (curMouse.Y - (int)origin.Y) / (slotHeight + Padding);
if (slotX < 0 || slotX >= numColumns || slotY < 0 || slotY >= numRows)
{
return;
}
Vector2 slotPos = origin + new Vector2(slotX * (slotWidth + Padding * Main.UIScale), slotY * (slotHeight + Padding * Main.UIScale));
if (curMouse.X > slotPos.X && curMouse.X < slotPos.X + slotWidth && curMouse.Y > slotPos.Y && curMouse.Y < slotPos.Y + slotHeight)
{
onHover(slotX + numColumns * slotY, ref hoverSlot);
}
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
float slotWidth = Main.inventoryBackTexture.Width * inventoryScale;
float slotHeight = Main.inventoryBackTexture.Height * inventoryScale;
Vector2 origin = GetDimensions().Position();
float oldScale = Main.inventoryScale;
Main.inventoryScale = inventoryScale;
Item[] temp = new Item[11];
for (int k = 0; k < numColumns * numRows; k++)
{
int context = 0;
Item item = getItem(k, ref context);
int col = k % numColumns;
int row = k / numColumns;
float x = (slotWidth + Padding) * col;
float y = (slotHeight + Padding) * row;
Vector2 drawPos = origin + new Vector2(x, y);
temp[10] = item;
ItemSlot.Draw(Main.spriteBatch, temp, context, 10, drawPos);
}
Main.inventoryScale = oldScale;
}
public void DrawText()
{
if (hoverSlot >= 0)
{
int context = 0;
Item hoverItem = getItem(hoverSlot, ref context);
if (!hoverItem.IsAir)
{
Main.HoverItem = hoverItem.Clone();
Main.instance.MouseText(string.Empty);
}
}
}
public int getHeight()
{
return height;
}
}
}