aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/GUI/TextInput.cs
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-07-03 15:41:35 -0400
committerMichael <[email protected]>2017-07-03 15:41:35 -0400
commit15b3b356b0b7be41bd7d975528c46b30c3558988 (patch)
tree9f10e88a4931351ca9dced6212aba960c5e51a8c /ShiftOS.Frontend/GUI/TextInput.cs
parent659ccfb29d68df8c8ae64501186cdbf4d87c1d52 (diff)
downloadshiftos_thereturn-15b3b356b0b7be41bd7d975528c46b30c3558988.tar.gz
shiftos_thereturn-15b3b356b0b7be41bd7d975528c46b30c3558988.tar.bz2
shiftos_thereturn-15b3b356b0b7be41bd7d975528c46b30c3558988.zip
I fixed and broke stuff at the same time!
Diffstat (limited to 'ShiftOS.Frontend/GUI/TextInput.cs')
-rw-r--r--ShiftOS.Frontend/GUI/TextInput.cs66
1 files changed, 38 insertions, 28 deletions
diff --git a/ShiftOS.Frontend/GUI/TextInput.cs b/ShiftOS.Frontend/GUI/TextInput.cs
index 7466cfd..e59e927 100644
--- a/ShiftOS.Frontend/GUI/TextInput.cs
+++ b/ShiftOS.Frontend/GUI/TextInput.cs
@@ -25,6 +25,21 @@ namespace ShiftOS.Frontend.GUI
_index--;
}
+ if(e.Key == Microsoft.Xna.Framework.Input.Keys.Back)
+ {
+ if(_index > 0)
+ {
+ _text = _text.Remove(_index - 1, 1);
+ _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++;
@@ -36,52 +51,47 @@ namespace ShiftOS.Frontend.GUI
Invalidate();
base.OnKeyEvent(e);
}
-
- private int textInputOffset = 0;
- private int maxCanFit = 5;
- string visibleText = "";
+
float caretPos = 2f;
protected void CalculateVisibleText()
{
- visibleText = "";
- caretPos = -1f;
- using (var gfx = Graphics.FromImage(new Bitmap(1, 1)))
+ using(var gfx = Graphics.FromImage(new Bitmap(1, 1)))
{
- for (int i = textInputOffset; i < _text.Length; i++)
+ string toCaret = _text.Substring(0, _index);
+ var measure = gfx.MeasureString(toCaret, _font);
+ caretPos = 2 + measure.Width;
+ while(caretPos - _textDrawOffset < 0)
{
- visibleText += _text[i];
- var measure = gfx.MeasureString(visibleText, _font);
- if (measure.Width > Width)
- {
- maxCanFit = visibleText.Length;
- if(_index < textInputOffset)
- {
- textInputOffset = MathHelper.Clamp(_index - (maxCanFit / 2), 0, _text.Length - 1);
-
- }
- if(_index > textInputOffset + maxCanFit)
- {
- textInputOffset = MathHelper.Clamp(_index + (maxCanFit / 2), 0, _text.Length - 1) - maxCanFit;
- }
- break;
- }
- Height = (int)measure.Height + 4;
+ _textDrawOffset -= 0.01f;
}
+ while(caretPos - _textDrawOffset > Width)
+ {
+ _textDrawOffset += 0.01f;
+ }
+
}
}
+ private float _textDrawOffset = 0;
protected override void OnPaint(Graphics gfx)
{
gfx.Clear(LoadedSkin.ControlColor);
- gfx.DrawString(visibleText, _font, new SolidBrush(LoadedSkin.ControlTextColor), 2, 2);
+ gfx.DrawString(_text, _font, new SolidBrush(LoadedSkin.ControlTextColor), 2 - _textDrawOffset, 2);
if (IsFocusedControl)
{
//Draw caret.
- gfx.FillRectangle(new SolidBrush(LoadedSkin.ControlTextColor), new RectangleF(caretPos, 2, 2, Height - 4));
+ gfx.FillRectangle(new SolidBrush(LoadedSkin.ControlTextColor), new RectangleF(caretPos - _textDrawOffset, 2, 2, Height - 4));
+ }
+ else
+ {
+ if (string.IsNullOrEmpty(_text))
+ {
+ gfx.DrawString(_label, _font, Brushes.Gray, 2, 2);
+ }
}
- gfx.DrawRectangle(new Pen(new SolidBrush(LoadedSkin.ControlTextColor), 1), new Rectangle(0, 0, Width - 1, Height - 1));
+ gfx.DrawRectangle(new Pen(new SolidBrush(LoadedSkin.ControlTextColor), 1), new System.Drawing.Rectangle(0, 0, Width - 1, Height - 1));
}