Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the EditableLyric in the lyric editor. #2261

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Components.Lyrics;

public partial class BlueprintLayer : BaseLayer
public partial class BlueprintLayer : Layer
{
private readonly IBindable<EditorModeWithEditStep> bindableModeWithEditStep = new Bindable<EditorModeWithEditStep>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Components.Lyrics;

public partial class CaretLayer : BaseLayer
public partial class CaretLayer : Layer
{
private readonly IBindable<ICaretPositionAlgorithm?> bindableCaretPositionAlgorithm = new Bindable<ICaretPositionAlgorithm?>();

Expand All @@ -28,58 +28,58 @@ public CaretLayer(Lyric lyric)
{
bindableCaretPositionAlgorithm.BindValueChanged(e =>
{
updateDrawableCaret(e.NewValue, DrawableCaretType.HoverCaret);
updateDrawableCaret(e.NewValue, DrawableCaretType.Caret);
updateDrawableCaret(e.NewValue, DrawableCaretState.Idle);
updateDrawableCaret(e.NewValue, DrawableCaretState.Hover);
}, true);

bindableHoverCaretPosition.BindValueChanged(e =>
{
applyTheCaretPosition(e.NewValue, DrawableCaretType.HoverCaret);
applyTheCaretPosition(e.NewValue, DrawableCaretState.Hover);
}, true);

bindableCaretPosition.BindValueChanged(e =>
{
applyTheCaretPosition(e.NewValue, DrawableCaretType.Caret);
applyTheCaretPosition(e.NewValue, DrawableCaretState.Idle);
}, true);

bindableRangeCaretPosition.BindValueChanged(e =>
{
applyRangeCaretPosition(e.NewValue, DrawableCaretType.Caret);
applyRangeCaretPosition(e.NewValue, DrawableCaretState.Idle);
}, true);
}

private void updateDrawableCaret(ICaretPositionAlgorithm? algorithm, DrawableCaretType type)
private void updateDrawableCaret(ICaretPositionAlgorithm? algorithm, DrawableCaretState state)
{
var oldCaret = getDrawableCaret(type);
var oldCaret = getDrawableCaret(state);
if (oldCaret != null)
RemoveInternal(oldCaret, true);

var caret = createCaret(algorithm, type);
var caret = createCaret(algorithm, state);
if (caret == null)
return;

caret.Hide();

AddInternal(caret);

static DrawableCaret? createCaret(ICaretPositionAlgorithm? algorithm, DrawableCaretType type) =>
static DrawableCaret? createCaret(ICaretPositionAlgorithm? algorithm, DrawableCaretState state) =>
algorithm?.GetCaretPositionType() switch
{
Type t when t == typeof(CreateRubyTagCaretPosition) => new DrawableCreateRubyTagCaret(type),
Type t when t == typeof(CuttingCaretPosition) => new DrawableCuttingCaret(type),
Type t when t == typeof(RecordingTimeTagCaretPosition) => new DrawableRecordingTimeTagCaret(type),
Type t when t == typeof(CreateRemoveTimeTagCaretPosition) => new DrawableCreateRemoveTimeTagCaret(type),
Type t when t == typeof(TypingCaretPosition) => new DrawableTypingCaret(type),
Type t when t == typeof(CreateRubyTagCaretPosition) => new DrawableCreateRubyTagCaret(state),
Type t when t == typeof(CuttingCaretPosition) => new DrawableCuttingCaret(state),
Type t when t == typeof(RecordingTimeTagCaretPosition) => new DrawableRecordingTimeTagCaret(state),
Type t when t == typeof(CreateRemoveTimeTagCaretPosition) => new DrawableCreateRemoveTimeTagCaret(state),
Type t when t == typeof(TypingCaretPosition) => new DrawableTypingCaret(state),
_ => null,
};
}

private void applyTheCaretPosition(ICaretPosition? position, DrawableCaretType type)
private void applyTheCaretPosition(ICaretPosition? position, DrawableCaretState state)
{
if (position == null)
return;

var caret = getDrawableCaret(type);
var caret = getDrawableCaret(state);
if (caret == null)
return;

Expand All @@ -93,12 +93,12 @@ private void applyTheCaretPosition(ICaretPosition? position, DrawableCaretType t
caret.ApplyCaretPosition(position);
}

private void applyRangeCaretPosition(RangeCaretPosition? rangeCaretPosition, DrawableCaretType type)
private void applyRangeCaretPosition(RangeCaretPosition? rangeCaretPosition, DrawableCaretState state)
{
if (rangeCaretPosition == null)
return;

var caret = getDrawableCaret(type);
var caret = getDrawableCaret(state);
if (caret == null)
throw new InvalidOperationException("Should be able to get the drawable caret.");

Expand All @@ -116,8 +116,8 @@ private void applyRangeCaretPosition(RangeCaretPosition? rangeCaretPosition, Dra
rangeIndexDrawableCaret.ApplyRangeCaretPosition(rangeCaretPosition);
}

private DrawableCaret? getDrawableCaret(DrawableCaretType type)
=> InternalChildren.OfType<DrawableCaret>().FirstOrDefault(x => x.Type == type);
private DrawableCaret? getDrawableCaret(DrawableCaretState state)
=> InternalChildren.OfType<DrawableCaret>().FirstOrDefault(x => x.State == state);

[BackgroundDependencyLoader]
private void load(ILyricCaretState lyricCaretState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,14 @@
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.CaretPosition;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States;

namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Components.Lyrics.Carets;

public abstract partial class DrawableRangeCaret<TCaretPosition> : DrawableCaret, ICanAcceptRangeIndex
where TCaretPosition : struct, IIndexCaretPosition
{
protected DrawableRangeCaret(DrawableCaretType type)
: base(type)
{
}

public sealed override void ApplyCaretPosition(ICaretPosition caret)
{
if (caret is not TCaretPosition tCaret)
throw new InvalidCastException();

ApplyCaretPosition(tCaret);
}

public void ApplyRangeCaretPosition(RangeCaretPosition rangeCaretPosition)
{
ApplyRangeCaretPosition(rangeCaretPosition.GetRangeCaretPositionWithType<TCaretPosition>());
}

protected abstract void ApplyCaretPosition(TCaretPosition caret);

protected abstract void ApplyRangeCaretPosition(RangeCaretPosition<TCaretPosition> caret);
}

public abstract partial class DrawableCaret<TCaretPosition> : DrawableCaret
where TCaretPosition : struct, ICaretPosition
{
protected DrawableCaret(DrawableCaretType type)
: base(type)
protected DrawableCaret(DrawableCaretState state)
: base(state)
{
}

Expand All @@ -63,19 +36,19 @@ public abstract partial class DrawableCaret : CompositeDrawable
[Resolved]
protected IPreviewLyricPositionProvider LyricPositionProvider { get; private set; } = null!;

public readonly DrawableCaretType Type;
public readonly DrawableCaretState State;

protected DrawableCaret(DrawableCaretType type)
protected DrawableCaret(DrawableCaretState state)
{
Type = type;
State = state;
}

protected static float GetAlpha(DrawableCaretType type) =>
type switch
protected static float GetAlpha(DrawableCaretState state) =>
state switch
{
DrawableCaretType.HoverCaret => 0.5f,
DrawableCaretType.Caret => 1,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null),
DrawableCaretState.Idle => 1,
DrawableCaretState.Hover => 0.5f,
_ => throw new ArgumentOutOfRangeException(nameof(state), state, null),
};

public abstract void ApplyCaretPosition(ICaretPosition caret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Components.Lyrics.Carets;

public enum DrawableCaretType
public enum DrawableCaretState
{
HoverCaret,
Idle,

Caret,
Hover,
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public partial class DrawableCreateRemoveTimeTagCaret : DrawableCaret<CreateRemo
private readonly TimeTagsInfo startTimeTagInfo;
private readonly TimeTagsInfo endTimeTagInfo;

public DrawableCreateRemoveTimeTagCaret(DrawableCaretType type)
: base(type)
public DrawableCreateRemoveTimeTagCaret(DrawableCaretState state)
: base(state)
{
// todo: should re-design the drawable caret.
InternalChildren = new Drawable[]
Expand All @@ -54,14 +54,14 @@ public DrawableCreateRemoveTimeTagCaret(DrawableCaretType type)
X = 18,
Anchor = Anchor.BottomLeft,
Origin = Anchor.CentreRight,
Alpha = GetAlpha(type),
Alpha = GetAlpha(state),
},
endTimeTagInfo = new TimeTagsInfo(TextIndex.IndexState.End)
{
X = -18,
Anchor = Anchor.BottomRight,
Origin = Anchor.CentreLeft,
Alpha = GetAlpha(type),
Alpha = GetAlpha(state),
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public partial class DrawableCreateRubyTagCaret : DrawableRangeCaret<CreateRubyT

private readonly SpriteIcon icon;

public DrawableCreateRubyTagCaret(DrawableCaretType type)
: base(type)
public DrawableCreateRubyTagCaret(DrawableCaretState state)
: base(state)
{
InternalChildren = new Drawable[]
{
Expand All @@ -44,7 +44,7 @@ public DrawableCreateRubyTagCaret(DrawableCaretType type)
BorderThickness = border_spacing,
BorderColour = Colour4.White,
RelativeSizeAxes = Axes.Both,
Alpha = GetAlpha(type),
Alpha = GetAlpha(state),
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Expand All @@ -58,7 +58,7 @@ public DrawableCreateRubyTagCaret(DrawableCaretType type)
Origin = Anchor.BottomLeft,
Icon = FontAwesome.Solid.PlusCircle,
Size = new Vector2(15),
Alpha = GetAlpha(type),
Alpha = GetAlpha(state),
},
};
}
Expand All @@ -75,7 +75,7 @@ private void load(OsuColour colours)
protected override void ApplyCaretPosition(CreateRubyTagCaretPosition caret)
{
// should not show the hover caret if already contains the selected range.
if (Type == DrawableCaretType.HoverCaret)
if (State == DrawableCaretState.Hover)
{
bool isClickToThisCaret = lyricCaretState.CaretPosition?.Lyric == caret.Lyric;
bool isDraggingToThisCaret = lyricCaretState.RangeCaretPosition?.IsInRange(caret.Lyric) ?? false;
Expand All @@ -94,7 +94,7 @@ protected override void ApplyCaretPosition(CreateRubyTagCaretPosition caret)
changeTheSizeByRect(rect);

// should not continuous showing the caret position if move the caret by keyboard.
if (Type == DrawableCaretType.Caret)
if (State == DrawableCaretState.Idle)
{
// todo: should wait until layer is attached to the parent.
// use quick way to fix this because it will cause crash if open the
Expand All @@ -104,7 +104,7 @@ protected override void ApplyCaretPosition(CreateRubyTagCaretPosition caret)

protected override bool OnClick(ClickEvent e)
{
if (Type == DrawableCaretType.HoverCaret)
if (State == DrawableCaretState.Hover)
return false;

this.ShowPopover();
Expand All @@ -119,7 +119,7 @@ protected override void ApplyRangeCaretPosition(RangeCaretPosition<CreateRubyTag
var rect = RectangleF.Union(LyricPositionProvider.GetRectByCharIndex(startCharIndex), LyricPositionProvider.GetRectByCharIndex(endCharIndex));
changeTheSizeByRect(rect);

if (Type == DrawableCaretType.Caret && caret.DraggingState == RangeCaretDraggingState.EndDrag)
if (State == DrawableCaretState.Idle && caret.DraggingState == RangeCaretDraggingState.EndDrag)
this.ShowPopover();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public partial class DrawableCuttingCaret : DrawableCaret<CuttingCaretPosition>
private readonly Container splitter;
private readonly SpriteIcon splitIcon;

public DrawableCuttingCaret(DrawableCaretType type)
: base(type)
public DrawableCuttingCaret(DrawableCaretState state)
: base(state)
{
Width = 10;
Origin = Anchor.TopCentre;
Expand All @@ -39,7 +39,7 @@ public DrawableCuttingCaret(DrawableCaretType type)
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Alpha = GetAlpha(type),
Alpha = GetAlpha(state),
Children = new Drawable[]
{
new Triangle
Expand All @@ -61,18 +61,18 @@ public DrawableCuttingCaret(DrawableCaretType type)
},
};

switch (type)
switch (state)
{
case DrawableCaretType.HoverCaret:
splitIcon.Show();
case DrawableCaretState.Idle:
splitIcon.Hide();
break;

case DrawableCaretType.Caret:
splitIcon.Hide();
case DrawableCaretState.Hover:
splitIcon.Show();
break;

default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) andy840119 <andy840119@gmail.com>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.CaretPosition;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States;

namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Components.Lyrics.Carets;

public abstract partial class DrawableRangeCaret<TCaretPosition> : DrawableCaret, ICanAcceptRangeIndex
where TCaretPosition : struct, IIndexCaretPosition
{
protected DrawableRangeCaret(DrawableCaretState state)
: base(state)
{
}

public sealed override void ApplyCaretPosition(ICaretPosition caret)
{
if (caret is not TCaretPosition tCaret)
throw new InvalidCastException();

ApplyCaretPosition(tCaret);
}

public void ApplyRangeCaretPosition(RangeCaretPosition rangeCaretPosition)
{
ApplyRangeCaretPosition(rangeCaretPosition.GetRangeCaretPositionWithType<TCaretPosition>());
}

protected abstract void ApplyCaretPosition(TCaretPosition caret);

protected abstract void ApplyRangeCaretPosition(RangeCaretPosition<TCaretPosition> caret);
}
Loading