void ChangeScale(int newScale) { scale = newScale; drawer?.Dispose(mainGrid); drawer = new GridDrawer(grid); drawer.AddFolder(resourcePath, defaultCellSize, scale); mainGrid.Invalidate(); }
private void OnMainGridPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; int cellSize = defaultCellSize / scale; Pen p = new Pen(Color.LightGray); drawer.DrawGrid(g, defaultCellSize, scale); drawer.DrawLabels(mainGrid, textLabelSize, defaultCellSize, scale); if (drawLines) { drawer.DrawLines(g, defaultCellSize, scale, p); } if (gridLoaded && gridToAdd != null) { GridDrawer.DrawLines(g, defaultCellSize, scale, new Pen(Color.Red, 2), gridToAdd.xsize, gridToAdd.ysize, loadX, loadY); } }
private void OnAppLoad(object sender, EventArgs e) { DoubleBuffered = true; BackgroundColor = Color.FromArgb(33, 33, 33); mainGrid.BackColor = BackgroundColor; int numOfRows = 64; int numOfColumns = 64; grid = new Grid(numOfColumns, numOfRows); drawer = new GridDrawer(grid); try { drawer.AddFolder(resourcePath, defaultCellSize, scale); } catch (System.IO.DirectoryNotFoundException) { MessageBox.Show("директория не найдена: " + resourcePath, "Произошла ошибка при открытии Electronics"); Close(); } var fileItem = new ToolStripMenuItem("File"); var newFileItem = new ToolStripMenuItem("New file") { ShortcutKeys = Keys.Control | Keys.F }; newFileItem.Click += (object o, EventArgs args) => { SaveFile(o, args); lastFileDirectory = null; grid.Clear(); }; fileItem.DropDownItems.Add(newFileItem); var saveAsItem = new ToolStripMenuItem("SaveAs") { ShortcutKeys = Keys.Control | Keys.T }; saveAsItem.Click += SaveAsFile; fileItem.DropDownItems.Add(saveAsItem); var saveItem = new ToolStripMenuItem("Save") { ShortcutKeys = Keys.Control | Keys.S }; saveItem.Click += SaveFile; fileItem.DropDownItems.Add(saveItem); var loadItem = new ToolStripMenuItem("Load"); loadItem.Click += (object o, EventArgs args) => { Stream fileStream; OpenFileDialog loadFileDialog = new OpenFileDialog() { Filter = "(*.elc)|*.elc", FilterIndex = 2, RestoreDirectory = true }; if (loadFileDialog.ShowDialog() == DialogResult.OK) { if ((fileStream = loadFileDialog.OpenFile()) != null) { try { byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); gridToAdd = new SerializedGrid(Encoding.UTF8.GetString(bytes)); lastFileDirectory = null; } catch (InvalidCastException) // serialization failed { MessageBox.Show("Не удалось открыть файл проекта. Неверный формат", "Произошла ошибка при открытии файла"); gridLoaded = false; } catch (Exception) { MessageBox.Show("Проверьте, присутствует ли Newtonsoft.Json.dll в папке программы", "Произошла ошибка при открытии файла"); gridLoaded = false; } finally { fileStream.Close(); } gridLoaded = true; } } }; loadItem.ShortcutKeys = Keys.Control | Keys.D; fileItem.DropDownItems.Add(loadItem); MenuItems.Items.Add(fileItem); ToolStripMenuItem settingsItem = new ToolStripMenuItem("Settings"); var updateItem = new ToolStripMenuItem("Auto-update") { Checked = true }; updateItem.Click += (object o, EventArgs args) => { var element = (ToolStripMenuItem)o; element.Checked = updateEnabled = !element.Checked; }; updateItem.ShortcutKeys = Keys.Control | Keys.U; settingsItem.DropDownItems.Add(updateItem); var drawLinesItem = new ToolStripMenuItem("Grid") { Checked = true }; drawLinesItem.Click += (object o, EventArgs args) => { var element = (ToolStripMenuItem)o; element.Checked = drawLines = !element.Checked; mainGrid.Invalidate(); }; drawLinesItem.ShortcutKeys = Keys.Control | Keys.G; settingsItem.DropDownItems.Add(drawLinesItem); var gridResizeItem = new ToolStripMenuItem("Size"); var textbox = new ToolStripTextBox("Enter Sizes:") { Text = "Xsize Ysize" }; textbox.LostFocus += (object o, EventArgs args) => { string[] sizes = textbox.Text.Split(";, ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); try { int xsize = int.Parse(sizes[0]); int ysize = int.Parse(sizes[1]); grid = grid.Resize(xsize, ysize); ChangeScale(scale); } catch (Exception) { } }; gridResizeItem.DropDownItems.Add(textbox); settingsItem.DropDownItems.Add(gridResizeItem); var clearItem = new ToolStripMenuItem("Clear") { ShortcutKeys = Keys.Control | Keys.C }; clearItem.Click += (object o, EventArgs args) => { grid.Clear(); mainGrid.Invalidate(); }; settingsItem.DropDownItems.Add(clearItem); var shrinkItem = new ToolStripMenuItem("Shrink") { ShortcutKeys = Keys.Control | Keys.H }; shrinkItem.Click += (object o, EventArgs args) => { grid = grid.Shrink(); ChangeScale(scale); }; settingsItem.DropDownItems.Add(shrinkItem); MenuItems.Items.Add(settingsItem); var speeditem = new ToolStripMenuItem("Speed"); for (int i = 1; i < 9; i++) { int k = 1000 / (int)Math.Pow(2, i); var speedOption = new ToolStripMenuItem(k.ToString() + " ms"); speedOption.Click += (object o, EventArgs args) => mainTimer.Interval = k; speeditem.DropDownItems.Add(speedOption); } MenuItems.Items.Add(speeditem); var scaleGridSettings = new ToolStripMenuItem("Scale"); for (int i = 1; i < 8; i++) { int k = i; var scaleOption = new ToolStripMenuItem(Math.Round((100.0 / Math.Pow(2, i - 1)), 1).ToString() + '%'); scaleOption.Click += (object o, EventArgs args) => ChangeScale(k); scaleGridSettings.DropDownItems.Add(scaleOption); } MenuItems.Items.Add(scaleGridSettings); formLoaded = true; }