using System; using System.Drawing; using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { /// /// Provides a control for users to select a boolean /// public class CheckBox : Control { /// /// Called when the state of this checkbox is changed /// /// The current screen instance /// Args public delegate void OnCheckedChanged(Screen screen, EventArgs e); /// /// Whether the box is checked /// public bool Checked; /// /// The text inside this checkbox /// public string Content; /// /// Creates a new checkbox /// /// The text inside this CheckBox public CheckBox(string content) { Content = content; Input += (screen, args) => { switch (args.Info.Key) { case ConsoleKey.LeftArrow: case ConsoleKey.RightArrow: case ConsoleKey.Spacebar: Checked = !Checked; CheckedChanged?.Invoke(screen, args); break; } }; Click += (screen, args) => { Checked = !Checked; CheckedChanged?.Invoke(screen, args); }; } /// public override bool Selectable { get; } = true; /// public override Pixel[,] Render() { char[,] inp1 = Content.ToNdArray2D(); char[,] inp = new char[inp1.GetLength(0), inp1.GetLength(1) + 4]; inp.Populate(' '); inp1.CopyTo(inp, new Point(4, 0)); inp[0, 0] = '['; inp[0, 1] = Checked ? 'X' : SpecialChars.Empty; inp[0, 2] = ']'; int w = inp.GetLength(0); int h = inp.GetLength(1); Pixel[,] output = new Pixel[w, h]; for (int x = 0; x < w; x++) for (int y = 0; y < h; y++) output[x, y] = new Pixel(Selected ? ForeColor : BackColor, Selected ? BackColor : ForeColor, inp[x, y]); Size = new Size(w, h); return output; } /// /// Called when the state of this checkbox is changed /// public event OnClick CheckedChanged; } }