1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-09 09:37:42 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

538 lines
20 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
#nullable disable
2022-06-17 15:37:17 +08:00
2018-03-20 20:59:32 +08:00
using System;
using System.Diagnostics;
using System.Linq;
using Humanizer;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
2018-03-19 18:49:57 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
2018-11-06 17:28:22 +08:00
namespace osu.Game.Screens.Edit.Compose.Components
{
public class BeatDivisorControl : CompositeDrawable
{
2018-03-20 12:57:25 +08:00
private readonly BindableBeatDivisor beatDivisor = new BindableBeatDivisor();
2018-04-13 17:19:50 +08:00
public BeatDivisorControl(BindableBeatDivisor beatDivisor)
{
2018-03-19 19:30:07 +08:00
this.beatDivisor.BindTo(beatDivisor);
2018-03-20 12:57:25 +08:00
}
2018-04-13 17:19:50 +08:00
2018-03-20 12:57:25 +08:00
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
2018-03-20 12:57:25 +08:00
{
Masking = true;
2018-04-13 17:19:50 +08:00
InternalChildren = new Drawable[]
{
new Box
{
Name = "Main background",
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background3,
},
new GridContainer
{
RelativeSizeAxes = Axes.Both,
Content = new[]
{
new Drawable[]
{
2018-03-21 15:04:43 +08:00
new Container
{
RelativeSizeAxes = Axes.Both,
2018-03-21 15:04:43 +08:00
Children = new Drawable[]
{
new Box
{
Name = "Tick area background",
2018-03-21 15:04:43 +08:00
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background5,
2018-03-21 15:04:43 +08:00
},
new TickSliderBar(beatDivisor)
2018-03-21 15:04:43 +08:00
{
RelativeSizeAxes = Axes.Both,
}
}
}
},
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background3
},
new Container
{
RelativeSizeAxes = Axes.Both,
Child = new GridContainer
{
RelativeSizeAxes = Axes.Both,
Content = new[]
{
new Drawable[]
{
new ChevronButton
{
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.ChevronLeft,
Action = beatDivisor.Previous
},
new DivisorDisplay { BeatDivisor = { BindTarget = beatDivisor } },
new ChevronButton
{
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.ChevronRight,
Action = beatDivisor.Next
}
},
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 20),
new Dimension(),
new Dimension(GridSizeMode.Absolute, 20)
}
}
}
}
}
},
2018-03-21 15:04:43 +08:00
new Drawable[]
{
new TextFlowContainer(s => s.Font = s.Font.With(size: 14))
2018-03-21 15:04:43 +08:00
{
Padding = new MarginPadding { Horizontal = 15 },
Text = "beat snap",
2018-03-21 15:04:43 +08:00
RelativeSizeAxes = Axes.X,
TextAnchor = Anchor.TopCentre
},
},
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Child = new GridContainer
{
RelativeSizeAxes = Axes.Both,
Content = new[]
{
new Drawable[]
{
new ChevronButton
{
Icon = FontAwesome.Solid.ChevronLeft,
Action = () => cycleDivisorType(-1)
},
new DivisorTypeText { BeatDivisor = { BindTarget = beatDivisor } },
new ChevronButton
{
Icon = FontAwesome.Solid.ChevronRight,
Action = () => cycleDivisorType(1)
}
},
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 20),
new Dimension(),
new Dimension(GridSizeMode.Absolute, 20)
}
}
}
}
}
},
},
RowDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 30),
new Dimension(GridSizeMode.Absolute, 20),
new Dimension(GridSizeMode.Absolute, 15)
}
}
};
}
2018-04-13 17:19:50 +08:00
private void cycleDivisorType(int direction)
{
Debug.Assert(Math.Abs(direction) == 1);
int nextDivisorType = (int)beatDivisor.ValidDivisors.Value.Type + direction;
if (nextDivisorType > (int)BeatDivisorType.Triplets)
nextDivisorType = (int)BeatDivisorType.Common;
else if (nextDivisorType < (int)BeatDivisorType.Common)
nextDivisorType = (int)BeatDivisorType.Triplets;
switch ((BeatDivisorType)nextDivisorType)
{
case BeatDivisorType.Common:
beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.COMMON;
break;
case BeatDivisorType.Triplets:
beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.TRIPLETS;
break;
case BeatDivisorType.Custom:
beatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.Custom(beatDivisor.ValidDivisors.Value.Presets.Max());
break;
}
}
internal class DivisorDisplay : OsuAnimatedButton, IHasPopover
2018-03-19 18:49:57 +08:00
{
public BindableBeatDivisor BeatDivisor { get; } = new BindableBeatDivisor();
private readonly OsuSpriteText divisorText;
2018-04-13 17:19:50 +08:00
public DivisorDisplay()
2018-03-19 18:49:57 +08:00
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
AutoSizeAxes = Axes.Both;
Add(divisorText = new OsuSpriteText
{
Font = OsuFont.Default.With(size: 20),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Margin = new MarginPadding
{
Horizontal = 5
}
});
Action = this.ShowPopover;
2018-03-19 18:49:57 +08:00
}
2018-04-13 17:19:50 +08:00
2018-03-19 19:14:34 +08:00
[BackgroundDependencyLoader]
2018-03-20 12:57:25 +08:00
private void load(OsuColour colours)
2018-03-19 19:14:34 +08:00
{
divisorText.Colour = colours.BlueLighter;
2018-03-19 19:14:34 +08:00
}
2018-04-13 17:19:50 +08:00
2018-03-19 18:49:57 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
updateState();
}
private void updateState()
{
BeatDivisor.BindValueChanged(val => divisorText.Text = $"1/{val.NewValue}", true);
}
public Popover GetPopover() => new CustomDivisorPopover
{
BeatDivisor = { BindTarget = BeatDivisor }
};
}
internal class CustomDivisorPopover : OsuPopover
{
public BindableBeatDivisor BeatDivisor { get; } = new BindableBeatDivisor();
private readonly OsuNumberBox divisorTextBox;
public CustomDivisorPopover()
{
Child = new FillFlowContainer
{
Width = 150,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10),
Children = new Drawable[]
{
divisorTextBox = new OsuNumberBox
{
RelativeSizeAxes = Axes.X,
PlaceholderText = "Beat divisor"
},
new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Text = "Related divisors will be added to the list of presets."
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
BeatDivisor.BindValueChanged(_ => updateState(), true);
divisorTextBox.OnCommit += (_, __) => setPresets();
Schedule(() => GetContainingInputManager().ChangeFocus(divisorTextBox));
}
private void setPresets()
{
if (!int.TryParse(divisorTextBox.Text, out int divisor) || divisor < 1 || divisor > 64)
{
updateState();
return;
}
if (!BeatDivisor.ValidDivisors.Value.Presets.Contains(divisor))
{
if (BeatDivisorPresetCollection.COMMON.Presets.Contains(divisor))
BeatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.COMMON;
else if (BeatDivisorPresetCollection.TRIPLETS.Presets.Contains(divisor))
BeatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.TRIPLETS;
else
BeatDivisor.ValidDivisors.Value = BeatDivisorPresetCollection.Custom(divisor);
}
BeatDivisor.Value = divisor;
this.HidePopover();
}
private void updateState()
{
divisorTextBox.Text = BeatDivisor.Value.ToString();
}
}
private class DivisorTypeText : OsuSpriteText
{
public BindableBeatDivisor BeatDivisor { get; } = new BindableBeatDivisor();
public DivisorTypeText()
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
Font = OsuFont.Default.With(size: 14);
}
protected override void LoadComplete()
{
base.LoadComplete();
BeatDivisor.ValidDivisors.BindValueChanged(val => Text = val.NewValue.Type.Humanize(LetterCasing.LowerCase), true);
2018-03-19 18:49:57 +08:00
}
}
2018-04-13 17:19:50 +08:00
internal class ChevronButton : IconButton
{
public ChevronButton()
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
2018-04-13 17:19:50 +08:00
// Small offset to look a bit better centered along with the divisor text
Y = 1;
2018-04-13 17:19:50 +08:00
Size = new Vector2(20);
2018-03-19 19:14:34 +08:00
IconScale = new Vector2(0.6f);
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
IconColour = Color4.Black;
HoverColour = colours.Gray7;
FlashColour = colours.Gray9;
}
}
2018-04-13 17:19:50 +08:00
private class TickSliderBar : SliderBar<int>
{
2018-03-20 20:31:17 +08:00
private Marker marker;
2018-04-13 17:19:50 +08:00
[Resolved]
private OsuColour colours { get; set; }
private readonly BindableBeatDivisor beatDivisor;
2018-04-13 17:19:50 +08:00
public TickSliderBar(BindableBeatDivisor beatDivisor)
{
CurrentNumber.BindTo(this.beatDivisor = beatDivisor);
2018-04-13 17:19:50 +08:00
Padding = new MarginPadding { Horizontal = 5 };
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
beatDivisor.ValidDivisors.BindValueChanged(_ => updateDivisors(), true);
}
private void updateDivisors()
{
ClearInternal();
CurrentNumber.ValueChanged -= moveMarker;
foreach (int divisor in beatDivisor.ValidDivisors.Value.Presets)
{
AddInternal(new Tick(divisor)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Colour = BindableBeatDivisor.GetColourFor(divisor, colours),
X = getMappedPosition(divisor),
});
}
2018-04-13 17:19:50 +08:00
2018-03-20 20:31:17 +08:00
AddInternal(marker = new Marker());
CurrentNumber.ValueChanged += moveMarker;
CurrentNumber.TriggerChange();
}
private void moveMarker(ValueChangedEvent<int> divisor)
{
marker.MoveToX(getMappedPosition(divisor.NewValue), 100, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
protected override void UpdateValue(float value)
{
}
2018-04-13 17:19:50 +08:00
public override bool HandleNonPositionalInput => IsHovered && !CurrentNumber.Disabled;
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
{
2018-10-02 11:02:47 +08:00
switch (e.Key)
{
case Key.Right:
beatDivisor.Next();
2019-02-21 17:56:34 +08:00
OnUserChange(Current.Value);
return true;
2019-04-01 11:44:46 +08:00
case Key.Left:
beatDivisor.Previous();
2019-02-21 17:56:34 +08:00
OnUserChange(Current.Value);
return true;
2019-04-01 11:44:46 +08:00
default:
return false;
}
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-03-20 20:31:17 +08:00
{
marker.Active = true;
handleMouseInput(e.ScreenSpaceMousePosition);
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
2018-03-20 20:31:17 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void OnMouseUp(MouseUpEvent e)
2018-03-20 20:31:17 +08:00
{
marker.Active = false;
base.OnMouseUp(e);
2018-03-20 20:31:17 +08:00
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-03-20 20:31:17 +08:00
{
handleMouseInput(e.ScreenSpaceMousePosition);
2018-03-20 20:31:17 +08:00
return true;
}
2018-04-13 17:19:50 +08:00
protected override void OnDrag(DragEvent e)
2018-03-20 20:31:17 +08:00
{
handleMouseInput(e.ScreenSpaceMousePosition);
2018-03-20 20:31:17 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void OnDragEnd(DragEndEvent e)
{
handleMouseInput(e.ScreenSpaceMousePosition);
}
private void handleMouseInput(Vector2 screenSpaceMousePosition)
2018-03-20 20:31:17 +08:00
{
// copied from SliderBar so we can do custom spacing logic.
float xPosition = (ToLocalSpace(screenSpaceMousePosition).X - RangePadding) / UsableWidth;
2018-04-13 17:19:50 +08:00
CurrentNumber.Value = beatDivisor.ValidDivisors.Value.Presets.OrderBy(d => Math.Abs(getMappedPosition(d) - xPosition)).First();
2019-02-21 17:56:34 +08:00
OnUserChange(Current.Value);
2018-03-20 20:31:17 +08:00
}
2018-04-13 17:19:50 +08:00
private float getMappedPosition(float divisor) => MathF.Pow((divisor - 1) / (beatDivisor.ValidDivisors.Value.Presets.Last() - 1), 0.90f);
2018-04-13 17:19:50 +08:00
private class Tick : Circle
{
public Tick(int divisor)
{
Size = new Vector2(6f, 12) * BindableBeatDivisor.GetSize(divisor);
2018-03-20 20:31:17 +08:00
InternalChild = new Box { RelativeSizeAxes = Axes.Both };
}
}
2018-04-13 17:19:50 +08:00
private class Marker : CompositeDrawable
{
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
Colour = colourProvider.Background3;
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomCentre;
Size = new Vector2(8, 6.5f);
2018-04-13 17:19:50 +08:00
RelativePositionAxes = Axes.X;
2018-04-13 17:19:50 +08:00
InternalChildren = new Drawable[]
{
new Triangle
{
RelativeSizeAxes = Axes.Both,
EdgeSmoothness = new Vector2(1),
Colour = Color4.White,
}
};
}
2018-04-13 17:19:50 +08:00
2018-03-20 20:31:17 +08:00
private bool active;
2018-04-13 17:19:50 +08:00
2018-03-20 20:31:17 +08:00
public bool Active
{
get => active;
set
{
this.FadeColour(value ? colourProvider.Background1 : colourProvider.Background3, 500, Easing.OutQuint);
2018-03-20 20:31:17 +08:00
active = value;
}
}
}
}
}
}