Exemplo n.º 1
0
        public SheetColumn(SheetPage sheetPage, string serializationName, string propertyName, SheetDataType dataType, string referenceSheet, bool isCollection, int insertIndex)
        {
            if (insertIndex < sheetPage.columns.Count)
            {
                sheetPage.columns.Insert(insertIndex, this);
            }
            else
            {
                sheetPage.columns.Add(this);
            }

            this.serializationName = serializationName;
            this.propertyName      = propertyName;
            this.dataType          = dataType;
            this.isCollection      = isCollection;
            this.referenceSheet    = referenceSheet;

            object defaultData = dataType.GetDefaultValue(isCollection);

            foreach (SheetRow sheetRow in sheetPage.rows)
            {
                SheetCell sheetCell = new SheetCell(defaultData);
                if (insertIndex < sheetPage.columns.Count)
                {
                    sheetRow.cells.Insert(insertIndex, sheetCell);
                }
                else
                {
                    sheetRow.cells.Add(sheetCell);
                }
            }
        }
Exemplo n.º 2
0
        public void ChangeIdentifier(DataSheet dataSheet, SheetPage sheetPage, string newIdentifier)
        {
            foreach (SheetPage datasheetPage in dataSheet.datasheetPages)
            {
                for (int i = 0; i < datasheetPage.columns.Count; i++)
                {
                    SheetColumn datasheetColumn = datasheetPage.columns[i];

                    if (datasheetColumn.dataType != SheetDataType.Reference)
                    {
                        continue;
                    }

                    if (datasheetColumn.referenceSheet != sheetPage.sheetName)
                    {
                        continue;
                    }

                    if (datasheetColumn.isCollection)
                    {
                        for (int j = 0; j < datasheetPage.rows.Count; j++)
                        {
                            SheetRow  datasheetRow  = datasheetPage.rows[j];
                            SheetCell datasheetCell = datasheetRow.cells[i];
                            string[]  dataArray     = datasheetCell.data as string[];
                            for (int h = 0; h < dataArray.Length; h++)
                            {
                                if (dataArray[h] == identifier)
                                {
                                    dataArray[h] = newIdentifier;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < datasheetPage.rows.Count; j++)
                        {
                            SheetRow  datasheetRow  = datasheetPage.rows[j];
                            SheetCell datasheetCell = datasheetRow.cells[i];
                            if ((string)datasheetCell.data == identifier)
                            {
                                datasheetCell.data = newIdentifier;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public SheetRowJsonable(SheetPage sheetPage, SheetRow sheetRow)
        {
            index          = sheetRow.index;
            identifier     = sheetRow.identifier;
            enumIdentifier = sheetRow.enumValue;

            data = new string[sheetPage.columns.Count];

            for (int i = 0; i < sheetPage.columns.Count; i++)
            {
                SheetColumn column = sheetPage.columns[i];
                SheetCell   cell   = sheetRow.cells[i];
                data[i] = column.dataType.GetStringValue(cell.data, column.isCollection);
            }
        }
Exemplo n.º 4
0
        private void DrawSaveButton(bool containsColumnChanges)
        {
            if (!GUILayout.Button(Localization.SAVE))
            {
                return;
            }

            string        oldReferenceSheet = sheetColumn.referenceSheet;
            bool          oldIsCollection   = sheetColumn.isCollection;
            SheetDataType oldDataType       = sheetColumn.dataType;

            sheetColumn.isCollection      = isCollection;
            sheetColumn.serializationName = serializationName;
            sheetColumn.propertyName      = propertyName;
            sheetColumn.dataType          = dataType;

            sheetColumn.referenceSheet = dataType == SheetDataType.Reference ? referenceSheet : "";
            bool sameReferenceSheet = oldReferenceSheet == sheetColumn.referenceSheet;

            if (containsColumnChanges)
            {
                int columnIndex = sheetPage.columns.IndexOf(sheetColumn);

                foreach (SheetRow sheetRow in sheetPage.rows)
                {
                    SheetCell sheetCell = sheetRow.cells[columnIndex];
                    if (oldIsCollection && isCollection)
                    {
                        sheetCell.data = ReplaceColumnValuesArrayToArray(sheetCell.data as Array, oldDataType, dataType, sameReferenceSheet);
                    }
                    else if (oldIsCollection && !isCollection)
                    {
                        sheetCell.data = ReplaceColumnValuesArrayToNonArray(sheetCell.data as Array, oldDataType, dataType, sameReferenceSheet);
                    }
                    else if (!oldIsCollection && isCollection)
                    {
                        sheetCell.data = ReplaceColumnValuesNonArrayToArray(sheetCell.data, oldDataType, dataType, sameReferenceSheet);
                    }
                    else
                    {
                        sheetCell.data = ReplaceColumnValuesNonArrayToNonArray(sheetCell.data, oldDataType, dataType, sameReferenceSheet);
                    }
                }
            }

            onColumnEdited();
            Close();
        }
Exemplo n.º 5
0
        private void CalculateDimensions()
        {
            int[,] widthSizes  = new int[sheetPage.columns.Count + 1, sheetPage.rows.Count + 1];
            int[,] heightSizes = new int[sheetPage.columns.Count + 1, sheetPage.rows.Count + 1];

            int identifierWidth = CalculateWidth("Identifier");

            widthSizes[0, 0] = identifierWidth;

            widthDimensions  = new List <int>();
            heightDimensions = new List <int>();

            for (int i = 0; i < sheetPage.rows.Count; i++)
            {
                SheetRow row = sheetPage.rows[i];
                identifierWidth      = CalculateWidth(row.identifier);
                widthSizes[0, i + 1] = identifierWidth;
            }

            for (int i = 0; i < sheetPage.columns.Count; i++)
            {
                SheetColumn column = sheetPage.columns[i];

                int columnWidth = CalculateWidth(column.serializationName);

                widthSizes[i + 1, 0]  = columnWidth;
                heightSizes[i + 1, 0] = WindowSettings.ONE_LINE_HEIGHT * 2;

                for (int j = 0; j < sheetPage.rows.Count; j++)
                {
                    SheetRow  row  = sheetPage.rows[j];
                    SheetCell cell = row.cells[i];

                    if (column.isCollection)
                    {
                        Array cellData     = (Array)cell.data;
                        int   highestWidth = 0;
                        for (int h = 0; h < cellData.Length; h++)
                        {
                            object cellObject = cellData.GetValue(h);
                            string content    = cellObject != null?cellObject.ToString() : Localization.NULLOBJECT_TO_STRING;

                            int width = CalculateWidth(content);
                            highestWidth = Mathf.Max(highestWidth, width);
                        }

                        int widthSet  = Mathf.Clamp(highestWidth, WindowSettings.MINWIDTH, WindowSettings.MAXWIDTH_INITALIZATION);
                        int heightSet = Mathf.Min(cellData.Length * WindowSettings.ONE_LINE_HEIGHT, WindowSettings.MAXHEIGHT_INITALIZATION);

                        widthSizes[i + 1, j + 1]  = widthSet;
                        heightSizes[i + 1, j + 1] = heightSet;
                    }
                    else
                    {
                        string content = cell.data != null?cell.data.ToString() : Localization.NULLOBJECT_TO_STRING;

                        int width = CalculateWidth(content);
                        width = Mathf.Clamp(width, WindowSettings.MINWIDTH, WindowSettings.MAXWIDTH_INITALIZATION);

                        widthSizes[i + 1, j + 1]  = width;
                        heightSizes[i + 1, j + 1] = WindowSettings.ONE_LINE_HEIGHT;
                    }
                }
            }

            for (int i = 0; i < widthSizes.GetLength(0); i++)
            {
                int heighestWidth = WindowSettings.MINWIDTH;

                for (int j = 0; j < widthSizes.GetLength(1); j++)
                {
                    int width = widthSizes[i, j];
                    heighestWidth = Mathf.Max(heighestWidth, width);
                }

                widthDimensions.Add(heighestWidth + WindowSettings.WIDTH_PADDING);
            }

            for (int i = 0; i < heightSizes.GetLength(1); i++)
            {
                int heighestHeight = WindowSettings.MINHEIGHT;

                for (int j = 0; j < heightSizes.GetLength(0); j++)
                {
                    int height = heightSizes[j, i];
                    heighestHeight = Mathf.Max(heighestHeight, height);
                }

                heightDimensions.Add(heighestHeight + WindowSettings.HEIGHT_PADDING);
            }
        }
Exemplo n.º 6
0
        public static SheetPage DeconstructDatasheetCode(DatasheetType datasheetType)
        {
            SheetPage sheetPage = new SheetPage();

            sheetPage.sheetName = datasheetType.GetIdentifier();
            sheetPage.index     = (int)datasheetType;

            Type recordType = Type.GetType(SheetStringDefinitions.NAMESPACE + "." + sheetPage.sheetRecordName + SheetStringDefinitions.ASSEMBLY_LOCATION);

            FieldInfo[] fieldInfos = recordType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfos        = fieldInfos.Where(i => i.IsPublic || i.GetCustomAttribute <SerializeField>() != null).ToArray();
            sheetPage.columns = new List <SheetColumn>(fieldInfos.Length);
            for (int i = 0; i < fieldInfos.Length; i++)
            {
                FieldInfo fieldInfo = fieldInfos[i];

                SheetColumn column = new SheetColumn();
                sheetPage.columns.Add(column);
                ColumnName columnNameAttribute = fieldInfo.GetCustomAttribute <ColumnName>();
                column.serializationName = columnNameAttribute.columnName;
                column.propertyName      = fieldInfo.Name.Remove(0, 1).FirstLetterToUpper();
                column.isCollection      = fieldInfo.FieldType.IsArray;
                column.dataType          = GetDataType(fieldInfo.FieldType);
                if (column.dataType == SheetDataType.Reference)
                {
                    string typeName = fieldInfo.FieldType.Name;
                    string datasheetName;
                    if (column.isCollection)
                    {
                        datasheetName = typeName.Remove(typeName.Length - SheetStringDefinitions.IDENTIFIER_SUFFIX.Length - SheetStringDefinitions.ARRAY_VARIABLE_SUFFIX.Length);
                    }
                    else
                    {
                        datasheetName = typeName.Remove(typeName.Length - SheetStringDefinitions.IDENTIFIER_SUFFIX.Length);
                    }

                    column.referenceSheet = Enum.Parse(typeof(DatasheetType), datasheetName).GetIdentifier();
                }
            }

            Type      modelType        = Type.GetType(SheetStringDefinitions.NAMESPACE + "." + sheetPage.sheetModelNameUpperCase + SheetStringDefinitions.ASSEMBLY_LOCATION);
            object    modelObject      = AssetDatabase.LoadAssetAtPath(sheetPage.scriptableObjectFileLocation, modelType);
            FieldInfo recordsFieldInfo = modelType.GetField(SheetStringDefinitions.RECORDS_PROPERTY_NAME, BindingFlags.NonPublic | BindingFlags.Instance);
            Array     modelRecords     = (Array)recordsFieldInfo.GetValue(modelObject);

            Type      recordBaseType            = recordType.BaseType;
            FieldInfo recordIdentifierFieldInfo = recordBaseType.GetField(SheetStringDefinitions.RECORD_IDENTIFIER_VARIABLE, BindingFlags.NonPublic | BindingFlags.Instance);

            Type identifierType = Type.GetType(sheetPage.sheetIdentifierName + SheetStringDefinitions.ASSEMBLY_LOCATION);

            sheetPage.rows = new List <SheetRow>(modelRecords.Length);
            for (int i = 0; i < modelRecords.Length; i++)
            {
                object record = modelRecords.GetValue(i);

                SheetRow sheetRow = new SheetRow();
                sheetPage.rows.Add(sheetRow);

                object identifierEnumValue = recordIdentifierFieldInfo.GetValue(record);
                sheetRow.index      = (int)identifierEnumValue;
                sheetRow.identifier = identifierEnumValue.GetIdentifier();
                sheetRow.enumValue  = identifierEnumValue.ToString();

                sheetRow.cells = new List <SheetCell>(fieldInfos.Length);
                for (int j = 0; j < fieldInfos.Length; j++)
                {
                    FieldInfo   fieldInfo = fieldInfos[j];
                    SheetColumn column    = sheetPage.columns[j];
                    object      data      = fieldInfo.GetValue(record);
                    if (data == null)
                    {
                        data = column.GetDefaultCellValue();
                    }
                    if (column.dataType == SheetDataType.Reference)
                    {
                        if (column.isCollection)
                        {
                            Array    arrayData  = ((Array)data);
                            string[] stringData = new string[arrayData.Length];
                            for (int h = 0; h < arrayData.Length; h++)
                            {
                                stringData[h] = arrayData.GetValue(h).GetIdentifier();
                            }

                            data = stringData;
                        }
                        else
                        {
                            data = data.GetIdentifier();
                        }
                    }

                    SheetCell cell = new SheetCell(data);
                    sheetRow.cells.Add(cell);
                }
            }

            return(sheetPage);
        }