forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIButtonChoice.cs
101 lines (93 loc) · 3.42 KB
/
UIButtonChoice.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;
namespace MagicStorage
{
public class UIButtonChoice : UIElement
{
private const int buttonSize = 32;
private const int buttonPadding = 8;
private Texture2D[] buttons;
private LocalizedText[] names;
private int choice = 0;
public int Choice
{
get
{
return choice;
}
}
public UIButtonChoice(Texture2D[] buttons, LocalizedText[] names)
{
if (buttons.Length != names.Length || buttons.Length == 0)
{
throw new ArgumentException();
}
this.buttons = buttons;
this.names = names;
int width = buttonSize * buttons.Length + buttonPadding * (buttons.Length - 1);
this.Width.Set(width, 0f);
this.MinWidth.Set(width, 0f);
this.Height.Set(buttonSize, 0f);
this.MinHeight.Set(buttonSize, 0f);
}
public override void Update(GameTime gameTime)
{
int oldChoice = choice;
if (StorageGUI.MouseClicked && Parent != null)
{
for (int k = 0; k < buttons.Length; k++)
{
if (MouseOverButton(StorageGUI.curMouse.X, StorageGUI.curMouse.Y, k))
{
choice = k;
break;
}
}
}
if (oldChoice != choice)
{
StorageGUI.RefreshItems();
}
}
private bool MouseOverButton(int mouseX, int mouseY, int button)
{
Rectangle dim = InterfaceHelper.GetFullRectangle(this);
float left = dim.X + button * (buttonSize + buttonPadding) * Main.UIScale;
float right = left + buttonSize * Main.UIScale;
float top = dim.Y;
float bottom = top + buttonSize * Main.UIScale;
return mouseX > left && mouseX < right && mouseY > top && mouseY < bottom;
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
Texture2D backTexture = MagicStorage.Instance.GetTexture("SortButtonBackground");
Texture2D backTextureActive = MagicStorage.Instance.GetTexture("SortButtonBackgroundActive");
CalculatedStyle dim = GetDimensions();
for (int k = 0; k < buttons.Length; k++)
{
Texture2D texture = k == choice ? backTextureActive : backTexture;
Vector2 drawPos = new Vector2(dim.X + k * (buttonSize + buttonPadding), dim.Y);
Color color = MouseOverButton(StorageGUI.curMouse.X, StorageGUI.curMouse.Y, k) ? Color.Silver : Color.White;
Main.spriteBatch.Draw(texture, drawPos, color);
Main.spriteBatch.Draw(buttons[k], drawPos + new Vector2(1f), Color.White);
}
}
public void DrawText()
{
for (int k = 0; k < buttons.Length; k++)
{
if (MouseOverButton(StorageGUI.curMouse.X, StorageGUI.curMouse.Y, k))
{
Main.instance.MouseText(names[k].Value);
}
}
}
}
}