2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-10-17 10:58:37 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK.Graphics;
|
2019-01-25 10:20:08 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
using osu.Framework.Input.Events;
|
2019-01-25 10:20:08 +00:00
|
|
|
|
using osu.Framework.Platform;
|
2018-06-29 14:28:15 +00:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK.Input;
|
2019-10-01 15:26:34 +00:00
|
|
|
|
using osu.Framework.Input.Bindings;
|
2021-10-17 10:58:37 +00:00
|
|
|
|
using osu.Game.Overlays;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A textbox which holds focus eagerly.
|
|
|
|
|
/// </summary>
|
2019-10-01 15:26:34 +00:00
|
|
|
|
public class FocusedTextBox : OsuTextBox, IKeyBindingHandler<GlobalAction>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
private bool focus;
|
2018-06-29 14:28:15 +00:00
|
|
|
|
|
2019-01-25 10:20:08 +00:00
|
|
|
|
private bool allowImmediateFocus => host?.OnScreenKeyboardOverlapsGameWindow != true;
|
|
|
|
|
|
|
|
|
|
public void TakeFocus()
|
|
|
|
|
{
|
2021-08-16 09:44:00 +00:00
|
|
|
|
if (!allowImmediateFocus)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-18 03:45:14 +00:00
|
|
|
|
Scheduler.Add(() => GetContainingInputManager().ChangeFocus(this), false);
|
2019-01-25 10:20:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 05:19:59 +00:00
|
|
|
|
public new void KillFocus() => base.KillFocus();
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public bool HoldFocus
|
|
|
|
|
{
|
2019-01-25 10:34:25 +00:00
|
|
|
|
get => allowImmediateFocus && focus;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
focus = value;
|
|
|
|
|
if (!focus && HasFocus)
|
2018-07-01 05:08:34 +00:00
|
|
|
|
base.KillFocus();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 13:14:00 +00:00
|
|
|
|
[Resolved]
|
2021-10-17 10:58:37 +00:00
|
|
|
|
private GameHost? host { get; set; }
|
2019-01-25 10:20:08 +00:00
|
|
|
|
|
2021-10-17 10:58:37 +00:00
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(OverlayColourProvider? colourProvider)
|
2019-01-25 10:20:08 +00:00
|
|
|
|
{
|
2021-10-17 10:58:37 +00:00
|
|
|
|
BackgroundUnfocused = colourProvider?.Background5 ?? new Color4(10, 10, 10, 255);
|
|
|
|
|
BackgroundFocused = colourProvider?.Background5 ?? new Color4(10, 10, 10, 255);
|
2019-01-25 10:20:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
// We may not be focused yet, but we need to handle keyboard input to be able to request focus
|
2018-09-26 05:01:15 +00:00
|
|
|
|
public override bool HandleNonPositionalInput => HoldFocus || base.HandleNonPositionalInput;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override void OnFocus(FocusEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2018-10-02 03:02:47 +00:00
|
|
|
|
base.OnFocus(e);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
BorderThickness = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
2018-06-30 14:51:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (!HasFocus) return false;
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
if (e.Key == Key.Escape)
|
2019-10-01 15:37:08 +00:00
|
|
|
|
return false; // disable the framework-level handling of escape key for conformity (we use GlobalAction.Back).
|
2018-07-02 02:04:40 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnKeyDown(e);
|
2018-06-30 14:51:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
|
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
|
2018-06-29 14:28:15 +00:00
|
|
|
|
{
|
2021-11-18 03:35:47 +00:00
|
|
|
|
if (e.Repeat)
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-07-20 18:52:02 +00:00
|
|
|
|
if (!HasFocus) return false;
|
|
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
|
if (e.Action == GlobalAction.Back)
|
2018-06-29 14:28:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (Text.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Text = string.Empty;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 15:26:34 +00:00
|
|
|
|
return false;
|
2018-06-29 14:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
|
2020-01-22 04:22:34 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
public override bool RequestsFocus => HoldFocus;
|
|
|
|
|
}
|
|
|
|
|
}
|