1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 07:53:06 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuTextBox.cs

159 lines
5.1 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 osu.Framework.Allocation;
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;
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
{
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)
{
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;
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);
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
}
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
{
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
}
}