public MainWindow() { InitializeComponent(); var bitmap = new WriteableBitmap(212, 104, 96, 96, PixelFormats.Bgra32, null); _inky = new DrawingInkyDriver(212, 104, InkyDisplayColour.Yellow, bitmap); imgInky.Source = bitmap; _ = DrawHelloWorld(); }
/// <summary> /// Write a string to the screen /// </summary> /// <param name="inky">Inky driver</param> /// <param name="x">The x pixel position</param> /// <param name="y">The y pixel position</param> /// <param name="colour">The colour of the text</param> /// <param name="font">The font used</param> /// <param name="text">The text to be displayed</param> /// <param name="spacing">Default space width</param> /// <returns>The bottom right corner of the text</returns> public static (int x, int y) DrawString(this IInkyDriver inky, int x, int y, InkyPixelColour colour, Dictionary <char, bool[, ]> font, string text, int spacing = 2) { if (inky is null) { throw new ArgumentNullException(nameof(inky)); } if (font is null) { throw new ArgumentNullException(nameof(font)); } if (text is null) { return(x, y); } var xp = x; var yp = y; for (var ci = 0; ci < text.Length; ci++) { var c = text[ci]; if (!font.TryGetValue(c, out var map)) { if (!font.TryGetValue('_', out map)) { continue; } } if (c == ' ') { xp += map.GetLength(0) / 3; yp = y + map.GetLength(0); continue; } for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { if (map[i, j]) { inky.TrySetPixel(xp + j, y + i, colour); } } } xp += map.GetLength(1) + spacing; yp = y + map.GetLength(0); } return(xp, yp); }
/// <summary> /// Try set the inky buffer pixel, as long as it's within the bounds of the screen /// </summary> /// <param name="inky">Inky driver</param> /// <param name="x">The x pixel position</param> /// <param name="y">The y pixel position</param> /// <param name="colour">The colour to set the pixel to</param> /// <returns>True if the pixel coordinates were within the bounds of the screen</returns> public static bool TrySetPixel(this IInkyDriver inky, int x, int y, InkyPixelColour colour) { if (inky is null) { throw new ArgumentNullException(nameof(inky)); } if (x < 0 || y < 0 || x >= inky.Width || y >= inky.Height) { return(false); } inky.SetPixel(x, y, colour); return(true); }
/// <summary> /// Draw a rectangle on the inky buffer /// </summary> /// <param name="inky">Inky driver</param> /// <param name="x1">The x pixel position of one corner</param> /// <param name="y1">The y pixel position of one corner</param> /// <param name="x2">The x pixel position of the opposite corner</param> /// <param name="y2">The y pixel position of the opposite corner</param> /// <param name="borderColour">The border colour of the rectangle</param> /// <param name="fillColour">The fill colour of the rectangle (optional)</param> public static void DrawRectangle(this IInkyDriver inky, int x1, int y1, int x2, int y2, InkyPixelColour borderColour, InkyPixelColour?fillColour = null) { if (inky is null) { throw new ArgumentNullException(nameof(inky)); } for (var i = Math.Min(x1, x2); i <= Math.Max(x1, x2); i++) { inky.SetPixel(i, y1, borderColour); if (y1 != y2) { inky.SetPixel(i, y2, borderColour); } } for (var i = Math.Min(y1, y2); i <= Math.Max(y1, y2); i++) { inky.SetPixel(x1, i, borderColour); if (x1 != x2) { inky.SetPixel(x2, i, borderColour); } } if (fillColour.HasValue && Math.Abs(x2 - x1) > 1 && Math.Abs(y2 - y1) > 1) { for (var i = Math.Min(x1, x2) + 1; i < Math.Max(x1, x2); i++) { for (var j = Math.Min(y1, y2) + 1; j < Math.Max(y1, y2); j++) { inky.SetPixel(i, j, fillColour.Value); } } } }