forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUIToggleButton.cs
More file actions
92 lines (83 loc) · 3.06 KB
/
UIToggleButton.cs
File metadata and controls
92 lines (83 loc) · 3.06 KB
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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.Localization;
using Terraria.UI;
namespace MagicStorage
{
public class UIToggleButton : UIElement
{
private int buttonSize;
private int buttonPadding;
private readonly Action onChanged;
private Texture2D _button;
private LocalizedText _name;
private bool _value;
public bool Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public UIToggleButton(Action onChanged, Texture2D button, LocalizedText name, int buttonSize = 21, int buttonPadding = 1)
{
this.buttonSize = buttonSize;
this.buttonPadding = buttonPadding;
this.onChanged = onChanged;
this._button = button;
this._name = name;
this.Width.Set(buttonSize, 0f);
this.MinWidth.Set(buttonSize, 0f);
this.Height.Set(buttonSize, 0f);
this.MinHeight.Set(buttonSize, 0f);
}
public override void Update(GameTime gameTime)
{
var oldValue = _value;
if (StorageGUI.MouseClicked && Parent != null)
{
if (MouseOverButton(StorageGUI.curMouse.X, StorageGUI.curMouse.Y))
{
_value = !_value;
}
}
if (oldValue != _value)
{
onChanged?.Invoke();
}
}
private bool MouseOverButton(int mouseX, int mouseY)
{
Rectangle dim = InterfaceHelper.GetFullRectangle(this);
float left = dim.X;
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();
Texture2D texture = _value ? backTextureActive : backTexture;
Vector2 drawPos = new Vector2(dim.X, dim.Y);
Color color = MouseOverButton(StorageGUI.curMouse.X, StorageGUI.curMouse.Y) ? Color.Silver : Color.White;
Main.spriteBatch.Draw(texture, new Rectangle((int) drawPos.X, (int) drawPos.Y, buttonSize, buttonSize), color);
Main.spriteBatch.Draw(_button, new Rectangle((int) drawPos.X + 1, (int) drawPos.Y + 1, buttonSize - 1, buttonSize - 1), Color.White);
}
public void DrawText()
{
if (MouseOverButton(StorageGUI.curMouse.X, StorageGUI.curMouse.Y))
{
Main.instance.MouseText(_name.Value);
}
}
}
}