-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNewUITextBox.cs
More file actions
335 lines (298 loc) · 8.55 KB
/
NewUITextBox.cs
File metadata and controls
335 lines (298 loc) · 8.55 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ReLogic.Graphics;
using System;
using ReLogic.Content;
using Terraria;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
namespace AutoTrash
{
// Copied over from RecipeBrowser
internal class NewUITextBox : UIPanel//UITextPanel<string>
{
private static readonly Asset<Texture2D> CloseButtonTexture = AutoTrash.instance.Assets.Request<Texture2D>("closeButton", AssetRequestMode.ImmediateLoad);
internal bool focused = false;
//private int _cursor;
//private int _frameCount;
private int _maxLength = 60;
private string hintText;
internal string currentString = "";
private int textBlinkerCount;
private int textBlinkerState;
public event Action OnFocus;
public event Action OnUnfocus;
public event Action OnTextChanged;
public event Action OnTabPressed;
public event Action OnEnterPressed;
//public event Action OnUpPressed;
internal bool unfocusOnEnter = true;
internal bool unfocusOnTab = true;
//public NewUITextBox(string text, float textScale = 1, bool large = false) : base("", textScale, large)
public NewUITextBox(string hintText, string text = "")
{
this.hintText = hintText;
currentString = text;
SetPadding(0);
BackgroundColor = Color.White;
BorderColor = Color.White;
// keyBoardInput.newKeyEvent += KeyboardInput_newKeyEvent;
var closeButton = new UIHoverImageButton(CloseButtonTexture, "");
closeButton.OnLeftClick += (a, b) => SetText("");
closeButton.Left.Set(-20f, 1f);
//closeButton.Top.Set(0f, .5f);
closeButton.VAlign = 0.5f;
//closeButton.HAlign = 0.5f;
Append(closeButton);
}
public override void LeftClick(UIMouseEvent evt)
{
Focus();
base.LeftClick(evt);
}
public override void RightClick(UIMouseEvent evt)
{
base.RightClick(evt);
SetText("");
}
public void SetUnfocusKeys(bool unfocusOnEnter, bool unfocusOnTab)
{
this.unfocusOnEnter = unfocusOnEnter;
this.unfocusOnTab = unfocusOnTab;
}
//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))
// {
// Write(obj.ToString());
// }
//}
public void Unfocus()
{
if (focused)
{
focused = false;
Main.blockInput = false;
OnUnfocus?.Invoke();
}
}
public void Focus()
{
if (!focused)
{
Main.clrInput();
focused = true;
Main.blockInput = true;
OnFocus?.Invoke();
}
}
public override void Update(GameTime gameTime)
{
Vector2 MousePosition = new Vector2((float)Main.mouseX, (float)Main.mouseY);
if (!ContainsPoint(MousePosition) && (Main.mouseLeft || Main.mouseRight)) // This solution is fine, but we need a way to cleanly "unload" a UIElement
{
// TODO, figure out how to refocus without triggering unfocus while clicking enable button.
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();
// OnTextChanged?.Invoke();
//}
//public void WriteAll(string text)
//{
// bool changed = text != Text;
// if (!changed) return;
// base.SetText(text);
// this._cursor = text.Length;
// //_cursor = Math.Min(Text.Length, _cursor);
// Recalculate();
// if (changed)
// {
// OnTextChanged?.Invoke();
// }
//}
public void SetText(string text)
{
if (text.ToString().Length > this._maxLength)
{
text = text.ToString().Substring(0, this._maxLength);
}
if (currentString != text)
{
currentString = text;
OnTextChanged?.Invoke();
}
}
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);
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
Rectangle hitbox = GetInnerDimensions().ToRectangle();
// Draw panel
base.DrawSelf(spriteBatch);
// Main.spriteBatch.Draw(Main.magicPixel, hitbox, Color.Yellow);
if (focused)
{
Terraria.GameInput.PlayerInput.WritingText = true;
Main.instance.HandleIME();
string newString = Main.GetInputText(currentString);
if (!newString.Equals(currentString))
{
currentString = newString;
OnTextChanged?.Invoke();
}
else
{
currentString = newString;
}
if (JustPressed(Keys.Tab))
{
if (unfocusOnTab) Unfocus();
OnTabPressed?.Invoke();
}
if (JustPressed(Keys.Enter))
{
Main.drawingPlayerChat = false;
if (unfocusOnEnter) Unfocus();
OnEnterPressed?.Invoke();
}
if (++textBlinkerCount >= 20)
{
textBlinkerState = (textBlinkerState + 1) % 2;
textBlinkerCount = 0;
}
Main.instance.DrawWindowsIMEPanel(new Vector2(98f, (float)(Main.screenHeight - 36)), 0f);
}
string displayString = currentString;
if (this.textBlinkerState == 1 && focused)
{
displayString = displayString + "|";
}
CalculatedStyle space = base.GetDimensions();
Color color = Color.Black;
if (currentString.Length == 0)
{
}
Vector2 drawPos = space.Position() + new Vector2(4, 2);
if (currentString.Length == 0 && !focused)
{
color *= 0.5f;
//Utils.DrawBorderString(spriteBatch, hintText, new Vector2(space.X, space.Y), Color.Gray, 1f);
spriteBatch.DrawString(FontAssets.MouseText.Value, hintText, drawPos, color);
}
else
{
//Utils.DrawBorderString(spriteBatch, displayString, drawPos, Color.White, 1f);
spriteBatch.DrawString(FontAssets.MouseText.Value, displayString, drawPos, color);
}
// 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();
// DynamicSpriteFont spriteFont = base.IsLarge ? Main.fontDeathText : FontAssets.MouseText.Value;
// Vector2 vector = new Vector2(spriteFont.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(spriteFont.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);
}
}
}