This repository was archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleVisuals.cs
More file actions
477 lines (459 loc) · 20 KB
/
Copy pathConsoleVisuals.cs
File metadata and controls
477 lines (459 loc) · 20 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using static System.Console;
using static System.Threading.Thread;
using static System.IO.File;
using static System.ConsoleColor;
using static System.ConsoleKey;
namespace MDD_FlowerStore;
/// <summary> The <see cref="ConsoleVisuals"/> classe contains all the visual elements for a console app. </summary>
public static class ConsoleVisuals
{
#region Private attributes
private const string titlePath = "title.txt";
private static string[] titleContent = ReadAllLines(titlePath);
private static int initialWindowWidth = WindowWidth;
private static int intialWindowHeight = WindowHeight;
private static (ConsoleColor, ConsoleColor) colorPanel = (White, Black);
private static (ConsoleColor, ConsoleColor) initialColorPanel = (colorPanel.Item1, colorPanel.Item2);
private static (ConsoleColor, ConsoleColor) terminalColorpanel = (ForegroundColor, BackgroundColor);
#endregion
#region Private properties
public static (string, string, string) defaultHeader = (" Projet BDD", "Exécution...", "Réalisé par Yann et Sherylann ");
public static (string, string, string) defaultFooter = (" [ESC] Retour", "[Z|↑] Monter [S|↓] Descendre", "[ENTRER] Sélectionner ");
private static int TitleHeight => titleContent.Length;
private static int HeaderHeigth => TitleHeight ;
private static int FooterHeigth => WindowHeight - 1;
private static int ContentHeigth => HeaderHeigth + 2;
private static bool WindowManipulated => WindowWidth != initialWindowWidth || WindowHeight != intialWindowHeight;
#endregion
#region Enums
/// <summary> The <see cref="Placement"/> enum defines the placement of a string in the console. </summary>
public enum Placement
{
/// <summary> The string is placed at the left of the console. </summary>
Left,
/// <summary> The string is placed at the center of the console. </summary>
Center,
/// <summary> The string is placed at the right of the console. </summary>
Right
}
#endregion
#region Low level methods
/// <summary> this method changes the font color of the console. </summary>
public static void ChangeFont(ConsoleColor newfont)
{
colorPanel.Item1 = newfont;
}
private static void TryNegative(bool negative = false)
{
ForegroundColor = negative ? colorPanel.Item2 : colorPanel.Item1;
BackgroundColor = negative ? colorPanel.Item1 : colorPanel.Item2;
}
private static void WritePositionnedString(string str, Placement position = Placement.Center, bool negative = false, int line = -1, bool chariot = false)
{
TryNegative(negative);
if (line < 0)
line = Console.CursorTop;
if (str.Length < Console.WindowWidth)
switch (position)
{
case (Placement.Left):
SetCursorPosition(0, line);
break;
case (Placement.Center):
SetCursorPosition((WindowWidth - str.Length) / 2, line);
break;
case (Placement.Right):
SetCursorPosition(WindowWidth - str.Length, line);
break;
}
else
SetCursorPosition(0, line);
if (chariot)
WriteLine(str);
else
Write(str);
TryNegative(default);
}
private static void ClearLine(int line)
{
TryNegative(default);
WritePositionnedString("".PadRight(Console.WindowWidth), Placement.Left, default, line);
}
/// <summary> This method clears a specified part of the console. </summary>
/// <param name="startIndex"> The index of the first line to clear. </param>
/// <param name="length"> The number of lines to clear. </param>
public static void ClearPanel(int startIndex = -1, int length = 1)
{
if (startIndex == -1)
startIndex = ContentHeigth;
for (int i = startIndex; i < startIndex + length; i++)
ClearLine(i);
}
/// <summary> This method clears the content of the console. </summary>
public static void ClearContent()
{
for (int i = ContentHeigth - 1; i < FooterHeigth; i++)
ClearLine(i);
}
private static void ClearAll()
{
colorPanel = terminalColorpanel;
for (int i = 0; i < WindowHeight; i++)
ContinuousPrint("".PadRight(WindowWidth), i, default, 100, 10);
Clear();
colorPanel = (White, Black);
}
private static void ContinuousPrint(string text, int line, bool negative = false, int stringTime = 2000, int endStringTime = 1000)
{
int t_interval = (int)(stringTime / text.Length);
for (int i = 0; i <= text.Length; i++)
{
string continuous = "";
for(int j = 0; j < i; j++)
continuous += text[j];
continuous = continuous.PadRight(text.Length);
WritePositionnedString(continuous.BuildString(WindowWidth, Placement.Center), default, negative, line);
if(i != text.Length)
Sleep(t_interval);
if(KeyAvailable)
{
ConsoleKeyInfo keyPressed = ReadKey(true);
if(keyPressed.Key == Enter || keyPressed.Key == Escape)
{
i = text.Length;
break;
}
}
}
WritePositionnedString(text.BuildString(WindowWidth, Placement.Center), default, negative, line);
Sleep(endStringTime);
}
private static bool IsScreenUpdated()
{
if (WindowManipulated || colorPanel != initialColorPanel)
{
WriteFullScreen(true);
initialWindowWidth = WindowWidth;
initialColorPanel = (colorPanel.Item1, colorPanel.Item2);
return true;
}
return false;
}
#endregion
#region Mid level methods
/// <summary> This method prints a float matrix in the console. </summary>
/// <param name="matrix"> The matrix to print. </param>
/// <param name="currentPosition"> The current position of the cursor. </param>
/// <param name="line"> The line where the matrix will be printed. </param>
public static void WriteMatrix(float[,] matrix, Position currentPosition, int line)
{
for(int i = line; i < matrix.GetLength(0); i++)
ClearLine(i);
SetCursorPosition(0, line);
for (int i = 0; i < matrix.GetLength(0); i++)
{
Write("{0,"+((WindowWidth / 2) - (matrix.GetLength(1))) + "}","");
for (int j = 0; j < matrix.GetLength(1); j++)
{
TryNegative(currentPosition.Equals(new Position(i, j)));
Write(matrix[i, j]);
TryNegative(default);
Write(" ");
}
WriteLine("");
}
}
/// <summary> This method prints the title of the console app. </summary>
public static void WriteTitle()
{
Clear();
SetCursorPosition(0, 0);
foreach (string line in titleContent)
{
WritePositionnedString(line.BuildString(WindowWidth, Placement.Center));
WriteLine("");
}
}
/// <summary> This method prints a banner in the console. </summary>
/// <param name="banner"> The banner to print. </param>
/// <param name="header"> If true, the banner is printed at the top of the console. If false, the banner is printed at the bottom of the console. </param>
/// <param name="straight"> If true, the title is not continuously printed. </param>
public static void WriteBanner((string, string, string)? banner = null, bool header = true, bool straight = false)
{
(string, string, string) newBanner = banner ??= header ? defaultHeader : defaultFooter;
TryNegative(true);
string strBanner = newBanner.Item2.BuildString(Console.WindowWidth, Placement.Center, true);
strBanner = strBanner.Substring(0, strBanner.Length - newBanner.Item3.Length) + newBanner.Item3;
strBanner = newBanner.Item1 + strBanner.Substring(newBanner.Item1.Length);
if (straight)
WritePositionnedString(strBanner, default, true, header ? HeaderHeigth : FooterHeigth);
else
ContinuousPrint(strBanner, header ? HeaderHeigth : FooterHeigth, true);
TryNegative();
}
/// <summary> This method prints a full screen in the console. </summary>
/// <param name="straight"> If true, the title is not continuously printed. </param>
/// <param name="header"> The header of the screen. </param>
/// <param name="footer"> The footer of the screen. </param>
public static void WriteFullScreen(bool straight = false, (string, string, string)? header = null, (string, string, string)? footer = null)
{
if (header is null)
header = defaultHeader;
if (footer is null)
footer = defaultFooter;
CursorVisible = false;
WriteTitle();
WriteBanner(header, true, straight);
WriteBanner(footer, false, straight);
ClearContent();
if (!straight)
LoadingScreen("[ Chargement ... ]");
ClearContent();
}
#endregion
#region High level methods
/// <summary> This method prints a message in the console and gets a string written by the user. </summary>
/// <param name="message"> The message to print. </param>
/// <param name="line"> The line where the message will be printed. </param>
/// <returns> The string written by the user. </returns>
public static string WritePrompt(string message, int line = -1)
{
if (line == -1)
line = ContentHeigth;
if (!IsScreenUpdated())
ClearPanel(line, 3);
ContinuousPrint(message.BuildString(message.Length, Placement.Center), line, default, 1500, 50);
string prompt = "";
do
{
ClearLine(line + 1);
Write("{0," + ((WindowWidth / 2) - (message.Length / 2) + 2) + "}", "> ");
CursorVisible = true;
prompt = ReadLine() ?? "";
CursorVisible = false;
} while (prompt is "");
ClearPanel(line, 3);
return prompt;
}
/// <summary> This method prints a paragraph in the console. </summary>
/// <param name="text"> The lines of the paragraph. </param>
/// <param name="negative"> If true, the paragraph is printed in the negative colors. </param>
/// <param name="line"> The height of the paragraph. </param>
public static void WriteParagraph(IEnumerable<string> text, bool negative = false, int line = -1, Placement placement = Placement.Center)
{
IsScreenUpdated();
if (line == -1)
line = ContentHeigth;
ClearPanel(line, text.Count());
TryNegative(negative);
int maxLength = text.Count() > 0 ? text.Max(s => s.Length) : 0;
foreach (string str in text)
{
WritePositionnedString(str.BuildString(maxLength, Placement.Center), placement, negative, line++);
if (line >= WindowHeight - 1)
break;
}
TryNegative(default);
//ClearPanel(line, text.Count());
}
/// <summary> This method prints a menu in the console and gets the choice of the user. </summary>
/// <param name="question"> The question to print. </param>
/// <param name="choices"> The choices of the menu. </param>
/// <param name="line"> The line where the menu is printed. </param>
/// <returns> The choice of the user. </returns>
public static int ScrollingMenu(string question, string[] choices, Placement location = Placement.Center, int line = -1, bool clear = true, int delay = 1500, int posdefaut = 0)
{
IsScreenUpdated();
if (line == -1)
line = ContentHeigth;
int currentPosition = posdefaut;
int maxLength = choices.Count() > 0 ? choices.Max(s => s.Length) : 0;
for (int i = 0; i < choices.Length; i++)
choices[i] = choices[i].PadRight(maxLength);
ContinuousPrint(question, line, default, delay, 50);
while (true)
{
string[] currentChoice = new string[choices.Length];
for (int i = 0; i < choices.Length; i++)
{
if (i == currentPosition)
{
currentChoice[i] = $" ▶ {choices[i]} ";
WritePositionnedString(currentChoice[i], location, true, line + 2 + i);
continue;
}
currentChoice[i] = $" {choices[i]} ";
WritePositionnedString(currentChoice[i], location, false, line + 2 + i);
}
switch (ReadKey(true).Key)
{
case UpArrow: case Z:
if (currentPosition == 0)
currentPosition = choices.Length - 1;
else if (currentPosition > 0)
currentPosition--;
break;
case DownArrow: case S:
if (currentPosition == choices.Length - 1)
currentPosition = 0;
else if (currentPosition < choices.Length - 1)
currentPosition++;
break;
case Enter:
if (clear)
ClearPanel(line, choices.Length + 2);
return currentPosition;
case Escape:
if (clear)
ClearPanel(line, choices.Length + 2);
return -1;
}
}
}
public static string ScrollingMenuString(string question, string[] choices, Placement location = Placement.Center, int line = -1)
{
IsScreenUpdated();
if (line == -1)
line = ContentHeigth;
string[] propositions = new string[choices.Length];
Array.Copy(choices, propositions, choices.Length);
int currentPosition = 0;
int maxLength = propositions.Count() > 0 ? propositions.Max(s => s.Length) : 0;
for (int i = 0; i < propositions.Length; i++)
propositions[i] = propositions[i].PadRight(maxLength);
ContinuousPrint(question, line, default, 1500, 50);
while (true)
{
string[] currentChoice = new string[propositions.Length];
for (int i = 0; i < propositions.Length; i++)
{
if (i == currentPosition)
{
currentChoice[i] = $" ▶ {propositions[i]} ";
WritePositionnedString(currentChoice[i], location, true, line + 2 + i);
continue;
}
currentChoice[i] = $" {propositions[i]} ";
WritePositionnedString(currentChoice[i], location, false, line + 2 + i);
}
switch (ReadKey(true).Key)
{
case UpArrow: case Z:
if (currentPosition == 0)
currentPosition = propositions.Length - 1;
else if (currentPosition > 0)
currentPosition--;
break;
case DownArrow: case S:
if (currentPosition == propositions.Length - 1)
currentPosition = 0;
else if (currentPosition < propositions.Length - 1)
currentPosition++;
break;
case Enter:
ClearPanel(line, propositions.Length + 2);
return choices[currentPosition];
case Escape:
ClearPanel(line, propositions.Length + 2);
return "Retour";
}
}
}
/// <summary> This method prints a menu in the console and gets the choice of the user. </summary>
/// <param name="question"> The question to print. </param>
/// <param name="min"> The minimum value of the number. </param>
/// <param name="max"> The maximum value of the number. </param>
/// <param name="start"> The starting value of the number. </param>
/// <param name="step"> The step of the number. </param>
/// <param name="line"> The line where the menu is printed. </param>
/// <returns> The number chose of the user. </returns>
public static float NumberSelector(string question, float min, float max, float start = 0,float step = 100, int line = -1)
{
IsScreenUpdated();
if (line == -1)
line = ContentHeigth;
float currentNumber = start;
ContinuousPrint(question, line, default, 1500, 50);
while (true)
{
WritePositionnedString($" ▶ {(float)Math.Round(currentNumber, 1)} ◀ ", Placement.Center, true, line + 2);
switch (ReadKey(true).Key)
{
case UpArrow: case Z:
if (currentNumber + step <= max)
currentNumber += step;
else if (currentNumber + step > max)
currentNumber = min;
break;
case DownArrow: case S:
if (currentNumber - step >= min)
currentNumber -= step;
else if (currentNumber - step < min)
currentNumber = max;
break;
case Enter:
ClearPanel(line, 4);
return currentNumber;
case Escape:
ClearPanel(line, 4);
return -1;
}
Sleep(1);
ClearLine(line +2);
}
}
/// <summary> This method prints a loading screen in the console. </summary>
/// <param name="text"> The text to print. </param>
public static void LoadingScreen(string text)
{
if(!IsScreenUpdated())
ClearPanel(ContentHeigth, 3);
WritePositionnedString(text.BuildString(WindowWidth, Placement.Center), default, default, ContentHeigth, true);
string loadingBar = "";
for(int j = 0; j < text.Length; j++)
loadingBar += '█';
ContinuousPrint(loadingBar, ContentHeigth + 2);
}
/// <summary> This method exits the program. </summary>
public static void ProgramExit()
{
LoadingScreen("[ Sortie du programme ... ]");
ClearAll();
CursorVisible = true;
Environment.Exit(0);
}
#endregion
#region Extensions
/// <summary> This method builds a string with a specific size and a specific placement. </summary>
/// <param name="str"> The string to build. </param>
/// <param name="size"> The size of the string. </param>
/// <param name="position"> The placement of the string. </param>
/// <param name="truncate"> If true, the string is truncated if it is too long. </param>
/// <returns> The built string. </returns>
public static string BuildString(this string str, int size, Placement position = Placement.Center, bool truncate = true)
{
int padding = size - str.Length;
if (truncate && padding < 0)
switch (position)
{
case (Placement.Left):
return str.Substring(0, size);
case (Placement.Center):
return str.Substring((- padding) / 2, size);
case (Placement.Right):
return str.Substring(- padding, size);
}
else
switch (position)
{
case (Placement.Left):
return str.PadRight(size);
case (Placement.Center):
return str.PadLeft(padding / 2 + padding % 2 + str.Length).PadRight(padding + str.Length);
case (Placement.Right):
return str.PadLeft(size);
}
return str;
}
#endregion
}