Skip to content

Commit 4ea630a

Browse files
authored
Merge pull request #1316 from andy840119/implement-get-value-by-text-tag-state
Remove something like state == TextIndex.IndexState.Start ? A : B
2 parents c45bc45 + fd0cf72 commit 4ea630a

12 files changed

Lines changed: 55 additions & 39 deletions

File tree

osu.Game.Rulesets.Karaoke.Tests/Utils/TextIndexUtilsTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public void TestOutOfRange(int index, TextIndex.IndexState state, string lyric,
9595
Assert.AreEqual(expected, actual);
9696
}
9797

98+
[TestCase(TextIndex.IndexState.Start, 1, -1, 1)]
99+
[TestCase(TextIndex.IndexState.End, 1, -1, -1)]
100+
[TestCase(TextIndex.IndexState.Start, "1", "-1", "1")]
101+
[TestCase(TextIndex.IndexState.End, "1", "-1", "-1")]
102+
public void TestGetValueByState(TextIndex.IndexState state, object startValue, object endValue, object expected)
103+
{
104+
var textIndex = new TextIndex(0, state);
105+
106+
object actual = TextIndexUtils.GetValueByState(textIndex, startValue, endValue);
107+
Assert.AreEqual(expected, actual);
108+
}
109+
98110
[TestCase(0, TextIndex.IndexState.Start, "0")]
99111
[TestCase(0, TextIndex.IndexState.End, "0(end)")]
100112
[TestCase(-1, TextIndex.IndexState.Start, "-1")]

osu.Game.Rulesets.Karaoke/Beatmaps/Formats/LrcEncoder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ internal static IReadOnlyDictionary<TextIndex, double> ToDictionary(IList<Karaok
5454

5555
// convert to dictionary, will get start's smallest time and end's largest time.
5656
return sortedTimeTags.Where(x => x.Time != null).GroupBy(x => x.Index)
57-
.Select(x =>
58-
x.Key.State == TextIndex.IndexState.Start ? x.FirstOrDefault() : x.LastOrDefault())
57+
.Select(x => TextIndexUtils.GetValueByState(x.Key, x.FirstOrDefault(), x.LastOrDefault()))
5958
.ToDictionary(
6059
k => k?.Index ?? throw new ArgumentNullException(nameof(k)),
6160
v => v?.Time ?? throw new ArgumentNullException(nameof(v)));

osu.Game.Rulesets.Karaoke/Edit/Components/Cursor/TimeTagTooltip.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using osu.Framework.Graphics;
66
using osu.Framework.Graphics.Containers;
77
using osu.Framework.Graphics.Shapes;
8-
using osu.Framework.Graphics.Sprites;
98
using osu.Game.Graphics;
109
using osu.Game.Graphics.Sprites;
1110
using osu.Game.Rulesets.Karaoke.Graphics.Cursor;
@@ -94,7 +93,7 @@ public override void SetContent(TimeTag timeTag)
9493

9594
trackTimer.Text = TimeTagUtils.FormattedString(timeTag);
9695
index.Text = $"Position: {timeTag.Index.Index}";
97-
indexState.Text = timeTag.Index.State == TextIndex.IndexState.Start ? "Start" : "End";
96+
indexState.Text = TextIndexUtils.GetValueByState(timeTag.Index, "start", "end");
9897
}
9998

10099
[BackgroundDependencyLoader]

osu.Game.Rulesets.Karaoke/Edit/Lyrics/OsuColourExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) andy840119 <andy840119@gmail.com>. Licensed under the GPL Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4-
using osu.Framework.Graphics.Sprites;
54
using osu.Game.Graphics;
65
using osu.Game.Rulesets.Karaoke.Objects;
6+
using osu.Game.Rulesets.Karaoke.Utils;
77
using osuTK.Graphics;
88

99
namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics
@@ -16,8 +16,7 @@ public static Color4 GetTimeTagColour(this OsuColour colours, TimeTag timeTag)
1616
if (!hasTime)
1717
return colours.Gray7;
1818

19-
bool start = timeTag.Index.State == TextIndex.IndexState.Start;
20-
return start ? colours.Yellow : colours.YellowDarker;
19+
return TextIndexUtils.GetValueByState(timeTag.Index, colours.Yellow, colours.YellowDarker);
2120
}
2221

2322
public static Color4 GetEditTimeTagCaretColour(this OsuColour colours)

osu.Game.Rulesets.Karaoke/Edit/Lyrics/Rows/Components/Carets/DrawableTimeTagEditCaret.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
using osu.Framework.Allocation;
55
using osu.Framework.Graphics;
6-
using osu.Framework.Graphics.Sprites;
76
using osu.Game.Graphics;
87
using osu.Game.Rulesets.Karaoke.Edit.Components.Sprites;
98
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.CaretPosition;
9+
using osu.Game.Rulesets.Karaoke.Utils;
1010
using osuTK;
1111

