From 34b82e867a5013abee10d6afc7a0a4d2b2e557f4 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Fri, 16 Aug 2024 23:07:10 +0800 Subject: [PATCH 1/2] Move PreviewKaraokeSpriteText into the child of LyricLayer. And LyricLayer should: - handle the offset of the PreviewKaraokeSpriteText. - implement the IPreviewLyricPositionProvider and provide the position info. --- .../Components/Lyrics/InteractableLyric.cs | 30 ++++----- .../Content/Components/Lyrics/LyricLayer.cs | 64 ++++++++++++++++++- .../Lyrics/PreviewKaraokeSpriteText.cs | 21 ------ .../Lyrics/Content/Compose/LyricEditor.cs | 6 +- 4 files changed, 76 insertions(+), 45 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs index ac6086c92..ef786a573 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs @@ -12,7 +12,6 @@ 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; @@ -20,9 +19,6 @@ namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Compone [Cached(typeof(IInteractableLyricState))] public sealed partial class InteractableLyric : CompositeDrawable, IHasTooltip, IInteractableLyricState { - [Cached(typeof(IPreviewLyricPositionProvider))] - private readonly PreviewKaraokeSpriteText karaokeSpriteText; - private readonly IBindable bindableMode = new Bindable(); private readonly IBindable bindableLyricPropertyWritableVersion; @@ -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(); @@ -62,16 +51,21 @@ public IEnumerable Layers { AddRangeInternal(value); - // todo: should apply proxy instead, but it let disable edit state not working. var lyricLayers = value.OfType().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().Single(); + baseDependencies.CacheAs(lyricLayer); + return baseDependencies; } public void TriggerDisallowEditEffect() @@ -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() diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/LyricLayer.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/LyricLayer.cs index 744cf4a2d..3e1a07fb5 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/LyricLayer.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/LyricLayer.cs @@ -1,26 +1,47 @@ // Copyright (c) andy840119 . 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? 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) @@ -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 } diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/PreviewKaraokeSpriteText.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/PreviewKaraokeSpriteText.cs index 248f1d554..141bf10aa 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/PreviewKaraokeSpriteText.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/PreviewKaraokeSpriteText.cs @@ -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++) { @@ -87,9 +84,6 @@ Tuple 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)); @@ -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++) { @@ -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)); @@ -154,9 +142,6 @@ 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 @@ -164,9 +149,6 @@ private RectangleF getRectByCharIndicator(int charIndex) #region Time tag public TimeTag? GetTimeTagByPosition(float position) - => getTimeTagByPosition(position - Position.X); - - private TimeTag? getTimeTagByPosition(float position) { var hoverIndex = getHoverIndex(); if (hoverIndex == null) @@ -202,9 +184,6 @@ Tuple 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); diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Compose/LyricEditor.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Compose/LyricEditor.cs index bbaa6fd6b..f5145abeb 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Compose/LyricEditor.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Compose/LyricEditor.cs @@ -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; @@ -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), From 5b577baa06727663fc63018072b458acda0bba0c Mon Sep 17 00:00:00 2001 From: andy840119 Date: Fri, 16 Aug 2024 23:11:05 +0800 Subject: [PATCH 2/2] Can jest get from the layers property. --- .../Lyrics/Content/Components/Lyrics/InteractableLyric.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs index ef786a573..9840c4761 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Content/Components/Lyrics/InteractableLyric.cs @@ -70,7 +70,7 @@ protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnl public void TriggerDisallowEditEffect() { - InternalChildren.OfType().ForEach(x => x.TriggerDisallowEditEffect(bindableMode.Value)); + Layers.ForEach(x => x.TriggerDisallowEditEffect(bindableMode.Value)); } [BackgroundDependencyLoader] @@ -86,7 +86,7 @@ private void triggerWritableVersionChanged() // adjust the style. bool editable = lockReason == null; - InternalChildren.OfType().ForEach(x => x.UpdateDisableEditState(editable)); + Layers.ForEach(x => x.UpdateDisableEditState(editable)); } public LocalisableString TooltipText => lockReason ?? string.Empty;