1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 02:22:56 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/FocusedTextBox.cs

59 lines
1.7 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-03-02 17:45:20 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK.Graphics;
2017-02-19 16:59:22 +08:00
using OpenTK.Input;
using osu.Framework.Input;
using System;
namespace osu.Game.Graphics.UserInterface
{
2017-07-18 17:03:59 +08:00
/// <summary>
/// A textbox which holds focus eagerly.
/// </summary>
2017-02-19 16:59:22 +08:00
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;
public bool HoldFocus
{
get { return focus; }
set
{
focus = value;
if (!focus && HasFocus)
GetContainingInputManager().ChangeFocus(null);
2017-02-19 16:59:22 +08:00
}
}
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 HandleKeyboardInput => HoldFocus || base.HandleKeyboardInput;
2017-05-30 15:33:26 +08:00
protected override void OnFocus(InputState state)
2017-02-19 16:59:22 +08:00
{
2017-05-30 15:33:26 +08:00
base.OnFocus(state);
2017-02-19 16:59:22 +08:00
BorderThickness = 0;
}
2017-08-16 18:17:29 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
2017-02-19 16:59:22 +08:00
{
if (!args.Repeat && args.Key == Key.Escape)
2017-02-19 16:59:22 +08:00
{
if (Text.Length > 0)
Text = string.Empty;
else
Exit?.Invoke();
2017-08-16 18:17:29 +08:00
return true;
2017-02-19 16:59:22 +08:00
}
2017-08-16 18:17:29 +08:00
return base.OnKeyDown(state, args);
2017-02-19 16:59:22 +08:00
}
2017-05-30 15:33:26 +08:00
public override bool RequestsFocus => HoldFocus;
2017-02-19 16:59:22 +08:00
}
}