|
| 1 | +// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using osu.Game.Beatmaps; |
| 5 | +using osu.Game.Graphics; |
| 6 | +using osu.Game.Rulesets.Mods; |
| 7 | +using osu.Game.Rulesets.Objects; |
| 8 | +using osu.Game.Rulesets.Objects.Drawables; |
| 9 | +using osu.Game.Rulesets.Osu.Objects.Drawables; |
| 10 | +using osu.Game.Screens.Edit; |
| 11 | +using osuTK.Graphics; |
| 12 | + |
| 13 | +namespace osu.Game.Rulesets.Osu.Mods |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Mod that colours <see cref="HitObject"/>s based on the musical division they are on |
| 17 | + /// </summary> |
| 18 | + public class OsuModSynesthesia : ModSynesthesia, IApplicableToBeatmap, IApplicableToDrawableHitObject |
| 19 | + { |
| 20 | + private readonly OsuColour colours = new OsuColour(); |
| 21 | + |
| 22 | + private IBeatmap? currentBeatmap { get; set; } |
| 23 | + |
| 24 | + public void ApplyToBeatmap(IBeatmap beatmap) |
| 25 | + { |
| 26 | + //Store a reference to the current beatmap to look up the beat divisor when notes are drawn |
| 27 | + if (currentBeatmap != beatmap) |
| 28 | + currentBeatmap = beatmap; |
| 29 | + } |
| 30 | + |
| 31 | + public void ApplyToDrawableHitObject(DrawableHitObject d) |
| 32 | + { |
| 33 | + if (currentBeatmap == null) return; |
| 34 | + |
| 35 | + Color4? timingBasedColour = null; |
| 36 | + |
| 37 | + d.HitObjectApplied += _ => |
| 38 | + { |
| 39 | + // slider tails are a painful edge case, as their start time is offset 36ms back (see `LegacyLastTick`). |
| 40 | + // to work around this, look up the slider tail's parenting slider's end time instead to ensure proper snap. |
| 41 | + double snapTime = d is DrawableSliderTail tail |
| 42 | + ? tail.Slider.GetEndTime() |
| 43 | + : d.HitObject.StartTime; |
| 44 | + timingBasedColour = BindableBeatDivisor.GetColourFor(currentBeatmap.ControlPointInfo.GetClosestBeatDivisor(snapTime), colours); |
| 45 | + }; |
| 46 | + |
| 47 | + // Need to set this every update to ensure it doesn't get overwritten by DrawableHitObject.OnApply() -> UpdateComboColour(). |
| 48 | + d.OnUpdate += _ => |
| 49 | + { |
| 50 | + if (timingBasedColour != null) |
| 51 | + d.AccentColour.Value = timingBasedColour.Value; |
| 52 | + }; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments