forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UISearchBar.cs
165 lines (152 loc) · 5.35 KB
/
UISearchBar.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ReLogic.Graphics;
using Terraria;
using Terraria.Localization;
using Terraria.GameInput;
using Terraria.ModLoader;
using Terraria.UI;
namespace MagicStorage
{
public class UISearchBar : UIElement
{
private static List<UISearchBar> searchBars = new List<UISearchBar>();
private const int padding = 4;
private LocalizedText defaultText = Language.GetText("Mods.MagicStorage.Search");
private string text = string.Empty;
private int cursorPosition = 0;
private bool hasFocus = false;
private int cursorTimer = 0;
public UISearchBar()
{
this.SetPadding(padding);
searchBars.Add(this);
}
public UISearchBar(LocalizedText defaultText) : this()
{
this.defaultText = defaultText;
}
public string Text
{
get
{
return text;
}
}
public void Reset()
{
text = string.Empty;
cursorPosition = 0;
hasFocus = false;
CheckBlockInput();
}
public override void Update(GameTime gameTime)
{
cursorTimer++;
cursorTimer %= 60;
if (StorageGUI.MouseClicked && Parent != null)
{
Rectangle dim = InterfaceHelper.GetFullRectangle(this);
MouseState mouse = StorageGUI.curMouse;
bool mouseOver = mouse.X > dim.X && mouse.X < dim.X + dim.Width && mouse.Y > dim.Y && mouse.Y < dim.Y + dim.Height;
if (!hasFocus && mouseOver)
{
hasFocus = true;
CheckBlockInput();
}
else if (hasFocus && !mouseOver)
{
hasFocus = false;
CheckBlockInput();
cursorPosition = text.Length;
}
}
else if (StorageGUI.curMouse.RightButton == ButtonState.Pressed && StorageGUI.oldMouse.RightButton == ButtonState.Released && Parent != null && hasFocus)
{
Rectangle dim = InterfaceHelper.GetFullRectangle(this);
MouseState mouse = StorageGUI.curMouse;
bool mouseOver = mouse.X > dim.X && mouse.X < dim.X + dim.Width && mouse.Y > dim.Y && mouse.Y < dim.Y + dim.Height;
if (!mouseOver)
{
hasFocus = false;
cursorPosition = text.Length;
CheckBlockInput();
}
}
if (hasFocus)
{
PlayerInput.WritingText = true;
Main.instance.HandleIME();
string newString = Main.GetInputText(text);
if (!newString.Equals(text))
{
text = newString;
cursorPosition = text.Length;
StorageGUI.RefreshItems();
}
if (KeyTyped(Keys.Enter) || KeyTyped(Keys.Tab) || KeyTyped(Keys.Escape))
{
hasFocus = false;
CheckBlockInput();
}
}
base.Update(gameTime);
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
Texture2D texture = ModLoader.GetTexture("MagicStorage/SearchBar");
CalculatedStyle dim = GetDimensions();
int innerWidth = (int)dim.Width - 2 * padding;
int innerHeight = (int)dim.Height - 2 * padding;
spriteBatch.Draw(texture, dim.Position(), new Rectangle(0, 0, padding, padding), Color.White);
spriteBatch.Draw(texture, new Rectangle((int)dim.X + padding, (int)dim.Y, innerWidth, padding), new Rectangle(padding, 0, 1, padding), Color.White);
spriteBatch.Draw(texture, new Vector2(dim.X + padding + innerWidth, dim.Y), new Rectangle(padding + 1, 0, padding, padding), Color.White);
spriteBatch.Draw(texture, new Rectangle((int)dim.X, (int)dim.Y + padding, padding, innerHeight), new Rectangle(0, padding, padding, 1), Color.White);
spriteBatch.Draw(texture, new Rectangle((int)dim.X + padding, (int)dim.Y + padding, innerWidth, innerHeight), new Rectangle(padding, padding, 1, 1), Color.White);
spriteBatch.Draw(texture, new Rectangle((int)dim.X + padding + innerWidth, (int)dim.Y + padding, padding, innerHeight), new Rectangle(padding + 1, padding, padding, 1), Color.White);
spriteBatch.Draw(texture, new Vector2(dim.X, dim.Y + padding + innerHeight), new Rectangle(0, padding + 1, padding, padding), Color.White);
spriteBatch.Draw(texture, new Rectangle((int)dim.X + padding, (int)dim.Y + padding + innerHeight, innerWidth, padding), new Rectangle(padding, padding + 1, 1, padding), Color.White);
spriteBatch.Draw(texture, new Vector2(dim.X + padding + innerWidth, dim.Y + padding + innerHeight), new Rectangle(padding + 1, padding + 1, padding, padding), Color.White);
bool isEmpty = text.Length == 0;
string drawText = isEmpty ? defaultText.Value : text;
DynamicSpriteFont font = Main.fontMouseText;
Vector2 size = font.MeasureString(drawText);
float scale = innerHeight / size.Y;
if (isEmpty && hasFocus)
{
drawText = string.Empty;
isEmpty = false;
}
Color color = Color.Black;
if (isEmpty)
{
color *= 0.75f;
}
spriteBatch.DrawString(font, drawText, new Vector2(dim.X + padding, dim.Y + padding), color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
if (!isEmpty && hasFocus && cursorTimer < 30)
{
float drawCursor = font.MeasureString(drawText.Substring(0, cursorPosition)).X * scale;
spriteBatch.DrawString(font, "|", new Vector2(dim.X + padding + drawCursor, dim.Y + padding), color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
}
}
public bool KeyTyped(Keys key)
{
return Main.keyState.IsKeyDown(key) && !Main.oldKeyState.IsKeyDown(key);
}
private static void CheckBlockInput()
{
Main.blockInput = false;
foreach (UISearchBar searchBar in searchBars)
{
if (searchBar.hasFocus)
{
Main.blockInput = true;
break;
}
}
}
}
}