forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModSearchBox.cs
More file actions
98 lines (83 loc) · 2.28 KB
/
ModSearchBox.cs
File metadata and controls
98 lines (83 loc) · 2.28 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
93
94
95
96
97
98
using System;
using MagicStorage.Common.Systems;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Terraria.GameContent.UI.Elements;
using Terraria.Localization;
using Terraria.ModLoader;
namespace MagicStorage
{
public class ModSearchBox
{
public const int ModIndexBaseGame = -1;
public const int ModIndexAll = -2;
private UITextPanel<string> _modButton;
public Action OnChanged;
public int ModIndex { get; private set; } = ModIndexAll;
public string ModName { get; private set; }
public UIPanel Button => _modButton;
public ModSearchBox(Action onChanged)
{
OnChanged = onChanged;
}
public void InitLangStuff()
{
_modButton ??= new UITextPanel<string>(MakeModButtonText(), 0.8f);
}
private void SetSearchMod(int index, bool silent)
{
if (ModIndex == index)
return;
ModIndex = index;
_modButton?.SetText(MakeModButtonText());
ModName = "";
if (index > -1)
ModName = MagicCache.AllMods[index].Name;
if (!silent)
OnChanged?.Invoke();
}
public void Reset(bool silent)
{
SetSearchMod(ModIndexAll, silent);
}
private string MakeModButtonText()
{
string name = ModIndex switch
{
ModIndexAll => Language.GetTextValue("Mods.MagicStorage.FilterAllMods"),
ModIndexBaseGame => "Terraria",
_ => MagicCache.AllMods[ModIndex].Name
};
if (name == "ModLoader")
name = "tModLoader";
return name;
}
public void Update(MouseState curMouse, MouseState oldMouse)
{
Rectangle dim = InterfaceHelper.GetFullRectangle(_modButton);
if (curMouse.X > dim.X && curMouse.X < dim.X + dim.Width && curMouse.Y > dim.Y && curMouse.Y < dim.Y + dim.Height)
{
_modButton.BackgroundColor = new Color(73, 94, 171);
var allMods = MagicCache.AllMods;
int index = ModIndex;
if (curMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released)
{
index++;
if (index >= allMods.Length)
index = ModIndexAll;
}
else if (curMouse.RightButton == ButtonState.Pressed && oldMouse.RightButton == ButtonState.Released)
{
index--;
if (index < -2)
index = allMods.Length - 1;
}
SetSearchMod(index, false);
}
else
{
_modButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
}
}
}
}