예제 #1
0
        public ModelDesigner()
        {
            InitializeComponent();

            // Setting the brush and sensor list for the default design, if it exists
            DesignDisplay.Source = _currentDesign?.Brush;
            SetSensorList(_currentDesign?.SensorList);

            ButtonNew.Click += (sender, args) =>
            {
                // Setting the design to a fresh one, resetting the brush, and resetting the sensor list
                _currentDesign       = new CarDesign();
                DesignDisplay.Source = _currentDesign.Brush;
                SetSensorList(_currentDesign.SensorList);
                // Clearing the TextBoxes
                TextBoxDesignName.Text = string.Empty;
                TextBoxMaxSpeed.Text   = string.Empty;
            };
            ButtonSave.Click += (sender, args) =>
            {
                // Checking if a name has been given
                var name = TextBoxDesignName.Text.Trim();
                if (name.Equals(string.Empty))
                {
                    MessageBox.Show("Please give the design a name");
                    return;
                }

                // Checking if a model with that name already exists
                if (CarModel.ModelExists(name))
                {
                    MessageBox.Show($"A model with the name \"{name}\" already exists");
                    return;
                }

                // Checking if the max speed has been formatted properly
                double maxSpeed;
                try
                {
                    maxSpeed = double.Parse(TextBoxMaxSpeed.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Bad input for Speed");
                    return;
                }

                // Adding the model to the list for the simulation
                CarModel.AddModel(new CarModel(maxSpeed, _currentDesign.SensorList, name));
                MessageBox.Show($"Model \"{name}\" was successfully added to the simulation");
            };
            ButtonExport.Click += (sender, args) =>
            {
                var fileDialog = new SaveFileDialog
                {
                    AddExtension     = true,
                    DefaultExt       = $".{ModelFileExtension}",
                    Filter           = $"Car Model File|*.{ModelFileExtension}",
                    InitialDirectory = Environment.CurrentDirectory,
                    CheckPathExists  = true,
                    OverwritePrompt  = true,
                    Title            = "Export Car Model"
                };

                // Exporting the current model, unless the user cancels the dialog
                if (fileDialog.ShowDialog() != true)
                {
                    return;
                }
                ExportDesign(_currentDesign, fileDialog.FileName);
            };
            ButtonLoad.Click += (sender, args) =>
            {
                var window = new ModelPickerWindow();
                window.ShowDialog();

                if (!window.Success)
                {
                    return;
                }
                // Setting the new design and other properties
                _currentDesign       = DesignFromModel(window.SelectedModel);
                DesignDisplay.Source = _currentDesign.Brush;
                SetSensorList(_currentDesign.SensorList);
                TextBoxDesignName.Text = window.SelectedModel.Name;
                TextBoxMaxSpeed.Text   = window.SelectedModel.MaxSpeed.ToString(CultureInfo.InvariantCulture);
            };
            ButtonImport.Click += (sender, args) =>
            {
                var fileDialog = new OpenFileDialog
                {
                    Filter           = $"Car Model Files|*.{ModelFileExtension}|All Files|*.*",
                    InitialDirectory = Environment.CurrentDirectory,
                    CheckFileExists  = true,
                    CheckPathExists  = true,
                    Title            = "Import Car Model"
                };

                // Importing the selected model, unless the user cancels the dialog
                if (fileDialog.ShowDialog() != true)
                {
                    return;
                }
                _currentDesign       = ImportDesign(fileDialog.FileName) ?? _currentDesign;
                DesignDisplay.Source = _currentDesign?.Brush;
                SetSensorList(_currentDesign.SensorList);
            };

            ButtonNewSensor.Click += (sender, args) =>
            {
                var window = new SensorCreationWindow();
                window.ShowDialog();

                if (!window.Success)
                {
                    return;
                }
                // Adding the new sensor and setting other properties
                var sensor = new SensorPrototype
                {
                    Direction = window.SensorDirection,
                    Range     = window.SensorRange,
                    Create    = Sensor.Sensors[window.SensorType]
                };
                _currentDesign.SensorList.Add(sensor);
                DesignDisplay.Source = _currentDesign?.Brush;
                SetSensorList(_currentDesign.SensorList);
            };
            ButtonRemoveSensor.Click += (sender, args) =>
            {
                var window = new SensorPickerWindow
                {
                    SensorList = new List <SensorPrototype>(_currentDesign.SensorList)
                };
                window.ShowDialog();

                if (!window.Success)
                {
                    return;
                }
                // Removing the selected sensor and setting other properties
                _currentDesign.SensorList.Remove(window.SelectedSensor);
                DesignDisplay.Source = _currentDesign.Brush;
                SetSensorList(_currentDesign.SensorList);
            };
        }