Skip to content

Commit

Permalink
Move PreviewKaraokeSpriteText into the child of LyricLayer.
Browse files Browse the repository at this point in the history
And LyricLayer should:
- handle the offset of the PreviewKaraokeSpriteText.
- implement the IPreviewLyricPositionProvider and provide the position info.
  • Loading branch information
andy840119 committed Aug 16, 2024
1 parent a177523 commit 34b82e8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
using osu.Framework.Localisation;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Screens.Edit;
using osuTK;

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

[Cached(typeof(IInteractableLyricState))]
public sealed partial class InteractableLyric : CompositeDrawable, IHasTooltip, IInteractableLyricState
{
[Cached(typeof(IPreviewLyricPositionProvider))]
private readonly PreviewKaraokeSpriteText karaokeSpriteText;

private readonly IBindable<LyricEditorMode> bindableMode = new Bindable<LyricEditorMode>();
private readonly IBindable<int> bindableLyricPropertyWritableVersion;

Expand All @@ -37,13 +33,6 @@ public InteractableLyric(Lyric lyric)

bindableLyricPropertyWritableVersion = lyric.LyricPropertyWritableVersion.GetBoundCopy();

karaokeSpriteText = new PreviewKaraokeSpriteText(lyric);

karaokeSpriteText.SizeChanged = (size) =>
{
TextSizeChanged?.Invoke(this, size);
};

bindableMode.BindValueChanged(x =>
{
triggerWritableVersionChanged();
Expand All @@ -62,16 +51,21 @@ public IEnumerable<Layer> Layers
{
AddRangeInternal(value);

// todo: should apply proxy instead, but it let disable edit state not working.
var lyricLayers = value.OfType<LyricLayer>().Single();
lyricLayers.ApplyDrawableLyric(karaokeSpriteText);
lyricLayers.SizeChanged = size =>
{
TextSizeChanged?.Invoke(this, size);
};
}
}

public Vector2 LyricPosition
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
get => karaokeSpriteText.Position;
set => karaokeSpriteText.Position = value;
var baseDependencies = new DependencyContainer(base.CreateChildDependencies(parent));

var lyricLayer = Layers.OfType<LyricLayer>().Single();
baseDependencies.CacheAs<IPreviewLyricPositionProvider>(lyricLayer);
return baseDependencies;
}

public void TriggerDisallowEditEffect()
Expand All @@ -80,11 +74,9 @@ public void TriggerDisallowEditEffect()
}

[BackgroundDependencyLoader]
private void load(EditorClock clock, ILyricEditorState state)
private void load(ILyricEditorState state)
{
bindableMode.BindTo(state.BindableMode);

karaokeSpriteText.Clock = clock;
}

private void triggerWritableVersionChanged()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Primitives;
using osu.Game.Graphics;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Screens.Edit;
using osuTK;

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