1212
namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.Rows.Components.Carets
@@ -40,7 +40,7 @@ protected override void Apply(TimeTagIndexCaretPosition caret)
4040
{
4141
var textIndex = caret.Index;
4242
Position = karaokeSpriteText.GetTextIndexPosition(textIndex);
43-
Origin = textIndex.State == TextIndex.IndexState.Start ? Anchor.BottomLeft : Anchor.BottomRight;
43+
Origin = TextIndexUtils.GetValueByState(textIndex, Anchor.BottomLeft, Anchor.BottomRight);
4444

4545
drawableTextIndex.State = textIndex.State;
4646
drawableTextIndex.Colour = colours.GetEditTimeTagCaretColour();

osu.Game.Rulesets.Karaoke/Edit/Lyrics/Rows/Components/Carets/DrawableTimeTagRecordCaret.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
using osu.Framework.Allocation;
55
using osu.Framework.Graphics;
6-
using osu.Framework.Graphics.Sprites;
76
using osu.Game.Graphics;
87
using osu.Game.Rulesets.Karaoke.Edit.Components.Sprites;
98
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.CaretPosition;
9+
using osu.Game.Rulesets.Karaoke.Utils;
1010
using osuTK;
1111

1212
namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.Rows.Components.Carets
@@ -41,7 +41,7 @@ protected override void Apply(TimeTagCaretPosition caret)
4141
var timeTag = caret.TimeTag;
4242
var textIndex = timeTag.Index;
4343
this.MoveTo(karaokeSpriteText.GetTimeTagPosition(timeTag), Preview ? 0 : 100, Easing.OutCubic);
44-
Origin = textIndex.State == TextIndex.IndexState.Start ? Anchor.BottomLeft : Anchor.BottomRight;
44+
Origin = TextIndexUtils.GetValueByState(textIndex, Anchor.BottomLeft, Anchor.BottomRight);
4545

4646
drawableTextIndex.State = textIndex.State;
4747
drawableTextIndex.Colour = colours.GetRecordingTimeTagCaretColour(timeTag);

osu.Game.Rulesets.Karaoke/Edit/Lyrics/Rows/Components/EditorKaraokeSpriteText.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public Vector2 GetTimeTagPosition(TimeTag timeTag)
104104
float extraPosition = extraSpacing(HitObject.TimeTags, timeTag);
105105
return basePosition + new Vector2(extraPosition, 0);
106106

107-
static float extraSpacing(IEnumerable<TimeTag> timeTagsInLyric, TimeTag timeTag)
107+
static float extraSpacing(IList<TimeTag> timeTagsInLyric, TimeTag timeTag)
108108
{
109-
bool isStart = timeTag.Index.State == TextIndex.IndexState.Start;
110-
var timeTags = isStart ? timeTagsInLyric.Reverse() : timeTagsInLyric;
111-
int duplicatedTagAmount = timeTags.SkipWhile(t => t != timeTag).Count(x => x.Index == timeTag.Index) - 1;
112-
int spacing = duplicatedTagAmount * time_tag_spacing * (isStart ? 1 : -1);
109+
var textIndex = timeTag.Index;
110+
var timeTags = TextIndexUtils.GetValueByState(textIndex, timeTagsInLyric.Reverse(), timeTagsInLyric);
111+
int duplicatedTagAmount = timeTags.SkipWhile(t => t != timeTag).Count(x => x.Index == textIndex) - 1;
112+
int spacing = duplicatedTagAmount * time_tag_spacing * TextIndexUtils.GetValueByState(textIndex, 1, -1);
113113
return spacing;
114114
}
115115
}
@@ -172,7 +172,7 @@ public RectangleF GetRomajiTagPosition(RomajiTag romajiTag)
172172
public Vector2 GetTimeTagPosition(TextIndex index)
173173
{
174174
var drawRectangle = GetCharacterRectangle(index.Index);
175-
return index.State == TextIndex.IndexState.Start ? drawRectangle.BottomLeft : drawRectangle.BottomRight;
175+
return TextIndexUtils.GetValueByState(index, drawRectangle.BottomLeft, drawRectangle.BottomRight);
176176
}
177177
}
178178
}

osu.Game.Rulesets.Karaoke/Edit/Lyrics/Rows/Components/Parts/DrawableTimeTag.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using osu.Game.Rulesets.Karaoke.Edit.Components.Cursor;
1111
using osu.Game.Rulesets.Karaoke.Edit.Components.Sprites;
1212
using osu.Game.Rulesets.Karaoke.Objects;
13+
using osu.Game.Rulesets.Karaoke.Utils;
1314
using osuTK;
1415

