Skip to content

Commit

Permalink
Merge pull request #2312 from andy840119/simply-logic-in-the-stage-info
Browse files Browse the repository at this point in the history
Simply logic in the stage info.
  • Loading branch information
andy840119 authored Dec 8, 2024
2 parents 30c19aa + f15e537 commit 420d4f6
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,49 +119,6 @@ public void TestAddToMapping()
});
}

[Test]
public void TestOffsetMapping()
{
Lyric lyric = new Lyric();
Lyric unSelectedLyric = new Lyric();

SetUpStageInfo<TestStageInfo>(stageInfo =>
{
var category = stageInfo.Category;
var element = category.AddElement(x => x.Name = "Element 1");
category.AddElement(x => x.Name = "Element 2");

// Add to Mapping
category.AddToMapping(element, lyric);
category.AddToMapping(element, unSelectedLyric);
});

PrepareHitObject(() => lyric);
PrepareHitObject(() => unSelectedLyric, false);

TriggerHandlerChanged(c =>
{
c.OffsetMapping(1);
});

AssertStageInfo<TestStageInfo>(stageInfo =>
{
var category = stageInfo.Category;

Assert.AreEqual("Element 2", category.GetElementByItem(lyric).Name);
Assert.AreEqual("Element 1", category.GetElementByItem(unSelectedLyric).Name); // should not change the id if lyric is not selected.
});
}

[Test]
public void TestOffsetMappingWithZeroValue()
{
PrepareHitObject(() => new Lyric());

// offset value should not be zero.
TriggerHandlerChangedWithException<InvalidOperationException>(c => c.OffsetMapping(0));
}

[Test]
public void TestRemoveFromMapping()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public abstract class BaseLyricStageElementCategoryGeneratorTest<TGenerator, TOb
: BaseStageElementCategoryGeneratorTest<TGenerator, TObject, TStageElement, Lyric, TConfig>
where TGenerator : StageInfoPropertyGenerator<TObject, TConfig>
where TObject : StageElementCategory<TStageElement, Lyric>
where TStageElement : StageElement, IComparable<TStageElement>, new()
where TStageElement : StageElement, new()
where TConfig : GeneratorConfig, new();

public abstract class BaseStageElementCategoryGeneratorTest<TGenerator, TObject, TStageElement, THitObject, TConfig>
: BaseStageInfoPropertyGeneratorTest<TGenerator, TObject, TConfig>
where TGenerator : StageInfoPropertyGenerator<TObject, TConfig>
where TObject : StageElementCategory<TStageElement, THitObject>
where TStageElement : StageElement, IComparable<TStageElement>, new()
where TStageElement : StageElement, new()
where THitObject : KaraokeHitObject, IHasPrimaryKey
where TConfig : GeneratorConfig, new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,6 @@ public void TestGetHitObjectIdsByElement()
Assert.IsEmpty(category.GetHitObjectIdsByElement(defaultElement));
}

[Test]
public void TestGetElementOrder()
{
var category = new TestStageElementCategory();
var element = category.AddElement();

int? existElementOrder = category.GetElementOrder(element);
Assert.AreEqual(1, existElementOrder);

var notExistElement = new TestStageElement();
int? notExistElementOrder = category.GetElementOrder(notExistElement);
Assert.IsNull(notExistElementOrder);
}

#endregion

private class TestStageElement : StageElement, IComparable<TestStageElement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public interface IStageElementCategoryChangeHandler<TStageElement>

void AddToMapping(TStageElement element);

void OffsetMapping(int offset);

void RemoveFromMapping();

void ClearUnusedMapping();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,6 @@ public void AddToMapping(TStageElement element)
});
}

public void OffsetMapping(int offset)
{
if (offset == 0)
throw new InvalidOperationException("Offset number should not be zero.");

PerformOnSelection<THitObject>(hitObject =>
{
performStageInfoChanged(s =>
{
var element = s.GetElementByItem(hitObject);
int mappingIndex = s.SortedElements.IndexOf(element);
if (mappingIndex < 0)
return;

int newMappingIndex = mappingIndex + offset;
var newElement = s.SortedElements.ElementAtOrDefault(newMappingIndex);
if (newElement == null)
return;

s.RemoveHitObjectFromMapping(hitObject);
s.AddToMapping(newElement, hitObject);
});
});
}

