-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathC64ConfigUIConsole.cs
More file actions
403 lines (353 loc) · 17 KB
/
C64ConfigUIConsole.cs
File metadata and controls
403 lines (353 loc) · 17 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using Highbyte.DotNet6502.AI.CodingAssistant;
using Highbyte.DotNet6502.App.SadConsole.SystemSetup;
using Highbyte.DotNet6502.Systems.Commodore64.Config;
using Highbyte.DotNet6502.Systems.Commodore64.Utils.BasicAssistant;
using Highbyte.DotNet6502.Utils;
using Microsoft.Extensions.Configuration;
using SadConsole.UI;
using SadConsole.UI.Controls;
using SadRogue.Primitives;
namespace Highbyte.DotNet6502.App.SadConsole.ConfigUI;
public class C64ConfigUIConsole : Window
{
public const int CONSOLE_WIDTH = USABLE_WIDTH;
public const int CONSOLE_HEIGHT = USABLE_HEIGHT;
private const int USABLE_WIDTH = 60;
private const int USABLE_HEIGHT = 28;
public C64SystemConfig C64SystemConfig => C64HostConfig.SystemConfig;
public readonly C64HostConfig C64HostConfig;
private readonly IConfiguration _configuration;
public C64ConfigUIConsole(SadConsoleHostApp sadConsoleHostApp, IConfiguration configuration) : base(CONSOLE_WIDTH, CONSOLE_HEIGHT)
{
C64HostConfig = (C64HostConfig)sadConsoleHostApp.CurrentHostSystemConfig.Clone();
Controls.ThemeColors = SadConsoleUISettings.ThemeColors;
Surface.DefaultBackground = Controls.ThemeColors.ControlHostBackground;
Surface.DefaultForeground = Controls.ThemeColors.ControlHostForeground;
Surface.Clear();
Title = "C64 Config";
var colors = Controls.GetThemeColors();
Cursor.PrintAppearanceMatchesHost = false;
Cursor.DisableWordBreak = true;
Cursor.SetPrintAppearance(colors.Title, Surface.DefaultBackground);
UseMouse = true;
UseKeyboard = true;
DrawUIItems();
_configuration = configuration;
}
private void DrawUIItems()
{
// Automatic download of C64 ROMs
var autoDownloadROMButton = new Button("Auto download ROM files")
{
Name = "autoDownloadROMButton",
Position = (1, 2),
};
autoDownloadROMButton.Click += async (s, e) =>
{
var autoDownloadROMInfoLabel = Controls["autoDownloadROMInfoLabel"] as Label;
try
{
await AutoDownloadROMs();
autoDownloadROMInfoLabel.DisplayText = "ROMs downloaded OK";
autoDownloadROMInfoLabel.TextColor = Color.Green;
}
catch (Exception ex)
{
autoDownloadROMInfoLabel.DisplayText = ex.Message;
autoDownloadROMInfoLabel.TextColor = Color.Red;
}
finally
{
IsDirty = true; // Mark as dirty to update the UI
}
};
Controls.Add(autoDownloadROMButton);
// Manual download link
var openROMDownloadURLButton = new Button("Manual ROM download link")
{
Name = "openROMDownloadURLButton",
Position = (31, autoDownloadROMButton.Position.Y),
};
openROMDownloadURLButton.Click += (s, e) => OpenURL(new Uri(C64SystemConfig.ROMDownloadUrls[C64SystemConfig.KERNAL_ROM_NAME]).GetLeftPart(UriPartial.Authority));
Controls.Add(openROMDownloadURLButton);
// Auto ROM download status label
var autoDownloadROMInfoLabel = new Label(Width - 10)
{
Name = "autoDownloadROMInfoLabel",
Position = (1, autoDownloadROMButton.Bounds.MaxExtentY + 1),
IsEnabled = false,
DisplayText = "",
TextColor = Controls.GetThemeColors().Appearance_ControlDisabled.Foreground
};
Controls.Add(autoDownloadROMInfoLabel);
// ROM directory
var romDirectoryLabel = CreateLabel("ROM directory", 1, autoDownloadROMInfoLabel.Position.Y + 2);
var romDirectoryTextBox = new TextBox(Width - 10)
{
Name = "romDirectoryTextBox",
Position = (1, romDirectoryLabel.Position.Y + 1),
};
romDirectoryTextBox.TextChanged += (s, e) => { C64SystemConfig.ROMDirectory = romDirectoryTextBox.Text; IsDirty = true; };
Controls.Add(romDirectoryTextBox);
var selectROMDirectoryButton = new Button("...")
{
Name = "selectROMDirectoryButton",
Position = (romDirectoryTextBox.Bounds.MaxExtentX + 2, romDirectoryTextBox.Position.Y),
};
selectROMDirectoryButton.Click += (s, e) => ShowROMFolderPickerDialog();
Controls.Add(selectROMDirectoryButton);
// Kernal ROM file
var kernalROMLabel = CreateLabel("Kernal ROM", 1, romDirectoryTextBox.Position.Y + 2);
var kernalROMTextBox = new TextBox(Width - 10)
{
Name = "kernalROMTextBox",
Position = (1, kernalROMLabel.Position.Y + 1)
};
kernalROMTextBox.TextChanged += (s, e) => { C64SystemConfig.SetROM(C64SystemConfig.KERNAL_ROM_NAME, kernalROMTextBox!.Text); IsDirty = true; };
Controls.Add(kernalROMTextBox);
var selectKernalROMButton = new Button("...")
{
Name = "selectKernalROMButton",
Position = (kernalROMTextBox.Bounds.MaxExtentX + 2, kernalROMTextBox.Position.Y),
};
selectKernalROMButton.Click += (s, e) => ShowROMFilePickerDialog(C64SystemConfig.KERNAL_ROM_NAME);
Controls.Add(selectKernalROMButton);
// Basic ROM file
var basicROMLabel = CreateLabel("Basic ROM", 1, kernalROMTextBox.Bounds.MaxExtentY + 2);
var basicROMTextBox = new TextBox(Width - 10)
{
Name = "basicROMTextBox",
Position = (1, basicROMLabel.Position.Y + 1)
};
basicROMTextBox.TextChanged += (s, e) => { C64SystemConfig.SetROM(C64SystemConfig.BASIC_ROM_NAME, basicROMTextBox!.Text); IsDirty = true; };
Controls.Add(basicROMTextBox);
var selectBasicROMButton = new Button("...")
{
Name = "selectBasicROMButton",
Position = (basicROMTextBox.Bounds.MaxExtentX + 2, basicROMTextBox.Position.Y),
};
selectBasicROMButton.Click += (s, e) => ShowROMFilePickerDialog(C64SystemConfig.BASIC_ROM_NAME);
Controls.Add(selectBasicROMButton);
// Chargen ROM file
var chargenROMLabel = CreateLabel("Chargen ROM", 1, basicROMTextBox.Bounds.MaxExtentY + 2);
var chargenROMTextBox = new TextBox(Width - 10)
{
Name = "chargenROMTextBox",
Position = (1, chargenROMLabel.Position.Y + 1),
};
chargenROMTextBox.TextChanged += (s, e) => { C64SystemConfig.SetROM(C64SystemConfig.CHARGEN_ROM_NAME, chargenROMTextBox!.Text); IsDirty = true; };
Controls.Add(chargenROMTextBox);
var selectChargenROMButton = new Button("...")
{
Name = "selectChargenROMButton",
Position = (chargenROMTextBox.Bounds.MaxExtentX + 2, chargenROMTextBox.Position.Y),
};
selectChargenROMButton.Click += (s, e) => ShowROMFilePickerDialog(C64SystemConfig.CHARGEN_ROM_NAME);
Controls.Add(selectChargenROMButton);
// AI coding assistant selection
var codingAssistantLabel = CreateLabel("Basic AI assistant: ", 1, selectChargenROMButton.Bounds.MaxExtentY + 2);
var codingAssistantValue = CreateLabel($"{C64HostConfig.CodeSuggestionBackendType}", codingAssistantLabel.Bounds.MaxExtentX + 1, codingAssistantLabel.Position.Y);
codingAssistantValue.TextColor = Controls.GetThemeColors().White;
var codingAssistantInfoLabel = new Label(Width - 10)
{
Name = "codingAssistantInfoLabel",
Position = (1, codingAssistantValue.Bounds.MaxExtentY + 1),
IsEnabled = false,
DisplayText = "Set AI assistant in appsetting.json.",
TextColor = Controls.GetThemeColors().Appearance_ControlDisabled.Foreground
};
Controls.Add(codingAssistantInfoLabel);
var codingAssistantTestButton = new Button("Test")
{
Name = "codingAssistantTestButton",
Position = (codingAssistantValue.Bounds.MaxExtentX + 2, codingAssistantValue.Position.Y),
};
codingAssistantTestButton.Click += async (s, e) =>
{
try
{
var codeSuggestionBackend = CodeSuggestionConfigurator.CreateCodeSuggestion(C64HostConfig.CodeSuggestionBackendType, _configuration, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_ADDITIONAL_SYSTEM_INSTRUCTION);
codingAssistantInfoLabel.DisplayText = "Testing...";
codingAssistantInfoLabel.TextColor = Color.White;
await codeSuggestionBackend.CheckAvailability();
if (codeSuggestionBackend.IsAvailable)
{
codingAssistantInfoLabel.DisplayText = "OK";
codingAssistantInfoLabel.TextColor = Color.Green;
}
else
{
codingAssistantInfoLabel.DisplayText = codeSuggestionBackend.LastError ?? "Error";
codingAssistantInfoLabel.TextColor = Color.Red;
}
}
catch (Exception ex)
{
codingAssistantInfoLabel.DisplayText = ex.Message;
codingAssistantInfoLabel.TextColor = Color.Red;
}
};
Controls.Add(codingAssistantTestButton);
var openBasicAIHelpURLButton = new Button("Help")
{
Name = "openBasicAIHelpURLButton",
Position = (codingAssistantTestButton.Bounds.MaxExtentX, codingAssistantInfoLabel.Position.Y),
};
openBasicAIHelpURLButton.Click += (s, e) => OpenURL("https://github.com/highbyte/dotnet-6502/blob/master/doc/SYSTEMS_C64_AI_CODE_COMPLETION.md");
Controls.Add(openBasicAIHelpURLButton);
//ComboBox codingAssistantComboBox = new ComboBox(codingAssistantLabel.Bounds.MaxExtentX + 1, codingAssistantLabel.Position.Y, 6, Enum.GetNames<CodeSuggestionBackendTypeEnum>().ToArray())
//{
// Position = (codingAssistantLabel.Bounds.MaxExtentX + 2, codingAssistantLabel.Position.Y),
// Name = "codingAssistantComboBox",
// SelectedItem = C64HostConfig.CodeSuggestionBackendType.ToString(),
//};
//codingAssistantComboBox.SelectedItemChanged += (s, e) =>
//{
// C64HostConfig.CodeSuggestionBackendType = (CodeSuggestionBackendTypeEnum)codingAssistantComboBox.SelectedItem;
// IsDirty = true;
//};
//Controls.Add(codingAssistantComboBox);
// Validaton errors
var validationErrorsLabel = CreateLabel("Validation errors", 1, codingAssistantInfoLabel.Bounds.MaxExtentY + 2, "validationErrorsLabel");
var validationErrorsListBox = new ListBox(Width - 3, 3)
{
Name = "validationErrorsListBox",
Position = (1, validationErrorsLabel.Bounds.MaxExtentY + 1),
IsScrollBarVisible = true,
IsEnabled = true,
};
Controls.Add(validationErrorsListBox);
var okButton = new Button(6, 1)
{
Name = "okButton",
Text = "OK",
Position = (1, Height - 2)
};
okButton.Click += (s, e) => { DialogResult = true; Hide(); };
Controls.Add(okButton);
var cancelButton = new Button(10, 1)
{
Text = "Cancel",
Position = (okButton.Bounds.MaxExtentX + 2, Height - 2)
};
cancelButton.Click += (s, e) => { DialogResult = false; Hide(); };
Controls.Add(cancelButton);
// Helper function to create a label and add it to the console
Label CreateLabel(string text, int col, int row, string? name = null)
{
var labelTemp = new Label(text) { Position = new Point(col, row), Name = name };
Controls.Add(labelTemp);
return labelTemp;
}
// Force OnIsDirtyChanged event which will set control states (see SetControlStates)
OnIsDirtyChanged();
}
private void OpenURL(string url)
{
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
throw new Exception($"Invalid URL: {url}");
// Launch the URL in the default browser
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(uri.AbsoluteUri) { UseShellExecute = true });
}
private void ShowROMFilePickerDialog(string romName)
{
var currentFolder = PathHelper.ExpandOSEnvironmentVariables(C64SystemConfig.ROMDirectory);
var window = new FilePickerConsole(FilePickerMode.OpenFile, currentFolder, C64SystemConfig.GetROM(romName).GetROMFilePath(currentFolder));
window.Center();
window.Closed += (s2, e2) =>
{
if (window.DialogResult)
{
C64SystemConfig.SetROM(romName, Path.GetFileName(window.SelectedFile!.FullName));
IsDirty = true;
}
};
window.Show(true);
}
private void ShowROMFolderPickerDialog()
{
var currentFolder = PathHelper.ExpandOSEnvironmentVariables(C64SystemConfig.ROMDirectory);
var window = new FilePickerConsole(FilePickerMode.OpenFolder, currentFolder);
window.Center();
window.Closed += (s2, e2) =>
{
if (window.DialogResult)
{
C64SystemConfig.ROMDirectory = window.SelectedDirectory.FullName;
IsDirty = true;
}
};
window.Show(true);
}
private async Task AutoDownloadROMs()
{
var romFolder = PathHelper.ExpandOSEnvironmentVariables(C64SystemConfig.ROMDirectory);
if (!Directory.Exists(romFolder))
{
Directory.CreateDirectory(romFolder);
}
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
//httpClient.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpClient.DefaultRequestHeaders.Accept.ParseAdd("*/*");
foreach (var romDownload in C64SystemConfig.ROMDownloadUrls)
{
var romName = romDownload.Key;
var romUrl = romDownload.Value;
var filename = Path.GetFileName(new Uri(romUrl).LocalPath);
var dest = Path.Combine(romFolder, filename);
try
{
using var response = await httpClient.GetAsync(romUrl);
if (!response.IsSuccessStatusCode)
throw new Exception($"Failed to get '{romUrl}' ({(int)response.StatusCode})");
await using var fs = new FileStream(dest, FileMode.Create, FileAccess.Write, FileShare.None);
await response.Content.CopyToAsync(fs);
System.Console.WriteLine($"Downloaded {filename} to {dest}");
// Update the C64SystemConfig with the downloaded ROM file
C64SystemConfig.SetROM(romName, filename);
}
catch (Exception ex)
{
if (File.Exists(dest))
File.Delete(dest);
throw new Exception($"Error downloading {romUrl}: {ex.Message}", ex);
}
}
}
protected override void OnIsDirtyChanged()
{
if (IsDirty)
SetControlStates();
}
private void SetControlStates()
{
var romDirectoryTextBox = Controls["romDirectoryTextBox"] as TextBox;
romDirectoryTextBox!.Text = C64SystemConfig.ROMDirectory;
romDirectoryTextBox!.IsDirty = true;
var kernalROMTextBox = Controls["kernalROMTextBox"] as TextBox;
kernalROMTextBox!.Text = C64SystemConfig.ROMs.Single(x => x.Name == C64SystemConfig.KERNAL_ROM_NAME).File!;
kernalROMTextBox!.IsDirty = true;
var basicROMTextBox = Controls["basicROMTextBox"] as TextBox;
basicROMTextBox!.Text = C64SystemConfig.ROMs.Single(x => x.Name == C64SystemConfig.BASIC_ROM_NAME).File!;
basicROMTextBox!.IsDirty = true;
var chargenROMTextBox = Controls["chargenROMTextBox"] as TextBox;
chargenROMTextBox!.Text = C64SystemConfig.ROMs.Single(x => x.Name == C64SystemConfig.CHARGEN_ROM_NAME).File!;
chargenROMTextBox!.IsDirty = true;
var codingAssistantTestButton = Controls["codingAssistantTestButton"] as Button;
codingAssistantTestButton.IsEnabled = C64HostConfig.CodeSuggestionBackendType != CodeSuggestionBackendTypeEnum.None;
var isOk = C64SystemConfig.IsValid(out List<string> validationErrors);
var okButton = Controls["okButton"] as Button;
okButton!.IsEnabled = isOk;
var validationErrorsLabel = Controls["validationErrorsLabel"] as Label;
validationErrorsLabel!.IsVisible = !isOk;
var validationErrorsListBox = Controls["validationErrorsListBox"] as ListBox;
validationErrorsListBox.IsVisible = !isOk;
validationErrorsListBox!.Items.Clear();
foreach (var error in validationErrors)
{
var coloredString = new ColoredString(error, foreground: Color.Red, background: Color.Black);
validationErrorsListBox!.Items.Add(coloredString);
}
}
}