1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 23:42:54 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineHitObjectBlueprint.cs

371 lines
12 KiB
C#
Raw Normal View History

2020-01-23 15:22:43 +08:00
// 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.
using System;
2020-02-05 18:43:13 +08:00
using System.Collections.Generic;
2020-01-23 15:22:43 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
2020-01-23 15:22:43 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
2020-01-23 15:22:43 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
2020-01-23 15:22:43 +08:00
using osu.Framework.Graphics.Containers;
2020-02-05 18:43:13 +08:00
using osu.Framework.Graphics.Effects;
2020-01-23 15:22:43 +08:00
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
2021-01-21 02:00:25 +08:00
using osu.Framework.Utils;
2020-09-29 16:04:59 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2020-01-23 15:22:43 +08:00
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Skinning;
2020-01-23 15:22:43 +08:00
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
{
public class TimelineHitObjectBlueprint : SelectionBlueprint
{
private const float circle_size = 38;
private Container repeatsContainer;
public Action<DragEvent> OnDragHandled;
2020-01-23 15:22:43 +08:00
[UsedImplicitly]
private readonly Bindable<double> startTime;
private Bindable<int> indexInCurrentComboBindable;
private Bindable<int> comboIndexBindable;
private readonly Drawable circle;
private readonly Container colouredComponents;
2020-09-29 16:04:59 +08:00
private readonly OsuSpriteText comboIndexText;
[Resolved]
private ISkinSource skin { get; set; }
2020-01-23 15:22:43 +08:00
public TimelineHitObjectBlueprint(HitObject hitObject)
: base(hitObject)
{
Anchor = Anchor.CentreLeft;
Origin = Anchor.CentreLeft;
startTime = hitObject.StartTimeBindable.GetBoundCopy();
startTime.BindValueChanged(time => X = (float)time.NewValue, true);
RelativePositionAxes = Axes.X;
RelativeSizeAxes = Axes.X;
Height = circle_size;
2020-01-23 15:22:43 +08:00
AddRangeInternal(new[]
2020-09-29 16:04:59 +08:00
{
circle = new ExtendableCircle
2020-09-29 16:04:59 +08:00
{
RelativeSizeAxes = Axes.Both,
2020-09-29 16:04:59 +08:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
colouredComponents = new Container
2020-09-29 16:04:59 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
comboIndexText = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.Centre,
Y = -1,
Font = OsuFont.Default.With(size: circle_size * 0.5f, weight: FontWeight.Regular),
},
}
2020-09-29 16:04:59 +08:00
},
});
2020-05-27 11:38:39 +08:00
if (hitObject is IHasDuration)
{
colouredComponents.Add(new DragArea(hitObject)
{
OnDragHandled = e => OnDragHandled?.Invoke(e)
});
}
}
protected override void LoadComplete()
2020-09-29 15:42:54 +08:00
{
base.LoadComplete();
2020-09-29 16:04:59 +08:00
if (HitObject is IHasComboInformation comboInfo)
{
indexInCurrentComboBindable = comboInfo.IndexInCurrentComboBindable.GetBoundCopy();
indexInCurrentComboBindable.BindValueChanged(_ => updateComboIndex(), true);
comboIndexBindable = comboInfo.ComboIndexBindable.GetBoundCopy();
comboIndexBindable.BindValueChanged(_ => updateComboColour(), true);
skin.SourceChanged += updateComboColour;
2020-09-29 16:04:59 +08:00
}
}
2020-09-29 16:04:59 +08:00
protected override void OnSelected()
{
// base logic hides selected blueprints when not selected, but timeline doesn't do that.
}
protected override void OnDeselected()
{
// base logic hides selected blueprints when not selected, but timeline doesn't do that.
}
private void updateComboIndex() => comboIndexText.Text = (indexInCurrentComboBindable.Value + 1).ToString();
2020-09-29 16:04:59 +08:00
private void updateComboColour()
{
if (!(HitObject is IHasComboInformation combo))
return;
2020-09-29 16:04:59 +08:00
var comboColours = skin.GetConfig<GlobalSkinColours, IReadOnlyList<Color4>>(GlobalSkinColours.ComboColours)?.Value ?? Array.Empty<Color4>();
var comboColour = combo.GetComboColour(comboColours);
if (HitObject is IHasDuration)
circle.Colour = ColourInfo.GradientHorizontal(comboColour, comboColour.Lighten(0.4f));
else
circle.Colour = comboColour;
var col = circle.Colour.TopLeft.Linear;
float brightness = col.R + col.G + col.B;
// decide the combo index colour based on brightness?
colouredComponents.Colour = OsuColour.Gray(brightness > 0.5f ? 0.2f : 0.9f);
2020-09-29 15:42:54 +08:00
}
2020-02-05 18:43:13 +08:00
protected override void Update()
{
base.Update();
// no bindable so we perform this every update
float duration = (float)(HitObject.GetEndTime() - HitObject.StartTime);
if (Width != duration)
{
Width = duration;
// kind of haphazard but yeah, no bindables.
if (HitObject is IHasRepeats repeats)
updateRepeats(repeats);
}
}
private void updateRepeats(IHasRepeats repeats)
{
repeatsContainer?.Expire();
colouredComponents.Add(repeatsContainer = new Container
{
RelativeSizeAxes = Axes.Both,
});
for (int i = 0; i < repeats.RepeatCount; i++)
{
repeatsContainer.Add(new Circle
{
Size = new Vector2(circle_size / 3),
Alpha = 0.2f,
Anchor = Anchor.CentreLeft,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.X,
X = (float)(i + 1) / (repeats.RepeatCount + 1),
});
}
2020-02-05 18:43:13 +08:00
}
2020-02-06 13:35:45 +08:00
protected override bool ShouldBeConsideredForInput(Drawable child) => true;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
circle.ReceivePositionalInputAt(screenSpacePos);
public override Quad SelectionQuad => circle.ScreenSpaceDrawQuad;
2020-02-05 18:43:13 +08:00
public override Vector2 ScreenSpaceSelectionPoint => ScreenSpaceDrawQuad.TopLeft;
2020-02-05 18:43:13 +08:00
public class DragArea : Circle
{
private readonly HitObject hitObject;
[Resolved]
private Timeline timeline { get; set; }
public Action<DragEvent> OnDragHandled;
2020-02-05 18:43:13 +08:00
public override bool HandlePositionalInput => hitObject != null;
public DragArea(HitObject hitObject)
{
this.hitObject = hitObject;
CornerRadius = circle_size / 2;
Masking = true;
Size = new Vector2(circle_size, 1);
Anchor = Anchor.CentreRight;
2020-02-05 18:43:13 +08:00
Origin = Anchor.Centre;
RelativePositionAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
2020-02-05 18:43:13 +08:00
InternalChildren = new Drawable[]
{
2020-02-05 18:43:13 +08:00
new Box
{
RelativeSizeAxes = Axes.Both,
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
updateState();
FinishTransforms();
}
2020-02-05 14:33:04 +08:00
protected override bool OnHover(HoverEvent e)
{
updateState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateState();
base.OnHoverLost(e);
}
private bool hasMouseDown;
protected override bool OnMouseDown(MouseDownEvent e)
{
hasMouseDown = true;
updateState();
return true;
}
protected override void OnMouseUp(MouseUpEvent e)
{
hasMouseDown = false;
updateState();
base.OnMouseUp(e);
}
private void updateState()
{
if (hasMouseDown)
{
this.ScaleTo(0.7f, 200, Easing.OutQuint);
}
else if (IsHovered)
{
this.ScaleTo(0.8f, 200, Easing.OutQuint);
}
else
{
this.ScaleTo(0.6f, 200, Easing.OutQuint);
}
this.FadeTo(IsHovered || hasMouseDown ? 0.8f : 0.2f, 200, Easing.OutQuint);
2020-02-05 14:33:04 +08:00
}
[Resolved]
private EditorBeatmap beatmap { get; set; }
2020-02-05 15:35:07 +08:00
[Resolved]
private IBeatSnapProvider beatSnapProvider { get; set; }
[Resolved(CanBeNull = true)]
private IEditorChangeHandler changeHandler { get; set; }
protected override bool OnDragStart(DragStartEvent e)
{
changeHandler?.BeginChange();
return true;
}
protected override void OnDrag(DragEvent e)
{
base.OnDrag(e);
OnDragHandled?.Invoke(e);
if (timeline.SnapScreenSpacePositionToValidTime(e.ScreenSpaceMousePosition).Time is double time)
{
switch (hitObject)
{
case IHasRepeats repeatHitObject:
// find the number of repeats which can fit in the requested time.
var lengthOfOneRepeat = repeatHitObject.Duration / (repeatHitObject.RepeatCount + 1);
2020-05-22 18:23:07 +08:00
var proposedCount = Math.Max(0, (int)Math.Round((time - hitObject.StartTime) / lengthOfOneRepeat) - 1);
if (proposedCount == repeatHitObject.RepeatCount)
return;
repeatHitObject.RepeatCount = proposedCount;
2020-10-08 17:06:46 +08:00
beatmap.Update(hitObject);
break;
2020-05-27 11:38:39 +08:00
case IHasDuration endTimeHitObject:
var snappedTime = Math.Max(hitObject.StartTime, beatSnapProvider.SnapTime(time));
2020-02-05 15:35:07 +08:00
2021-02-05 22:39:14 +08:00
if (endTimeHitObject.EndTime == snappedTime || Precision.AlmostEquals(snappedTime, hitObject.StartTime, beatmap.GetBeatLengthAtTime(snappedTime)))
return;
2020-02-05 15:35:07 +08:00
2020-05-27 11:37:44 +08:00
endTimeHitObject.Duration = snappedTime - hitObject.StartTime;
2020-10-08 17:06:46 +08:00
beatmap.Update(hitObject);
break;
}
}
}
protected override void OnDragEnd(DragEndEvent e)
{
base.OnDragEnd(e);
OnDragHandled?.Invoke(null);
changeHandler?.EndChange();
}
2020-01-23 15:22:43 +08:00
}
/// <summary>
/// A circle with externalised end caps so it can take up the full width of a relative width area.
/// </summary>
2021-04-13 19:57:02 +08:00
public class ExtendableCircle : CompositeDrawable
{
2021-04-15 20:53:21 +08:00
private readonly Circle content;
2021-04-13 19:57:02 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => content.ReceivePositionalInputAt(screenSpacePos);
2021-04-13 19:57:02 +08:00
public override Quad ScreenSpaceDrawQuad => content.ScreenSpaceDrawQuad;
public ExtendableCircle()
{
2021-04-13 19:57:02 +08:00
Padding = new MarginPadding { Horizontal = -circle_size / 2f };
2021-04-15 20:53:21 +08:00
InternalChild = content = new Circle
{
2021-04-13 19:57:02 +08:00
RelativeSizeAxes = Axes.Both,
EdgeEffect = new EdgeEffectParameters
{
2021-04-13 19:57:02 +08:00
Type = EdgeEffectType.Shadow,
Radius = 5,
Colour = Color4.Black.Opacity(0.4f)
}
};
}
}
2020-01-23 15:22:43 +08:00
}
}