1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 10:47:25 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/FocusedTextBox.cs

78 lines
2.1 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK.Graphics;
using System;
2018-07-21 10:38:28 +08:00
using osu.Framework.Input.EventArgs;
using osu.Framework.Input.States;
2018-06-29 22:28:15 +08:00
using osu.Game.Input.Bindings;
2018-07-02 10:04:40 +08:00
using OpenTK.Input;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// A textbox which holds focus eagerly.
/// </summary>
public class FocusedTextBox : OsuTextBox
{
protected override Color4 BackgroundUnfocused => new Color4(10, 10, 10, 255);
protected override Color4 BackgroundFocused => new Color4(10, 10, 10, 255);
public Action Exit;
private bool focus;
2018-06-29 22:28:15 +08:00
2018-04-13 17:19:50 +08:00
public bool HoldFocus
{
get { return focus; }
set
{
focus = value;
if (!focus && HasFocus)
2018-07-01 13:08:34 +08:00
base.KillFocus();
2018-04-13 17:19:50 +08:00
}
}
// We may not be focused yet, but we need to handle keyboard input to be able to request focus
public override bool HandleKeyboardInput => HoldFocus || base.HandleKeyboardInput;
protected override void OnFocus(InputState state)
{
base.OnFocus(state);
BorderThickness = 0;
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (!HasFocus) return false;
2018-07-02 10:04:40 +08:00
if (args.Key == Key.Escape)
return false; // disable the framework-level handling of escape key for confority (we use GlobalAction.Back).
return base.OnKeyDown(state, args);
}
2018-06-29 22:28:15 +08:00
public override bool OnPressed(GlobalAction action)
{
if (action == GlobalAction.Back)
{
if (Text.Length > 0)
{
Text = string.Empty;
return true;
}
}
return base.OnPressed(action);
}
2018-06-27 15:06:26 +08:00
protected override void KillFocus()
2018-04-13 17:19:50 +08:00
{
2018-06-27 15:06:26 +08:00
base.KillFocus();
Exit?.Invoke();
2018-04-13 17:19:50 +08:00
}
public override bool RequestsFocus => HoldFocus;
}
}