public static void SaveChanges(LevelEditorReference reference) { foreach (var cell in reference.cells) { if (cell.Key == "Type") { continue; } var memberInfo = reference.members[cell.Key]; Type value; if (memberInfo is FieldInfo) { value = ((FieldInfo)memberInfo).FieldType; } else if (memberInfo is PropertyInfo) { value = ((PropertyInfo)memberInfo).PropertyType; } else { throw new Exception(); } object toWriteValue; if (value == typeof(string)) { toWriteValue = cell.Value.InnerHTML; } else if (value == typeof(double)) { toWriteValue = double.Parse(cell.Value.InnerHTML); } else { throw new Exception(); } if (memberInfo is FieldInfo) { ((FieldInfo)memberInfo).SetValue(reference.gameObject, toWriteValue); } else { ((PropertyInfo)memberInfo).SetValue(reference.gameObject, toWriteValue); } } Refresh(); }
public static async Task <LevelEditorReference> CreateReference(GameObject gameObject, HTMLTableElement table, int indent = 0) { if (gameObject.Name == null) { return(null); } HTMLTableRowElement row = new HTMLTableRowElement(); row.Indent(indent); var cell = new HTMLTableDataCellElement(); cell.Style.BorderBottom = "1px solid black"; cell.AppendChild(new HTMLAnchorElement { InnerHTML = gameObject.Name, Href = "javascript:void(0)", OnClick = v => Select(gameObject) }); cell.AppendChild(new Text(" ")); var cross = new HTMLAnchorElement { OnClick = v => Remove(gameObject), Href = "javascript:void(0)" }; cross.AppendChild(LevelEditor.cross = LevelEditor.cross.CloneNode().As <HTMLImageElement>()); cell.AppendChild(cross); row.AppendChild(cell); table.AppendChild(row); LevelEditorReference result = new LevelEditorReference { gameObject = gameObject, cells = new Dictionary <string, HTMLElement>(), members = new Dictionary <string, MemberInfo>() }; string type; if (gameObject is Character) { type = "Character"; } else if (gameObject is Shot) { type = "Shot"; } else if (gameObject is RealGameObject) { type = "Real Thing"; } else if (gameObject is DrawnGameObject) { type = "Illusion"; } else if (gameObject is Movement) { type = "Movement by User"; } else if (gameObject is Shoot_OnKey) { type = "Shooting by User"; } else if (gameObject is Level) { type = "Level"; } else { throw new Exception($"Type not allowed: {gameObject.GetType().FullName}"); } row = new HTMLTableRowElement(); row.Indent(indent); cell = new HTMLTableDataCellElement { InnerHTML = "Type" }; HTMLTableDataCellElement cell2 = new HTMLTableDataCellElement { InnerHTML = type }; result.cells.Add("Type", cell2); row.AppendChild(cell); row.AppendChild(cell2); table.AppendChild(row); List <MemberInfo> fields = new List <MemberInfo>(gameObject.GetType().GetFields()); fields.AddRange(gameObject.GetType().GetProperties()); foreach (var field in fields) { if (field.IsStatic) { continue; } Type memberType; if (field is FieldInfo) { memberType = ((FieldInfo)field).FieldType; } else if (field is PropertyInfo) { memberType = ((PropertyInfo)field).PropertyType; } else { throw new Exception(); } var customAttributes2 = (ObjectCreatorAttribute[])field.GetCustomAttributes(typeof(ObjectCreatorAttribute)); string s; GameObject o; if (allowed.Contains(memberType) || customAttributes2.Length == 1) { object value; if (field is FieldInfo) { value = ((FieldInfo)field).GetValue(gameObject); } else if (field is PropertyInfo) { value = ((PropertyInfo)field).GetValue(gameObject); } else { throw new Exception(); } if (value == null) { continue; } if (customAttributes2.Length == 1) { value = await GameObject.Create(value); } row = new HTMLTableRowElement(); row.Indent(indent); var contentEditable = ContentEditable.True; string valueString; s = value as string; if (s != null) { valueString = s; } else if (value is double) { valueString = ((double)value).ToString(); } else { contentEditable = ContentEditable.False; if (value is List <GameObject> ) { valueString = "List of Objects"; } else if (value is List <OnKeyEvent> ) { valueString = "List of Key Press Actions"; } else if (value is GameObject) { valueString = "Object"; } else { throw new Exception(); } } string name = field.Name; var customAttributes = (LevelEditorNameAttribute[])field.GetCustomAttributes(typeof(LevelEditorNameAttribute)); if (customAttributes.Length == 1) { name = customAttributes[0].LevelEditorName; } cell = new HTMLTableDataCellElement { InnerHTML = name }; cell2 = new HTMLTableDataCellElement { ContentEditable = contentEditable, InnerHTML = valueString }; result.cells.Add(field.Name, cell2); result.members.Add(field.Name, field); row.AppendChild(cell); row.AppendChild(cell2); table.AppendChild(row); if (value is List <GameObject> || value is List <OnKeyEvent> ) { foreach (GameObject _gameObject in (System.Collections.IList)(value as List <GameObject>) ?? (System.Collections.IList)(value as List <OnKeyEvent>)) { await CreateReference(_gameObject, table, indent + 1); } } o = value as GameObject; if (o != null) { await CreateReference(o, table, indent + 1); } } } row = new HTMLTableRowElement(); row.Indent(indent); cell = new HTMLTableDataCellElement(); cell.AppendChild(new HTMLButtonElement { InnerHTML = "Save Changes", OnClick = e => SaveChanges(result) }); row.AppendChild(cell); table.AppendChild(row); return(result); }