1516
namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.Rows.Components.Parts
@@ -26,7 +27,7 @@ public class DrawableTimeTag : CompositeDrawable, IHasCustomTooltip<TimeTag>
2627
public DrawableTimeTag(TimeTag timeTag)
2728
{
2829
AutoSizeAxes = Axes.Both;
29-
Origin = timeTag.Index.State == TextIndex.IndexState.Start ? Anchor.BottomLeft : Anchor.BottomRight;
30+
Origin = TextIndexUtils.GetValueByState(timeTag.Index, Anchor.BottomLeft, Anchor.BottomRight);
3031

3132
this.timeTag = timeTag;
3233
var state = timeTag.Index.State;

osu.Game.Rulesets.Karaoke/Edit/Lyrics/Rows/Extends/RecordingTimeTags/RecordingTimeTagPart.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ private void load(OsuColour colours, RecordingTimeTagEditor timeline, ILyricCare
9292
}
9393

9494
var timeTag = timeTagCaretPosition.TimeTag;
95+
var textIndex = timeTag.Index;
9596
var state = timeTag.Index.State;
9697

97-
Origin = state == TextIndex.IndexState.Start ? Anchor.BottomLeft : Anchor.BottomRight;
98+
Origin = TextIndexUtils.GetValueByState(textIndex, Anchor.BottomLeft, Anchor.BottomRight);
9899
drawableTextIndex.Colour = colours.GetRecordingTimeTagCaretColour(timeTag);
99100
drawableTextIndex.State = state;
100101

@@ -131,11 +132,11 @@ public RecordingTimeTagVisualization(Lyric lyric, TimeTag timeTag)
131132
this.lyric = lyric;
132133
this.timeTag = timeTag;
133134

134-
var state = timeTag.Index.State;
135-
bool start = state == TextIndex.IndexState.Start;
135+
var textIndex = timeTag.Index;
136136

137137
Anchor = Anchor.CentreLeft;
138-
Origin = start ? Anchor.CentreLeft : Anchor.CentreRight;
138+
Origin = TextIndexUtils.GetValueByState(textIndex, Anchor.CentreLeft, Anchor.CentreRight);
139+
139140
RelativePositionAxes = Axes.X;
140141
Size = new Vector2(RecordingTimeTagEditor.TIMELINE_HEIGHT);
141142

@@ -148,13 +149,13 @@ public RecordingTimeTagVisualization(Lyric lyric, TimeTag timeTag)
148149
Anchor = Anchor.Centre,
149150
Origin = Anchor.Centre,
150151
RelativeSizeAxes = Axes.Both,
151-
State = state
152+
State = textIndex.State
152153
},
153154
new OsuSpriteText
154155
{
155156
Text = LyricUtils.GetTimeTagDisplayRubyText(lyric, timeTag),
156-
Anchor = start ? Anchor.BottomLeft : Anchor.BottomRight,
157-
Origin = start ? Anchor.TopLeft : Anchor.TopRight,
157+
Anchor = TextIndexUtils.GetValueByState(textIndex, Anchor.BottomLeft, Anchor.BottomRight),
158+
Origin = TextIndexUtils.GetValueByState(textIndex, Anchor.TopLeft, Anchor.TopRight),
158159
}
159160
};
160161
}

osu.Game.Rulesets.Karaoke/Edit/Lyrics/Rows/Extends/TimeTags/TimeTagEditorHitObjectBlueprint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,22 @@ public TimeTagPiece(TimeTag timeTag)
160160
RelativeSizeAxes = Axes.Y;
161161
Width = 10;
162162

163-
var state = timeTag.Index.State;
163+
var textIndex = timeTag.Index;
164164
InternalChildren = new Drawable[]
165165
{
166166
new Box
167167
{
168168
RelativeSizeAxes = Axes.Y,
169169
Width = 1.5f,
170-
Anchor = state == TextIndex.IndexState.Start ? Anchor.CentreLeft : Anchor.CentreRight,
171-
Origin = state == TextIndex.IndexState.Start ? Anchor.CentreLeft : Anchor.CentreRight
170+
Anchor = TextIndexUtils.GetValueByState(textIndex, Anchor.CentreLeft, Anchor.CentreRight),
171+
Origin = TextIndexUtils.GetValueByState(textIndex, Anchor.CentreLeft, Anchor.CentreRight),
172172
},
173173
new DrawableTextIndex
174174
{
175175
Size = new Vector2(10),
176176
Anchor = Anchor.BottomCentre,
177177
Origin = Anchor.BottomCentre,
178-
State = state
178+
State = textIndex.State
179179
}
180180
};
181181
}

0 commit comments

Comments
 (0)