-
Notifications
You must be signed in to change notification settings - Fork 8
/
UICheckbox.cs
77 lines (65 loc) · 2.1 KB
/
UICheckbox.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
namespace AutoTrash
{
internal class UICheckbox : UIText
{
public static Asset<Texture2D> checkboxTexture;
public static Asset<Texture2D> checkmarkTexture;
public event EventHandler OnSelectedChanged;
private bool selected = false;
private bool disabled = false;
internal string hoverText;
public bool Selected {
get { return selected; }
set {
if (value != selected) {
selected = value;
OnSelectedChanged?.Invoke(this, EventArgs.Empty);
}
}
}
public UICheckbox(string text, string hoverText, float textScale = 1, bool large = false) : base(text, textScale, large) {
this.Left.Pixels += 20;
//TextColor = Color.Blue;
text = " " + text;
this.hoverText = hoverText;
SetText(text);
OnLeftClick += UICheckbox_onLeftClick;
Recalculate();
}
private void UICheckbox_onLeftClick(UIMouseEvent evt, UIElement listeningElement) {
if (disabled)
return;
this.Selected = !Selected;
}
public void SetDisabled(bool disabled = true) {
this.disabled = disabled;
if (disabled) {
Selected = false;
}
TextColor = disabled ? Color.Gray : Color.White;
}
public void SetHoverText(string hoverText) {
this.hoverText = hoverText;
}
protected override void DrawSelf(SpriteBatch spriteBatch) {
base.DrawSelf(spriteBatch);
CalculatedStyle innerDimensions = base.GetInnerDimensions();
Vector2 pos = new Vector2(innerDimensions.X, innerDimensions.Y - 5);
//Rectangle hitbox = GetInnerDimensions().ToRectangle();
//Main.spriteBatch.Draw(Main.magicPixel, hitbox, Color.Red * 0.6f);
spriteBatch.Draw(checkboxTexture.Value, pos, null, disabled ? Color.Gray : Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
if (Selected)
spriteBatch.Draw(checkmarkTexture.Value, pos, null, disabled ? Color.Gray : Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
if (IsMouseHovering) {
Main.hoverItemName = hoverText;
}
}
}
}