diff --git a/Game.Client/GarrysMod/LuaPanel.cs b/Game.Client/GarrysMod/LuaPanel.cs index 66e74a69..b575272e 100644 --- a/Game.Client/GarrysMod/LuaPanel.cs +++ b/Game.Client/GarrysMod/LuaPanel.cs @@ -1674,9 +1674,7 @@ static int Panel__SetTextHidden(ILuaInterface lua) { [LuaMethod] static int Panel__SetPopupStayAtBack(ILuaInterface lua) { - Panel? panel = GetPanelOrError(lua); - if (panel != null && lua.GetBool(2)) - Surface.MovePopupToBack(panel); + GetPanelOrError(lua)?.SetPopupStayAtBack(lua.GetBool(2)); return 0; } diff --git a/Source.Common/GUI/IPanel.cs b/Source.Common/GUI/IPanel.cs index a1384073..ffb463a2 100644 --- a/Source.Common/GUI/IPanel.cs +++ b/Source.Common/GUI/IPanel.cs @@ -127,6 +127,8 @@ public interface IPanel : IExtDisposable // Used by the drag/drop manager to always draw on top bool IsTopmostPopup(); void SetTopmostPopup(bool state); + bool IsPopupStayAtBack(); + void SetPopupStayAtBack(bool state); void InvalidateLayout(bool layoutNow = false, bool reloadScheme = false); int GetAlpha(); diff --git a/Source.GUI.Controls/Menu.cs b/Source.GUI.Controls/Menu.cs index d1afb5ba..7bd41b55 100644 --- a/Source.GUI.Controls/Menu.cs +++ b/Source.GUI.Controls/Menu.cs @@ -853,7 +853,7 @@ public override void OnMouseWheeled(int delta) { } public override void OnKillFocus(Panel? newPanel) { - if (newPanel == null || !HasParent(newPanel)) { + if (newPanel == null || !newPanel.HasParent(this)) { if (!IsKeyboardInputEnabled() && newPanel == null) return; diff --git a/Source.GUI.Controls/Panel.cs b/Source.GUI.Controls/Panel.cs index ad7ce231..6620db98 100644 --- a/Source.GUI.Controls/Panel.cs +++ b/Source.GUI.Controls/Panel.cs @@ -310,6 +310,7 @@ public void SetScheme(IScheme scheme) { bool MouseInput; bool KbInput; bool TopmostPopup; + bool PopupStayAtBack; [PanelAnimationVar("alpha", "255")] float Alpha; IBorder? Border; IScheme? Scheme; @@ -1228,6 +1229,8 @@ public bool IsProportional() { } public bool IsTopmostPopup() => TopmostPopup; + public bool IsPopupStayAtBack() => PopupStayAtBack; + public void SetPopupStayAtBack(bool state) => PopupStayAtBack = state; public bool IsVisible() { return Visible; diff --git a/Source.GUI.Controls/RichText.cs b/Source.GUI.Controls/RichText.cs index 8049e2f9..f3bbce3e 100644 --- a/Source.GUI.Controls/RichText.cs +++ b/Source.GUI.Controls/RichText.cs @@ -713,9 +713,94 @@ public int PixelToCursorSpace(int cx, int cy) { return i; } - //public override void OnKeyCodeTyped(ButtonCode code) // todo - //{ - //} + public override void OnKeyCodeTyped(ButtonCode code) { + bool shift = Input.IsKeyDown(ButtonCode.KeyLShift) || Input.IsKeyDown(ButtonCode.KeyRShift); + bool ctrl = Input.IsKeyDown(ButtonCode.KeyLControl) || Input.IsKeyDown(ButtonCode.KeyRControl); + bool alt = Input.IsKeyDown(ButtonCode.KeyLAlt) || Input.IsKeyDown(ButtonCode.KeyRAlt); + bool winkey = Input.IsKeyDown(ButtonCode.KeyLWin) || Input.IsKeyDown(ButtonCode.KeyRWin); + bool fallThrough = false; + + if (ctrl || (winkey && IsOSX)) { + switch (code) { + case ButtonCode.KeyA: + CursorPos = 0; + Select[0] = 0; + Select[1] = TextStream.Count; + return; + + case ButtonCode.KeyInsert: + case ButtonCode.KeyC: + case ButtonCode.KeyX: + CopySelected(); + return; + + case ButtonCode.KeyPageUp: + case ButtonCode.KeyHome: + GotoTextStart(); + break; + + case ButtonCode.KeyPageDown: + case ButtonCode.KeyEnd: + GotoTextEnd(); + break; + + default: + fallThrough = true; + break; + } + } + else if (alt) + fallThrough = true; + else { + switch (code) { + case ButtonCode.KeyTab: + case ButtonCode.KeyLShift: + case ButtonCode.KeyRShift: + case ButtonCode.KeyEscape: + case ButtonCode.KeyEnter: + fallThrough = true; + break; + + case ButtonCode.KeyDelete: + if (shift) + CopySelected(); + break; + + case ButtonCode.KeyHome: + GotoTextStart(); + break; + + case ButtonCode.KeyEnd: + GotoTextEnd(); + break; + + case ButtonCode.KeyPageUp: + if (VertScrollBar.IsVisible()) { + int window = VertScrollBar.GetRangeWindow(); + int newval = VertScrollBar.GetValue(); + VertScrollBar.SetValue(newval - window - 1); + } + break; + + case ButtonCode.KeyPageDown: + if (VertScrollBar.IsVisible()) { + int window = VertScrollBar.GetRangeWindow(); + int newval = VertScrollBar.GetValue(); + VertScrollBar.SetValue(newval + window + 1); + } + break; + + default: + return; + } + } + + if (code != ButtonCode.KeyLControl && code != ButtonCode.KeyRControl) + Select[1] = CursorPos; + + if (fallThrough) + base.OnKeyCodeTyped(code); + } public bool GetSelectedRange(out int start, out int end) { if (Select[0] == -1) { @@ -966,6 +1051,7 @@ public virtual void CutSelected() { RequestFocus(); } + [UiMessage("DoCopySelected")] public virtual void CopySelected() { if (GetSelectedRange(out int x0, out int x1)) { List buf = []; diff --git a/Source.GUI.Controls/TextEntry.cs b/Source.GUI.Controls/TextEntry.cs index b806a15b..a466f35a 100644 --- a/Source.GUI.Controls/TextEntry.cs +++ b/Source.GUI.Controls/TextEntry.cs @@ -772,6 +772,13 @@ public override void OnKeyCodeTyped(ButtonCode code) { GotoWordLeft(); break; + case ButtonCode.KeyBackspace: + if (GetSelectedRange(out _, out _)) + DeleteSelected(); + else + DeleteWordLeft(); + break; + case ButtonCode.KeyEnter: if (Multiline) { DeleteSelected(); @@ -969,16 +976,7 @@ private void GotoWordLeft() { if (CursorPos < 1) return; - while (--CursorPos >= 0) - if (!char.IsWhiteSpace(TextStream[CursorPos])) - break; - - - while (--CursorPos >= 0) - if (char.IsWhiteSpace(TextStream[CursorPos])) - break; - - CursorPos++; + CursorPos = FindWordLeft(CursorPos); ScrollLeft(); @@ -987,6 +985,54 @@ private void GotoWordLeft() { Repaint(); } + enum CharClass { Whitespace, Word, Punctuation } + + static CharClass GetCharClass(char ch) { + if (char.IsWhiteSpace(ch)) + return CharClass.Whitespace; + if (char.IsLetterOrDigit(ch) || ch == '_') + return CharClass.Word; + return CharClass.Punctuation; + } + + private int FindWordLeft(int pos) { + while (pos > 0 && GetCharClass(TextStream[pos - 1]) == CharClass.Whitespace) + pos--; + + if (pos > 0) { + CharClass charClass = GetCharClass(TextStream[pos - 1]); + while (pos > 0 && GetCharClass(TextStream[pos - 1]) == charClass) + pos--; + } + + return pos; + } + + private int FindWordRight(int pos) { + int count = TextStream.Count; + if (pos < count) { + CharClass charClass = GetCharClass(TextStream[pos]); + if (charClass != CharClass.Whitespace) + while (pos < count && GetCharClass(TextStream[pos]) == charClass) + pos++; + } + + while (pos < count && GetCharClass(TextStream[pos]) == CharClass.Whitespace) + pos++; + + return pos; + } + + private void DeleteWordLeft() { + int start = FindWordLeft(CursorPos); + if (start == CursorPos) + return; + + Select[0] = start; + Select[1] = CursorPos; + DeleteSelected(); + } + private void ScrollLeft() { if (Multiline) return; @@ -1007,16 +1053,7 @@ private void ScrollLeft() { private void GotoWordRight() { SelectCheck(); - while (++CursorPos < TextStream.Count) - if (char.IsWhiteSpace(TextStream[CursorPos])) - break; - - while (++CursorPos < TextStream.Count) - if (!char.IsWhiteSpace(TextStream[CursorPos])) - break; - - if (CursorPos > TextStream.Count) - CursorPos = TextStream.Count; + CursorPos = FindWordRight(CursorPos); ScrollRight(); diff --git a/Source.MaterialSystem/Surface/MatSystemSurface.cs b/Source.MaterialSystem/Surface/MatSystemSurface.cs index 286e65c2..3150167f 100644 --- a/Source.MaterialSystem/Surface/MatSystemSurface.cs +++ b/Source.MaterialSystem/Surface/MatSystemSurface.cs @@ -1071,6 +1071,9 @@ public void MovePopupToBack(IPanel panel) { } public void MovePopupToFront(IPanel panel) { + if (panel.IsPopupStayAtBack()) + return; + int index = PopupList.IndexOf(panel); if (index == -1) return;