-
Notifications
You must be signed in to change notification settings - Fork 101
/
ModSearchBox.cs
101 lines (75 loc) · 2.12 KB
/
ModSearchBox.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 MagicStorage.Common.Systems;
using Microsoft.Xna.Framework;
using Terraria.GameContent.UI.Elements;
using Terraria.Localization;
using Terraria.UI;
namespace MagicStorage
{
public class ModSearchBox : UITextPanel<string>
{
public const int ModIndexBaseGame = -1;
public const int ModIndexAll = -2;
public Action<int, int> OnChanged;
public int ModIndex { get; private set; } = ModIndexAll;
public ModSearchBox(Action<int, int> onChanged, float textScale = 1, bool large = false) : base("", textScale, large)
{
OnChanged = onChanged;
BackgroundColor = new Color(63, 82, 151) * 0.7f;
Reset(true);
}
private void SetSearchMod(int index, bool silent)
{
if (ModIndex == index)
return;
int old = ModIndex;
ModIndex = index;
SetText(MakeModButtonText());
if (!silent)
OnChanged?.Invoke(old, ModIndex);
}
public static string GetNameFromIndex(int index) {
string name = index switch
{
ModIndexAll => Language.GetTextValue("Mods.MagicStorage.FilterAllMods"),
ModIndexBaseGame => "Terraria",
_ => MagicCache.AllMods[index].Name
};
if (name == "ModLoader")
name = "tModLoader";
return name;
}
public void Reset(bool silent)
{
ModIndex = ModIndexAll - 1;
SetSearchMod(ModIndexAll, silent);
}
private string MakeModButtonText() => GetNameFromIndex(ModIndex);
public override void MouseOver(UIMouseEvent evt) {
base.MouseOver(evt);
BackgroundColor = new Color(73, 94, 171);
}
public override void MouseOut(UIMouseEvent evt) {
base.MouseOut(evt);
BackgroundColor = new Color(63, 82, 151) * 0.7f;
}
public override void LeftClick(UIMouseEvent evt) {
base.LeftClick(evt);
var allMods = MagicCache.AllMods;
int index = ModIndex;
index++;
if (index >= allMods.Length)
index = ModIndexAll;
SetSearchMod(index, false);
}
public override void RightClick(UIMouseEvent evt) {
base.RightClick(evt);
var allMods = MagicCache.AllMods;
int index = ModIndex;
index--;
if (index < -2)
index = allMods.Length - 1;
SetSearchMod(index, false);
}
}
}