2020-05-22 21:12:09 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace CC_Functions.Commandline.TUI
|
|
|
|
{
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Represents a pixel
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public class Pixel
|
|
|
|
{
|
2020-06-12 15:12:13 +02:00
|
|
|
public static readonly Pixel Empty = new Pixel();
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// This pixels background color
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public ConsoleColor BackColor;
|
2020-05-22 21:12:09 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// This pixels content character
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public char Content;
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// This pixels foregound color
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public ConsoleColor ForeColor;
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Generates a new pixel
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="backColor">The background color</param>
|
|
|
|
/// <param name="foreColor">The foreground color</param>
|
|
|
|
/// <param name="content">The new content</param>
|
2020-05-22 21:12:09 +02:00
|
|
|
public Pixel(ConsoleColor backColor, ConsoleColor foreColor, char content)
|
|
|
|
{
|
|
|
|
BackColor = backColor;
|
|
|
|
ForeColor = foreColor;
|
|
|
|
Content = content;
|
|
|
|
}
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Generates a new pixel
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="content">The content for this pixel</param>
|
2020-05-22 21:12:09 +02:00
|
|
|
public Pixel(char content) : this(Console.BackgroundColor, Console.ForegroundColor, content)
|
|
|
|
{
|
|
|
|
}
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Generates a new pixel
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public Pixel() : this(' ')
|
|
|
|
{
|
|
|
|
}
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Use this in functions that require equality comparers
|
|
|
|
/// </summary>
|
|
|
|
public static IEqualityComparer<Pixel> ColorContentComparer { get; } = new ColorContentEqualityComparer();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether this is equal to another pixel
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="other">The other pixel to compare</param>
|
|
|
|
/// <returns>Whether they are equal</returns>
|
|
|
|
protected bool Equals(Pixel other) => ColorContentComparer.Equals(this, other);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether this is equal to another object
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The other object to compare</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(null, obj)) return false;
|
|
|
|
if (ReferenceEquals(this, obj)) return true;
|
|
|
|
if (obj.GetType() != GetType()) return false;
|
|
|
|
return Equals((Pixel) obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generates an integer for comparing this object
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The generated hash</returns>
|
2020-06-12 11:24:03 +02:00
|
|
|
public override int GetHashCode() => HashCode.Combine(BackColor, ForeColor, Content);
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Whether two pixels are equal
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="a">First pixel to compare</param>
|
|
|
|
/// <param name="b">Second pixel to compare</param>
|
|
|
|
/// <returns>Whether they are equal</returns>
|
2020-06-12 14:58:13 +02:00
|
|
|
public static bool operator ==(Pixel a, Pixel b) => !ReferenceEquals(a, null) && a.Equals(b);
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Whether to pixels are not equal
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="a">First pixel to compare</param>
|
|
|
|
/// <param name="b">Second pixel to compare</param>
|
|
|
|
/// <returns>Whether they are not equal</returns>
|
2020-06-12 14:58:13 +02:00
|
|
|
public static bool operator !=(Pixel a, Pixel b) => !(a == b);
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Returns the content of this pixel
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <returns>The content of this pixel</returns>
|
2020-05-22 21:12:09 +02:00
|
|
|
public override string ToString() => Content.ToString();
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
|
|
private sealed class ColorContentEqualityComparer : IEqualityComparer<Pixel>
|
|
|
|
{
|
|
|
|
public bool Equals(Pixel x, Pixel y)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(x, y)) return true;
|
|
|
|
if (ReferenceEquals(x, null)) return false;
|
|
|
|
if (ReferenceEquals(y, null)) return false;
|
|
|
|
if (x.GetType() != y.GetType()) return false;
|
|
|
|
return x.GetHashCode().Equals(y.GetHashCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetHashCode(Pixel obj) => obj.GetHashCode();
|
|
|
|
}
|
2020-05-22 21:12:09 +02:00
|
|
|
}
|
|
|
|
}
|