Make3.Renderer/Domain/ConsoleImageDrawer.cs

78 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
namespace Domain;
public class ConsoleImageDrawer
{
private char[,]? _image; // Изображение хранится внутри класса
// 1. Нарисовать изображение
public void DrawImage(char[,] image)
{
Console.Clear();
_image = image;
for (int i = 0; i < _image.GetLength(0); i++)
{
for (int j = 0; j < _image.GetLength(1); j++)
{
Console.SetCursorPosition(j, i);
Console.Write(_image[i, j]);
}
}
}
// 2. Заменить фон в указанной области на цвет
public void SetBackgroundColor(int startRow, int startCol, int height, int width, ConsoleColor color)
{
if (_image == null)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Image is not set");
Console.ForegroundColor = ConsoleColor.Black;
return;
}
Console.BackgroundColor = color;
for (int i = startRow; i < Math.Min(startRow + height, _image.GetLength(0)); i++)
{
for (int j = startCol; j < Math.Min(startCol + width, _image.GetLength(1)); j++)
{
Console.SetCursorPosition(j, i);
Console.Write(_image[i, j]);
}
}
Console.ResetColor(); // Сброс цвета
}
// 3. Отменить фон (вернуть черный)
public void ResetBackgroundColor(int startRow, int startCol, int height, int width)
{
SetBackgroundColor(startRow, startCol, height, width, ConsoleColor.Black);
}
// 4. Изменить цвет указанного символа
public void SetCharacterColor(int row, int col, ConsoleColor foregroundColor)
{
if (_image == null)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Image is not set");
Console.ForegroundColor = ConsoleColor.Black;
return;
}
if (row >= 0 && row < _image.GetLength(0) && col >= 0 && col < _image.GetLength(1))
{
Console.SetCursorPosition(col, row);
Console.ForegroundColor = foregroundColor;
Console.Write(_image[row, col]);
Console.ResetColor(); // Сброс цвета
}
}
// 5. Стереть изображение
public void ClearImage()
{
Console.Clear();
}
}