From be6cd3a0c02168b998e58b77a4d08a4b7cb8cfe3 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 3 Jul 2017 11:29:54 -0400 Subject: [PATCH] text input stuff --- ShiftOS.Frontend/Desktop/WindowManager.cs | 37 +++++++++- ShiftOS.Frontend/GUI/TextInput.cs | 55 ++++++++++++-- .../GraphicsSubsystem/UIManager.cs | 2 +- ShiftOS.Frontend/ShiftOS.cs | 71 ++++++++++++++----- 4 files changed, 141 insertions(+), 24 deletions(-) diff --git a/ShiftOS.Frontend/Desktop/WindowManager.cs b/ShiftOS.Frontend/Desktop/WindowManager.cs index 1bceb93..5efb30e 100644 --- a/ShiftOS.Frontend/Desktop/WindowManager.cs +++ b/ShiftOS.Frontend/Desktop/WindowManager.cs @@ -156,7 +156,42 @@ namespace ShiftOS.Frontend.Desktop { if (Shiftorium.UpgradeInstalled("wm_titlebar")) { - if(MouseY < LoadedSkin.TitlebarHeight) + if (Shiftorium.UpgradeInstalled("close_button")) + { + var closebuttonsize = LoadedSkin.CloseButtonSize; + var closebuttonloc = LoadedSkin.CloseButtonFromSide; + if (LoadedSkin.TitleButtonPosition == 0) + closebuttonloc = new Point(Width - closebuttonsize.Width - closebuttonloc.X, closebuttonloc.Y); + if(MouseX > closebuttonloc.X && MouseY > closebuttonloc.Y && MouseX < closebuttonloc.X + closebuttonsize.Width && MouseY < closebuttonloc.Y + closebuttonsize.Height) + { + Close(); + } + } + if (Shiftorium.UpgradeInstalled("minimize_button")) + { + var closebuttonsize = LoadedSkin.MinimizeButtonSize; + var closebuttonloc = LoadedSkin.MinimizeButtonFromSide; + if (LoadedSkin.TitleButtonPosition == 0) + closebuttonloc = new Point(Width - closebuttonsize.Width - closebuttonloc.X, closebuttonloc.Y); + if (MouseX > closebuttonloc.X && MouseY > closebuttonloc.Y && MouseX < closebuttonloc.X + closebuttonsize.Width && MouseY < closebuttonloc.Y + closebuttonsize.Height) + { + if (IsFocusedControl || ContainsFocusedControl) + UIManager.FocusedControl = null; + Visible = false; + } + } + if (Shiftorium.UpgradeInstalled("maximize_button")) + { + var closebuttonsize = LoadedSkin.MaximizeButtonSize; + var closebuttonloc = LoadedSkin.MaximizeButtonFromSide; + if (LoadedSkin.TitleButtonPosition == 0) + closebuttonloc = new Point(Width - closebuttonsize.Width - closebuttonloc.X, closebuttonloc.Y); + if (MouseX > closebuttonloc.X && MouseY > closebuttonloc.Y && MouseX < closebuttonloc.X + closebuttonsize.Width && MouseY < closebuttonloc.Y + closebuttonsize.Height) + { + AppearanceManager.Maximize(this); + } + } + if (MouseY < LoadedSkin.TitlebarHeight) { var screenpoint = PointToScreen(MouseX, MouseY); lastmousex = this.X - screenpoint.X; diff --git a/ShiftOS.Frontend/GUI/TextInput.cs b/ShiftOS.Frontend/GUI/TextInput.cs index 851f7d2..7466cfd 100644 --- a/ShiftOS.Frontend/GUI/TextInput.cs +++ b/ShiftOS.Frontend/GUI/TextInput.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Xna.Framework; using ShiftOS.Frontend.GraphicsSubsystem; using static ShiftOS.Engine.SkinEngine; @@ -14,6 +15,7 @@ namespace ShiftOS.Frontend.GUI private string _label = "Type here!"; private string _text = ""; private int _index = 0; + private Font _font = new Font("Tahoma", 9f); protected override void OnKeyEvent(KeyEvent e) { @@ -21,25 +23,66 @@ namespace ShiftOS.Frontend.GUI { if (_index > 0) _index--; + } if (e.Key == Microsoft.Xna.Framework.Input.Keys.Right) if (_index < _text.Length) _index++; - if (e.KeyChar != '\0') - _text.Insert(_index, e.KeyChar.ToString()); + if (e.KeyChar != '\0') { + _text = _text.Insert(_index, e.KeyChar.ToString()); + _index++; + } + CalculateVisibleText(); + Invalidate(); base.OnKeyEvent(e); } - protected override void OnLayout() + private int textInputOffset = 0; + private int maxCanFit = 5; + string visibleText = ""; + float caretPos = 2f; + + protected void CalculateVisibleText() { - base.OnLayout(); + visibleText = ""; + caretPos = -1f; + using (var gfx = Graphics.FromImage(new Bitmap(1, 1))) + { + for (int i = textInputOffset; i < _text.Length; i++) + { + 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; + } + } } - private int _textOffset = 0; - + protected override void OnPaint(Graphics gfx) { gfx.Clear(LoadedSkin.ControlColor); + gfx.DrawString(visibleText, _font, new SolidBrush(LoadedSkin.ControlTextColor), 2, 2); + if (IsFocusedControl) + { + //Draw caret. + gfx.FillRectangle(new SolidBrush(LoadedSkin.ControlTextColor), new RectangleF(caretPos, 2, 2, Height - 4)); + } + gfx.DrawRectangle(new Pen(new SolidBrush(LoadedSkin.ControlTextColor), 1), new Rectangle(0, 0, Width - 1, Height - 1)); + } } diff --git a/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs b/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs index 705fc5d..372c5a6 100644 --- a/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs +++ b/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs @@ -43,7 +43,7 @@ namespace ShiftOS.Frontend.GraphicsSubsystem public static void DrawControls(GraphicsDevice graphics, SpriteBatch batch) { - foreach (var ctrl in topLevels) + foreach (var ctrl in topLevels.ToArray()) { using(var bmp = new System.Drawing.Bitmap(ctrl.Width, ctrl.Height)) { diff --git a/ShiftOS.Frontend/ShiftOS.cs b/ShiftOS.Frontend/ShiftOS.cs index 396132c..6bfa4dc 100644 --- a/ShiftOS.Frontend/ShiftOS.cs +++ b/ShiftOS.Frontend/ShiftOS.cs @@ -65,24 +65,63 @@ namespace ShiftOS.Frontend //Let's give it a try. Engine.Infobox.Show("Welcome to ShiftOS!", "This is a test infobox. Clicking OK will dismiss it."); - //Let's set up the Main Menu UI. + //Let's initiate the engine just for a ha. - var justthes = new GUI.PictureBox(); - justthes.AutoSize = true; - justthes.ImageLayout = GUI.ImageLayout.Stretch; - justthes.Image = Properties.Resources.justthes; - justthes.X = 15; - justthes.Y = 15; - justthes.Opacity = 0.5; - UIManager.AddTopLevel(justthes); + //We'll create a few UI elements when the save system loads + SaveSystem.GameReady += () => + { + var headerLabel = new GUI.TextControl(); + headerLabel.Font = SkinEngine.LoadedSkin.HeaderFont; + headerLabel.AutoSize = true; + headerLabel.Text = "ShiftOS engine startup stats"; + headerLabel.X = 30; + headerLabel.Y = 30; + UIManager.AddTopLevel(headerLabel); - _titleLabel = new GUI.TextControl(); - _titleLabel.Text = " - main menu - "; - _titleLabel.AutoSize = true; - _titleLabel.X = justthes.X; - _titleLabel.Y = justthes.Y + justthes.Height + 15; - _titleLabel.Font = SkinEngine.LoadedSkin.HeaderFont; - UIManager.AddTopLevel(_titleLabel); + var statslabel = new GUI.TextControl(); + statslabel.AutoSize = true; + statslabel.X = 30; + statslabel.Y = headerLabel.Y + headerLabel.Height + 30; + UIManager.AddTopLevel(statslabel); + statslabel.Text = $@"Save System +======================= + +System name: {SaveSystem.CurrentSave.SystemName} +Users: {SaveSystem.CurrentSave.Users.Count} + +Current user: {SaveSystem.CurrentUser.Username} + +Sandbox mode: {SaveSystem.IsSandbox} +Installed upgrades: {SaveSystem.CurrentSave.CountUpgrades()} - may be inaccurate if in sandbox mode +Available upgrades: {Shiftorium.GetAvailable().Count()} +Total upgrades: {Shiftorium.GetDefaults().Count()} + +ShiftFS +============================ + +Mounted file systems: {Objects.ShiftFS.Utils.Mounts.Count} + +Reflection Manager +===================== + +Reflection manager found {ReflectMan.Types.Count()} Common Language Runtime types that ShiftOS can reflect over. + + +"; + statslabel.Layout(); + }; + + //We'll use sandbox mode + SaveSystem.IsSandbox = true; + + SaveSystem.Begin(); + + var textinput = new GUI.TextInput(); + textinput.Width = 250; + textinput.Height = 20; + textinput.X = 0; + textinput.Y = 0; + UIManager.AddTopLevel(textinput); base.Initialize();