diff --git a/app/AnimeMatrix/AnimeMatrixDevice.cs b/app/AnimeMatrix/AnimeMatrixDevice.cs index 4e251755..58664d8e 100644 --- a/app/AnimeMatrix/AnimeMatrixDevice.cs +++ b/app/AnimeMatrix/AnimeMatrixDevice.cs @@ -1,6 +1,5 @@ // Source thanks to https://github.com/vddCore/Starlight with some adjustments from me -using GHelper; using GHelper.AnimeMatrix.Communication; using System.Drawing.Drawing2D; using System.Drawing.Imaging; @@ -8,7 +7,6 @@ using System.Drawing.Text; using System.Globalization; using System.Management; using System.Text; -using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; namespace Starlight.AnimeMatrix { @@ -80,6 +78,7 @@ namespace Starlight.AnimeMatrix } + public class AnimeMatrixDevice : Device { int UpdatePageLength = 490; @@ -99,6 +98,9 @@ namespace Starlight.AnimeMatrix private static AnimeType _model = AnimeType.GA402; + [System.Runtime.InteropServices.DllImport("gdi32.dll")] + private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts); + private PrivateFontCollection fonts = new PrivateFontCollection(); public AnimeMatrixDevice() : base(0x0B05, 0x193B, 640) @@ -128,8 +130,21 @@ namespace Starlight.AnimeMatrix _displayBuffer = new byte[LedCount]; + LoadMFont(); + } + private void LoadMFont() + { + byte[] fontData = GHelper.Properties.Resources.MFont; + IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length); + System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length); + uint dummy = 0; + + fonts.AddMemoryFont(fontPtr, GHelper.Properties.Resources.MFont.Length); + AddFontMemResourceEx(fontPtr, (uint)GHelper.Properties.Resources.MFont.Length, IntPtr.Zero, ref dummy); + System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr); + } public string GetModel() { @@ -369,20 +384,57 @@ namespace Starlight.AnimeMatrix public void PresentClock() { - int second = DateTime.Now.Second; - string time; - - if (CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern.Contains("H")) - time = DateTime.Now.ToString("H" + ((second % 2 == 0) ? ":" : " ") + "mm"); - else - time = DateTime.Now.ToString("h" + ((second % 2 == 0) ? ":" : " ") + "mmtt"); + string second = (DateTime.Now.Second % 2 == 0) ? ":" : " "; + string time = DateTime.Now.ToString("HH" + second + "mm"); + /* if (_model == AnimeType.GA401) + { PresentText(time); - else - PresentTextDiagonal(time); + return; + } + */ + + Clear(); + TextDiagonal(time, 16, 12, 22); + TextDiagonal(DateTime.Now.ToString("yyyy'. 'MM'. 'dd"), 11F, 2, 8); + Present(); + } + public void TextDiagonal(string text, float fontSize = 10, int deltaX = 0, int deltaY = 10) + { + + int maxX = (int)Math.Sqrt(MaxRows * MaxRows + MaxColumns * MaxColumns); + int textHeight; + + using (Bitmap bmp = new Bitmap(maxX, MaxRows)) + { + using (Graphics g = Graphics.FromImage(bmp)) + { + g.CompositingQuality = CompositingQuality.HighQuality; + g.SmoothingMode = SmoothingMode.AntiAlias; + g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel; + + using (Font font = new Font(fonts.Families[0], fontSize, FontStyle.Regular, GraphicsUnit.Pixel)) + { + SizeF textSize = g.MeasureString(text, font); + textHeight = (int)textSize.Height; + g.DrawString(text, font, Brushes.White, 0, 0); + } + } + + for (int y = 0; y < bmp.Height; y++) + { + for (int x = 0; x < bmp.Width; x++) + { + var pixel = bmp.GetPixel(x, y); + var color = (pixel.R + pixel.G + pixel.B) / 3; + if (color > 100) SetLedDiagonal(x, y, (byte)color, deltaX, deltaY); + } + } + } + } public void PresentText(string text1, string text2 = "") @@ -410,16 +462,41 @@ namespace Starlight.AnimeMatrix } + bmp.Save("test.bmp", ImageFormat.Bmp); + GenerateFrame(bmp); Present(); } } - public void GenerateFrame(Image image, float zoom = 1, int panX = 0, int panY = 0, InterpolationMode quality = InterpolationMode.Default) + public void GenerateFrame(Image image, float zoom = 100, int panX = 0, int panY = 0, InterpolationMode quality = InterpolationMode.Default) { - using (Bitmap bmp = GeneratePicture(image, zoom, panX, panY, quality)) + + int width = MaxColumns / 2 * 6; + int height = MaxRows; + + int targetWidth = MaxColumns * 2; + + float scale; + + using (Bitmap bmp = new Bitmap(targetWidth, height)) { + scale = Math.Min((float)width / (float)image.Width, (float)height / (float)image.Height) * zoom / 100; + + using (var graph = Graphics.FromImage(bmp)) + { + var scaleWidth = (float)(image.Width * scale); + var scaleHeight = (float)(image.Height * scale); + + graph.InterpolationMode = quality; + graph.CompositingQuality = CompositingQuality.HighQuality; + graph.SmoothingMode = SmoothingMode.AntiAlias; + + graph.DrawImage(image, (float)Math.Round(targetWidth - (scaleWidth + panX) * targetWidth / width), -panY, (float)Math.Round(scaleWidth * targetWidth / width), scaleHeight); + + } + for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) @@ -435,85 +512,17 @@ namespace Starlight.AnimeMatrix } - public Bitmap GeneratePicture(Image image, float zoom = 100, int panX = 0, int panY = 0, InterpolationMode quality = InterpolationMode.Default) + public void SetLedDiagonal(int x, int y, byte color, int deltaX = 0, int deltaY = 10) { + x += deltaX; + y -= deltaY; - int width = MaxColumns * 3; - int height = MaxRows; - - int targetWidth = MaxColumns * 2; - - float scale; - - Bitmap bmp = new Bitmap(targetWidth, height); - { - scale = Math.Min((float)width / (float)image.Width, (float)height / (float)image.Height) * zoom / 100; - - using (var graph = Graphics.FromImage(bmp)) - { - var scaleWidth = (float)(image.Width * scale); - var scaleHeight = (float)(image.Height * scale); - - graph.InterpolationMode = quality; - graph.CompositingQuality = CompositingQuality.HighQuality; - graph.SmoothingMode = SmoothingMode.AntiAlias; - - graph.DrawImage(image, (float)Math.Round(targetWidth - (scaleWidth + panX) * targetWidth / width), -panY, (float)Math.Round(scaleWidth * targetWidth / width), scaleHeight); - return bmp; - } - - } + int plX = (x - y) / 2; + int plY = x + y; + SetLedPlanar(plX, plY, color); } - public void SetLedDiagonal(int x, int y, byte color, int delta = 10) - { - //x += delta; - y -= delta; - - int dx = (x - y) / 2; - int dy = x + y; - SetLedPlanar(dx, dy, color); - } - - public void PresentTextDiagonal(string text) - { - - Clear(); - - int maxX = (int)Math.Sqrt(MaxRows * MaxRows + MaxColumns * MaxColumns); - int textHeight; - - using (Bitmap bmp = new Bitmap(maxX, MaxRows)) - { - using (Graphics g = Graphics.FromImage(bmp)) - { - g.CompositingQuality = CompositingQuality.HighQuality; - g.SmoothingMode = SmoothingMode.AntiAlias; - g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel; - - using (Font font = new Font("Consolas", 11, FontStyle.Regular, GraphicsUnit.Pixel)) - { - SizeF textSize = g.MeasureString(text, font); - textHeight = (int)textSize.Height; - g.DrawString(text, font, Brushes.White, 4, 0); - } - } - - for (int y = 0; y < bmp.Height; y++) - { - for (int x = 0; x < bmp.Width; x++) - { - var pixel = bmp.GetPixel(x, y); - var color = (pixel.R + pixel.G + pixel.B) / 3; - SetLedDiagonal(x, y, (byte)color, textHeight - 8); - } - } - } - - Present(); - } - private bool IsRowInRange(int row) { return (row >= 0 && row < MaxRows); diff --git a/app/Properties/Resources.Designer.cs b/app/Properties/Resources.Designer.cs index 52eb7f23..1feaca86 100644 --- a/app/Properties/Resources.Designer.cs +++ b/app/Properties/Resources.Designer.cs @@ -610,6 +610,16 @@ namespace GHelper.Properties { } } + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] MFont { + get { + object obj = ResourceManager.GetObject("MFont", resourceCulture); + return ((byte[])(obj)); + } + } + /// /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). /// diff --git a/app/Properties/Resources.resx b/app/Properties/Resources.resx index b1630dae..0bd6a5cb 100644 --- a/app/Properties/Resources.resx +++ b/app/Properties/Resources.resx @@ -289,4 +289,7 @@ ..\Resources\icons8-rocket-32.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\Resources\Font.otf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file diff --git a/app/Resources/Font.otf b/app/Resources/Font.otf new file mode 100644 index 00000000..ef798d03 Binary files /dev/null and b/app/Resources/Font.otf differ diff --git a/app/Resources/Vector 3.png b/app/Resources/Vector 3.png deleted file mode 100644 index 024ea3fd..00000000 Binary files a/app/Resources/Vector 3.png and /dev/null differ diff --git a/app/Settings.cs b/app/Settings.cs index 1ad9a984..e54b38ab 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -10,7 +10,6 @@ using GHelper.Peripherals; using GHelper.Peripherals.Mouse; using GHelper.UI; using System.Diagnostics; -using System.Drawing.Imaging; using System.Timers; namespace GHelper