Пример #1
0
        //one specific CA is deleted here.
        public static void DeleteSpecificCA(CAGrid2DVM caGrid2DVMIn)
        {
            string aFilename = null;

            try
            {
                if (caGrid2DVMIn != null)
                {
                    aFilename = @".\SavedCA\" + caGrid2DVMIn.CAName + ".xml";
                    File.Delete(aFilename);
                }
            }
            catch (Exception ex)
            {
                if (ex is UnauthorizedAccessException || ex is DirectoryNotFoundException || ex is IOException || ex is PathTooLongException)
                {
                    DialogMediator.ShowMessageBox(null, "Exception during deleting CA", "Problem with deleting following CA : \n" + aFilename
                                                  + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #2
0
        //this method gets the frame color of specific cell, it depends on if the cell is selected, marked, or if the mouse pointer is over cell.
        private Color?GetFrameColor(CellVM cellVMIn, bool isMouseOverIn)
        {
            CAGrid2DVM aCAGrid2DVM = this.DataContext as CAGrid2DVM;

            if (aCAGrid2DVM != null)
            {
                if (cellVMIn.IsSelected == true)
                {
                    return(aCAGrid2DVM.SelectedSelectionFrameColor);
                }
                else if (cellVMIn.IsMarked)
                {
                    return(aCAGrid2DVM.SelectedMarkingColor);
                }
                else if (isMouseOverIn == true)
                {
                    return(aCAGrid2DVM.SelectedMouseOverColor);
                }
                else
                {
                    return(aCAGrid2DVM.SelectedBackgroundColor);
                }
            }

            return(null);
        }
Пример #3
0
        //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;
        }
Пример #4
0
 //add specific input CA to CA list.
 public void AddNewCA(CAGrid2DVM aCAGrid2DVM)
 {
     if (myGrid2DViewModelList.Count < Constants.MaxNrOfCellularAutomatons)
     {
         myGrid2DViewModelList.Add(aCAGrid2DVM);
     }
     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);
     }
 }
Пример #5
0
        //this method draws the specific cell
        private void CreateCAGridCellGeometry(CellVM cellVMIn, ref DrawingVisualWithDataContext drawingVisualWithDataContextIn, bool isMouseOverIn)
        {
            CAGrid2DVM aCAGrid2DVM = this.DataContext as CAGrid2DVM;

            if (aCAGrid2DVM != null)
            {
                DrawingContext dc = drawingVisualWithDataContextIn.RenderOpen();

                int aGridPenWidth = 0;
                ConstantLists.LineThicknessValue.TryGetValue(aCAGrid2DVM.GridThickness, out aGridPenWidth); // the currently selected grid thickness

                int aFramePenWidth = 0;
                ConstantLists.LineThicknessValue.TryGetValue(aCAGrid2DVM.SelFrameThickness, out aFramePenWidth); // the currently selected frame thickness

                //three brushes are drawn one after one, and that will create different areas of cell - grid, frame and content
                //Brush for grid
                Brush aGridBrush = new SolidColorBrush(aCAGrid2DVM.SelectedGridColor);
                Rect  aGridRect  = new Rect(cellVMIn.X, cellVMIn.Y, aCAGrid2DVM.CellGridWidth, aCAGrid2DVM.CellGridHeight);
                dc.DrawRectangle(aGridBrush, null, aGridRect);

                //Brush for frame
                Brush aFrameBrush = new SolidColorBrush((Color)GetFrameColor(cellVMIn, isMouseOverIn));
                Rect  aFrameRect  = new Rect(cellVMIn.X + aGridPenWidth, cellVMIn.Y + aGridPenWidth, aCAGrid2DVM.CellGridWidth - 2 * aGridPenWidth, aCAGrid2DVM.CellGridHeight - 2 * aGridPenWidth);
                dc.DrawRectangle(aFrameBrush, null, aFrameRect);

                Color?aStateColor = aCAGrid2DVM.GetFillColorForState(cellVMIn.State);  // current color of this cell
                if (aStateColor == null)
                {
                    throw new CAExplorerException();
                }

                // brush for content
                Brush aContentBrush = new SolidColorBrush((Color)aStateColor);
                Rect  aContentRect  = new Rect(cellVMIn.X + aGridPenWidth + aFramePenWidth, cellVMIn.Y + aGridPenWidth + aFramePenWidth, aCAGrid2DVM.CellObjectWidth, aCAGrid2DVM.CellObjectHeight);
                dc.DrawRectangle(aContentBrush, null, aContentRect);

                aGridBrush.Freeze();
                aContentBrush.Freeze();
                aFrameBrush.Freeze();

                dc.Close();
            }
        }
Пример #6
0
        //copying data from CAGrid viewmodel to SetCAColors viewmodel.
        private void CopyFromCAGrid2DVM_To_SetCAColorsVM(CAGrid2DVM caGrid2DVMIn, ref SetCAColorsVM setCAColorsVM)
        {
            setCAColorsVM.SelectedGridColor           = caGrid2DVMIn.SelectedGridColor;
            setCAColorsVM.SelectedMarkingColor        = caGrid2DVMIn.SelectedMarkingColor;
            setCAColorsVM.SelectedMouseOverColor      = caGrid2DVMIn.SelectedMouseOverColor;
            setCAColorsVM.SelectedSelectionFrameColor = caGrid2DVMIn.SelectedSelectionFrameColor;
            setCAColorsVM.SelectedBackgroundColor     = caGrid2DVMIn.SelectedBackgroundColor;
            setCAColorsVM.SelectedStartInterpColor    = caGrid2DVMIn.SelectedStartInterpColor;
            setCAColorsVM.SelectedEndInterpColor      = caGrid2DVMIn.SelectedEndInterpColor;

            setCAColorsVM.SetStateColorsDirectlyRBEnabled = caGrid2DVMIn.CAGrid2DModel.NumberOfStates < Constants.MaxColorCountForDirectColors ? true : false;

            if (caGrid2DVMIn.StateColorAssigning == StateColorAssigningType.Direct)
            {
                setCAColorsVM.SetStateColorsDirectlyChecked = true;
                setCAColorsVM.SetStateColorsInterpChecked   = false;
            }
            else if (caGrid2DVMIn.StateColorAssigning == StateColorAssigningType.Interpolated)
            {
                setCAColorsVM.SetStateColorsInterpChecked   = true;
                setCAColorsVM.SetStateColorsDirectlyChecked = false;
            }

            if (setCAColorsVM.SetStateColorsDirectlyRBEnabled == false)
            {
                setCAColorsVM.SetStateColorsDirectlyChecked = false;
                setCAColorsVM.SetStateColorsInterpChecked   = true;
            }

            setCAColorsVM.StateCount = (int)caGrid2DVMIn.CAGrid2DModel.NumberOfStates;

            setCAColorsVM.StateColorsCollection.Clear();
            if (setCAColorsVM.SetStateColorsDirectlyRBEnabled == true)
            {
                for (int i = 0; i < caGrid2DVMIn.CAGrid2DModel.NumberOfStates; i++)
                {
                    setCAColorsVM.StateColorsCollection.Add(new StateAndColor(caGrid2DVMIn.StateToColorCollection[i]));
                }
            }
        }
Пример #7
0
 //in this method the cells are modified if someting changed
 private void ModifyCAGridCells()
 {
     if (myVisualChildren != null)
     {
         CAGrid2DVM aCAGrid2DVM = DataContext as CAGrid2DVM;
         if (aCAGrid2DVM != null)
         {
             for (int i = 0; i < myVisualChildren.Count; i++)
             {
                 DrawingVisualWithDataContext aDVWithDC = myVisualChildren[i] as DrawingVisualWithDataContext;
                 if (aDVWithDC != null)
                 {
                     CellVM aCellVM = aDVWithDC.DataContext as CellVM;
                     if (aCellVM != null)
                     {
                         CreateCAGridCellGeometry(aCellVM, ref aDVWithDC, false);
                     }
                 }
             }
         }
     }
 }
Пример #8
0
        //if mouse button is pressed then current cell will be selected
        public void OnMouseDown(Object sender, MouseEventArgs e)
        {
            HitTestResult aHTR = VisualTreeHelper.HitTest(this, e.GetPosition(sender as IInputElement));
            DrawingVisualWithDataContext aDrawingVisualWithDataContext = aHTR.VisualHit as DrawingVisualWithDataContext;

            if (aDrawingVisualWithDataContext != null)
            {
                if (aDrawingVisualWithDataContext.DataContext != null)
                {
                    //here is the returning of before selected cell to normal state
                    if (myLastSelectedDrawingVisual != null && myLastSelectedDrawingVisual.DataContext != null)
                    {
                        CellVM aCellVM_LS = myLastSelectedDrawingVisual.DataContext as CellVM;

                        if (aCellVM_LS != null)
                        {
                            aCellVM_LS.IsSelected = false;
                            CreateCAGridCellGeometry(aCellVM_LS, ref myLastSelectedDrawingVisual, false);
                        }
                    }

                    CellVM aCellVM = aDrawingVisualWithDataContext.DataContext as CellVM;

                    if (aCellVM != null)
                    {
                        CAGrid2DVM aCAGrid2DVM = this.DataContext as CAGrid2DVM;

                        if (aCAGrid2DVM != null)
                        {
                            aCellVM.IsSelected = true;
                            CreateCAGridCellGeometry(aCellVM, ref aDrawingVisualWithDataContext, false);

                            aCAGrid2DVM.SelectedCellViewModel = aCellVM;
                            myLastSelectedDrawingVisual       = aDrawingVisualWithDataContext;
                        }
                    }
                }
            }
        }
Пример #9
0
        //this method creates the cells
        private void CreateCAGridCells()
        {
            if (myVisualChildren != null)
            {
                myVisualChildren.Clear();
            }

            CAGrid2DVM aCAGrid2DVM = DataContext as CAGrid2DVM;

            if (aCAGrid2DVM != null)
            {
                //for every Cell view model one cell is created
                foreach (CellVM aCellVM in aCAGrid2DVM.Cells)
                {
                    DrawingVisualWithDataContext aDrawingVisualWithDataContext = CreateCell(aCellVM);

                    if (aDrawingVisualWithDataContext != null)
                    {
                        myVisualChildren.Add(aDrawingVisualWithDataContext);
                    }
                }
            }
        }
Пример #10
0
        //loading specific CA from file
        public void LoadCAFromFile(object messageIn)
        {
            bool?aDialogResult = null;

            string aDefaultExtension = ".xml";
            string aFileExtensions   = "Xml files (.xml)|*.xml";

            string aFileName = null;

            aDialogResult = DialogMediator.ShowOpenFileDialog(this, aFileExtensions, aDefaultExtension, out aFileName);

            if (aDialogResult == true)
            {
                var aCAGrid2DVM = new CAGrid2DVM();

                try
                {
                    //reading CA from specified file and adding it to list of CAs
                    CAGrid2DRW.ReadCA(aFileName, this, ref aCAGrid2DVM);
                    myGrid2DViewModelList.Add(aCAGrid2DVM);
                }
                catch (Exception ex)
                {
                    if (ex is System.IO.FileNotFoundException || ex is System.Security.SecurityException || ex is UriFormatException || ex is System.Xml.XmlException ||
                        ex is FormatException || ex is OverflowException || ex is ArgumentOutOfRangeException || ex is NullReferenceException || ex is CAExplorerException)
                    {
                        DialogMediator.ShowMessageBox(null, "Exception during reading CA", "Problem with reading following CA file : \n" + aFileName
                                                      + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Пример #11
0
        public void DataContextChangedEventHandler(Object sender, DependencyPropertyChangedEventArgs args)
        {
            CAGrid2DVM aOldCAGrid2DVM = args.OldValue as CAGrid2DVM;
            CAGrid2DVM aNewCAGrid2DVM = args.NewValue as CAGrid2DVM;

            if (aOldCAGrid2DVM != null)
            {
                aOldCAGrid2DVM.CAGridPropertyChangedEvent -= Refresh;
                foreach (CellVM aCellVM in aOldCAGrid2DVM.Cells)
                {
                    aCellVM.CellStateChangedEvent -= OnCellStateChanged;
                }
            }

            if (aNewCAGrid2DVM != null)
            {
                aNewCAGrid2DVM.CAGridPropertyChangedEvent += Refresh;
            }

            myLastPointedDrawingVisual  = null;
            myLastSelectedDrawingVisual = null;

            CreateCAGridCells();
        }
Пример #12
0
        //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;
                }
            }
        }
Пример #13
0
        //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);
            }
        }
Пример #14
0
        //creating new empty CA
        public CAGrid2DVM CreateNewEmptyCA()
        {
            var aCAGrid2DVM = new CAGrid2DVM();

            return(aCAGrid2DVM);
        }
Пример #15
0
        //here is implemented writing of one specific CA data.
        public static void WriteCA(string fileNameIn, CAGrid2DVM caGrid2DVMIn)
        {
            XmlWriter aXmlWriter = null;

            try
            {
                if (!Directory.Exists(fileNameIn))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(fileNameIn));
                }

                aXmlWriter = new XmlTextWriter(fileNameIn, Encoding.UTF8);

                aXmlWriter.WriteStartDocument();

                aXmlWriter.WriteStartElement("CA");

                aXmlWriter.WriteElementString("ContentType", "CellularAutomata");

                aXmlWriter.WriteElementString("CARuleFamily", caGrid2DVMIn.CAGrid2DModel.CARule.CARuleFamilyString);

                aXmlWriter.WriteElementString("CARule", caGrid2DVMIn.CAGrid2DModel.CARule.CARuleData.CARuleName);

                aXmlWriter.WriteElementString("Name", caGrid2DVMIn.CAName);

                aXmlWriter.WriteStartElement("Columns");
                aXmlWriter.WriteValue(caGrid2DVMIn.Columns);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("Rows");
                aXmlWriter.WriteValue(caGrid2DVMIn.Rows);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("GridThickness");
                aXmlWriter.WriteValue((int)caGrid2DVMIn.GridThickness);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("SelFrameThickness");
                aXmlWriter.WriteValue((int)caGrid2DVMIn.SelFrameThickness);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("CAGridCellInitializationMethod");
                aXmlWriter.WriteValue((int)caGrid2DVMIn.CAGrid2DModel.CAGridInitializationMethodType);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("CellObjectWidth");
                aXmlWriter.WriteValue(caGrid2DVMIn.CellObjectWidth);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("CellObjectHeight");
                aXmlWriter.WriteValue(caGrid2DVMIn.CellObjectHeight);
                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteStartElement("StateColorAssigning");
                aXmlWriter.WriteValue((int)caGrid2DVMIn.StateColorAssigning);
                aXmlWriter.WriteEndElement();

                WriteColor(aXmlWriter, "SelectedGridColor", caGrid2DVMIn.SelectedGridColor);
                WriteColor(aXmlWriter, "SelectedSelectionFrameColor", caGrid2DVMIn.SelectedSelectionFrameColor);
                WriteColor(aXmlWriter, "SelectedMarkingColor", caGrid2DVMIn.SelectedMarkingColor);
                WriteColor(aXmlWriter, "SelectedMouseOverColor", caGrid2DVMIn.SelectedMouseOverColor);
                WriteColor(aXmlWriter, "SelectedBackgroundColor", caGrid2DVMIn.SelectedBackgroundColor);
                WriteColor(aXmlWriter, "SelectedStartInterpColor", caGrid2DVMIn.SelectedStartInterpColor);
                WriteColor(aXmlWriter, "SelectedEndInterpColor", caGrid2DVMIn.SelectedEndInterpColor);

                foreach (StateAndColor aStateAndColor in caGrid2DVMIn.StateToColorCollection)
                {
                    WriteColor(aXmlWriter, "StateColor", aStateAndColor.StateColor, "id", aStateAndColor.State.ToString(CultureInfo.InvariantCulture));
                }

                foreach (CellVM aCellVM in caGrid2DVMIn.Cells)
                {
                    aXmlWriter.WriteStartElement("Cell");
                    aXmlWriter.WriteAttributeString("X", aCellVM.CellModel.X.ToString(CultureInfo.InvariantCulture));
                    aXmlWriter.WriteAttributeString("Y", aCellVM.CellModel.Y.ToString(CultureInfo.InvariantCulture));
                    aXmlWriter.WriteAttributeString("IsMarked", aCellVM.IsMarked.ToString(CultureInfo.InvariantCulture));
                    aXmlWriter.WriteValue(aCellVM.CellModel.CurrentCellState);
                    aXmlWriter.WriteEndElement();
                }

                aXmlWriter.WriteEndElement();

                aXmlWriter.WriteEndDocument();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (aXmlWriter != null)
                {
                    aXmlWriter.Close();
                }
            }
        }
