forked from JavidPack/WheresMyItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewUITextBox.cs
266 lines (234 loc) · 6.2 KB
/
NewUITextBox.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ReLogic.Graphics;
using System;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
using Terraria.GameContent;
using Terraria.WorldBuilding;
namespace WheresMyItems
{
// NewUITextBox is a WIP that may make it into tmodloader proper.
internal class NewUITextBox : UITextPanel<string>
{
private bool focused = false;
private int _cursor;
private int _frameCount;
private int _maxLength = 30;
private string hintText;
public event Action OnFocus;
public event Action OnUnfocus;
public event Action OnTabPressed;
public NewUITextBox(string text, float textScale = 1, bool large = false) : base("", textScale, large)
{
hintText = text;
// keyBoardInput.newKeyEvent += KeyboardInput_newKeyEvent;
}
public override void Click(UIMouseEvent evt)
{
Focus();
base.Click(evt);
}
private void KeyboardInput_newKeyEvent(char obj)
{
// Problem: keyBoardInput.newKeyEvent only fires on regular keyboard buttons.
if (!focused) return;
//if (obj.Equals((char)Keys.Back)) // '\b'
//{
// Backspace();
//}
//else if (obj.Equals((char)Keys.Enter))
//{
// Unfocus();
// Main.chatRelease = false;
//}
//else
if (Char.IsLetterOrDigit(obj))
{
if (Char.IsDigit(obj))
{
Main.blockKey = "D" + obj.ToString();
}
Write(obj.ToString());
}
}
public void Unfocus()
{
if (focused)
{
focused = false;
Main.blockInput = false;
OnUnfocus?.Invoke();
//WheresMyItemsUI.visible = false;
}
}
public void Focus()
{
if (!focused)
{
focused = true;
Main.blockInput = true;
Main.clrInput();
OnFocus?.Invoke();
counter = 0;
}
}
public override void Update(GameTime gameTime)
{
Vector2 MousePosition = new Vector2((float)Main.mouseX, (float)Main.mouseY);
if (focused && !ContainsPoint(MousePosition) && (Main.mouseLeft || Main.mouseRight) && !WheresMyItemsUI.dragging)
{
Main.LocalPlayer.mouseInterface = true;
Unfocus();
}
base.Update(gameTime);
}
public void Write(string text)
{
base.SetText(base.Text.Insert(this._cursor, text));
this._cursor += text.Length;
_cursor = Math.Min(Text.Length, _cursor);
Recalculate();
}
public override void SetText(string text, float textScale, bool large)
{
if (text.ToString().Length > this._maxLength)
{
text = text.ToString().Substring(0, this._maxLength);
}
base.SetText(text, textScale, large);
this.MinWidth.Set(120, 0f);
this._cursor = Math.Min(base.Text.Length, this._cursor);
}
public void SetTextMaxLength(int maxLength)
{
this._maxLength = maxLength;
}
public void Backspace()
{
if (this._cursor == 0)
{
return;
}
base.SetText(base.Text.Substring(0, base.Text.Length - 1));
Recalculate();
}
public void CursorLeft()
{
if (this._cursor == 0)
{
return;
}
this._cursor--;
}
public void CursorRight()
{
if (this._cursor < base.Text.Length)
{
this._cursor++;
}
}
private static bool JustPressed(Keys key)
{
return Main.inputText.IsKeyDown(key) && !Main.oldInputText.IsKeyDown(key);
}
internal int counter = 0;
protected override void DrawSelf(SpriteBatch spriteBatch)
{
counter++;
if (focused)
{
Terraria.GameInput.PlayerInput.WritingText = true;
Main.instance.HandleIME();
SetText(Main.GetInputText(Text));
//Main.keyCount = 0;
}
if (counter > 5 && WheresMyItems.RandomBuffHotKey.JustPressed)
{
Unfocus();
}
if (JustPressed(Keys.Tab))
{
OnTabPressed?.Invoke();
}
//if (Main.keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter))
if (Main.keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter))
{
Main.chatRelease = false;
Main.drawingPlayerChat = false;
Main.inputTextEnter = true;
}
if (Main.inputTextEnter || Main.inputTextEscape)
{
Main.chatRelease = false;
Unfocus();
Main.inputTextEscape = false;
Main.playerInventory = false;
Main.LocalPlayer.releaseInventory = false;
Main.LocalPlayer.controlInv = false;
}
//HandleSpecialKeys();
this._cursor = base.Text.Length;
//base.DrawSelf(spriteBatch);
{
CalculatedStyle innerDimensions2 = base.GetInnerDimensions();
Vector2 pos2 = innerDimensions2.Position();
if (IsLarge)
{
pos2.Y -= 10f * TextScale * TextScale;
}
else
{
pos2.Y -= 2f * TextScale;
}
//pos2.X += (innerDimensions2.Width - TextSize.X) * 0.5f;
if (IsLarge)
{
Utils.DrawBorderStringBig(spriteBatch, Text, pos2, TextColor, TextScale, 0f, 0f, -1);
return;
}
Utils.DrawBorderString(spriteBatch, Text, pos2, TextColor, TextScale, 0f, 0f, -1);
}
this._frameCount++;
CalculatedStyle innerDimensions = base.GetInnerDimensions();
Vector2 pos = innerDimensions.Position();
ReLogic.Content.Asset<DynamicSpriteFont> spriteFont = base.IsLarge ? FontAssets.DeathText : FontAssets.MouseText;
Vector2 vector = new Vector2(TileFont.MeasureString(base.Text.Substring(0, this._cursor)).X, base.IsLarge ? 32f : 16f) * base.TextScale;
if (base.IsLarge)
{
pos.Y -= 8f * base.TextScale;
}
else
{
pos.Y -= 1f * base.TextScale;
}
if (Text.Length == 0)
{
Vector2 hintTextSize = new Vector2(TileFont.MeasureString(hintText.ToString()).X, IsLarge ? 32f : 16f) * TextScale;
pos.X += 5;//(hintTextSize.X);
if (base.IsLarge)
{
Utils.DrawBorderStringBig(spriteBatch, hintText, pos, Color.Gray, base.TextScale, 0f, 0f, -1);
return;
}
Utils.DrawBorderString(spriteBatch, hintText, pos, Color.Gray, base.TextScale, 0f, 0f, -1);
pos.X -= 5;
//pos.X -= (innerDimensions.Width - hintTextSize.X) * 0.5f;
}
if (!focused) return;
pos.X += /*(innerDimensions.Width - base.TextSize.X) * 0.5f*/ +vector.X - (base.IsLarge ? 8f : 4f) * base.TextScale + 6f;
if ((this._frameCount %= 40) > 20)
{
return;
}
if (base.IsLarge)
{
Utils.DrawBorderStringBig(spriteBatch, "|", pos, base.TextColor, base.TextScale, 0f, 0f, -1);
return;
}
Utils.DrawBorderString(spriteBatch, "|", pos, base.TextColor, base.TextScale, 0f, 0f, -1);
}
}
}