public virtual void CreateEditor() { EditorWindow <T> editor = new EditorWindow <T>(); editor.init(800, 600, (uint)(PlatformSupportAbstract <T> .WindowFlags.Risizeable)); editor.Caption = "Editing Default Something"; Type gameObjectType = this.GetType(); T y_location = M.Zero <T>(); BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; System.Reflection.FieldInfo[] gameObjectFields = gameObjectType.GetFields(bindingFlags); foreach (FieldInfo gameObjectField in gameObjectFields) { object[] TestAttributes = gameObjectField.GetCustomAttributes(typeof(GameDataAttribute), false); if (TestAttributes.Length > 0) { GameDataAttribute gameDataAttribute = (GameDataAttribute)TestAttributes[0]; TextWidget <T> name = new TextWidget <T>(gameDataAttribute.Name, M.Zero <T>(), y_location, M.New <T>(15)); editor.AddChild(name); object test = gameObjectField.GetValue(this); TextWidget <T> value = new TextWidget <T>(test.ToString(), name.Width, y_location, M.New <T>(15)); editor.AddChild(value); y_location.AddEquals(name.Height); } } }
public override void WriteField(XmlWriter xmlWriter, object fieldToWrite) { object list = fieldToWrite; int listCount = (int)list.GetType().GetProperty("Count").GetValue(list, null); for (int index = 0; index < listCount; index++) { object item = list.GetType().GetMethod("get_Item").Invoke(list, new object[] { index }); if (item is GameObject <DoubleComponent> ) { xmlWriter.WriteStartElement("Item"); GameDataAttribute.WriteTypeAttributes(xmlWriter, item); ((GameObject <DoubleComponent>)item).WriteGameObjectData(xmlWriter); xmlWriter.WriteEndElement(); } else { xmlWriter.WriteValue(item); xmlWriter.WriteValue(" "); } } }
public override object ReadField(XmlReader xmlReader) { object list = GameDataAttribute.ReadTypeAttributes(xmlReader); while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element) { if (xmlReader.Name == "Item") { object listItem = GameDataAttribute.ReadTypeAttributes(xmlReader); if (listItem is GameObject <DoubleComponent> ) { GameObject <DoubleComponent> listGameObject = (GameObject <DoubleComponent>)listItem; listGameObject.LoadGameObjectData(xmlReader); MethodInfo addMethod = list.GetType().GetMethod("Add"); addMethod.Invoke(list, new object[] { listGameObject }); } else { throw new NotImplementedException("List of non-GameObjects not deserializable"); } } } else if (xmlReader.NodeType == XmlNodeType.EndElement) { break; } } return(list); }
public override object ReadField(XmlReader xmlReader) { //this bit may be broken..jd IVector <DoubleComponent> newVector2D = (IVector <DoubleComponent>)GameDataAttribute.ReadTypeAttributes(xmlReader); string xString = xmlReader.GetAttribute("x"); string yString = xmlReader.GetAttribute("y"); newVector2D.Set(Convert.ToDouble(xString), Convert.ToDouble(yString)); return(newVector2D); }
public virtual void WriteGameObjectData(XmlWriter xmlWriter) { Type gameObjectType = this.GetType(); BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; System.Reflection.FieldInfo[] fieldsOfGameObject = gameObjectType.GetFields(bindingFlags); foreach (FieldInfo fieldOfGameObject in fieldsOfGameObject) { object[] gameDataAttributes = fieldOfGameObject.GetCustomAttributes(typeof(GameDataAttribute), false); if (gameDataAttributes.Length > 0) { if (gameDataAttributes.Length > 1) { throw new Exception("You can only have one GameDataAttribute on any given Field."); } object objectWithAttribute = fieldOfGameObject.GetValue(this); if (objectWithAttribute != null) { GameDataAttribute singleGameDataAttribute = (GameDataAttribute)gameDataAttributes[0]; String Name = singleGameDataAttribute.Name; if (Name.Contains(" ")) { throw new Exception(this.ToString() + " : '" + Name + "' has a space. Attribute names con not contain spaces."); } xmlWriter.WriteStartElement(Name); GameDataAttribute.WriteTypeAttributes(xmlWriter, objectWithAttribute); xmlWriter.WriteEndAttribute(); if (objectWithAttribute == null) { throw new Exception(this.ToString() + " : " + fieldOfGameObject.ToString() + " must have a default Value.\n" + "\n" + "All data marked as [GameData] must be a primitive or a struct or if a class have a DEFALT Value or filed initializer."); } if (objectWithAttribute is GameObject <T> ) { ((GameObject <T>)objectWithAttribute).WriteGameObjectData(xmlWriter); } else { singleGameDataAttribute.WriteField(xmlWriter, objectWithAttribute); } xmlWriter.WriteEndElement(); } } } }
public virtual void LoadGameObjectData(XmlReader xmlReader) { while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element) { string AttributeNameForElement = xmlReader.Name; BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; System.Reflection.FieldInfo[] fieldsOfGameObject = this.GetType().GetFields(bindingFlags); foreach (FieldInfo fieldOfGameObject in fieldsOfGameObject) { object[] gameDataAttributes = fieldOfGameObject.GetCustomAttributes(typeof(GameDataAttribute), false); if (gameDataAttributes.Length > 0) { GameDataAttribute singleGameDataAttribute = (GameDataAttribute)gameDataAttributes[0]; string AttributeNameForField = singleGameDataAttribute.Name; if (AttributeNameForField == AttributeNameForElement) { if (fieldOfGameObject.FieldType.IsSubclassOf(typeof(GameObject <T>))) { GameObject <T> newGameObject = (GameObject <T>)GameDataAttribute.ReadTypeAttributes(xmlReader); newGameObject.LoadGameObjectData(xmlReader); fieldOfGameObject.SetValue(this, newGameObject); } else { object objectReadByAttribute = singleGameDataAttribute.ReadField(xmlReader); fieldOfGameObject.SetValue(this, objectReadByAttribute); } break; } } } } else if (xmlReader.NodeType == XmlNodeType.EndElement) { break; } } }