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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using ShiftOS.Frontend.Apps;
using ShiftOS.Frontend.GraphicsSubsystem;
using static ShiftOS.Engine.SkinEngine;
namespace ShiftOS.Frontend.GUI
{
public class TextInput : Control
{
private string _label = "Type here!";
private string _text = "";
private int _index = 0;
private System.Drawing.Font _font = new System.Drawing.Font("Tahoma", 9f);
public int Index
{
get
{
return _index;
}
set
{
if (_index == value)
return;
if(_text.Length == 0)
{
_index = 0;
return;
}
_index = MathHelper.Clamp(value, 0, _text.Length);
CalculateVisibleText();
Invalidate();
}
}
public override void MouseStateChanged()
{
if (MouseLeftDown == true)
{
UIManager.FocusedControl = this;
Invalidate();
}
base.MouseStateChanged();
}
public string Text
{
get
{
return _text;
}
set
{
if (_text == value)
return;
_text = value;
if(_index >= _text.Length)
{
_index = _text.Length;
}
CalculateVisibleText();
Invalidate();
}
}
protected override void OnKeyEvent(KeyEvent e)
{
if (e.Key == Microsoft.Xna.Framework.Input.Keys.Enter)
return;
if(e.Key == Microsoft.Xna.Framework.Input.Keys.Left)
{
if (_index > 0)
_index--;
}
if(e.Key == Microsoft.Xna.Framework.Input.Keys.Delete)
{
if(_index < _text.Length - 1)
{
_text = _text.Remove(_index, 1);
}
}
if (e.Key == Microsoft.Xna.Framework.Input.Keys.Right)
if (_index < _text.Length)
_index++;
if (e.KeyChar != '\0') {
if (e.KeyChar == '\b')
{
if (_index > 0)
{
_text = _text.Remove(_index - 1, 1);
_index--;
}
}
else
{
_text = _text.Insert(_index, e.KeyChar.ToString());
_index++;
}
}
caretMS = 0;
CalculateVisibleText();
Invalidate();
base.OnKeyEvent(e);
}
float caretPos = 2f;
protected void CalculateVisibleText()
{
using(var gfx = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
{
string toCaret = _text.Substring(0, _index);
var measure = gfx.SmartMeasureString(toCaret, _font);
caretPos = 2 + measure.Width;
if(caretPos - _textDrawOffset < 0)
{
_textDrawOffset += (caretPos - _textDrawOffset);
}
if(caretPos - _textDrawOffset > Width)
{
_textDrawOffset -= caretPos - _textDrawOffset;
}
}
}
private float _textDrawOffset = 0;
protected override void OnPaint(GraphicsContext gfx)
{
gfx.DrawRectangle(0, 0, Width, Height, UIManager.SkinTextures["ControlTextColor"]);
gfx.DrawRectangle(1, 1, Width - 2, Height - 2, UIManager.SkinTextures["ControlColor"]);
int textY = (Height - _font.Height) / 2;
int caretHeight = _font.Height;
if (!string.IsNullOrWhiteSpace(Text))
{
gfx.DrawString(Text, (int)(2 - _textDrawOffset), textY, LoadedSkin.ControlTextColor.ToMonoColor(), _font);
}
if (IsFocusedControl)
{
if (caretMS <= 250)
{
//draw caret
gfx.DrawRectangle((int)(caretPos - _textDrawOffset), textY, 2, caretHeight, UIManager.SkinTextures["ControlTextColor"]);
}
}
else
{
if (string.IsNullOrWhiteSpace(Text) && !string.IsNullOrWhiteSpace(_label))
{
gfx.DrawString(_label, 2, textY, Color.Gray, _font);
}
}
}
private double caretMS = 0;
protected override void OnLayout(GameTime gameTime)
{
caretMS += gameTime.ElapsedGameTime.TotalMilliseconds;
if (caretMS >= 500)
caretMS = 0;
Invalidate();
}
}
}
|