-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathUITextbox.cs
322 lines (286 loc) · 8.4 KB
/
UITextbox.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
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System;
using Terraria;
namespace HEROsMod.UIKit
{
internal class UITextbox : UIView
{
private RasterizerState _rasterizerState = new RasterizerState() { ScissorTestEnable = true };
internal static Asset<Texture2D> textboxBackground;
internal static Texture2D textboxFill;
private static Texture2D TextboxFill
{
get
{
if (textboxFill == null)
{
Color[] edgeColors = new Color[textboxBackground.Value.Width * textboxBackground.Value.Height];
textboxBackground.Value.GetData(edgeColors);
Color[] fillColors = new Color[textboxBackground.Value.Height];
for (int y = 0; y < fillColors.Length; y++)
{
fillColors[y] = edgeColors[textboxBackground.Value.Width - 1 + y * textboxBackground.Value.Width];
}
textboxFill = new Texture2D(UIView.graphics, 1, fillColors.Length);
textboxFill.SetData(fillColors);
}
return textboxFill;
}
}
private bool focused = false;
public bool HadFocus { get { return focused; } }
public bool Numeric { get; set; }
public bool HasDecimal { get; set; }
private static float blinkTime = 1f;
private static float timer = 0f;
//bool eventSet = false;
private float width = 200;
public delegate void KeyPressedHandler(object sender, char key);
public event EventHandler OnTabPress;
public event EventHandler OnEnterPress;
public event EventHandler OnLostFocus;
public event KeyPressedHandler KeyPressed;
private bool drawCarrot = false;
private UILabel label = new UILabel();
private static int padding = 4;
private string text = "";
public string Text
{
get { return text; }
set { text = value; }
}
private int maxCharacters = 20;
public int MaxCharacters
{
get { return maxCharacters; }
set { maxCharacters = value; }
}
private bool passwordBox = false;
public bool PasswordBox
{
get { return passwordBox; }
set { passwordBox = value; }
}
private string passwordString
{
get
{
string result = "";
for (int i = 0; i < Text.Length; i++) result += "*";
return result;
}
}
public UITextbox()
{
this.onLeftClick += new EventHandler(UITextbox_onLeftClick);
this.onRightClick += (a, b) => {
Text = "";
KeyPressed?.Invoke(this, ' ');
};
label.ForegroundColor = Color.Black;
label.Scale = Height / label.Height;
label.TextOutline = false;
Numeric = false;
HasDecimal = false;
label.Position = new Vector2(4, 4);
this.AddChild(label);
}
private void UITextbox_onLeftClick(object sender, EventArgs e)
{
Focus();
}
public void Focus()
{
if (!focused)
{
focused = true;
// Main.blockInput = true;
// Main.clrInput();
timer = 0f;
//eventSet = true;
}
//ModUtils.StopListeningForKeyEvents();
//Main.RemoveKeyEvent();
//keyBoardInput.newKeyEvent += new Action<char>(KeyboardInput_newKeyEvent);
}
public void Unfocus()
{
if (focused)
{
focused = false;
// Main.blockInput = false;
OnLostFocus?.Invoke(this, EventArgs.Empty);
}
//if (!eventSet) return;
//eventSet = false;
//keyBoardInput.newKeyEvent -= new Action<char>(KeyboardInput_newKeyEvent);
//ModUtils.StartListeningForKeyEvents();
//Main.AddKeyEvent();
}
/*void KeyboardInput_newKeyEvent(char obj)
{
if (obj.Equals('\b'))
{
if (Text.Length > 0)
{
Text = Text.Substring(0, Text.Length - 1);
SetLabelPosition();
KeyPressed?.Invoke(this, obj);
}
}
else if(obj.Equals(''))
{
this.Unfocus();
}
else if (obj.Equals('\t'))
{
OnTabPress?.Invoke(this, new EventArgs());
}
else if (obj.Equals('\r'))
{
Main.chatRelease = false;
OnEnterPress?.Invoke(this, new EventArgs());
}*/
/*else
{
for (int i = 0; i < label.font.Characters.Count; i++)
{
if (Text.Length < MaxCharacters && obj == label.font.Characters[i])
{
if(Numeric && obj.Equals('.'))
{
bool containsDecimal = false;
for(int j = 0; j < text.Length; j++)
{
if(text[j] == obj)
{
containsDecimal = true;
break;
}
}
if(!containsDecimal)
{
Text += obj;
SetLabelPosition();
if (KeyPressed != null)
KeyPressed(this, obj);
}
}
if (!Numeric || char.IsNumber(obj) || (text.Length == 0 && obj.Equals('-')))
{
Text += obj;
SetLabelPosition();
if (KeyPressed != null)
KeyPressed(this, obj);
}
break;
}
}
}*/
//}
private void SetLabelPosition()
{
label.Position = new Vector2(padding, 0);
Vector2 size = label.font.MeasureString(Text + "|") * label.Scale;
if (passwordBox)
{
size = label.font.MeasureString(passwordString + "|") * label.Scale;
}
if (size.X > Width - padding * 2)
{
label.Position = new Vector2(padding - (size.X - (Width - padding * 2)), 0);
}
}
protected override float GetWidth()
{
return width;
}
protected override void SetWidth(float width)
{
this.width = width;
}
protected override float GetHeight()
{
return textboxBackground.Value.Height;
}
public override void Update()
{
base.Update();
if (!IsMouseInside() && MouseLeftButton)
{
Unfocus();
}
if (focused)
{
timer += ModUtils.DeltaTime;
if (timer < blinkTime / 2) drawCarrot = true;
else drawCarrot = false;
if (timer >= blinkTime) timer = 0;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
if (focused)
{
Terraria.GameInput.PlayerInput.WritingText = true;
Main.instance.HandleIME();
string oldText = Text;
Text = Main.GetInputText(Text);
if (oldText != Text)
{
KeyPressed?.Invoke(this, ' ');
}
if (Main.keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Tab) && !Main.oldInputText.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Tab))
{
OnTabPress?.Invoke(this, new EventArgs());
}
if (Main.keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter) && !Main.oldInputText.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter))
{
OnEnterPress?.Invoke(this, new EventArgs());
}
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Main.UIScaleMatrix);
Main.instance.DrawWindowsIMEPanel(new Vector2(98f, (float)(Main.screenHeight - 36)), 0f);
}
spriteBatch.Draw(textboxBackground.Value, DrawPosition, null, Color.White, 0f, Origin, 1f, SpriteEffects.None, 0f);
int fillWidth = (int)Width - 2 * textboxBackground.Value.Width;
Vector2 pos = DrawPosition;
pos.X += textboxBackground.Value.Width;
spriteBatch.Draw(TextboxFill, pos - Origin, null, Color.White, 0f, Vector2.Zero, new Vector2(fillWidth, 1f), SpriteEffects.None, 0f);
pos.X += fillWidth;
spriteBatch.Draw(textboxBackground.Value, pos, null, Color.White, 0f, Origin, 1f, SpriteEffects.FlipHorizontally, 0f);
string drawString = Text;
if (PasswordBox) drawString = passwordString;
if (drawCarrot && focused) drawString += "|";
label.Text = drawString;
pos = DrawPosition - Origin;
if (pos.X <= Main.screenWidth && pos.Y <= Main.screenHeight && pos.X + Width >= 0 && pos.Y + Height >= 0)
{
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, _rasterizerState, null, Main.UIScaleMatrix);
Rectangle cutRect = new Rectangle((int)pos.X, (int)pos.Y, (int)Width, (int)Height);
/*if (cutRect.X < 0)
{
cutRect.Width += cutRect.X;
cutRect.X = 0;
}
if (cutRect.Y < 0)
{
cutRect.Height += cutRect.Y;
cutRect.Y = 0;
}
if (cutRect.X + Width > Main.screenWidth) cutRect.Width = Main.screenWidth - cutRect.X;
if (cutRect.Y + Height > Main.screenHeight) cutRect.Height = Main.screenHeight - cutRect.Y;*/
cutRect = ModUtils.GetClippingRectangle(spriteBatch, cutRect);
Rectangle currentRect = spriteBatch.GraphicsDevice.ScissorRectangle;
spriteBatch.GraphicsDevice.ScissorRectangle = cutRect;
base.Draw(spriteBatch);
spriteBatch.GraphicsDevice.ScissorRectangle = currentRect;
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, null, null, null, null, Main.UIScaleMatrix);
}
}
}
}