public void RemoveFromMapping()
{
PerformOnSelection<THitObject>(hitObject =>
Expand Down
6 changes: 3 additions & 3 deletions osu.Game.Rulesets.Karaoke/Edit/Checks/CheckStageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private readonly IList<Func<TStageInfo, IReadOnlyList<KaraokeHitObject>, IEnumer
= new List<Func<TStageInfo, IReadOnlyList<KaraokeHitObject>, IEnumerable<Issue>>>();

public void RegisterCategory<TStageElement, THitObject>(Func<TStageInfo, StageElementCategory<TStageElement, THitObject>> categoryAction, int minimumRequiredElements)
where TStageElement : StageElement, IComparable<TStageElement>, new()
where TStageElement : StageElement, new()
where THitObject : KaraokeHitObject, IHasPrimaryKey
{
stageInfoCategoryActions.Add((info, hitObjects) =>
Expand All @@ -46,7 +46,7 @@ public void RegisterCategory<TStageElement, THitObject>(Func<TStageInfo, StageEl

private IEnumerable<Issue> checkElementCategory<TStageElement, THitObject>(StageElementCategory<TStageElement, THitObject> category, IReadOnlyList<THitObject> hitObjects,
int minimumRequiredElements)
where TStageElement : StageElement, IComparable<TStageElement>, new()
where TStageElement : StageElement, new()
where THitObject : KaraokeHitObject, IHasPrimaryKey
{
// check mapping.
Expand All @@ -68,7 +68,7 @@ private IEnumerable<Issue> checkElementCategory<TStageElement, THitObject>(Stage
}

private IEnumerable<Issue> checkMappings<TStageElement, THitObject>(StageElementCategory<TStageElement, THitObject> category, IReadOnlyList<THitObject> hitObjects)
where TStageElement : StageElement, IComparable<TStageElement>, new()
where TStageElement : StageElement, new()
where THitObject : KaraokeHitObject, IHasPrimaryKey
{
var elements = category.AvailableElements;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// 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 Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Stages.Infos.Classic;

public class ClassicLyricLayout : StageElement, IComparable<ClassicLyricLayout>
public class ClassicLyricLayout : StageElement
{
[JsonIgnore]
public readonly Bindable<ClassicLyricLayoutAlignment> AlignmentBindable = new(ClassicLyricLayoutAlignment.Center);
Expand All @@ -20,11 +18,7 @@ public class ClassicLyricLayout : StageElement, IComparable<ClassicLyricLayout>
public ClassicLyricLayoutAlignment Alignment
{
get => AlignmentBindable.Value;
set
{
AlignmentBindable.Value = value;
TriggerOrderVersionChanged();
}
set => AlignmentBindable.Value = value;
}

[JsonIgnore]
Expand All @@ -36,11 +30,7 @@ public ClassicLyricLayoutAlignment Alignment
public float HorizontalMargin
{
get => HorizontalMarginBindable.Value;
set
{
HorizontalMarginBindable.Value = value;
TriggerOrderVersionChanged();
}
set => HorizontalMarginBindable.Value = value;
}

[JsonIgnore]
Expand All @@ -53,19 +43,6 @@ public float HorizontalMargin
public int Line
{
get => LineBindable.Value;
set
{
LineBindable.Value = value;
TriggerOrderVersionChanged();
}
}

public int CompareTo(ClassicLyricLayout? other)
{
return ComparableUtils.CompareByProperty(this, other,
x => x.Line,
x => x.Alignment,
x => x.HorizontalMargin,
x => x.ID);
set => LineBindable.Value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public class ClassicStageInfo : StageInfo
{
#region Category

/// <summary>
/// Category to save the <see cref="Lyric"/>'s and <see cref="Note"/>'s style.
/// </summary>
public ClassicStyleCategory StyleCategory { get; set; } = new();

/// <summary>
/// The definition for the <see cref="Lyric"/>.<br/>
/// Like the line height or font size.
/// </summary>
public ClassicStageDefinition StageDefinition { get; set; } = new();

/// <summary>
/// Category to save the <see cref="Lyric"/>'s and <see cref="Note"/>'s style.
/// </summary>
public ClassicStyleCategory StyleCategory { get; set; } = new();

/// <summary>
/// Category to save the <see cref="Lyric"/>'s layout.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace osu.Game.Rulesets.Karaoke.Stages.Infos.Classic;

public class ClassicStyle : StageElement, IComparable<ClassicStyle>
public class ClassicStyle : StageElement
{
[JsonIgnore]
public readonly Bindable<int?> LyricStyleIndexBindable = new();
Expand All @@ -34,11 +34,4 @@ public int? NoteStyleIndex
get => NoteStyleIndexBindable.Value;
set => NoteStyleIndexBindable.Value = value;
}

public int CompareTo(ClassicStyle? other)
{
return ComparableUtils.CompareByProperty(this, other,
x => x.Name,
x => x.ID);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// 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 System.Collections.Generic;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Stages.Infos.Preview;

public class PreviewLyricLayout : StageElement, IComparable<PreviewLyricLayout>
public class PreviewLyricLayout : StageElement
{
/// <summary>
/// <see cref="Lyric"/>'s timing with row index.
Expand All @@ -24,10 +22,4 @@ public class PreviewLyricLayout : StageElement, IComparable<PreviewLyricLayout>
/// <see cref="Lyric"/>'s end time.
/// </summary>
public double EndTime { get; set; }

public int CompareTo(PreviewLyricLayout? other)
{
return ComparableUtils.CompareByProperty(this, other,
x => x.ID);
}
}
36 changes: 8 additions & 28 deletions osu.Game.Rulesets.Karaoke/Stages/Infos/Preview/PreviewStageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ public class PreviewStageInfo : StageInfo, IHasCalculatedProperty
{
#region Category

/// <summary>
/// Category to save the <see cref="Lyric"/>'s and <see cref="Note"/>'s style.
/// </summary>
[JsonIgnore]
private PreviewStyleCategory styleCategory { get; set; } = new();

/// <summary>
/// The definition for the <see cref="Lyric"/>.
/// Like how many lyrics can in the playfield at the same time.
/// </summary>
public PreviewStageDefinition StageDefinition { get; set; } = new();

/// <summary>
/// Category to save the <see cref="Lyric"/>'s and <see cref="Note"/>'s style.
/// This property will not be saved because it's real-time calculated.
/// </summary>
[JsonIgnore]
private PreviewStyleCategory styleCategory { get; set; } = new();

/// <summary>
/// Category to save the <see cref="Lyric"/>'s layout.
/// This property will not be saved because it's real-time calculated.
/// </summary>
[JsonIgnore]
private PreviewLyricLayoutCategory layoutCategory { get; set; } = new();
Expand All @@ -38,32 +40,12 @@ public class PreviewStageInfo : StageInfo, IHasCalculatedProperty

#region Validation

private bool calculatedPropertyIsUpdated;

/// <summary>
/// Mark the stage info's calculated property as invalidate.
/// </summary>
/// <returns></returns>
public void TriggerRecalculate()
{
calculatedPropertyIsUpdated = false;
}

/// <summary>
/// Check if the stage info's calculated property is calculated and the value is the latest.
/// </summary>
/// <returns></returns>
public bool IsUpdated() => calculatedPropertyIsUpdated;

/// <summary>
/// If the calculated property is not updated, then re-calculate the property inside the stage info in the <see cref="KaraokeBeatmapProcessor"/>
/// </summary>
/// <param name="beatmap"></param>
public void ValidateCalculatedProperty(IBeatmap beatmap)
{
if (IsUpdated())
return;

var calculator = new PreviewStageTimingCalculator(beatmap, StageDefinition);

// also, clear all mapping in the layout and re-create one.
Expand All @@ -83,8 +65,6 @@ public void ValidateCalculatedProperty(IBeatmap beatmap)
});
layoutCategory.AddToMapping(element, lyric);
}

calculatedPropertyIsUpdated = true;
}

#endregion
Expand Down
11 changes: 1 addition & 10 deletions osu.Game.Rulesets.Karaoke/Stages/Infos/Preview/PreviewStyle.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// 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.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Stages.Infos.Preview;

public class PreviewStyle : StageElement, IComparable<PreviewStyle>
public class PreviewStyle : StageElement
{
/// <summary>
/// <see cref="Lyric"/>'s skin lookup index.
Expand All @@ -18,11 +16,4 @@ public class PreviewStyle : StageElement, IComparable<PreviewStyle>
/// <see cref="Note"/>'s skin lookup index.
/// </summary>
public int? NoteStyleIndex { get; set; }

public int CompareTo(PreviewStyle? other)
{
return ComparableUtils.CompareByProperty(this, other,
x => x.Name,
x => x.ID);
}
}
Loading

0 comments on commit 420d4f6

Please sign in to comment.