87 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-10-25 14:31:50 -04:00
// Copyright (C) 2024 Intro-Skipper contributors <intro-skipper.org>
// SPDX-License-Identifier: GPL-3.0-only.
2024-10-25 14:15:12 -04:00
2022-05-01 00:33:22 -05:00
using System;
namespace IntroSkipper.Data;
2022-05-01 00:33:22 -05:00
2022-07-05 16:21:13 -05:00
#pragma warning disable CA1036 // Override methods on comparable types
2022-05-01 00:33:22 -05:00
/// <summary>
/// Range of contiguous time.
/// </summary>
public class TimeRange : IComparable
{
2022-05-03 01:09:50 -05:00
/// <summary>
/// Initializes a new instance of the <see cref="TimeRange"/> class.
2022-05-03 01:09:50 -05:00
/// </summary>
public TimeRange()
{
Start = 0;
End = 0;
}
2022-05-01 00:33:22 -05:00
/// <summary>
/// Initializes a new instance of the <see cref="TimeRange"/> class.
2022-05-01 00:33:22 -05:00
/// </summary>
/// <param name="start">Time range start.</param>
/// <param name="end">Time range end.</param>
2022-05-01 00:33:22 -05:00
public TimeRange(double start, double end)
{
Start = start;
End = end;
}
/// <summary>
/// Initializes a new instance of the <see cref="TimeRange"/> class.
2022-05-01 00:33:22 -05:00
/// </summary>
/// <param name="original">Original TimeRange.</param>
2022-05-01 00:33:22 -05:00
public TimeRange(TimeRange original)
{
Start = original.Start;
End = original.End;
}
/// <summary>
2022-05-09 22:56:03 -05:00
/// Gets or sets the time range start (in seconds).
2022-05-01 00:33:22 -05:00
/// </summary>
2022-05-09 22:56:03 -05:00
public double Start { get; set; }
2022-05-01 00:33:22 -05:00
2022-05-09 22:56:03 -05:00
/// <summary>
/// Gets or sets the time range end (in seconds).
/// </summary>
public double End { get; set; }
2022-05-01 00:33:22 -05:00
2022-05-09 22:56:03 -05:00
/// <summary>
/// Gets the duration of this time range (in seconds).
/// </summary>
public double Duration => End - Start;
2022-05-01 00:33:22 -05:00
2022-05-09 22:56:03 -05:00
/// <summary>
/// Compare TimeRange durations.
2022-05-09 22:56:03 -05:00
/// </summary>
/// <param name="obj">Object to compare with.</param>
/// <returns>int.</returns>
2022-05-09 22:56:03 -05:00
public int CompareTo(object? obj)
{
if (obj is not TimeRange tr)
2022-05-09 22:56:03 -05:00
{
throw new ArgumentException("obj must be a TimeRange");
2022-05-09 22:56:03 -05:00
}
return tr.Duration.CompareTo(Duration);
2022-05-09 22:56:03 -05:00
}
2022-08-29 23:56:13 -05:00
/// <summary>
/// Tests if this TimeRange object intersects the provided TimeRange.
/// </summary>
/// <param name="tr">Second TimeRange object to test.</param>
/// <returns>true if tr intersects the current TimeRange, false otherwise.</returns>
public bool Intersects(TimeRange tr)
{
return
(Start < tr.Start && tr.Start < End) ||
(Start < tr.End && tr.End < End);
}
2022-05-01 00:33:22 -05:00
}