1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 00:47:24 +08:00
osu-lazer/osu.Game/Screens/Select/SearchTextBox.cs

106 lines
3.2 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Game.Graphics;
2017-02-08 10:19:58 +08:00
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Screens.Select
{
2017-02-08 10:19:58 +08:00
public class SearchTextBox : OsuTextBox
{
protected override Color4 BackgroundUnfocused => new Color4(10, 10, 10, 255);
protected override Color4 BackgroundFocused => new Color4(10, 10, 10, 255);
public Action Exit;
2017-02-03 18:12:57 +08:00
private bool focus;
public bool HoldFocus
{
get { return focus; }
set
{
focus = value;
if (!focus)
TriggerFocusLost();
}
}
private SpriteText placeholder;
protected override string InternalText
{
get { return base.InternalText; }
set
{
base.InternalText = value;
if (placeholder != null)
{
if (string.IsNullOrEmpty(value))
placeholder.Text = "type to search";
else
placeholder.Text = string.Empty;
}
}
}
2017-02-03 18:12:57 +08:00
public SearchTextBox()
{
Height = 35;
Add(new Drawable[]
{
placeholder = new SpriteText
{
Font = @"Exo2.0-MediumItalic",
Text = "type to search",
Colour = new Color4(180, 180, 180, 255),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Margin = new MarginPadding { Left = 10 },
},
new TextAwesome
{
Icon = FontAwesome.fa_search,
Origin = Anchor.CentreRight,
Anchor = Anchor.CentreRight,
Margin = new MarginPadding { Right = 10 },
}
});
}
2017-02-03 18:12:57 +08:00
protected override void Update()
{
2017-02-03 18:12:57 +08:00
if (HoldFocus) RequestFocus();
base.Update();
}
2017-02-03 18:12:57 +08:00
2017-02-08 10:19:58 +08:00
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))
Exit?.Invoke();
base.OnFocusLost(state);
}
2017-02-03 18:12:57 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (args.Key == Key.Left || args.Key == Key.Right || args.Key == Key.Enter)
return false;
return base.OnKeyDown(state, args);
}
}
}