1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 08:52:54 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/FocusedTextBox.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.8 KiB
C#
Raw Normal View History

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