public partial class LyricLayer : Layer
public partial class LyricLayer : Layer, IPreviewLyricPositionProvider
{
[Resolved]
private OsuColour colours { get; set; } = null!;

private readonly PreviewKaraokeSpriteText previewKaraokeSpriteText;

public Action<Vector2>? SizeChanged;

public LyricLayer(Lyric lyric)
: base(lyric)
{
InternalChild = previewKaraokeSpriteText = new PreviewKaraokeSpriteText(lyric);

previewKaraokeSpriteText.SizeChanged = size =>
{
SizeChanged?.Invoke(size);
};
}

[BackgroundDependencyLoader]
private void load(EditorClock clock)
{
previewKaraokeSpriteText.Clock = clock;
}

public void ApplyDrawableLyric(Drawable drawable)
public Vector2 LyricPosition
{
InternalChild = drawable;
get => previewKaraokeSpriteText.Position;
set => previewKaraokeSpriteText.Position = value;
}

public override void UpdateDisableEditState(bool editable)
Expand All @@ -32,4 +53,41 @@ public override void TriggerDisallowEditEffect(LyricEditorMode editorMode)
{
this.FlashColour(colours.Red, 200);
}

#region Text char index

public int? GetCharIndexByPosition(float position)
=> previewKaraokeSpriteText.GetCharIndexByPosition(position - LyricPosition.X);

public RectangleF GetRectByCharIndex(int charIndex)
=> previewKaraokeSpriteText.GetRectByCharIndex(charIndex).Offset(LyricPosition);

#endregion

#region Text indicator

public int GetCharIndicatorByPosition(float position)
=> previewKaraokeSpriteText.GetCharIndicatorByPosition(position - LyricPosition.X);

public RectangleF GetRectByCharIndicator(int charIndex)
=> previewKaraokeSpriteText.GetRectByCharIndicator(charIndex).Offset(LyricPosition);

#endregion

#region Ruby tag

public RectangleF? GetRubyTagByPosition(RubyTag rubyTag)
=> previewKaraokeSpriteText.GetRubyTagByPosition(rubyTag)?.Offset(LyricPosition);

#endregion

#region Time tag

public TimeTag? GetTimeTagByPosition(float position)
=> previewKaraokeSpriteText.GetTimeTagByPosition(position - LyricPosition.X);

public Vector2 GetPositionByTimeTag(TimeTag timeTag)
=> previewKaraokeSpriteText.GetPositionByTimeTag(timeTag) + LyricPosition;

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ private void triggerSizeChangedEvent()
#region Text char index

public int? GetCharIndexByPosition(float position)
=> getCharIndexByPosition(position - Position.X);

private int? getCharIndexByPosition(float position)
{
for (int i = 0; i < Text.Length; i++)
{
Expand All @@ -87,9 +84,6 @@ Tuple<double, double> getTriggerPositionByTimeIndex(int charIndex)
}

public RectangleF GetRectByCharIndex(int charIndex)
=> getRectByCharIndex(charIndex).Offset(Position);

private RectangleF getRectByCharIndex(int charIndex)
{
if (charIndex < 0 || charIndex >= Text.Length)
throw new ArgumentOutOfRangeException(nameof(charIndex));
Expand All @@ -102,9 +96,6 @@ private RectangleF getRectByCharIndex(int charIndex)
#region Text indicator

public int GetCharIndicatorByPosition(float position)
=> getCharIndicatorByPosition(position - Position.X);

private int getCharIndicatorByPosition(float position)
{
for (int i = 0; i < Text.Length; i++)
{
Expand All @@ -123,9 +114,6 @@ float getTriggerPositionByTimeIndex(int charIndex)
}

public RectangleF GetRectByCharIndicator(int charIndex)
=> getRectByCharIndicator(charIndex).Offset(Position);

private RectangleF getRectByCharIndicator(int charIndex)
{
if (charIndex < 0 || charIndex > Text.Length)
throw new ArgumentOutOfRangeException(nameof(charIndex));
Expand Down Expand Up @@ -154,19 +142,13 @@ private RectangleF getRectByCharIndicator(int charIndex)
#region Ruby tag

public RectangleF? GetRubyTagByPosition(RubyTag rubyTag) =>
getRubyTagByPosition(rubyTag)?.Offset(Position);

private RectangleF? getRubyTagByPosition(RubyTag rubyTag) =>
spriteText.GetRubyTagPosition(rubyTag);

#endregion

#region Time tag

public TimeTag? GetTimeTagByPosition(float position)
=> getTimeTagByPosition(position - Position.X);

private TimeTag? getTimeTagByPosition(float position)
{
var hoverIndex = getHoverIndex();
if (hoverIndex == null)
Expand Down Expand Up @@ -202,9 +184,6 @@ Tuple<float, float> getTriggerRange(TextIndex textIndex)
}

public Vector2 GetPositionByTimeTag(TimeTag timeTag)
=> getPositionByTimeTag(timeTag) + Position;

private Vector2 getPositionByTimeTag(TimeTag timeTag)
{
var basePosition = spriteText.GetTimeTagPosition(timeTag.Index);
float extraPosition = extraSpacing(HitObject.TimeTags, timeTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public LyricEditor()
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
LyricPosition = new Vector2(border),
TextSizeChanged = (self, size) =>
{
self.Width = size.X + border * 2;
Expand All @@ -62,7 +61,10 @@ public LyricEditor()
{
Spacing = 10,
},
new LyricLayer(lyric),
new LyricLayer(lyric)
{
LyricPosition = new Vector2(border),
},
new EditLyricLayer(lyric),
new TimeTagLayer(lyric),
new CaretLayer(lyric),
Expand Down

0 comments on commit 34b82e8

Please sign in to comment.