//copying data from CAGrid viewmodel to CAProperties viewmodel private void CopyFromCAGrid2DVM_To_CAPropertiesVM(CAGrid2DVM caGrid2DVMIn, ref CAPropertiesDialogVM caPropertiesVM) { caPropertiesVM.CAName = caGrid2DVMIn.CAName; caPropertiesVM.CACellSizeX = (int)caGrid2DVMIn.CellObjectWidth; caPropertiesVM.CACellSizeY = (int)caGrid2DVMIn.CellObjectHeight; caPropertiesVM.CARows = caGrid2DVMIn.Rows; caPropertiesVM.CAColumns = caGrid2DVMIn.Columns; foreach (ComboBoxItem aGridThickness in ConstantLists.CAGridThicknessItems) { if (caGrid2DVMIn.GridThickness == (LineThickness)aGridThickness.ComboBoxId) { caPropertiesVM.CAGridThicknessSelectedItem = aGridThickness.ComboBoxString; break; } } foreach (ComboBoxItem aGridThickness in ConstantLists.CAGridThicknessItems) { if (caGrid2DVMIn.GridThickness == (LineThickness)aGridThickness.ComboBoxId) { caPropertiesVM.CAGridThicknessSelectedItem = aGridThickness.ComboBoxString; break; } } foreach (ComboBoxItem aSelFrameThickness in ConstantLists.CASelFrameThicknessItems) { if (caGrid2DVMIn.SelFrameThickness == (LineThickness)aSelFrameThickness.ComboBoxId) { caPropertiesVM.CASelFrameThicknessSelectedItem = aSelFrameThickness.ComboBoxString; break; } } foreach (ComboBoxItem aCAGridInitializationMethodItem in ConstantLists.CAInitializationMethodItems) { if (caGrid2DVMIn.CAGridInitializationMethodType == (CAGridInitializationMethodTypes)aCAGridInitializationMethodItem.ComboBoxId) { caPropertiesVM.CAGridCellInitializationSelectedItem = aCAGridInitializationMethodItem.ComboBoxString; break; } } ICARuleFamily aCARuleFamily = caGrid2DVMIn.CAGrid2DModel.CARule; caPropertiesVM.CARuleFamilySelectedItem = aCARuleFamily.CARuleFamilyString; caPropertiesVM.CARuleSelectedItem = aCARuleFamily.CARuleData.CARuleName; caPropertiesVM.OriginalCAName = caGrid2DVMIn.CAName; caPropertiesVM.ReadOnlyCAProperty = true; }
//modifying the existing CA public void ModifySelectedCA(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { try { //stopping last selected CA before continuing with modification mySelectedCAGrid2DViewModel.StopTimer(); //instantiating Properties viewmodel for modification of CA. var aCAPropertiesDialogVM = new CAPropertiesDialogVM(myCAMainWindowModel.ListOfCARules, myGrid2DViewModelList); bool?aDialogResult = null; aCAPropertiesDialogVM.ReadOnlyCAProperty = true; //copying data from CA Grid viewmodel to Properties viewmodel. CopyFromCAGrid2DVM_To_CAPropertiesVM(mySelectedCAGrid2DViewModel, ref aCAPropertiesDialogVM); //showing Properties dialog aDialogResult = DialogMediator.ShowModalDialog("Modify CA Properties", aCAPropertiesDialogVM, this); if (aDialogResult == true) { //copying modified data from Properties viewmodel to CAGrid viewmodel. CopyFromCAPropertiesVM_To_CAGrid2DVM(aCAPropertiesDialogVM, false, ref mySelectedCAGrid2DViewModel); //redrawing CA after modification mySelectedCAGrid2DViewModel.RedrawGrid(); } } catch (Exception ex) { DialogMediator.ShowMessageBox(null, "Exception during modification of CA", "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); throw; } } }
//copying data from CAProperties viewmodel to CAGrid viewmodel. private void CopyFromCAPropertiesVM_To_CAGrid2DVM(CAPropertiesDialogVM caPropertiesVM, bool shallCreateIn, ref CAGrid2DVM caGrid2DVMIn) { CARuleFamilies?aCARuleFamilyEnum = null; if (shallCreateIn == true) { foreach (ComboBoxItem aCARuleFamilyEnumItem in ConstantLists.CARuleFamilyItems) { if (aCARuleFamilyEnumItem.ComboBoxString == caPropertiesVM.CARuleFamilySelectedItem) { aCARuleFamilyEnum = (CARuleFamilies)aCARuleFamilyEnumItem.ComboBoxId; break; } } ICARuleData aCARuleData = null; foreach (ICARuleData aCARuleDataItem in CAMainWindowModel.ListOfCARules) { if (aCARuleDataItem.CARuleFamily == aCARuleFamilyEnum && aCARuleDataItem.CARuleName == caPropertiesVM.CARuleSelectedItem) { aCARuleData = aCARuleDataItem; break; } } ICARuleFamily aCARuleFamily = CARuleFactory.CreateCARuleFamily((CARuleFamilies)aCARuleFamilyEnum, aCARuleData); CAGridInitializationMethodTypes?aCAGridInitializationMethodEnum = null; foreach (ComboBoxItem aCAGridInitializationMethodItem in ConstantLists.CAInitializationMethodItems) { if (aCAGridInitializationMethodItem.ComboBoxString == caPropertiesVM.CAGridCellInitializationSelectedItem) { aCAGridInitializationMethodEnum = (CAGridInitializationMethodTypes)aCAGridInitializationMethodItem.ComboBoxId; break; } } ICAGridCellInitialization aCAGridCellInitialization = CAGridCellInitializationFactory.CreateCAGridCellInitialization((CAGridInitializationMethodTypes)aCAGridInitializationMethodEnum); caGrid2DVMIn.CreateCells(caPropertiesVM.CAColumns, caPropertiesVM.CARows, aCARuleFamily, aCAGridCellInitialization); caGrid2DVMIn.SelectedBackgroundColor = this.SelectedDefaultBackgroundColor; caGrid2DVMIn.SelectedGridColor = this.SelectedDefaultGridColor; caGrid2DVMIn.SelectedMarkingColor = this.SelectedDefaultMarkingColor; caGrid2DVMIn.SelectedMouseOverColor = this.SelectedDefaultMouseOverColor; caGrid2DVMIn.SelectedSelectionFrameColor = this.SelectedDefaultSelectionFrameColor; caGrid2DVMIn.SelectedStartInterpColor = this.SelectedDefaultStartInterpColor; caGrid2DVMIn.SelectedEndInterpColor = this.SelectedDefaultEndInterpColor; caGrid2DVMIn.StateToColorCollection.Clear(); foreach (StateAndColor aStateAndColorItem in this.DefaultStateColorsCollection) { caGrid2DVMIn.StateToColorCollection.Add(new StateAndColor(aStateAndColorItem)); } caGrid2DVMIn.StateColorAssigning = caGrid2DVMIn.CAGrid2DModel.NumberOfStates < Constants.MaxColorCountForDirectColors ? StateColorAssigningType.Direct : StateColorAssigningType.Interpolated; } caGrid2DVMIn.CellObjectWidth = caPropertiesVM.CACellSizeX; caGrid2DVMIn.CellObjectHeight = caPropertiesVM.CACellSizeY; caGrid2DVMIn.CAName = caPropertiesVM.CAName; foreach (ComboBoxItem aGridThickness in ConstantLists.CAGridThicknessItems) { if (caPropertiesVM.CAGridThicknessSelectedItem == aGridThickness.ComboBoxString) { caGrid2DVMIn.GridThickness = (LineThickness)aGridThickness.ComboBoxId; break; } } foreach (ComboBoxItem aSelFrameThickness in ConstantLists.CASelFrameThicknessItems) { if (caPropertiesVM.CASelFrameThicknessSelectedItem == aSelFrameThickness.ComboBoxString) { caGrid2DVMIn.SelFrameThickness = (LineThickness)aSelFrameThickness.ComboBoxId; break; } } }
//creating and adding new CA public void CreateAndAddNewCA(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { mySelectedCAGrid2DViewModel.StopTimer(); } if (myGrid2DViewModelList.Count < Constants.MaxNrOfCellularAutomatons) { try { //instantiating Properties viewmodel for configuration of new CA. var aCAPropertiesDialogVM = new CAPropertiesDialogVM(myCAMainWindowModel.ListOfCARules, myGrid2DViewModelList); bool?aDialogResult = null; var aCAGrid2DViewModel = new CAGrid2DVM(); aCAPropertiesDialogVM.ReadOnlyCAProperty = false; aCAPropertiesDialogVM.CAColumns = Constants.DefaultCAColumns; aCAPropertiesDialogVM.CARows = Constants.DefaultCARows; aCAPropertiesDialogVM.CACellSizeX = Constants.DefaultCellSizeX; aCAPropertiesDialogVM.CACellSizeY = Constants.DefaultCellSizeY; bool aCANameWasFound = true; int i = 0; string aNewCAName = null; //finding new default name of new CA, format is CA + increasing number do { aCANameWasFound = true; foreach (CAGrid2DVM aCAGrid2DVM in myGrid2DViewModelList) { if (aCAGrid2DVM.CAName == "CA" + i.ToString(CultureInfo.CurrentCulture)) { aCANameWasFound = false; break; } } if (aCANameWasFound == true) { aNewCAName = "CA" + i.ToString(CultureInfo.CurrentCulture); } i++; } while (aCANameWasFound != true); aCAPropertiesDialogVM.CAName = aNewCAName; //showing Properties dialog for configuration of new CA. aDialogResult = DialogMediator.ShowModalDialog("New CA Properties", aCAPropertiesDialogVM, this); if (aDialogResult == true) { //copying the inputted data from properties viewmodel to CAGrid viewmodel. CopyFromCAPropertiesVM_To_CAGrid2DVM(aCAPropertiesDialogVM, true, ref aCAGrid2DViewModel); //adding new CA to list myGrid2DViewModelList.Add(aCAGrid2DViewModel); //setting newly added CA as selected SelectedCAGrid2DViewModel = aCAGrid2DViewModel; } } catch (Exception ex) { DialogMediator.ShowMessageBox(null, "Exception during creating of CA", "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); throw; } } else { DialogMediator.ShowMessageBox(this, "Add New CA", "There can be maximum " + Constants.MaxNrOfCellularAutomatons + " CAs loaded! \n" + "Delete some CA first, before creating new CA.", MessageBoxButton.OK, MessageBoxImage.Warning); } }