using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CC_Functions.Misc;
namespace CC_Functions.Commandline.TUI
{
///
/// A basic button type
///
public class Button : Control
{
///
/// The text inside this button
///
public string Content;
///
/// Creates a new button
///
/// The text inside this button
public Button(string content)
{
Content = content;
char[,] tmp = Content.ToNdArray2D();
Size = new Size(tmp.GetLength(1), tmp.GetLength(0));
}
///
public override bool Selectable { get; } = true;
///
public override Pixel[,] Render()
{
char[,] inp = Indent(SplitLines(Content, Size.Width), Size.Width).ToNdArray2D();
inp = inp.Resize(Size.Height, Size.Width);
Pixel[,] output = new Pixel[Size.Height, Size.Width];
for (int x = 0; x < Size.Height; x++)
for (int y = 0; y < Size.Width; y++)
output[x, y] = new Pixel(Selected ? ForeColor : BackColor, Selected ? BackColor : ForeColor, inp[x, y]);
return output;
}
private string Indent(string source, int maxLen)
{
string[] tmp = source.Split('\n');
for (int i = 0; i < tmp.Length; i++) tmp[i] = new string(SpecialChars.Empty, (maxLen - tmp[i].Length) / 2) + tmp[i];
return string.Join('\n', tmp);
}
private string SplitLines(string source, int maxLen)
{
List parts = new List(source.Split());
while (parts.Any(s => s.Length > maxLen))
{
parts = parts.SelectMany(s =>
{
if (s.Length > maxLen)
return s.Insert(maxLen, "\n").Split('\n');
else
return new[] {s};
}).ToList();
}
return string.Join('\n', parts);
}
}
}