-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentSystem.cs
More file actions
257 lines (210 loc) · 7.49 KB
/
ComponentSystem.cs
File metadata and controls
257 lines (210 loc) · 7.49 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System.Drawing;
using Fallout.TerminalHacker.Models;
using Fallout.TerminalHacker.Services;
namespace Fallout.TerminalHacker.Components;
/// <summary>
/// Base interface for window components (buttons, scrollbars, etc.)
/// </summary>
public interface IComponent
{
/// <summary>
/// Component position relative to window bounds (not content area!)
/// </summary>
Rectangle Bounds { get; }
/// <summary>
/// Parent window this component belongs to
/// </summary>
IWindow? Parent { get; set; }
/// <summary>
/// Whether the component is visible
/// </summary>
bool Visible { get; set; }
/// <summary>
/// Whether the component is enabled for interaction
/// </summary>
bool Enabled { get; set; }
/// <summary>
/// Z-order for overlapping components (higher = on top)
/// </summary>
int ZOrder { get; set; }
/// <summary>
/// Render the component
/// </summary>
Task RenderAsync(IConsoleWriter writer, CancellationToken cancellationToken = default);
/// <summary>
/// Handle mouse events (if applicable)
/// </summary>
bool HandleClick(Point absolutePosition);
/// <summary>
/// Handle keyboard events (if applicable)
/// </summary>
bool HandleKey(ConsoleKeyInfo keyInfo);
/// <summary>
/// Check if a point is within this component
/// </summary>
bool Contains(Point absolutePosition);
}
/// <summary>
/// Base component implementation
/// </summary>
public abstract class BaseComponent : IComponent
{
protected Rectangle _bounds;
public virtual Rectangle Bounds
{
get => _bounds;
protected set => _bounds = value;
}
public IWindow? Parent { get; set; }
public bool Visible { get; set; } = true;
public bool Enabled { get; set; } = true;
public int ZOrder { get; set; } = 0;
protected BaseComponent(int left, int top, int width, int height)
{
_bounds = new Rectangle(left, top, width, height);
}
public abstract Task RenderAsync(IConsoleWriter writer, CancellationToken cancellationToken = default);
public virtual bool HandleClick(Point absolutePosition)
{
return Contains(absolutePosition) && Enabled;
}
public virtual bool HandleKey(ConsoleKeyInfo keyInfo)
{
return false; // Default: don't handle keys
}
public virtual bool Contains(Point absolutePosition)
{
if (Parent == null) return false;
var absoluteBounds = new Rectangle(
Parent.Left + _bounds.Left,
Parent.Top + _bounds.Top,
_bounds.Width,
_bounds.Height
);
return absoluteBounds.Contains(absolutePosition);
}
/// <summary>
/// Get absolute position of component
/// </summary>
protected Point GetAbsolutePosition()
{
if (Parent == null) return _bounds.Location;
return new Point(Parent.Left + _bounds.Left, Parent.Top + _bounds.Top);
}
}
/// <summary>
/// Border button component - appears in window border
/// </summary>
public class BorderButton : BaseComponent
{
public string Text { get; set; }
public CharStyle Style { get; set; } = CharStyle.Normal;
public CharStyle HoverStyle { get; set; } = CharStyle.Bright;
public bool IsHovered { get; set; }
public event EventHandler? Click;
public BorderButton(int left, int top, string text)
: base(left, top, text.Length + 2, 1) // +2 for brackets
{
Text = text;
}
public override async Task RenderAsync(IConsoleWriter writer, CancellationToken cancellationToken = default)
{
if (!Visible || Parent == null) return;
var absolutePos = GetAbsolutePosition();
writer.CursorPosition = new Point(absolutePos.X, absolutePos.Y);
var style = IsHovered && Enabled ? HoverStyle : Style;
using var colorScope = new ColorScope(writer, style);
var buttonText = Enabled ? $"[{Text}]" : $"({Text})";
await writer.WriteAsync(buttonText, cancellationToken);
}
public override bool HandleClick(Point absolutePosition)
{
if (base.HandleClick(absolutePosition))
{
Click?.Invoke(this, EventArgs.Empty);
return true;
}
return false;
}
}
/// <summary>
/// Scroll bar component for scrollable content
/// </summary>
public class ScrollBar : BaseComponent
{
public int TotalItems { get; set; }
public int VisibleItems { get; set; }
public int ScrollPosition { get; set; }
public event EventHandler<int>? ScrollChanged;
public ScrollBar(int left, int top, int height)
: base(left, top, 1, height)
{
}
public override async Task RenderAsync(IConsoleWriter writer, CancellationToken cancellationToken = default)
{
if (!Visible || Parent == null) return;
var absolutePos = GetAbsolutePosition();
// Calculate scroll thumb position
int scrollableArea = _bounds.Height;
int thumbPosition = TotalItems > VisibleItems
? (ScrollPosition * scrollableArea) / Math.Max(1, TotalItems - VisibleItems)
: 0;
for (int i = 0; i < _bounds.Height; i++)
{
writer.CursorPosition = new Point(absolutePos.X, absolutePos.Y + i);
if (i == thumbPosition && TotalItems > VisibleItems)
{
using var scope = new ColorScope(writer, CharStyle.Bright);
await writer.WriteAsync("█", cancellationToken);
}
else
{
using var scope = new ColorScope(writer, CharStyle.Dimmed);
await writer.WriteAsync("░", cancellationToken);
}
}
}
public override bool HandleClick(Point absolutePosition)
{
if (base.HandleClick(absolutePosition))
{
var relativeY = absolutePosition.Y - GetAbsolutePosition().Y;
var newScrollPos = (relativeY * Math.Max(0, TotalItems - VisibleItems)) / Math.Max(1, _bounds.Height);
if (newScrollPos != ScrollPosition)
{
ScrollPosition = Math.Max(0, Math.Min(TotalItems - VisibleItems, newScrollPos));
ScrollChanged?.Invoke(this, ScrollPosition);
}
return true;
}
return false;
}
}
/// <summary>
/// Status bar component - appears at bottom border
/// </summary>
public class StatusBar : BaseComponent
{
public string Text { get; set; } = string.Empty;
public CharStyle Style { get; set; } = CharStyle.Inverse;
public StatusBar(int left, int top, int width)
: base(left, top, width, 1)
{
}
public override async Task RenderAsync(IConsoleWriter writer, CancellationToken cancellationToken = default)
{
if (!Visible || Parent == null) return;
var absolutePos = GetAbsolutePosition();
writer.CursorPosition = new Point(absolutePos.X, absolutePos.Y);
using var colorScope = new ColorScope(writer, Style);
// Pad or truncate text to fit the width
var displayText = Text.Length > _bounds.Width
? Text.Substring(0, _bounds.Width)
: Text.PadRight(_bounds.Width);
await writer.WriteAsync(displayText, cancellationToken);
}
public void SetText(string text)
{
Text = text;
}
}