2020-05-22 21:12:09 +02:00
|
|
|
using System;
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
namespace CC_Functions.Commandline.TUI
|
|
|
|
{
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Abstract class inherited by all controls
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public abstract class Control
|
|
|
|
{
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Called when [enter] is pressed while the control is selected
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="screen">An instance of the calling screen</param>
|
|
|
|
/// <param name="e">Args</param>
|
|
|
|
public delegate void OnClick(Screen screen, EventArgs e);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when the control is selected and unknown input is given
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="screen">An instance of the calling screen</param>
|
|
|
|
/// <param name="e">Args</param>
|
|
|
|
public delegate void OnInput(Screen screen, InputEventArgs e);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when the controls Size property is changed
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="caller">The calling control</param>
|
|
|
|
/// <param name="e">Args</param>
|
|
|
|
public delegate void OnResize(Control caller, EventArgs e);
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
|
|
private Size _size;
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Whether the control can be interacted with
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public bool Enabled = true;
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Whether the control should be rendered
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public bool Visible = true;
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// The size of the control
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public Size Size
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
2020-05-23 15:32:24 +02:00
|
|
|
if (_size != value)
|
|
|
|
{
|
|
|
|
_size = value;
|
|
|
|
Resize?.Invoke(this, new EventArgs());
|
|
|
|
}
|
2020-05-22 21:12:09 +02:00
|
|
|
}
|
|
|
|
get => _size;
|
|
|
|
}
|
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
|
|
|
/// The position of this control
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public Point Point { get; set; }
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// The foreground color for this control
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public ConsoleColor ForeColor { get; set; } = Console.ForegroundColor;
|
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
|
|
|
/// The background color for this control
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public ConsoleColor BackColor { get; set; } = Console.BackgroundColor;
|
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 the control can be selected
|
2020-05-23 15:32:24 +02:00
|
|
|
/// </summary>
|
2020-05-22 21:12:09 +02:00
|
|
|
public abstract bool Selectable { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Whether the object is selected. Used internally and for drawing
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public bool Selected { get; internal set; } = false;
|
|
|
|
|
2020-05-22 21:12:09 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Called when the controls Size property is changed
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public event OnResize Resize;
|
|
|
|
|
2020-05-22 21:12:09 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Renders the control
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// <returns>The rendered pixels</returns>
|
|
|
|
public abstract Pixel[,] Render();
|
|
|
|
|
2020-05-22 21:12:09 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Called when [enter] is pressed while the control is selected
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public event OnClick Click;
|
|
|
|
|
2020-05-22 21:12:09 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Called when the control is selected and unknown input is given
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
public event OnInput Input;
|
|
|
|
|
2020-05-22 21:12:09 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Invokes click events
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="screen">The calling screen</param>
|
|
|
|
internal void InvokeClick(Screen screen)
|
|
|
|
{
|
|
|
|
Click?.Invoke(screen, new EventArgs());
|
|
|
|
}
|
2020-05-23 17:22:45 +02:00
|
|
|
|
2020-05-22 21:12:09 +02:00
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
/// Invokes input events
|
2020-05-22 21:12:09 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="screen">The calling screen</param>
|
|
|
|
/// <param name="info">The input data</param>
|
|
|
|
internal void InvokeInput(Screen screen, ConsoleKeyInfo info)
|
|
|
|
{
|
|
|
|
Input?.Invoke(screen, new InputEventArgs(info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|