Element.cs
2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
namespace Flicker
{
public abstract class Element : IRenderable
{
public readonly int Height;
public readonly int Width;
public readonly int X;
public readonly int Y;
protected Element(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
protected Element(float x, float y, float width, float height)
{
x = x.Clamp(0, .99f);
y = y.Clamp(0, .99f);
width = width.Clamp(0, .99f);
height = height.Clamp(0, .99f);
X = (int)(Console.BufferWidth * x);
Y = (int)(Console.BufferHeight * y);
Width = (int)(Console.BufferWidth * width);
Height = (int)(Console.BufferHeight * height);
}
public bool Visible { get; set; } = true;
public char Border { get; set; } = ' ';
public int Padding { get; set; } = 1; // Must be at least 1 (to make room for header), TODO enforce this
public ConsoleColor Foreground { get; set; } = ConsoleColor.White;
public ConsoleColor Background { get; set; } = ConsoleColor.Black;
public Renderer AssociatedRenderer { get; set; }
public virtual void HandleKey(ConsoleKeyInfo key) { }
/// <summary>
/// Draw this element
/// </summary>
void IRenderable.Render(bool selected)
{
if (!Visible) return;
Console.ForegroundColor = Foreground;
Console.BackgroundColor = Background;
Console.CursorLeft = X;
Console.CursorTop = Y;
Tools.Console.Fill(
X,
Y,
Width,
Height,
' '
);
if (Border != ' ')
{
// Top and bottom borders
Console.CursorLeft = X;
Console.CursorTop = Y;
Console.Write(new string(Border, Width));
Console.CursorLeft = X;
Console.CursorTop = Y + Height - 1;
Console.Write(new string(Border, Width));
// Left and right borders
for (var y = Y; y < Y + Height - 1; ++y)
{
Tools.Console.WriteAt(X, y, Border.ToString());
Tools.Console.WriteAt(X + Width - 1, y, Border.ToString());
}
}
if (selected)
Tools.Console.WriteAt(
X, Y,
new string('\u2580', Width),
ConsoleColor.Red
);
Console.CursorLeft = X + Padding * 2;
Console.CursorTop = Y + Padding;
CustomRender();
Console.ResetColor();
}
public void Select() => AssociatedRenderer.Select(this);
public void Destroy() => AssociatedRenderer.Destroy(this);
protected virtual void CustomRender() { }
}
}