public void SaveDotMatrixToSetting(DotMatrixSetting setting) { int selectedIndex = (testControl.Content as ListBox).SelectedIndex; if (selectedIndex == -1) { return; } setting = new DotMatrixSetting(setting.Name, width, height); setting.SetMatrix(dotMatrix); listBoxItems[selectedIndex] = setting; (testControl.Content as ListBox).SelectedIndex = selectedIndex; }
public void Convert(object sender, RoutedEventArgs e) { try { switch (conversionType) { case ConversionType.MatrixIn: { int oldCount = listBoxItems.Count; string[] textLines = tbxCode.Text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int newHeight = -1; for (int row = 0; row < textLines.Length; row++) { textLines[row] = textLines[row].Replace("\r", "").Replace(" ", ""); if (newHeight == -1 && textLines[row] == "") { newHeight = row; } } int numChunks = textLines.Count(x => x == "") + 1; int newWidth = textLines[0].Length; byte[][] byteData = new byte[numChunks][]; for (int chunk = 0; chunk < numChunks; ++chunk) { byteData[chunk] = new byte[newHeight]; for (int i = 0; i < newHeight; ++i) { byteData[chunk][i] = System.Convert.ToByte(textLines[chunk * (newHeight + 1) + i], 2); } DotMatrixSetting newSetting = new DotMatrixSetting($"#{chunk}", newWidth, newHeight); newSetting.SetMatrix(byteData[chunk]); if (!noImport) { listBoxItems.Add(newSetting); } if (trim) { newSetting.TrimMatrix(); } } if (listBoxItems.Count > oldCount && loadImmediately) { (testControl.Content as ListBox).SelectedIndex = oldCount; SettingCommands.Load.Execute(null, null); } break; } case ConversionType.MatrixOut: { string code = ""; for (int i = 0; i < this.height; i++) { for (int j = 0; j < this.width; j++) { code += (dotMatrix[i][j] ? "1" : "0") + " "; } code += "\n"; } tbxCode.Text = code; break; } case ConversionType.CodesIn: { if (overwriteSettings) { listBoxItems.Clear(); } string[] strCodes = tbxCode.Text.Replace("\r", "").Replace(" ", "").Replace("0x", "").Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int oldNumSettings = listBoxItems.Count; int newNumSettings = strCodes.Length; DotMatrixSetting firstSetting = null; for (int k = 0; k < strCodes.Length; k++) { int newHeight; int newWidth; if (strCodes[k].Count(x => (x == ',')) <= 1) { newWidth = 8; } else { newWidth = strCodes[k].IndexOf(',') * 4; } strCodes[k] = strCodes[k].Replace(",", ""); newHeight = strCodes[k].Length / (newWidth / 4); UInt64 mask = UInt64.MaxValue; mask = mask >> (64 - newWidth); string[] matrixLines = new string[newHeight]; UInt64[] splitMatrix = new UInt64[newHeight]; DotMatrixSetting newSetting = new DotMatrixSetting($"#{k + oldNumSettings}", newWidth, newHeight); for (int i = newHeight; i != 0; i--) { matrixLines[i - 1] = strCodes[k].Substring(strCodes[k].Length - 2); splitMatrix[i - 1] = System.Convert.ToUInt64(matrixLines[i - 1], 16); strCodes[k] = strCodes[k].Remove(strCodes[k].Length - 2); } if (flipDimensions) { for (int i = 0; i < newHeight; i++) { for (int j = 0; j < newWidth; j++) { newSetting[i, j] = (splitMatrix[newHeight - i - 1] & (1UL << (j))) != 0; } } } else { for (int i = 0; i < newHeight; i++) { for (int j = 0; j < newWidth; j++) { newSetting[i, j] = (splitMatrix[i] & (1UL << (newWidth - j - 1))) != 0; } } } if (!noImport) { listBoxItems.Add(newSetting); } if (trim) { newSetting.TrimMatrix(); } if (k == 0) { firstSetting = newSetting; } } if (loadImmediately && firstSetting != null) { LoadDotMatrixFromSetting(firstSetting); if (listBoxItems.Count > oldNumSettings) { (testControl.Content as ListBox).SelectedIndex = oldNumSettings; } } break; } case ConversionType.CodesOut: { string code = ""; foreach (DotMatrixSetting setting in listBoxItems) { for (int i = 0; i < setting.Height; i++) { UInt32 num = 0; for (int j = 0; j < setting.Width; j++) { if (setting[i, j]) { num |= (1U << (byte)(setting.Width - 1 - j)); } } code += num.ToString("X2"); //code += num.ToString("X2") + ","; } //code = code.Remove(code.Length - 1); code += "\n"; } tbxCode.Text = code; break; } case ConversionType.SettingsIn: { if (overwriteSettings) { listBoxItems.Clear(); } string[] strCodes = tbxCode.Text.Replace("\r", "").Replace(" ", "").Replace("0x", "").Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int oldNumSettings = listBoxItems.Count; int newNumSettings = strCodes.Length; DotMatrixSetting firstSetting = null; for (int k = 0; k < strCodes.Length; k++) { string name = strCodes[k].Substring(0, strCodes[k].LastIndexOf(',')); strCodes[k] = strCodes[k].Substring(strCodes[k].LastIndexOf(',') + 1); int newHeight; int newWidth = System.Convert.ToInt32(strCodes[k].Substring(0, 2), 16); int numSymbolsPerRow = (newWidth + 3) / 4; strCodes[k] = strCodes[k].Substring(2); strCodes[k] = strCodes[k].Replace(",", ""); newHeight = strCodes[k].Length / numSymbolsPerRow; UInt64 mask = UInt64.MaxValue; mask = mask >> (64 - newWidth); string[] matrixLines = new string[newHeight]; UInt64[] splitMatrix = new UInt64[newHeight]; DotMatrixSetting newSetting = new DotMatrixSetting(name, newWidth, newHeight); for (int i = newHeight; i != 0; i--) { matrixLines[i - 1] = strCodes[k].Substring(strCodes[k].Length - numSymbolsPerRow); splitMatrix[i - 1] = System.Convert.ToUInt64(matrixLines[i - 1], 16); strCodes[k] = strCodes[k].Remove(strCodes[k].Length - numSymbolsPerRow); } if (flipDimensions) { for (int i = 0; i < newHeight; i++) { for (int j = 0; j < newWidth; j++) { newSetting[i, j] = (splitMatrix[newHeight - i - 1] & (1UL << (j))) != 0; } } } else { for (int i = 0; i < newHeight; i++) { for (int j = 0; j < newWidth; j++) { newSetting[i, j] = (splitMatrix[i] & (1UL << (newWidth - j - 1))) != 0; } } } if (!noImport) { listBoxItems.Add(newSetting); } if (trim) { newSetting.TrimMatrix(); } if (k == 0) { firstSetting = newSetting; } } if (loadImmediately && firstSetting != null) { LoadDotMatrixFromSetting(firstSetting); if (listBoxItems.Count > oldNumSettings) { (testControl.Content as ListBox).SelectedIndex = oldNumSettings; } } break; } case ConversionType.SettingsOut: { string code = ""; foreach (DotMatrixSetting setting in listBoxItems) { code += setting.Name; code += ","; code += setting.Width.ToString("X2"); for (int i = 0; i < setting.Height; i++) { UInt64 num = 0; for (int j = 0; j < setting.Width; j++) { if (setting[i, j]) { num |= (1UL << (byte)(setting.Width - 1 - j)); } } code += num.ToString($"X{(setting.Width + 3) / 4}"); //code += num.ToString("X2") + ","; } //code = code.Remove(code.Length - 1); code += "\r\n"; } tbxCode.Text = code; break; } } } catch { MessageBox.Show("Invalid Conversion", "Invalid Conversion"); } }