osu/osu.Game/Graphics/UserInterface/OsuTextBox.cs

110 lines
3.4 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 09:19:50 +00:00
using osu.Framework.Allocation;
2020-02-07 20:42:47 +00:00
using osu.Framework.Audio.Track;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites;
2018-11-20 07:51:59 +00:00
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.Color4Extensions;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2020-02-07 20:42:47 +00:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers;
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Graphics.UserInterface
{
2019-12-24 05:21:16 +00:00
public class OsuTextBox : BasicTextBox
2018-04-13 09:19:50 +00:00
{
protected override float LeftRightPadding => 10;
2019-12-24 09:23:09 +00:00
protected override float CaretWidth => 3;
2018-04-13 09:19:50 +00:00
protected override SpriteText CreatePlaceholder() => new OsuSpriteText
{
2019-02-20 07:52:36 +00:00
Font = OsuFont.GetFont(italics: true),
2018-04-13 09:19:50 +00:00
Colour = new Color4(180, 180, 180, 255),
Margin = new MarginPadding { Left = 2 },
};
public OsuTextBox()
{
Height = 40;
TextContainer.Height = 0.5f;
CornerRadius = 5;
LengthLimit = 1000;
2018-04-13 09:19:50 +00:00
2018-06-27 07:06:26 +00:00
Current.DisabledChanged += disabled => { Alpha = disabled ? 0.3f : 1; };
2018-04-13 09:19:50 +00:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colour)
{
2019-03-22 16:44:05 +00:00
BackgroundUnfocused = Color4.Black.Opacity(0.5f);
BackgroundFocused = OsuColour.Gray(0.3f).Opacity(0.8f);
BackgroundCommit = BorderColour = colour.Yellow;
2018-04-13 09:19:50 +00:00
}
2019-12-24 05:21:16 +00:00
protected override Color4 SelectionColour => new Color4(249, 90, 255, 255);
2018-10-02 03:02:47 +00:00
protected override void OnFocus(FocusEvent e)
2018-04-13 09:19:50 +00:00
{
BorderThickness = 3;
2018-10-02 03:02:47 +00:00
base.OnFocus(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override void OnFocusLost(FocusLostEvent e)
2018-04-13 09:19:50 +00:00
{
BorderThickness = 0;
2018-10-02 03:02:47 +00:00
base.OnFocusLost(e);
2018-04-13 09:19:50 +00:00
}
protected override Drawable GetDrawableCharacter(char c) => new OsuSpriteText { Text = c.ToString(), Font = OsuFont.GetFont(size: CalculatedTextSize) };
2020-02-07 20:42:47 +00:00
protected override Caret CreateCaret() => new BeatCaret
{
CaretWidth = CaretWidth,
SelectionColour = SelectionColour,
};
private class BeatCaret : BasicCaret
{
private bool hasSelection;
public BeatCaret()
{
AddInternal(new CaretBeatSyncedContainer(this));
}
public override void DisplayAt(Vector2 position, float? selectionWidth)
{
base.DisplayAt(position, selectionWidth);
hasSelection = selectionWidth != null;
if (selectionWidth == null)
ClearTransforms(targetMember: nameof(Alpha));
}
private class CaretBeatSyncedContainer : BeatSyncedContainer
{
private readonly BeatCaret caret;
public CaretBeatSyncedContainer(BeatCaret caret)
{
this.caret = caret;
MinimumBeatLength = 300;
}
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
{
if (!caret.hasSelection)
caret.FadeTo(0.7f).FadeTo(0.4f, timingPoint.BeatLength, Easing.InOutSine);
}
}
}
2018-04-13 09:19:50 +00:00
}
}