Skip to content

Commit

Permalink
Merge pull request #2272 from andy840119/restrict-hover-y-position-in…
Browse files Browse the repository at this point in the history
…-lyric-provider

Restrict hover y position in lyric provider.
  • Loading branch information
andy840119 authored Aug 16, 2024
2 parents 1aa4435 + 9b39895 commit 9cfd5fd
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,29 @@ private void load(OsuColour colour)
RelativeSizeAxes = Axes.Both,
Colour = colour.BlueDarker,
},
karaokeSpriteText = new PreviewKaraokeSpriteText(lyric)
new Container
{
Position = new Vector2(24, 8),
},
mask = new Container
{
Masking = true,
BorderThickness = 1,
BorderColour = colour.RedDarker,
Child = new Box
Padding = new MarginPadding
{
Vertical = 8,
Horizontal = 24,
},
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
RelativeSizeAxes = Axes.Both,
Colour = colour.RedDarker,
Alpha = 0.3f,
karaokeSpriteText = new PreviewKaraokeSpriteText(lyric),
mask = new Container
{
Masking = true,
BorderThickness = 1,
BorderColour = colour.RedDarker,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colour.RedDarker,
Alpha = 0.3f,
},
},
},
},
spriteText = new OsuSpriteText
Expand Down Expand Up @@ -105,7 +114,7 @@ public void TestGetCharIndexByPosition()
triggerUpdate(() =>
{
var mousePosition = getMousePosition();
int? charIndex = karaokeSpriteText.GetCharIndexByPosition(mousePosition.X);
int? charIndex = karaokeSpriteText.GetCharIndexByPosition(mousePosition);
updateText(charIndex.ToString());

if (charIndex == null)
Expand Down Expand Up @@ -147,11 +156,18 @@ public void TestGetCharIndicatorByPosition()
triggerUpdate(() =>
{
var mousePosition = getMousePosition();
int charIndex = karaokeSpriteText.GetCharIndicatorByPosition(mousePosition.X);
int? charIndex = karaokeSpriteText.GetCharIndicatorByPosition(mousePosition);
updateText(charIndex.ToString());

var position = karaokeSpriteText.GetRectByCharIndicator(charIndex);
showPosition(position);
if (charIndex == null)
{
hidePosition();
}
else
{
var position = karaokeSpriteText.GetRectByCharIndicator(charIndex.Value);
showPosition(position);
}
});
});
}
Expand Down Expand Up @@ -199,7 +215,7 @@ public void TestGetTimeTagByPosition()
triggerUpdate(() =>
{
var mousePosition = getMousePosition();
var timeTag = karaokeSpriteText.GetTimeTagByPosition(mousePosition.X);
var timeTag = karaokeSpriteText.GetTimeTagByPosition(mousePosition);
updateText(timeTag != null ? TimeTagUtils.FormattedString(timeTag) : null);

if (timeTag == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ void setRubyTagIndex(RubyTag rubyTag, int? startPosition, int? endPosition)
switch (anchor)
{
case Anchor.CentreLeft:
float leftPosition = rect.Value.Left + offset;
var leftPosition = rect.Value.BottomLeft + new Vector2(offset, 0);
return previewLyricPositionProvider.GetCharIndexByPosition(leftPosition);

case Anchor.CentreRight:
float rightPosition = rect.Value.Right + offset;
var rightPosition = rect.Value.BottomRight + new Vector2(offset, 0);
return previewLyricPositionProvider.GetCharIndexByPosition(rightPosition);

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ protected override void OnDragEnd(DragEndEvent e)
private object? getCaretIndexByPosition(UIEvent mouseEvent)
{
var algorithm = lyricCaretState.CaretPositionAlgorithm;
float xPosition = ToLocalSpace(mouseEvent.ScreenSpaceMousePosition).X;
var position = ToLocalSpace(mouseEvent.ScreenSpaceMousePosition);
return algorithm switch
{
CuttingCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndicatorByPosition(xPosition),
TypingCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndicatorByPosition(xPosition),
CuttingCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndicatorByPosition(position),
TypingCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndicatorByPosition(position),
NavigateCaretPositionAlgorithm => null,
CreateRubyTagCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndexByPosition(xPosition),
CreateRemoveTimeTagCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndexByPosition(xPosition),
CreateRubyTagCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndexByPosition(position),
CreateRemoveTimeTagCaretPositionAlgorithm => previewLyricPositionProvider.GetCharIndexByPosition(position),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Content.Compone

public interface IPreviewLyricPositionProvider
{
int? GetCharIndexByPosition(float position);
int? GetCharIndexByPosition(Vector2 position);

RectangleF GetRectByCharIndex(int charIndex);

int GetCharIndicatorByPosition(float position);
int? GetCharIndicatorByPosition(Vector2 position);

RectangleF GetRectByCharIndicator(int charIndex);
RectangleF GetRectByCharIndicator(int gapIndex);

RectangleF? GetRubyTagByPosition(RubyTag rubyTag);

TimeTag? GetTimeTagByPosition(float position);
TimeTag? GetTimeTagByPosition(Vector2 position);

Vector2 GetPositionByTimeTag(TimeTag timeTag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public override void TriggerDisallowEditEffect(LyricEditorMode editorMode)

#region Text char index

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

public RectangleF GetRectByCharIndex(int charIndex)
=> previewKaraokeSpriteText.GetRectByCharIndex(charIndex).Offset(LyricPosition);
Expand All @@ -66,11 +66,11 @@ public RectangleF GetRectByCharIndex(int charIndex)

#region Text indicator

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

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

#endregion

Expand All @@ -83,8 +83,8 @@ public RectangleF GetRectByCharIndicator(int charIndex)

#region Time tag

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

public Vector2 GetPositionByTimeTag(TimeTag timeTag)
=> previewKaraokeSpriteText.GetPositionByTimeTag(timeTag) + LyricPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,16 @@ private void triggerSizeChangedEvent()

#region Text char index

public int? GetCharIndexByPosition(float position)
public int? GetCharIndexByPosition(Vector2 position)
{
for (int i = 0; i < Text.Length; i++)
{
(double startX, double endX) = getTriggerPositionByTimeIndex(i);
if (position >= startX && position <= endX)
var rectangle = spriteText.GetCharacterDrawRectangle(i);
if (rectangle.Contains(position))
return i;
}

return null;

Tuple<double, double> getTriggerPositionByTimeIndex(int charIndex)
{
var rectangle = spriteText.GetCharacterDrawRectangle(charIndex);
return new Tuple<double, double>(rectangle.Left, rectangle.Right);
}
}

public RectangleF GetRectByCharIndex(int charIndex)
Expand All @@ -95,45 +89,64 @@ public RectangleF GetRectByCharIndex(int charIndex)

#region Text indicator

public int GetCharIndicatorByPosition(float position)
public int? GetCharIndicatorByPosition(Vector2 position)
{
for (int i = 0; i < Text.Length; i++)
for (int i = 0; i < Text.Length + 1; i++)
{
float textCenterPosition = getTriggerPositionByTimeIndex(i);
if (position < textCenterPosition)
var rect = getTriggerPositionByTimeIndex(i);
if (rect.Contains(position))
return i;
}

return Text.Length;
return null;

float getTriggerPositionByTimeIndex(int charIndex)
RectangleF getTriggerPositionByTimeIndex(int gapIndex)
{
var rectangle = spriteText.GetCharacterDrawRectangle(charIndex);
return rectangle.Centre.X;
if (gapIndex == 0)
{
var rectangle = spriteText.GetCharacterDrawRectangle(gapIndex);
return new RectangleF(rectangle.Left, rectangle.Top, rectangle.Width / 2, rectangle.Height);
}

if (gapIndex == Text.Length)
{
var rectangle = spriteText.GetCharacterDrawRectangle(gapIndex - 1);
return new RectangleF(rectangle.Centre.X, rectangle.Top, rectangle.Width / 2, rectangle.Height);
}

var leftRectangle = spriteText.GetCharacterDrawRectangle(gapIndex - 1);
var rightRectangle = spriteText.GetCharacterDrawRectangle(gapIndex);

float x = leftRectangle.Centre.X;
float y = Math.Min(leftRectangle.Y, rightRectangle.Y);
float width = rightRectangle.Centre.X - leftRectangle.Centre.X;
float height = Math.Max(leftRectangle.Height, rightRectangle.Height);

return new RectangleF(x, y, width, height);
}
}

public RectangleF GetRectByCharIndicator(int charIndex)
public RectangleF GetRectByCharIndicator(int gapIndex)
{
if (charIndex < 0 || charIndex > Text.Length)
throw new ArgumentOutOfRangeException(nameof(charIndex));
if (gapIndex < 0 || gapIndex > Text.Length)
throw new ArgumentOutOfRangeException(nameof(gapIndex));

const float min_spacing_width = 1;

if (charIndex == 0)
if (gapIndex == 0)
{
var referenceRectangle = spriteText.GetCharacterDrawRectangle(charIndex);
var referenceRectangle = spriteText.GetCharacterDrawRectangle(gapIndex);
return new RectangleF(referenceRectangle.X - min_spacing_width, referenceRectangle.Y, min_spacing_width, referenceRectangle.Height);
}

if (charIndex == Text.Length)
if (gapIndex == Text.Length)
{
var referenceRectangle = spriteText.GetCharacterDrawRectangle(charIndex - 1);
var referenceRectangle = spriteText.GetCharacterDrawRectangle(gapIndex - 1);
return new RectangleF(referenceRectangle.Right, referenceRectangle.Top, min_spacing_width, referenceRectangle.Height);
}

var leftRectangle = spriteText.GetCharacterDrawRectangle(charIndex - 1);
var rightRectangle = spriteText.GetCharacterDrawRectangle(charIndex);
var leftRectangle = spriteText.GetCharacterDrawRectangle(gapIndex - 1);
var rightRectangle = spriteText.GetCharacterDrawRectangle(gapIndex);
return new RectangleF(leftRectangle.Right, leftRectangle.Top, rightRectangle.X - leftRectangle.Right, leftRectangle.Y);
}

Expand All @@ -148,7 +161,7 @@ public RectangleF GetRectByCharIndicator(int charIndex)

#region Time tag

public TimeTag? GetTimeTagByPosition(float position)
public TimeTag? GetTimeTagByPosition(Vector2 position)
{
var hoverIndex = getHoverIndex();
if (hoverIndex == null)
Expand All @@ -165,20 +178,20 @@ public RectangleF GetRectByCharIndicator(int charIndex)
{
var textIndex = new TextIndex(i, indexState);
var triggerRange = getTriggerRange(textIndex);
if (position >= triggerRange.Item1 && position <= triggerRange.Item2)
if (triggerRange.Contains(position))
return textIndex;
}
}

// hover the last time-tag if exceed the range.
return null;

Tuple<float, float> getTriggerRange(TextIndex textIndex)
RectangleF getTriggerRange(TextIndex textIndex)
{
var rect = spriteText.GetCharacterDrawRectangle(textIndex.Index);
return TextIndexUtils.GetValueByState(textIndex,
() => new Tuple<float, float>(rect.Left, rect.Centre.X),
() => new Tuple<float, float>(rect.Centre.X, rect.Right));
() => new RectangleF(rect.Left, rect.Top, rect.Width / 2, rect.Height),
() => new RectangleF(rect.Centre.X, rect.Top, rect.Width / 2, rect.Height));
}
}
}
Expand Down

0 comments on commit 9cfd5fd

Please sign in to comment.