This repository has been archived on 2023-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
PainterlyUNO/Matrix App/forms/MatrixDesigner.cs

773 lines
25 KiB
C#
Raw Normal View History

2021-06-09 16:43:27 +00:00
#define DEBUG_ENABLED
#define DEBUG_ENABLED
using System;
2021-06-11 11:36:55 +00:00
using System.Diagnostics;
2021-06-09 16:43:27 +00:00
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Timers;
using System.Windows.Forms;
using Matrix_App.adds;
using Matrix_App.forms;
using Matrix_App.Properties;
using Matrix_App.Themes;
2021-06-09 17:53:36 +00:00
using static Matrix_App.Defaults;
using static Matrix_App.ArduinoInstruction;
2021-06-09 16:43:27 +00:00
using static Matrix_App.Utils;
2021-06-09 17:53:36 +00:00
using Timer = System.Timers.Timer;
2021-06-09 16:43:27 +00:00
namespace Matrix_App
{
public partial class MatrixDesignerMain : Form
{
private void PortsOnClick(object? sender, EventArgs e)
{
new Settings(ref commandQueue, ref port);
}
2021-06-09 16:43:27 +00:00
#region Private-Members
/// <summary>
/// Port update Timer
/// Reloads available port names at consecutive rates
2021-06-09 16:43:27 +00:00
/// </summary>
2021-06-09 17:53:36 +00:00
private Timer? delay;
2021-06-09 16:43:27 +00:00
private bool runningGif;
2021-06-09 16:43:27 +00:00
private static SerialPort port = new();
2021-06-09 16:43:27 +00:00
private PortCommandQueue commandQueue = new(ref port);
2021-06-09 16:43:27 +00:00
/// <summary>
/// Gif like frame video buffer
2021-06-09 16:43:27 +00:00
/// </summary>
2021-06-11 11:36:55 +00:00
public static byte[][] gifBuffer = CreateImageRGB_NT(MatrixStartWidth, MatrixStartHeight, MatrixStartFrames);
public static readonly ThreadQueue IMAGE_DRAWER = new("Matrix Image Drawer", 4);
2021-06-09 16:43:27 +00:00
#endregion
#region Setup
public MatrixDesignerMain()
{
// generate UI
InitializeComponent();
// Set Parent of our matrix
matrixView.Instance(this);
// Generate filter access buttons
MatrixGifGenerator.GenerateBaseUi(pregeneratedModsBase, matrixView, this);
2021-06-09 17:53:36 +00:00
Init();
// apply light-mode by default
2021-06-09 16:43:27 +00:00
new LightMode().ApplyTheme(this);
Show();
2021-06-09 16:43:27 +00:00
}
2021-06-09 19:19:30 +00:00
2021-06-09 17:53:36 +00:00
private void Init()
2021-06-09 16:43:27 +00:00
{
// create gif playback timer
2021-06-09 17:53:36 +00:00
delay = new Timer((int) Delay.Value);
delay.Elapsed += Timelineupdate;
2021-06-09 16:43:27 +00:00
delay.AutoReset = true;
// Set color wheel event handler
2021-06-09 17:53:36 +00:00
ZeichnenFarbRad.handler = ColorWheel_Handler!;
2021-06-09 16:43:27 +00:00
// setup port settings
port.BaudRate = BaudRate;
port.ReadTimeout = ReadTimeoutMs;
port.WriteTimeout = WriteTimeoutMs;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
2021-06-09 16:43:27 +00:00
// setup matrix
AdjustMatrixTable();
2021-06-09 17:53:36 +00:00
// search for initial ports
//GatherPortNames();
2021-06-09 16:43:27 +00:00
HideEasterEgg();
}
private void HideEasterEgg()
{
if (DateTime.Now.DayOfWeek != DayOfWeek.Wednesday)
2021-06-09 17:53:36 +00:00
return;
2021-06-09 19:19:30 +00:00
if (new Random().Next(0, 9) >= 1)
2021-06-09 17:53:36 +00:00
return;
using (Bitmap wednesdayFrog = new(Resources.Frosch))
2021-06-09 16:43:27 +00:00
{
2021-06-09 17:53:36 +00:00
matrixWidth.Value = wednesdayFrog.Width;
matrixHeight.Value = wednesdayFrog.Height;
ResizeGif();
2021-06-09 17:53:36 +00:00
for (var x = 0; x < wednesdayFrog.Width; x++)
for (var y = 0; y < wednesdayFrog.Height; y++)
2021-06-09 16:43:27 +00:00
{
var pixel = wednesdayFrog.GetPixel(x, y);
2021-06-09 16:43:27 +00:00
matrixView.SetPixelNoRefresh(x, y, pixel);
2021-06-09 16:43:27 +00:00
}
}
2021-06-09 17:53:36 +00:00
matrixView.Refresh();
2021-06-09 16:43:27 +00:00
}
#endregion
#region UI-Methods
#region Port-ComboBox
#endregion
#region Scale
2021-06-09 16:43:27 +00:00
/// <summary>
/// Applies a new size to the gif and matrix
2021-06-09 16:43:27 +00:00
/// </summary>
private void AdjustMatrixTable()
{
var width = (int) matrixWidth.Value;
var height = (int) matrixHeight.Value;
2021-06-09 16:43:27 +00:00
matrixView.resize(width, height);
ResizeGif();
// Delay.Minimum = Math.Min(Width.Value * Height.Value * 5, 500);
2021-06-09 16:43:27 +00:00
}
private void Width_ValueChanged(object sender, EventArgs e)
{
AdjustMatrixTable();
commandQueue.EnqueueArduinoCommand(
OpcodeScale, // opcode
(byte) matrixWidth.Value,
(byte) matrixHeight.Value
2021-06-09 16:43:27 +00:00
);
}
private void Height_ValueChanged(object sender, EventArgs e)
{
AdjustMatrixTable();
commandQueue.EnqueueArduinoCommand(
OpcodeScale, // opcode
(byte) matrixWidth.Value,
(byte) matrixHeight.Value
2021-06-09 16:43:27 +00:00
);
}
#endregion
#region Edit/Draw
#region TextBoxen
2021-06-09 17:53:36 +00:00
private void DrawTextBoxRed_KeyUp(object sender, KeyEventArgs e)
2021-06-09 16:43:27 +00:00
{
if (int.TryParse(ZeichnenTextBoxRed.Text, out var value) && value < 256 && value >= 0)
{
ZeichnenTrackBarRed.Value = value;
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
}
else if (value >= 256)
{
ZeichnenTrackBarRed.Value = 255;
2021-06-09 17:53:36 +00:00
ZeichnenTextBoxRed.Text = @"255";
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
}
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
2021-06-09 17:53:36 +00:00
private void DrawTextBoxGreen_KeyUp(object sender, KeyEventArgs e)
2021-06-09 16:43:27 +00:00
{
if (int.TryParse(ZeichnenTextBoxGreen.Text, out var value) && value < 256 && value >= 0)
{
ZeichnenTrackBarGreen.Value = value;
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
}
else if (value >= 256)
{
ZeichnenTrackBarGreen.Value = 255;
2021-06-09 17:53:36 +00:00
ZeichnenTextBoxGreen.Text = @"255";
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
}
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
2021-06-09 17:53:36 +00:00
private void DrawTextBoxBlue_KeyUp(object sender, KeyEventArgs e)
2021-06-09 16:43:27 +00:00
{
if (int.TryParse(ZeichnenTextBoxBlue.Text, out var value) && value < 256 && value >= 0)
{
ZeichnenTrackBarBlue.Value = value;
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
}
else if (value >= 256)
{
ZeichnenTrackBarBlue.Value = 255;
2021-06-09 17:53:36 +00:00
ZeichnenTextBoxBlue.Text = @"255";
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
}
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
2021-06-09 16:43:27 +00:00
#endregion
#region TackBars
2021-06-09 16:43:27 +00:00
private void ZeichnenTrackBarRed_Scroll(object sender, EventArgs e)
{
ZeichnenTextBoxRed.Text = ZeichnenTrackBarRed.Value.ToString();
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
private void ZeichnenTrackBarGreen_Scroll(object sender, EventArgs e)
{
ZeichnenTextBoxGreen.Text = ZeichnenTrackBarGreen.Value.ToString();
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
private void ZeichnenTrackBarBlue_Scroll(object sender, EventArgs e)
{
ZeichnenTextBoxBlue.Text = ZeichnenTrackBarBlue.Value.ToString();
ZeichnenFarbRad.setRGB((byte) ZeichnenTrackBarRed.Value, (byte) ZeichnenTrackBarGreen.Value,
(byte) ZeichnenTrackBarBlue.Value);
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
2021-06-09 16:43:27 +00:00
#endregion
/// <summary>
/// Sets a new color to the edit tab
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="color"></param>
public void SetColor(Color color)
{
ZeichnenTrackBarRed.Value = color.R;
ZeichnenTrackBarGreen.Value = color.G;
ZeichnenTrackBarBlue.Value = color.B;
ZeichnenTextBoxRed.Text = color.R.ToString();
ZeichnenTextBoxGreen.Text = color.G.ToString();
ZeichnenTextBoxBlue.Text = color.B.ToString();
ZeichnenFarbRad.setRGB(color.R, color.G, color.B);
}
/// <summary>
/// Updates trackbars and RGB-textboxes according to color wheel settings
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-06-09 17:53:36 +00:00
private void ColorWheel_Handler(object sender, EventArgs e)
2021-06-09 16:43:27 +00:00
{
ZeichnenTrackBarRed.Value = ZeichnenFarbRad.getRed();
ZeichnenTrackBarGreen.Value = ZeichnenFarbRad.getGreen();
ZeichnenTrackBarBlue.Value = ZeichnenFarbRad.getBlue();
ZeichnenTextBoxRed.Text = ZeichnenFarbRad.getRed().ToString();
ZeichnenTextBoxGreen.Text = ZeichnenFarbRad.getGreen().ToString();
ZeichnenTextBoxBlue.Text = ZeichnenFarbRad.getBlue().ToString();
matrixView.SetPaintColor(Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value));
2021-06-09 16:43:27 +00:00
}
/// <summary>
/// Fills the entire Matrix with a color
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-06-09 17:53:36 +00:00
private void DrawFill_Click(object sender, EventArgs e)
2021-06-09 16:43:27 +00:00
{
var color = Color.FromArgb(ZeichnenTrackBarRed.Value, ZeichnenTrackBarGreen.Value,
ZeichnenTrackBarBlue.Value);
2021-06-09 16:43:27 +00:00
matrixView.SetPaintColor(color);
matrixView.Fill(color);
commandQueue.EnqueueArduinoCommand(
OpcodeFill, // Opcode
2021-11-30 15:11:07 +00:00
(byte) ZeichnenTrackBarRed.Value, // Red
(byte) ZeichnenTrackBarGreen.Value, // Green
(byte) ZeichnenTrackBarBlue.Value // Blue
);
2021-06-09 16:43:27 +00:00
}
/// <summary>
/// Sets the entire Matrix to black
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-06-09 17:53:36 +00:00
private void DrawClear_Click(object sender, EventArgs e)
2021-06-09 16:43:27 +00:00
{
matrixView.Fill(Color.Black);
commandQueue.EnqueueArduinoCommand(
OpcodeFill, // opcode
0, // red
0, // green
0 // blue
2021-06-09 16:43:27 +00:00
);
}
#endregion
#region Image-Drag-Drop
2021-06-09 16:43:27 +00:00
/// <summary>
/// Handles click event, opens a file dialog to choose and image file
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DragDrop_Click(object sender, EventArgs e)
{
using OpenFileDialog openFileDialog = new()
2021-06-09 16:43:27 +00:00
{
2021-06-09 17:53:36 +00:00
InitialDirectory = "c:\\",
Filter = @"image files (*.PNG;*.JPG;*.GIF)|*.*",
FilterIndex = 2,
RestoreDirectory = true
};
2021-06-09 16:43:27 +00:00
2021-06-09 17:53:36 +00:00
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
2021-06-09 16:43:27 +00:00
2021-06-09 17:53:36 +00:00
LoadFromFile(filePath);
2021-06-09 16:43:27 +00:00
}
}
/// <summary>
/// Loads an image file froim disk and sets the matrix to it.
/// If the image is an gif, the gif buffer will be set to the gif, as well as the matrix itself.
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="filePath"></param>
2021-06-09 17:53:36 +00:00
private void LoadFromFile(string filePath)
2021-06-09 16:43:27 +00:00
{
// load gif
if (filePath.ToLower().EndsWith(".gif"))
{
var gif = Image.FromFile(filePath);
2021-06-09 17:53:36 +00:00
var frames = Math.Min(gif.GetFrameCount(FrameDimension.Time), 120);
2021-06-09 16:43:27 +00:00
if (gif.GetFrameCount(FrameDimension.Time) > 120)
MessageBox.Show(
@"Das Gif ist zu Groß. Die Maximalgröße sind 120 Frames. Das Gif wird abgeschnitten sein, damit es in die Maximalgröße passt.",
@"Gif to large");
2021-06-09 16:43:27 +00:00
FrameCount.Value = frames;
Timeline.Maximum = frames - 1;
// resize gif buffer to fit loaded gif frame count
ResizeGif();
// fetch and store frames
2021-06-09 17:53:36 +00:00
for (var i = 0; i < frames; i++)
2021-06-09 16:43:27 +00:00
{
gif.SelectActiveFrame(FrameDimension.Time, i);
// resize gif to fit scale
var bitmap = ResizeImage(gif, matrixView.matrixWidth(), matrixView.matrixHeight());
// fetch each pixel and store
2021-06-09 17:53:36 +00:00
for (var x = 0; x < bitmap.Width; x++)
for (var y = 0; y < bitmap.Height; y++)
2021-06-09 16:43:27 +00:00
{
var pixel = bitmap.GetPixel(x, y);
2021-06-09 16:43:27 +00:00
var index = x + y * bitmap.Width;
2021-06-09 16:43:27 +00:00
matrixView.SetPixelNoRefresh(x, y, pixel);
2021-06-09 16:43:27 +00:00
gifBuffer[i][index * 3 + 0] = pixel.R;
gifBuffer[i][index * 3 + 1] = pixel.G;
gifBuffer[i][index * 3 + 2] = pixel.B;
2021-06-09 16:43:27 +00:00
}
}
2021-06-09 16:43:27 +00:00
matrixView.Refresh();
Timeline.Value = 0;
}
else
{
Bitmap bitmap = ResizeImage(new Bitmap(filePath), matrixView.matrixWidth(), matrixView.matrixHeight());
2021-06-09 16:43:27 +00:00
matrixView.SetImage(bitmap);
for (var x = 0; x < bitmap.Width; x++)
for (var y = 0; y < bitmap.Height; y++)
2021-06-09 16:43:27 +00:00
{
var pixel = bitmap.GetPixel(x, y);
2021-06-09 16:43:27 +00:00
var index = x + y * bitmap.Width;
2021-06-09 16:43:27 +00:00
gifBuffer[Timeline.Value][index * 3 + 0] = pixel.R;
gifBuffer[Timeline.Value][index * 3 + 1] = pixel.G;
gifBuffer[Timeline.Value][index * 3 + 2] = pixel.B;
2021-06-09 16:43:27 +00:00
}
}
2021-06-09 17:53:36 +00:00
WriteImage(gifBuffer[Timeline.Value]);
2021-06-09 16:43:27 +00:00
}
private void DragDrop_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void DragDrop_DragDrop(object sender, DragEventArgs e)
{
string[] picturePath = (string[]) e.Data.GetData(DataFormats.FileDrop);
2021-06-09 16:43:27 +00:00
2021-06-09 17:53:36 +00:00
LoadFromFile(picturePath[0]);
2021-06-09 16:43:27 +00:00
}
2021-06-09 16:43:27 +00:00
#endregion
#region Timeline
public void ResetTimeline()
{
Timeline.Value = 1;
Timeline.Value = 0;
}
public int GetDelayTime()
{
return (int) Delay.Value;
2021-06-09 16:43:27 +00:00
}
private void FrameCount_ValueChanged(object sender, EventArgs e)
{
ResizeGif();
Timeline.Value = 0;
if (FrameCount.Value == 1)
{
Timeline.Enabled = false;
}
else
{
Timeline.Enabled = true;
Timeline.Maximum = (int) FrameCount.Value - 1;
2021-06-09 16:43:27 +00:00
}
}
private void Timeline_ValueChanged(object sender, EventArgs e)
{
2021-06-11 11:36:55 +00:00
var timeFrame = Timeline.Value;
WriteImage(gifBuffer[timeFrame]);
lock (matrixView)
2021-06-09 16:43:27 +00:00
{
matrixView.SetImage(gifBuffer[timeFrame]);
}
2021-06-09 16:43:27 +00:00
}
/// <summary>
/// Stores the current matrix at the index noted by the timeline into the Gif
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Apply_Click(object sender, EventArgs e)
{
var width = matrixView.matrixWidth();
var height = matrixView.matrixHeight();
2021-06-09 16:43:27 +00:00
for (var y = 0; y < height; y++)
2021-06-09 16:43:27 +00:00
{
var i = y * width;
2021-06-09 16:43:27 +00:00
for (var x = 0; x < width; x++)
2021-06-09 16:43:27 +00:00
{
var tmp = (i + x) * 3;
2021-06-09 16:43:27 +00:00
var color = matrixView.GetPixel(x, y);
2021-07-06 12:17:53 +00:00
gifBuffer[Timeline.Value][tmp + 0] = color.R;
gifBuffer[Timeline.Value][tmp + 1] = color.G;
2021-06-09 17:53:36 +00:00
gifBuffer[Timeline.Value][tmp + 2] = color.B;
2021-06-09 16:43:27 +00:00
}
}
}
private void Timelineupdate(object source, ElapsedEventArgs e)
2021-06-09 16:43:27 +00:00
{
if (Timeline.InvokeRequired)
2021-06-09 19:19:30 +00:00
// invoke on the combo-boxes thread
2021-06-09 16:43:27 +00:00
Timeline.Invoke(new Action(() =>
{
if (Timeline.Value < Timeline.Maximum)
Timeline.Value = Timeline.Value + 1;
else
Timeline.Value = 0;
}));
}
/// <summary>
/// Starts playing the timeline
2021-06-09 16:43:27 +00:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Play_Click(object sender, EventArgs e)
{
2021-06-09 17:53:36 +00:00
if (FrameCount.Value != 1)
2021-06-09 16:43:27 +00:00
{
if (!runningGif)
{
2021-06-09 17:53:36 +00:00
Play.Text = @"Stop";
2021-06-09 16:43:27 +00:00
Timeline.Value = 0;
runningGif = true;
2021-06-09 17:53:36 +00:00
if (delay != null)
delay.Enabled = true;
2021-06-09 16:43:27 +00:00
Play.Image = new Bitmap(Resources.Stop);
2021-06-09 16:43:27 +00:00
}
else
{
Play.Image = new Bitmap(Resources.Play);
2021-06-09 17:53:36 +00:00
Play.Text = @"Play";
2021-06-09 16:43:27 +00:00
runningGif = false;
if (delay != null)
2021-06-09 17:53:36 +00:00
delay.Enabled = false;
2021-06-09 16:43:27 +00:00
}
}
}
private void Timeline_MouseDown(object sender, MouseEventArgs e)
{
if (!runningGif) return;
Play.Image = new Bitmap(Resources.Play);
Play.Text = @"Play";
runningGif = false;
if (delay != null)
delay.Enabled = false;
2021-06-09 16:43:27 +00:00
}
2021-06-09 17:53:36 +00:00
private void Delay_ValueChanged(object sender, EventArgs _)
2021-06-09 16:43:27 +00:00
{
if (delay != null)
2021-06-09 17:53:36 +00:00
delay.Interval = (int) Delay.Value;
2021-06-09 16:43:27 +00:00
}
#endregion
#region Properties
2021-06-09 16:43:27 +00:00
private void Save_Click(object sender, EventArgs e)
{
SaveFileDialog save = new()
2021-06-09 17:53:36 +00:00
{
InitialDirectory = "c:\\",
Filter = @"image files (*.PNG;*.JPG;*.GIF)|*.*",
FilterIndex = 2,
RestoreDirectory = true
};
2021-06-09 16:43:27 +00:00
if (save.ShowDialog() == DialogResult.OK)
{
string filePath = save.FileName;
2021-06-09 17:53:36 +00:00
Bitmap[] gifBitmap = new Bitmap[gifBuffer.Length];
GifWriter writer = new(File.Create(filePath));
2021-06-09 17:53:36 +00:00
for (var i = 0; i < FrameCount.Value; i++)
2021-06-09 16:43:27 +00:00
{
gifBitmap[i] = new Bitmap((int) matrixWidth.Value, (int) matrixHeight.Value);
2021-06-09 16:43:27 +00:00
2021-06-09 17:53:36 +00:00
for (var j = 0; j < gifBuffer[i].Length / 3; j++)
2021-06-09 16:43:27 +00:00
{
var y = j / (int) matrixWidth.Value;
var x = j % (int) matrixWidth.Value;
2021-06-09 16:43:27 +00:00
gifBitmap[i].SetPixel(x, y,
Color.FromArgb(gifBuffer[i][j * 3], gifBuffer[i][j * 3 + 1], gifBuffer[i][j * 3 + 2]));
2021-06-09 16:43:27 +00:00
}
writer.WriteFrame(gifBitmap[i], (int) Delay.Value);
2021-06-09 16:43:27 +00:00
}
2021-06-09 16:43:27 +00:00
writer.Dispose();
}
}
private void ConfigButton_Click(object sender, EventArgs e)
{
commandQueue.EnqueueArduinoCommand(4);
PortCommandQueue.WaitForLastDequeue();
2021-06-09 16:43:27 +00:00
byte[] data = commandQueue.GetLastData();
if (commandQueue.GetMark() > 0)
{
int width = data[0];
int height = data[1];
2021-07-06 12:17:53 +00:00
matrixWidth.Value = width;
matrixHeight.Value = height;
2021-07-06 12:17:53 +00:00
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
2021-06-09 16:43:27 +00:00
{
var i0 = x * 3 + y * width * 3;
2021-07-06 12:17:53 +00:00
var x1 = height - y - 1;
var y1 = width - x - 1;
2021-07-06 12:17:53 +00:00
var i1 = x1 * 3 + y1 * width * 3;
// degamma
gifBuffer[0][i0 + 0] = (byte) MathF.Sqrt(data[i1 + 0 + 2] / 258.0f * 65536.0f);
gifBuffer[0][i0 + 1] = (byte) MathF.Sqrt(data[i1 + 1 + 2] / 258.0f * 65536.0f);
gifBuffer[0][i0 + 2] = (byte) MathF.Sqrt(data[i1 + 2 + 2] / 258.0f * 65536.0f);
2021-06-09 16:43:27 +00:00
}
2021-07-06 12:17:53 +00:00
2021-06-09 16:43:27 +00:00
Timeline.Value = 1;
Timeline.Value = 0;
}
}
private void showGridCheckbox_CheckedChanged(object sender, EventArgs e)
{
matrixView.DrawGrid = showGridCheckbox.Checked;
matrixView.Refresh();
}
private void BrushSizeSlider_Scroll(object sender, EventArgs e)
{
matrixView.brushSize = BrushSizeSlider.Value;
}
/// <summary>
/// Resizes the Gif image buffer
2021-06-09 16:43:27 +00:00
/// </summary>
private void ResizeGif()
{
var frames = (int) FrameCount.Value;
2021-06-09 17:53:36 +00:00
gifBuffer = new byte[frames + 1][];
for (var i = 0; i <= frames; i++)
2021-06-09 17:53:36 +00:00
gifBuffer[i] = new byte[matrixView.matrixWidth() * matrixView.matrixHeight() * 3];
2021-06-09 16:43:27 +00:00
}
#endregion
#region Appearance
private void ApplyDarkModeButton_Click(object sender, EventArgs e)
{
new DarkMode().ApplyTheme(this);
}
private void ApplyLightModeButton_Click(object sender, EventArgs e)
{
new LightMode().ApplyTheme(this);
}
#endregion
#endregion
#region IO-Utils
2021-06-09 17:53:36 +00:00
private void WriteImage(byte[] rgbImageData)
2021-06-09 16:43:27 +00:00
{
var gammaImage = new byte[rgbImageData.Length];
var width = matrixView.matrixWidth();
var height = matrixView.matrixHeight();
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
{
var i0 = x * 3 + y * width * 3;
;
var x1 = height - y - 1;
var y1 = width - x - 1;
var i1 = x1 * 3 + y1 * width * 3;
gammaImage[i0 + 0] = rgbImageData[i1 + 0];
gammaImage[i0 + 1] = rgbImageData[i1 + 1];
gammaImage[i0 + 2] = rgbImageData[i1 + 2];
}
for (var i = 0; i < rgbImageData.Length; i++)
gammaImage[i] = (byte) ((gammaImage[i] * gammaImage[i] * 258) >> 16);
commandQueue.EnqueueArduinoCommand(OpcodeImage, gammaImage);
2021-06-09 16:43:27 +00:00
}
/// <summary>
/// Converts the matrix's pixel ARGB buffer to an 3-byte RGB tuple array and sends them to the arduino
2021-06-09 16:43:27 +00:00
/// </summary>
public void EnqueuePixelSet()
{
var pixels = matrixView.getPixels();
byte[] image = new byte[pixels.Length * 3];
for (var x = 0; x < pixels.Length; x++)
2021-06-09 16:43:27 +00:00
{
image[x * 3 + 0] = (byte) ((pixels[x] >> 16) & 0xFF);
image[x * 3 + 1] = (byte) ((pixels[x] >> 8) & 0xFF);
image[x * 3 + 2] = (byte) ((pixels[x] >> 0) & 0xFF);
2021-06-09 16:43:27 +00:00
}
2021-06-09 17:53:36 +00:00
WriteImage(image);
2021-06-09 16:43:27 +00:00
}
#endregion
private void PushButtonOnClick(object? sender, EventArgs e)
{
var bytes = matrixWidth.Value * matrixHeight.Value * 3 * gifBuffer.Length + 5;
var data = new byte[(int) bytes];
data[0] = (byte) matrixWidth.Value;
data[1] = (byte) matrixHeight.Value;
data[2] = (byte) gifBuffer.Length;
data[3] = (byte) ((int) Delay.Value >> 8);
data[4] = (byte) ((int) Delay.Value & 0xFF);
for (var frame = 0; frame < gifBuffer.Length; frame++)
{
for (var pixel = 0; pixel < gifBuffer[0].Length; pixel++)
{
data[frame * gifBuffer[0].Length + pixel + 5] = (byte) (gifBuffer[frame][pixel] * gifBuffer[frame][pixel] * 258 >> 16);
}
}
commandQueue.EnqueueArduinoCommand(OpcodePush, data);
}
2021-06-09 16:43:27 +00:00
}
}