Пример #16
0
        //reading of one specific CA.
        public static bool ReadCA(string fileNameIn, CAMainWindowVM caMainWindowVMIn, ref CAGrid2DVM caGrid2DVMIn)
        {
            int aColumns = 0;
            int aRows    = 0;

            var aIsMarkedList         = new List <bool>();
            var aCurrentCellStateList = new List <int>();
            var aXList = new List <int>();
            var aYList = new List <int>();
            var aStateToColorCollection = new List <StateAndColor>();

            CAGridInitializationMethodTypes?aCAGridCellInitializationMethod = null;
            string aCARuleFamilyString = null;
            string aCARule             = null;

            bool aIsCA = false;

            XmlReader aXmlReader = null;

            try
            {
                aXmlReader = XmlReader.Create(fileNameIn);

                //reading all data connected to CA.
                while (aXmlReader.Read())
                {
                    if (aXmlReader.NodeType == XmlNodeType.Element)
                    {
                        switch (aXmlReader.Name)
                        {
                        case "ContentType":
                        {
                            string aContent = aXmlReader.ReadString();
                            if (aContent == "CellularAutomata")
                            {
                                aIsCA = true;
                            }
                            else if (aContent != "CellularAutomata")
                            {
                                return(false);
                            }
                        }
                        break;

                        case "CAGridCellInitializationMethod":
                        {
                            aCAGridCellInitializationMethod = (CAGridInitializationMethodTypes)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                        }
                        break;

                        case "CARuleFamily":
                        {
                            aCARuleFamilyString = aXmlReader.ReadString();
                        }
                        break;

                        case "CARule":
                        {
                            aCARule = aXmlReader.ReadString();
                        }
                        break;

                        case "Name":
                        {
                            caGrid2DVMIn.CAName = aXmlReader.ReadString();
                        }
                        break;

                        case "Columns":
                        {
                            aColumns = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                            if (aColumns < Constants.MinCAColumns)
                            {
                                throw new CAExplorerException("The value of columns must be higher than " + Constants.MinCAColumns.ToString(CultureInfo.InvariantCulture) + "!");
                            }
                        }
                        break;

                        case "Rows":
                        {
                            aRows = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                            if (aRows < Constants.MinCARows)
                            {
                                throw new CAExplorerException("The value of rows must be higher than " + Constants.MinCARows.ToString(CultureInfo.InvariantCulture) + "!");
                            }
                        }
                        break;

                        case "GridThickness":
                        {
                            caGrid2DVMIn.GridThickness = (LineThickness)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                        }
                        break;

                        case "SelFrameThickness":
                        {
                            caGrid2DVMIn.SelFrameThickness = (LineThickness)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                        }
                        break;

                        case "CellObjectWidth":
                        {
                            caGrid2DVMIn.CellObjectWidth = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                            if (caGrid2DVMIn.CellObjectWidth < Constants.MinCellSizeX)
                            {
                                throw new CAExplorerException("The value of cell object width must be higher than " + Constants.MinCellSizeX.ToString(CultureInfo.InvariantCulture) + "!");
                            }
                        }
                        break;

                        case "CellObjectHeight":
                        {
                            caGrid2DVMIn.CellObjectHeight = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                            if (caGrid2DVMIn.CellObjectHeight < Constants.MinCellSizeY)
                            {
                                throw new CAExplorerException("The value of cell object height must be higher " + Constants.MinCellSizeY.ToString(CultureInfo.InvariantCulture) + "!");
                            }
                        }
                        break;

                        case "StateColorAssigning":
                        {
                            caGrid2DVMIn.StateColorAssigning = (StateColorAssigningType)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                        }
                        break;

                        case "SelectedGridColor":
                        {
                            caGrid2DVMIn.SelectedGridColor = ReadColor(aXmlReader, "SelectedGridColor");
                        }
                        break;

                        case "SelectedSelectionFrameColor":
                        {
                            caGrid2DVMIn.SelectedSelectionFrameColor = ReadColor(aXmlReader, "SelectedSelectionFrameColor");
                        }
                        break;

                        case "SelectedMarkingColor":
                        {
                            caGrid2DVMIn.SelectedMarkingColor = ReadColor(aXmlReader, "SelectedMarkingColor");
                        }
                        break;

                        case "SelectedMouseOverColor":
                        {
                            caGrid2DVMIn.SelectedMouseOverColor = ReadColor(aXmlReader, "SelectedMouseOverColor");
                        }
                        break;

                        case "SelectedBackgroundColor":
                        {
                            caGrid2DVMIn.SelectedBackgroundColor = ReadColor(aXmlReader, "SelectedBackgroundColor");
                        }
                        break;

                        case "SelectedStartInterpColor":
                        {
                            caGrid2DVMIn.SelectedStartInterpColor = ReadColor(aXmlReader, "SelectedStartInterpColor");
                        }
                        break;

                        case "SelectedEndInterpColor":
                        {
                            caGrid2DVMIn.SelectedEndInterpColor = ReadColor(aXmlReader, "SelectedEndInterpColor");
                        }
                        break;

                        case "Cell":
                        {
                            string aXString        = null;
                            string aYString        = null;
                            string aIsMarkedString = null;

                            int aAttributeCount = aXmlReader.AttributeCount;
                            if (aAttributeCount != 3)
                            {
                                throw new CAExplorerException("The cell element must have exactly 3 attributes!");
                            }

                            aXString        = aXmlReader.GetAttribute(0);
                            aYString        = aXmlReader.GetAttribute(1);
                            aIsMarkedString = aXmlReader.GetAttribute(2);

                            int aXConverted = Convert.ToInt32(aXString, CultureInfo.InvariantCulture);
                            int aYConverted = Convert.ToInt32(aYString, CultureInfo.InvariantCulture);
                            aXList.Add(aXConverted);
                            aYList.Add(aYConverted);

                            bool aIsMarked = Convert.ToBoolean(aIsMarkedString, CultureInfo.InvariantCulture);
                            aIsMarkedList.Add(aIsMarked);

                            int aCurrentCellState = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture);
                            aCurrentCellStateList.Add(aCurrentCellState);
                        }
                        break;

                        case "StateColor":
                        {
                            int aNr = 0;
                            if (aXmlReader.HasAttributes)
                            {
                                aNr = Convert.ToInt32(aXmlReader.GetAttribute(0), CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                throw new CAExplorerException("The attribute id must be present in the State Color element!");
                            }

                            Color aStateColor = ReadColor(aXmlReader, "StateColor");
                            aStateToColorCollection.Add(new StateAndColor(aNr, aStateColor));
                        }
                        break;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (aXmlReader != null)
                {
                    aXmlReader.Close();
                }
            }

            if (aIsCA == false)
            {
                return(false);
            }

            //storing of read data in the viewmodels and models.
            CARuleFamilies?aCARuleFamilyEnum = null;

            foreach (ComboBoxItem aCARuleFamilyEnumItem in ConstantLists.CARuleFamilyItems)
            {
                if (aCARuleFamilyEnumItem.ComboBoxString == aCARuleFamilyString)
                {
                    aCARuleFamilyEnum = (CARuleFamilies)aCARuleFamilyEnumItem.ComboBoxId;
                    break;
                }
            }

            ICARuleData aCARuleData = null;

            foreach (ICARuleData aCARuleDataItem in caMainWindowVMIn.CAMainWindowModel.ListOfCARules)
            {
                if (aCARuleDataItem.CARuleFamily == aCARuleFamilyEnum && aCARule == aCARuleDataItem.CARuleName)
                {
                    aCARuleData = aCARuleDataItem;
                    break;
                }
            }

            ICARuleFamily aCARuleFamily = CARuleFactory.CreateCARuleFamily((CARuleFamilies)aCARuleFamilyEnum, aCARuleData);

            aCARuleData.CARuleName = aCARule;

            ICAGridCellInitialization aCAGridCellInitialization = CAGridCellInitializationFactory.CreateCAGridCellInitialization((CAGridInitializationMethodTypes)aCAGridCellInitializationMethod);

            caGrid2DVMIn.CreateCells(aColumns, aRows, aCARuleFamily, aCAGridCellInitialization);

            caGrid2DVMIn.StateToColorCollection.Clear();
            foreach (StateAndColor aStateAndColorItem in aStateToColorCollection)
            {
                caGrid2DVMIn.StateToColorCollection.Add(aStateAndColorItem);
            }

            for (int aCellNr = 0; aCellNr < aCurrentCellStateList.Count; aCellNr++)
            {
                caGrid2DVMIn.Cells[aCellNr].IsMarked = aIsMarkedList[aCellNr];

                caGrid2DVMIn.Cells[aCellNr].CellModel.CurrentCellState = aCurrentCellStateList[aCellNr];
                caGrid2DVMIn.Cells[aCellNr].CellModel.X = aXList[aCellNr];
                caGrid2DVMIn.Cells[aCellNr].CellModel.Y = aYList[aCellNr];
            }

            return(true);
        }
Пример #17
0
        //copying data from SetCAColors viewmodel to CAGrid viewmodel
        private void CopyFromSetCAColorsVM_To_CAGrid2DVM(SetCAColorsVM setCAColorsVM, ref CAGrid2DVM caGrid2DVMIn)
        {
            caGrid2DVMIn.SelectedGridColor           = setCAColorsVM.SelectedGridColor;
            caGrid2DVMIn.SelectedMarkingColor        = setCAColorsVM.SelectedMarkingColor;
            caGrid2DVMIn.SelectedMouseOverColor      = setCAColorsVM.SelectedMouseOverColor;
            caGrid2DVMIn.SelectedSelectionFrameColor = setCAColorsVM.SelectedSelectionFrameColor;
            caGrid2DVMIn.SelectedBackgroundColor     = setCAColorsVM.SelectedBackgroundColor;
            caGrid2DVMIn.SelectedStartInterpColor    = setCAColorsVM.SelectedStartInterpColor;
            caGrid2DVMIn.SelectedEndInterpColor      = setCAColorsVM.SelectedEndInterpColor;

            if (setCAColorsVM.SetStateColorsDirectlyChecked)
            {
                caGrid2DVMIn.StateColorAssigning = StateColorAssigningType.Direct;
            }
            else if (setCAColorsVM.SetStateColorsInterpChecked)
            {
                caGrid2DVMIn.StateColorAssigning = StateColorAssigningType.Interpolated;
            }
            else
            {
                throw new CAExplorerException("Unknown state color assigning was used!");
            }

            caGrid2DVMIn.StateToColorCollection.Clear();
            foreach (StateAndColor aStateAndColor in setCAColorsVM.StateColorsCollection)
            {
                caGrid2DVMIn.StateToColorCollection.Add(new StateAndColor(aStateAndColor));
            }
        }
Пример #18
0
        //all available CAs are read here.
        public static void ReadAllAvailableCAs(CAMainWindowVM caMainWindowVMIn)
        {
            string[] aAvailableCAs = null;

            try
            {
                aAvailableCAs = Directory.GetFiles(@".\SavedCA\", "*.xml");
            }
            catch (Exception ex)
            {
                if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException || ex is DirectoryNotFoundException)
                {
                    DialogMediator.ShowMessageBox(null, "Exception during reading CAs", "Problem with reading files from directory \\SavedCA\\ \n" +
                                                  ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
                else
                {
                    throw;
                }
            }

            if (aAvailableCAs != null)
            {
                string aCAsWithProblem = null;
                string aCAExceptions   = null;

                foreach (string aAvailableCAItem in aAvailableCAs)
                {
                    try
                    {
                        CAGrid2DVM aCAGrid2DVM = caMainWindowVMIn.CreateNewEmptyCA();
                        bool       aWasOK      = CAGrid2DRW.ReadCA(aAvailableCAItem, caMainWindowVMIn, ref aCAGrid2DVM);

                        if (aWasOK == true)
                        {
                            caMainWindowVMIn.AddNewCA(aCAGrid2DVM);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is FileNotFoundException || ex is System.Security.SecurityException || ex is UriFormatException || ex is XmlException ||
                            ex is FormatException || ex is OverflowException || ex is ArgumentOutOfRangeException || ex is NullReferenceException ||
                            ex is CAExplorerException || ex is InvalidOperationException)
                        {
                            aCAsWithProblem += aAvailableCAItem + "\n";
                            aCAExceptions   += ex.Message + "\n";
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                if (aCAsWithProblem != null)
                {
                    DialogMediator.ShowMessageBox(null, "Exception during reading CA", "Problem with reading following CA files : \n" + aCAsWithProblem
                                                  + "\n" + "Following exceptions occured : \n" + aCAExceptions, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                }
            }
        }