mirror of
https://github.com/ppy/osu.git
synced 2025-01-28 06:42:54 +08:00
Add FocusedTextBox abstraction.
This commit is contained in:
parent
2a099bb56c
commit
520e040396
49
osu.Game/Graphics/UserInterface/FocusedTextBox.cs
Normal file
49
osu.Game/Graphics/UserInterface/FocusedTextBox.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Input;
|
||||
using osu.Framework.Input;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
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)
|
||||
TriggerFocusLost();
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool OnFocus(InputState state)
|
||||
{
|
||||
var result = base.OnFocus(state);
|
||||
BorderThickness = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override void OnFocusLost(InputState state)
|
||||
{
|
||||
if (state.Keyboard.Keys.Any(key => key == Key.Escape))
|
||||
{
|
||||
if (Text.Length > 0)
|
||||
Text = string.Empty;
|
||||
else
|
||||
Exit?.Invoke();
|
||||
}
|
||||
base.OnFocusLost(state);
|
||||
}
|
||||
|
||||
public override bool RequestingFocus => HoldFocus;
|
||||
}
|
||||
}
|
@ -8,7 +8,6 @@ using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Graphics.UserInterface;
|
||||
using osu.Framework.Input;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Overlays;
|
||||
using OpenTK.Graphics;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
|
@ -47,18 +47,19 @@ namespace osu.Game.Screens.Select
|
||||
Direction = FlowDirections.Vertical,
|
||||
Children = new Drawable[]
|
||||
{
|
||||
searchTextBox = new SearchTextBox { RelativeSizeAxes = Axes.X },
|
||||
searchTextBox = new SearchTextBox {
|
||||
RelativeSizeAxes = Axes.X,
|
||||
OnChange = (TextBox sender, bool newText) =>
|
||||
{
|
||||
if (newText)
|
||||
FilterChanged?.Invoke();
|
||||
},
|
||||
Exit = () => Exit?.Invoke(),
|
||||
},
|
||||
new GroupSortTabs()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
searchTextBox.OnChange += (TextBox sender, bool newText) =>
|
||||
{
|
||||
if (newText)
|
||||
FilterChanged?.Invoke();
|
||||
};
|
||||
searchTextBox.Exit = () => Exit?.Invoke();
|
||||
}
|
||||
|
||||
public void Deactivate()
|
||||
|
@ -16,26 +16,8 @@ namespace osu.Game.Screens.Select
|
||||
/// <summary>
|
||||
/// A textbox which holds focus eagerly.
|
||||
/// </summary>
|
||||
public class SearchTextBox : OsuTextBox
|
||||
public class SearchTextBox : FocusedTextBox
|
||||
{
|
||||
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)
|
||||
TriggerFocusLost();
|
||||
}
|
||||
}
|
||||
|
||||
public override bool RequestingFocus => HoldFocus;
|
||||
|
||||
public SearchTextBox()
|
||||
{
|
||||
Height = 35;
|
||||
@ -53,25 +35,6 @@ namespace osu.Game.Screens.Select
|
||||
PlaceholderText = "type to search";
|
||||
}
|
||||
|
||||
protected override bool OnFocus(InputState state)
|
||||
{
|
||||
var result = base.OnFocus(state);
|
||||
BorderThickness = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override void OnFocusLost(InputState state)
|
||||
{
|
||||
if (state.Keyboard.Keys.Any(key => key == Key.Escape))
|
||||
{
|
||||
if (Text.Length > 0)
|
||||
Text = string.Empty;
|
||||
else
|
||||
Exit?.Invoke();
|
||||
}
|
||||
base.OnFocusLost(state);
|
||||
}
|
||||
|
||||
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
||||
{
|
||||
if (HandlePendingText(state)) return true;
|
||||
|
@ -70,6 +70,7 @@
|
||||
<Compile Include="Graphics\Cursor\CursorTrail.cs" />
|
||||
<Compile Include="Graphics\Sprites\OsuSpriteText.cs" />
|
||||
<Compile Include="Graphics\UserInterface\BackButton.cs" />
|
||||
<Compile Include="Graphics\UserInterface\FocusedTextBox.cs" />
|
||||
<Compile Include="Graphics\UserInterface\Nub.cs" />
|
||||
<Compile Include="Graphics\UserInterface\OsuPasswordTextBox.cs" />
|
||||
<Compile Include="Graphics\UserInterface\OsuSliderBar.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user