Esempio n. 1
0
        /// <summary>
        /// Add's a sensor
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        private IEnumerable <char> AddSensor(params string[] args)
        {
            if (args.Length < 4)
            {
                throw new CommandInputException("Invalid command usage, should be : sensor add <model> <side> <sensor> [range]");
            }
            ;

            if (args[0] != "add")
            {
                throw new CommandInputException("Invalid command useage, should be : sensor add <model> <side> <sensor> [range]");
            }
            ;

            CarModel     model;
            Direction    side;
            CreateSensor sensorFactory;

            try
            {
                model = CarModel.GetModel(args[1]);
            }
            catch (Exception)
            {
                throw new CommandInputException("Invalid command: Model doesn't exist.");
            }
            try
            {
                side = StringConverters.StringToDirection(args[2]);
            }
            catch (Exception)
            {
                throw new CommandInputException("Invalid command: Side is wrong used.");
            }
            try
            {
                sensorFactory = StringConverters.GetSensor(args[3]);
            }
            catch (Exception)
            {
                throw new CommandInputException("Invalid command: Could not create sensor.");
            }
            var range = 0;

            if (args.Length == 5)
            {
                try
                {
                    range = int.Parse(args[4]);
                }
                catch (Exception)
                {
                    throw new CommandInputException("Invalid command: Could not parse range.");
                }
            }

            var sensor = new SensorPrototype
            {
                Direction = side,
                Range     = range,
                Create    = sensorFactory
            };

            model.Sensors.Add(sensor);

            CitySystem.City.Instance.Cars
            .FindAll(car => car.Model == model)
            .ForEach(car => car.Sensors.Add(sensor.Create(car, sensor.Direction, sensor.Range)));



            return($"Added sensor {args[3]} to {model.Name}");
        }
Esempio n. 2
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);
            };
        }