/// <summary> /// display dialog for adding a component /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addComponentButton_Click(object sender, EventArgs e) { var chooseComponent = new ChooseComponentForm(ComponentCompatibility.GetCompatibleComponents(_target)); chooseComponent.ShowDialog(); if (chooseComponent.DialogResult != DialogResult.OK) { return; } var componentType = chooseComponent.Selected; var component = Activator.CreateInstance(componentType, _target); _target.AddComponents((AbstractComponent)component); Invalidate(); }
/// <summary> /// Performs validation on an entity and saves it /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void saveMenuItem_Click(object sender, System.EventArgs e) { if (!ComponentCompatibility.ValidateEntity(CurrentEntity, out List <string> reasons)) { MessageBox.Show($"Entity has errors. Please correct these errors before saving: {string.Join(',', reasons)}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (CurrentEntity.HasComponent <BrainComponent>()) { var brain = CurrentEntity.GetComponent <BrainComponent>(); // Validate the brain to check for any incorrect wirings. if (!BrainMapping.ValidateBrain(brain, brain.Owner, out List <string> invalidInputs, out List <string> invalidOutputs)) { string invalidInputsString = string.Join(",\n", invalidInputs); string invalidOutputsString = string.Join(",\n", invalidOutputs); MessageBox.Show($"The following brain inputs: {invalidInputsString} and outputs: {invalidOutputsString} are invalid."); return; } } if (saveEntityFileDialog.ShowDialog() != DialogResult.OK) { return; } try { using (StreamWriter sw = new StreamWriter(saveEntityFileDialog.OpenFile())) { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new Vector2Converter()); serializer.Converters.Add(new Texture2DConverter(null, null)); serializer.TypeNameHandling = TypeNameHandling.All; serializer.Serialize(sw, CurrentEntity); } } catch (Exception exception) { MessageBox.Show($"Something went wrong saving entity file {exception.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public NotCompatibleException(IComponent component, IComponent notCompatibleComponent, ComponentCompatibility compatibilityReason) { Component = component ?? throw new ArgumentNullException(nameof(component)); NotCompatibleComponent = notCompatibleComponent ?? throw new ArgumentNullException(nameof(notCompatibleComponent)); CompatibilityReason = compatibilityReason ?? throw new ArgumentNullException(nameof(compatibilityReason)); }