Example #1
0
        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;
        }
Example #2
0
        private void OnMainGridMouseDown(object sender, MouseEventArgs e)
        {
            fileSaved    = false;
            mousePressed = true;

            if (e.Button == MouseButtons.Right)
            {
                gridToAdd  = null;
                gridLoaded = false;
            }

            int x = e.X * scale;
            int y = e.Y * scale;

            x -= x % defaultCellSize;
            y -= y % defaultCellSize;
            if (x >= (grid.xsize * defaultCellSize) || y >= (grid.ysize * defaultCellSize))
            {
                return;
            }

            if (gridLoaded && gridToAdd != null)
            {
                gridLoaded = false;
                PlaceGridOnField(x, y);
                gridToAdd = null;
                return;
            }

            IElement element = null;

            if (drawObjectType.IsSubclassOf(typeof(BaseElement)) && e.Button != MouseButtons.Right)
            {
                element = (IElement)Activator.CreateInstance(drawObjectType);
            }

            int gridX = e.X / (defaultCellSize / scale);
            int gridY = e.Y / (defaultCellSize / scale);

            if (gridX < 0 || gridX >= grid.xsize || gridY < 0 || gridY >= grid.ysize)
            {
                return;
            }

            if (drawObjectType == typeof(Eraser) || e.Button == MouseButtons.Right)
            {
                grid.SetElement(null, gridX, gridY);       // delete element at [gridX, gridY]
            }
            else if (grid.elements[gridX, gridY] != null)  // if element exists - click on it
            {
                if (drawObjectType == typeof(TextLabeler)) // add text to element if supported
                {
                    BaseElement el = grid.elements[gridX, gridY];
                    if (el.SupportsLabeling)
                    {
                        string text = string.Empty;
                        text    = CreateInputBox();
                        el.Name = text;
                    }
                }
                else // if element exists - click on it
                {
                    grid.elements[gridX, gridY].Click();
                }
            }
            else // set new element at [gridX, gridY]
            {
                grid.SetElement(element, gridX, gridY);
            }
            mainGrid.Invalidate();
        }