diff options
Diffstat (limited to 'ShiftOS.Frontend/GUI')
| -rw-r--r-- | ShiftOS.Frontend/GUI/Control.cs | 66 | ||||
| -rw-r--r-- | ShiftOS.Frontend/GUI/TextInput.cs | 66 |
2 files changed, 102 insertions, 30 deletions
diff --git a/ShiftOS.Frontend/GUI/Control.cs b/ShiftOS.Frontend/GUI/Control.cs index 90e7ba5..fcd5bf0 100644 --- a/ShiftOS.Frontend/GUI/Control.cs +++ b/ShiftOS.Frontend/GUI/Control.cs @@ -10,6 +10,7 @@ using ShiftOS.Frontend.GraphicsSubsystem; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using Microsoft.Xna.Framework; +using System.Runtime.InteropServices; namespace ShiftOS.Frontend.GUI { @@ -36,6 +37,48 @@ namespace ShiftOS.Frontend.GUI private int _mouseX = 0; private int _mouseY = 0; private bool _captureMouse = false; + + public bool RequiresPaint + { + get + { + bool requires_child_repaint = false; + foreach (var child in _children) + { + requires_child_repaint = child.RequiresPaint; + if (requires_child_repaint) + break; + } + return _invalidated || requires_child_repaint; + } + } + + public Image TextureCache + { + get + { + return _texCache; + } + } + + public byte[] PaintCache + { + get + { + var data = _texCache.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); + var rgb = new byte[Math.Abs(data.Stride) * data.Height]; + Marshal.Copy(data.Scan0, rgb, 0, rgb.Length); + for(int i = 0; i < rgb.Length; i += 4) + { + byte r = rgb[i]; + byte b = rgb[i + 2]; + rgb[i] = b; + rgb[i + 2] = r; + } + _texCache.UnlockBits(data); + return rgb; + } + } public bool CaptureMouse { @@ -74,6 +117,9 @@ namespace ShiftOS.Frontend.GUI } set { + if (_anchor == value) + return; + _anchor = value; Invalidate(); } @@ -96,6 +142,8 @@ namespace ShiftOS.Frontend.GUI } set { + if (_opacity == value) + return; _opacity = value; Invalidate(); } @@ -164,6 +212,9 @@ namespace ShiftOS.Frontend.GUI } set { + if (_visible == value) + return; + _visible = value; Invalidate(); } @@ -213,6 +264,8 @@ namespace ShiftOS.Frontend.GUI } set { + if (_x == value) + return; _x = value; Invalidate(); } @@ -226,6 +279,8 @@ namespace ShiftOS.Frontend.GUI } set { + if (_y == value) + return; _y = value; Invalidate(); } @@ -239,6 +294,8 @@ namespace ShiftOS.Frontend.GUI } set { + if (_w == value) + return; _w = value; Invalidate(); } @@ -252,6 +309,8 @@ namespace ShiftOS.Frontend.GUI } set { + if (_h == value) + return; _h = value; Invalidate(); } @@ -329,7 +388,6 @@ namespace ShiftOS.Frontend.GUI } _invalidated = false; } - gfx.DrawImage(_texCache, 0, 0); foreach (var child in _children) { if (child.Visible) @@ -339,6 +397,10 @@ namespace ShiftOS.Frontend.GUI var cBmp = new Bitmap(child.Width, child.Height); child.Paint(System.Drawing.Graphics.FromImage(cBmp)); cBmp.SetOpacity((float)child.Opacity); + using(var cGfx = Graphics.FromImage(_texCache)) + { + cGfx.DrawImage(child.TextureCache, child.X, child.Y); + } child._invalidated = false; child._texCache = cBmp; gfx.DrawImage(cBmp, new System.Drawing.Point(child.X, child.Y)); @@ -352,7 +414,7 @@ namespace ShiftOS.Frontend.GUI } } } - + gfx.DrawImage(_texCache, 0, 0); } } 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)); } |
