|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using nanoFramework.Presentation.Media; |
| 5 | +using nanoFramework.UI; |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace nanoFramework |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Console class to display text on screens |
| 12 | + /// </summary> |
| 13 | + public static class Console |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// The font to use in the screen |
| 17 | + /// </summary> |
| 18 | + public static Font Font { get; set; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// The foreground color. Default is White. |
| 22 | + /// </summary> |
| 23 | + public static Color ForegroundColor { get; set; } = Color.White; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The background color. Default is Black. |
| 27 | + /// </summary> |
| 28 | + public static Color BackgroundColor { get; set; } = Color.Black; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the height of the screen in rows |
| 32 | + /// </summary> |
| 33 | + public static int WindowHeight { get => DisplayControl.ScreenHeight / Font.Height; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the width of the screen in columns. Note this is based on the max character size. |
| 37 | + /// If you are using a non fix font, you will most likely be able to writ"e more characters. |
| 38 | + /// </summary> |
| 39 | + public static int WindowWidth { get => DisplayControl.ScreenWidth / Font.MaxWidth; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets or sets the column position. |
| 43 | + /// </summary> |
| 44 | + public static int CursorLeft { get; set; } = 0; |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets or sets the row position. |
| 48 | + /// </summary> |
| 49 | + public static int CursorTop { get; set; } = 0; |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Clears the screen. |
| 53 | + /// </summary> |
| 54 | + public static void Clear() |
| 55 | + { |
| 56 | + DisplayControl.Clear(); |
| 57 | + CursorLeft = 0; |
| 58 | + CursorTop = 0; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Resets the colors to their default, White for the foreground and Black for the background. |
| 63 | + /// </summary> |
| 64 | + public static void ResetColor() |
| 65 | + { |
| 66 | + ForegroundColor = Color.White; |
| 67 | + BackgroundColor = Color.Black; |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Writes a text on the screen at the cursor position. |
| 72 | + /// Cursor position will automatically increase. |
| 73 | + /// </summary> |
| 74 | + /// <remarks>No new line character will be recognized, use WriteLine instead.</remarks> |
| 75 | + /// <param name="text">The text to display.</param> |
| 76 | + public static void Write(string text) |
| 77 | + { |
| 78 | + // NOTE: Some work needs to be adjusted on the native side to properly handle the character display. Once done, adjustments will be needed. |
| 79 | + // Make the math for the lines, it's based out the Max width of the character and won't be perfect |
| 80 | + // Console is recommended with fixed size fonts |
| 81 | + ushort width = (ushort)(DisplayControl.ScreenWidth - CursorLeft * Font.MaxWidth); |
| 82 | + if (text.Length <= width / Font.MaxWidth) |
| 83 | + { |
| 84 | + DisplayControl.Write(text, (ushort)(CursorLeft * Font.MaxWidth), (ushort)(CursorTop * Font.Height), (ushort)(DisplayControl.ScreenWidth - 1), (ushort)(DisplayControl.ScreenHeight - 1), Font, ForegroundColor, BackgroundColor); |
| 85 | + CursorLeft += text.Length; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + DisplayControl.Write(text.Substring(0, width / Font.MaxWidth + 1), (ushort)(CursorLeft * Font.MaxWidth), (ushort)(CursorTop * Font.Height), (ushort)(DisplayControl.ScreenWidth - 1), (ushort)(DisplayControl.ScreenHeight - 1), Font, ForegroundColor, BackgroundColor); |
| 90 | + CursorTop++; |
| 91 | + string newTxt = text.Substring(width / Font.MaxWidth + 1); |
| 92 | + DisplayControl.Write(newTxt, 0, (ushort)(CursorTop * Font.Height), (ushort)(DisplayControl.ScreenWidth - 1), (ushort)(DisplayControl.ScreenHeight - 1), Font, ForegroundColor, BackgroundColor); |
| 93 | + CursorLeft = newTxt.Length % WindowWidth; |
| 94 | + CursorTop += newTxt.Length / WindowWidth; |
| 95 | + CursorTop = CursorTop > WindowHeight ? WindowHeight : CursorTop; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Writes a text on the screen at the cursor position and goes to the next line. |
| 101 | + /// </summary> |
| 102 | + /// <param name="text">The text to display.</param> |
| 103 | + public static void WriteLine(string text) |
| 104 | + { |
| 105 | + Write(text); |
| 106 | + CursorLeft = 0; |
| 107 | + CursorTop++; |
| 108 | + CursorTop = CursorTop > WindowHeight ? WindowHeight : CursorTop; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments