예제 #1
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;
                }
            }
        }
예제 #2
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);
        }