161 lines
5.5 KiB
C#
161 lines
5.5 KiB
C#
#nullable enable
|
|
using System;
|
|
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// 범위 값을 나타내는 제네릭 구조체
|
|
/// </summary>
|
|
public struct UTKRangeValue<T> : IEquatable<UTKRangeValue<T>> where T : struct
|
|
{
|
|
public T Min { get; set; }
|
|
public T Max { get; set; }
|
|
|
|
public UTKRangeValue(T min, T max)
|
|
{
|
|
Min = min;
|
|
Max = max;
|
|
}
|
|
|
|
public bool Equals(UTKRangeValue<T> other)
|
|
{
|
|
return Min.Equals(other.Min) && Max.Equals(other.Max);
|
|
}
|
|
|
|
public override bool Equals(object? obj)
|
|
{
|
|
return obj is UTKRangeValue<T> other && Equals(other);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(Min, Max);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{Min} ~ {Max}";
|
|
}
|
|
|
|
public static bool operator ==(UTKRangeValue<T> left, UTKRangeValue<T> right)
|
|
{
|
|
return left.Equals(right);
|
|
}
|
|
|
|
public static bool operator !=(UTKRangeValue<T> left, UTKRangeValue<T> right)
|
|
{
|
|
return !left.Equals(right);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 정수 범위 (IntRange)
|
|
/// </summary>
|
|
public struct UTKIntRange : IEquatable<UTKIntRange>
|
|
{
|
|
public int Min { get; set; }
|
|
public int Max { get; set; }
|
|
|
|
public UTKIntRange(int min, int max)
|
|
{
|
|
Min = min;
|
|
Max = max;
|
|
}
|
|
|
|
public bool Contains(int value) => value >= Min && value <= Max;
|
|
public int Clamp(int value) => Math.Max(Min, Math.Min(Max, value));
|
|
public int Range => Max - Min;
|
|
|
|
public bool Equals(UTKIntRange other) => Min == other.Min && Max == other.Max;
|
|
public override bool Equals(object? obj) => obj is UTKIntRange other && Equals(other);
|
|
public override int GetHashCode() => HashCode.Combine(Min, Max);
|
|
public override string ToString() => $"{Min} ~ {Max}";
|
|
|
|
public static bool operator ==(UTKIntRange left, UTKIntRange right) => left.Equals(right);
|
|
public static bool operator !=(UTKIntRange left, UTKIntRange right) => !left.Equals(right);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 실수 범위 (FloatRange)
|
|
/// </summary>
|
|
public struct UTKFloatRange : IEquatable<UTKFloatRange>
|
|
{
|
|
public float Min { get; set; }
|
|
public float Max { get; set; }
|
|
|
|
public UTKFloatRange(float min, float max)
|
|
{
|
|
Min = min;
|
|
Max = max;
|
|
}
|
|
|
|
public bool Contains(float value) => value >= Min && value <= Max;
|
|
public float Clamp(float value) => Math.Max(Min, Math.Min(Max, value));
|
|
public float Range => Max - Min;
|
|
|
|
public bool Equals(UTKFloatRange other) =>
|
|
Math.Abs(Min - other.Min) < float.Epsilon &&
|
|
Math.Abs(Max - other.Max) < float.Epsilon;
|
|
|
|
public override bool Equals(object? obj) => obj is UTKFloatRange other && Equals(other);
|
|
public override int GetHashCode() => HashCode.Combine(Min, Max);
|
|
public override string ToString() => $"{Min:F2} ~ {Max:F2}";
|
|
|
|
public static bool operator ==(UTKFloatRange left, UTKFloatRange right) => left.Equals(right);
|
|
public static bool operator !=(UTKFloatRange left, UTKFloatRange right) => !left.Equals(right);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 날짜 범위 (DateRange)
|
|
/// </summary>
|
|
public struct UTKDateRange : IEquatable<UTKDateRange>
|
|
{
|
|
public DateTime Start { get; set; }
|
|
public DateTime End { get; set; }
|
|
|
|
public UTKDateRange(DateTime start, DateTime end)
|
|
{
|
|
Start = start.Date;
|
|
End = end.Date;
|
|
}
|
|
|
|
public bool Contains(DateTime date) => date.Date >= Start && date.Date <= End;
|
|
public TimeSpan Duration => End - Start;
|
|
public int TotalDays => (int)Duration.TotalDays;
|
|
|
|
public bool Equals(UTKDateRange other) => Start == other.Start && End == other.End;
|
|
public override bool Equals(object? obj) => obj is UTKDateRange other && Equals(other);
|
|
public override int GetHashCode() => HashCode.Combine(Start, End);
|
|
public override string ToString() => $"{Start:yyyy-MM-dd} ~ {End:yyyy-MM-dd}";
|
|
|
|
public static bool operator ==(UTKDateRange left, UTKDateRange right) => left.Equals(right);
|
|
public static bool operator !=(UTKDateRange left, UTKDateRange right) => !left.Equals(right);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 날짜시간 범위 (DateTimeRange)
|
|
/// </summary>
|
|
public struct UTKDateTimeRange : IEquatable<UTKDateTimeRange>
|
|
{
|
|
public DateTime Start { get; set; }
|
|
public DateTime End { get; set; }
|
|
|
|
public UTKDateTimeRange(DateTime start, DateTime end)
|
|
{
|
|
Start = start;
|
|
End = end;
|
|
}
|
|
|
|
public bool Contains(DateTime dateTime) => dateTime >= Start && dateTime <= End;
|
|
public TimeSpan Duration => End - Start;
|
|
|
|
public bool Equals(UTKDateTimeRange other) => Start == other.Start && End == other.End;
|
|
public override bool Equals(object? obj) => obj is UTKDateTimeRange other && Equals(other);
|
|
public override int GetHashCode() => HashCode.Combine(Start, End);
|
|
public override string ToString() => $"{Start:yyyy-MM-dd HH:mm} ~ {End:yyyy-MM-dd HH:mm}";
|
|
|
|
public static bool operator ==(UTKDateTimeRange left, UTKDateTimeRange right) => left.Equals(right);
|
|
public static bool operator !=(UTKDateTimeRange left, UTKDateTimeRange right) => !left.Equals(right);
|
|
}
|
|
}
|