1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 22:43:22 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuTextBox.cs

214 lines
7.0 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 17:19:50 +08:00
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2020-02-08 04:42:47 +08:00
using osu.Framework.Audio.Track;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
2020-02-09 03:25:16 +08:00
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2020-02-08 04:42:47 +08:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
2019-12-24 13:21:16 +08:00
public class OsuTextBox : BasicTextBox
2018-04-13 17:19:50 +08:00
{
private readonly SampleChannel[] textAddedSamples = new SampleChannel[4];
private SampleChannel capsTextAddedSample;
private SampleChannel textRemovedSample;
private SampleChannel textCommittedSample;
private SampleChannel caretMovedSample;
/// <summary>
/// Whether to allow playing a different samples based on the type of character.
/// If set to false, the same sample will be used for all characters.
/// </summary>
protected virtual bool AllowUniqueCharacterSamples => true;
2018-04-13 17:19:50 +08:00
protected override float LeftRightPadding => 10;
2019-12-24 17:23:09 +08:00
protected override float CaretWidth => 3;
2018-04-13 17:19:50 +08:00
protected override SpriteText CreatePlaceholder() => new OsuSpriteText
{
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(italics: true),
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
2018-06-27 15:06:26 +08:00
Current.DisabledChanged += disabled => { Alpha = disabled ? 0.3f : 1; };
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colour, AudioManager audio)
2018-04-13 17:19:50 +08:00
{
2019-03-23 00:44:05 +08:00
BackgroundUnfocused = Color4.Black.Opacity(0.5f);
BackgroundFocused = OsuColour.Gray(0.3f).Opacity(0.8f);
BackgroundCommit = BorderColour = colour.Yellow;
for (int i = 0; i < textAddedSamples.Length; i++)
textAddedSamples[i] = audio.Samples.Get($@"Keyboard/key-press-{1 + i}");
capsTextAddedSample = audio.Samples.Get(@"Keyboard/key-caps");
textRemovedSample = audio.Samples.Get(@"Keyboard/key-delete");
textCommittedSample = audio.Samples.Get(@"Keyboard/key-confirm");
caretMovedSample = audio.Samples.Get(@"Keyboard/key-movement");
2018-04-13 17:19:50 +08:00
}
2019-12-24 13:21:16 +08:00
protected override Color4 SelectionColour => new Color4(249, 90, 255, 255);
protected override void OnTextAdded(string added)
{
base.OnTextAdded(added);
if (added.Any(char.IsUpper) && AllowUniqueCharacterSamples)
capsTextAddedSample?.Play();
else
textAddedSamples[RNG.Next(0, 3)]?.Play();
}
protected override void OnTextRemoved(string removed)
{
base.OnTextRemoved(removed);
textRemovedSample?.Play();
}
protected override void OnTextCommitted(bool textChanged)
{
base.OnTextCommitted(textChanged);
textCommittedSample?.Play();
}
protected override void OnCaretMoved(bool selecting)
{
base.OnCaretMoved(selecting);
caretMovedSample?.Play();
}
2018-10-02 11:02:47 +08:00
protected override void OnFocus(FocusEvent e)
2018-04-13 17:19:50 +08:00
{
BorderThickness = 3;
2018-10-02 11:02:47 +08:00
base.OnFocus(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnFocusLost(FocusLostEvent e)
2018-04-13 17:19:50 +08:00
{
BorderThickness = 0;
2018-10-02 11:02:47 +08:00
base.OnFocusLost(e);
2018-04-13 17:19:50 +08:00
}
protected override Drawable GetDrawableCharacter(char c) => new FallingDownContainer
{
AutoSizeAxes = Axes.Both,
Child = new OsuSpriteText { Text = c.ToString(), Font = OsuFont.GetFont(size: CalculatedTextSize) },
};
2020-02-08 04:42:47 +08:00
2020-02-09 14:36:44 +08:00
protected override Caret CreateCaret() => new OsuCaret
2020-02-08 04:42:47 +08:00
{
CaretWidth = CaretWidth,
SelectionColour = SelectionColour,
};
2020-02-09 14:36:44 +08:00
private class OsuCaret : Caret
2020-02-08 04:42:47 +08:00
{
2020-02-09 03:25:16 +08:00
private const float caret_move_time = 60;
private readonly CaretBeatSyncedContainer beatSync;
2020-02-08 04:42:47 +08:00
2020-02-09 14:36:44 +08:00
public OsuCaret()
2020-02-08 04:42:47 +08:00
{
2020-02-09 03:25:16 +08:00
RelativeSizeAxes = Axes.Y;
Size = new Vector2(1, 0.9f);
Colour = Color4.Transparent;
Anchor = Anchor.CentreLeft;
Origin = Anchor.CentreLeft;
Masking = true;
CornerRadius = 1;
InternalChild = beatSync = new CaretBeatSyncedContainer
{
RelativeSizeAxes = Axes.Both,
};
2020-02-08 04:42:47 +08:00
}
2020-02-09 03:25:16 +08:00
public override void Hide() => this.FadeOut(200);
public float CaretWidth { get; set; }
public Color4 SelectionColour { get; set; }
2020-02-08 04:42:47 +08:00
public override void DisplayAt(Vector2 position, float? selectionWidth)
{
2020-02-09 03:25:16 +08:00
beatSync.HasSelection = selectionWidth != null;
2020-02-08 04:42:47 +08:00
2020-02-09 03:25:16 +08:00
if (selectionWidth != null)
{
this.MoveTo(new Vector2(position.X, position.Y), 60, Easing.Out);
this.ResizeWidthTo(selectionWidth.Value + CaretWidth / 2, caret_move_time, Easing.Out);
this.FadeColour(SelectionColour, 200, Easing.Out);
}
else
{
this.MoveTo(new Vector2(position.X - CaretWidth / 2, position.Y), 60, Easing.Out);
this.ResizeWidthTo(CaretWidth, caret_move_time, Easing.Out);
this.FadeColour(Color4.White, 200, Easing.Out);
}
2020-02-08 04:42:47 +08:00
}
private class CaretBeatSyncedContainer : BeatSyncedContainer
{
2020-02-09 03:25:16 +08:00
private bool hasSelection;
public bool HasSelection
{
set
{
hasSelection = value;
if (value)
this.FadeTo(0.5f, 200, Easing.Out);
}
}
2020-02-08 04:42:47 +08:00
2020-02-09 03:25:16 +08:00
public CaretBeatSyncedContainer()
2020-02-08 04:42:47 +08:00
{
MinimumBeatLength = 300;
2020-02-09 03:25:16 +08:00
InternalChild = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
};
2020-02-08 04:42:47 +08:00
}
2020-06-23 12:49:18 +08:00
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
2020-02-08 04:42:47 +08:00
{
2020-02-09 03:25:16 +08:00
if (!hasSelection)
this.FadeTo(0.7f).FadeTo(0.4f, timingPoint.BeatLength, Easing.InOutSine);
2020-02-08 04:42:47 +08:00
}
}
}
2018-04-13 17:19:50 +08:00
}
}