public EntityComponentViewModel(EntityComponent component) { Source = component; TypeDescriptor = TypeDescriptorFactory.Default.Find(component.GetType()); Name = ComponentName(); IsEnablable = HasEnabledProperty(); }
private void OnComponentAdded(EntityComponent component) { var expander = new EntityComponentExpander(component); expander.Root.IsExpanded = true; var componentInfo = GetEntityComponentInfo(component.GetType()); expander.Root.Header = componentInfo.Name; var propertyEditors = new List <UserControl>(); foreach (var prop in componentInfo.Properties) { if (prop.Name == "Id") { continue; } var elem = GetEditorForProperty(component, prop); expander.ComponentList.Children.Add(elem); } componentGridList.Children.Add(expander); }
/// <inheritdoc/> public void Check(EntityComponent component, Entity entity, AssetItem assetItem, string targetUrlInStorage, AssetCompilerResult result) { var typeDescriptor = TypeDescriptorFactory.Default.Find(component.GetType()); var componentName = typeDescriptor.Type.Name; // TODO: Should we check attributes for another name? var members = typeDescriptor.Members; foreach (var member in members) { // Value types cannot be null, and must always have a proper default value if (member.Type.IsValueType) { continue; } MemberRequiredAttribute memberRequired; if ((memberRequired = member.GetCustomAttributes <MemberRequiredAttribute>(inherit: true) .FirstOrDefault()) != null) { if (member.Get(component) is null) { WriteResult(result, componentName, targetUrlInStorage, entity.Name, member.Name, memberRequired.ReportAs); } } } }
public void AddComponent(EntityComponent component) { AddComponentPrivate(component); if (IsInEntities) { Entities.OnComponentAdded(this, component.GetType()); } }
public bool AddComponent(EntityComponent component) { if(!HasComponentOfType(component.GetType())) { if(component.AllDependenciesLinked()) { component.entity = this; component.OnLink(); components.Add(component); return true; } Console.WriteLine("Error! Not all dependencies were linked in type {0}", component.GetType()); return false; } Console.WriteLine("Error! Already had component of type {0}", component.GetType()); return false; }
public IActorRef GetOrCreateActorForComponent(EntityComponent component) { if (!(component is T asT)) { throw new ArgumentException( $"Wrong component type. Expected {typeof(T).FullName} got {component.GetType().Name}"); } return(GetOrCreateActorForComponent(asT)); }
public static void InjectEntityComponents(this EntityComponent component) { var fields = component.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); var properties = component.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); var getComponentMethod = component.Entity.GetType().GetMethod("Get", new Type[] { }); foreach (var field in fields) { if (field.GetCustomAttribute <EntityComponentAttribute>() != null) { var componentType = field.FieldType; var injectedComponent = getComponentMethod.MakeGenericMethod(componentType) .Invoke(component.Entity, new object[] { }); if (injectedComponent == null) { throw new NullEntityComponentException(componentType.Name); } field.SetValue(component, injectedComponent); } } foreach (var prop in properties) { if (prop.GetCustomAttribute <EntityComponentAttribute>() != null) { var componentType = prop.PropertyType; var injectedComponent = getComponentMethod.MakeGenericMethod(componentType) .Invoke(component.Entity, new object[] { }); if (injectedComponent == null) { throw new NullEntityComponentException(componentType.Name); } prop.SetValue(component, injectedComponent); } } }
private ComponentChange ParseChange(RTData data) { uint i = 1; long entityId = data.GetInt(i).Value; i++; bool isRemoved = data.GetInt(i) == 1; i++; int componentTypeInd = data.GetInt(i).Value; i++; Type t = EntityComponent.GetType(componentTypeInd); EntityComponent after = EntityComponent.Create(t, data, i); return(new ComponentChange(entityId, isRemoved, null, after)); }
//public List<EntityComponentProperty> Properties; public static void RestoreEntityComponentData(EntityComponent entityComponent, CloneEntityComponentData data) { foreach (var componentProperty in data.Properties) { switch (componentProperty.Type) { case EntityComponentPropertyType.Field: { var field = entityComponent.GetType().GetTypeInfo().GetDeclaredField(componentProperty.Name); if (field == null) // Field disappeared? should we issue a warning? { continue; } var result = MergeObject(field.GetValue(entityComponent), componentProperty.Value); field.SetValue(entityComponent, result); } break; case EntityComponentPropertyType.Property: { var property = entityComponent.GetType().GetTypeInfo().GetDeclaredProperty(componentProperty.Name); if (property == null) // Property disappeared? should we issue a warning? { continue; } var result = MergeObject(property.GetValue(entityComponent, null), componentProperty.Value); if (property.CanWrite) { property.SetValue(entityComponent, result, null); } } break; default: throw new NotImplementedException(); } } }
public static CloneEntityComponentData GenerateEntityComponentData(EntityComponent entityComponent) { var data = new CloneEntityComponentData { Properties = new List <EntityComponentProperty>() }; foreach (var field in entityComponent.GetType().GetTypeInfo().DeclaredFields) { //if (!field.GetCustomAttributes(typeof(DataMemberConvertAttribute), true).Any()) // continue; data.Properties.Add(new EntityComponentProperty(EntityComponentPropertyType.Field, field.Name, field.GetValue(entityComponent))); } foreach (var property in entityComponent.GetType().GetTypeInfo().DeclaredProperties) { //if (!property.GetCustomAttributes(typeof(DataMemberConvertAttribute), true).Any()) // continue; data.Properties.Add(new EntityComponentProperty(EntityComponentPropertyType.Property, property.Name, property.GetValue(entityComponent, null))); } return(data); }
public EntityComponent AddComponent(EntityComponent component) { #if UNITY_EDITOR foreach (var entityComponent in components) { if (entityComponent.GetType() == component.GetType()) { Assert.Fail("添加了相同的组建"); } } #endif components.Add(component); component.Entity = this; return(component); }
private UserControl GetEditorForProperty(EntityComponent component, ComponentPropertyItem property) { var type = property.PropertyType; if (property.PropertyType.IsEnum) { return(new DataTypeEditors.EnumEditor(component, property)); } else if (type == typeof(int)) { return(new DataTypeEditors.Int32Editor(component, property)); } else if (type == typeof(float)) { return(new DataTypeEditors.SingleEditor(component, property)); } else if (type == typeof(bool)) { return(new DataTypeEditors.BooleanEditor(component, property)); } else if (type == typeof(Xenko.Core.Mathematics.Vector3)) { return(new DataTypeEditors.Vector3Editor(component, property)); } else if (type == typeof(Xenko.Core.Mathematics.Vector2)) { return(new DataTypeEditors.Vector2Editor(component, property)); } else if (type == typeof(Xenko.Core.Mathematics.Quaternion)) { if (component.GetType().Name == "TransformComponent" && property.Name == "Rotation") { return(new DataTypeEditors.RotationEditor(component, property)); } else { return(new DataTypeEditors.QuaternionEditor(component, property)); } } else { return(new DataTypeEditors.UnsupportedEditor(component, property)); } }
/// <inheritdoc/> public void Check(EntityComponent component, Entity entity, AssetItem assetItem, string targetUrlInStorage, AssetCompilerResult result) { var type = component.GetType(); var componentName = type.Name; // QUESTION: Should we check attributes for another name? var fields = type.GetFields(); // public fields, only those can be serialized? var properties = type.GetRuntimeProperties(); // all properties may have a DataMember attribute foreach (var field in fields) { if (field.FieldType.IsValueType) { continue; // value types cannot be null, and must always have a proper default value } MemberRequiredAttribute memberRequired; if ((memberRequired = field.GetCustomAttribute <MemberRequiredAttribute>()) != null) { if (field.GetValue(component) == null) { WriteResult(result, componentName, targetUrlInStorage, entity.Name, field.Name, memberRequired.ReportAs); } } } foreach (var prop in properties) { if (prop.PropertyType.IsValueType) { continue; // value types cannot be null, and must always have a proper default value } MemberRequiredAttribute memberRequired; if ((memberRequired = prop.GetCustomAttribute <MemberRequiredAttribute>()) != null) { if (prop.GetValue(component) == null) { WriteResult(result, componentName, targetUrlInStorage, entity.Name, prop.Name, memberRequired.ReportAs); } } } }
public override bool Equals(object obj) { EntityComponent other = obj as EntityComponent; if (other == null || other.GetType() != this.GetType()) { return(false); } for (int i = 0; i < _numbers.Count; i++) { if (_numbers [i] != other._numbers [i]) { return(false); } } for (int i = 0; i < _vectors.Count; i++) { if (_vectors [i] != other._vectors [i]) { return(false); } } for (int i = 0; i < _bools.Count; i++) { if (_bools [i] != other._bools [i]) { return(false); } } for (int i = 0; i < _strings.Count; i++) { if (_strings [i] != other._strings [i]) { return(false); } } return(true); }
public static T GetBaseEntity <T>(EntityComponent <T> entityComponent) where T : BaseEntity { var propertyInfo = entityComponent.GetType().GetProperty("baseEntity", BindingFlags.Instance | BindingFlags.NonPublic); return((T)propertyInfo.GetValue(entityComponent, null)); }
/// <summary> /// Search through all fields and properties of the component and inject dependent components based on <see cref="InjectComponentAttribute"/>. /// </summary> /// <param name="component"></param> public static void InjectComponents(this EntityComponent component) { var fields = component.GetType().GetFields(bindingFlags); var properties = component.GetType().GetProperties(bindingFlags); var getComponentMethod = component.Entity.GetType().GetMethod("Get", new Type[] { }); var getAllComponentsMethod = component.Entity.GetType().GetMethod("GetAll", new Type[] { }); foreach (var field in fields) { if (field.GetCustomAttribute <InjectComponentAttribute>() != null) { object injectedComponent; var componentType = field.FieldType; if (typeof(IEnumerable <EntityComponent>).IsAssignableFrom(componentType)) { injectedComponent = getAllComponentsMethod.MakeGenericMethod(componentType.GetGenericArguments()[0]) .Invoke(component.Entity, new object[] { }); } else { injectedComponent = getComponentMethod.MakeGenericMethod(componentType) .Invoke(component.Entity, new object[] { }); } if (injectedComponent == null) { throw new NullEntityComponentException(componentType.Name); } field.SetValue(component, injectedComponent); } } foreach (var prop in properties) { if (prop.GetCustomAttribute <InjectComponentAttribute>() != null) { object injectedComponent; var componentType = prop.PropertyType; if (typeof(IEnumerable <EntityComponent>).IsAssignableFrom(componentType)) { injectedComponent = getAllComponentsMethod.MakeGenericMethod(componentType.GetGenericArguments()[0]) .Invoke(component.Entity, new object[] { }); } else { injectedComponent = getComponentMethod.MakeGenericMethod(componentType) .Invoke(component.Entity, new object[] { }); } if (injectedComponent == null) { throw new NullEntityComponentException(componentType.Name); } prop.SetValue(component, injectedComponent); } } }
public void AddComponent(EntityComponent component) { _componentMap[component.GetType()] = component; }
/// <summary> /// Display components that are tagged with the <see cref="DisplayAttribute"/>. /// </summary> /// <param name="context">Context of the view model.</param> /// <param name="viewModel">The current view model</param> /// <param name="component">The entity component to display</param> private void AutoDisplayComponent(ViewModelContext context, IViewModelNode viewModel, EntityComponent component) { var displayComp = DisplayAttribute.GetDisplay(component.GetType()); if (displayComp == null) return; var componentViewModel = viewModel.GetChildrenByName("Component"); if (componentViewModel == null) return; // Change the name of the component being displayed if (!string.IsNullOrEmpty(displayComp.Name)) { var componentName = viewModel.GetChildrenByName("PropertyKeyName"); if (componentName != null) { componentName.Value = displayComp.Name; } } var propertyToDisplay = new List<Tuple<DisplayAttribute, ViewModelNode>>(); var memberInfos = new List<MemberInfo>(); memberInfos.AddRange(component.GetType().GetProperties()); memberInfos.AddRange(component.GetType().GetFields()); // Process fields and properties foreach (var property in memberInfos) { var display = DisplayAttribute.GetDisplay(property); if (display == null) continue; IViewModelContent modelContent = null; object modelValue = null; var propertyInfo = property as PropertyInfo; if (propertyInfo != null) { if (typeof(ParameterCollection).IsAssignableFrom(propertyInfo.PropertyType)) { modelValue = propertyInfo.GetValue(component, null); } else { modelContent = new PropertyInfoViewModelContent(new ParentNodeValueViewModelContent(), propertyInfo); } } var fieldInfo = property as FieldInfo; if (fieldInfo != null) { if (typeof(ParameterCollection).IsAssignableFrom(fieldInfo.FieldType)) { modelValue = fieldInfo.GetValue(component); } else { modelContent = new FieldInfoViewModelContent(new ParentNodeValueViewModelContent(), fieldInfo); } } var propertyViewModel = modelValue != null ? new ViewModelNode(display.Name ?? property.Name, modelValue) : new ViewModelNode(display.Name ?? property.Name, modelContent); propertyViewModel.GenerateChildren(context); propertyToDisplay.Add(new Tuple<DisplayAttribute, ViewModelNode>(display, propertyViewModel)); } foreach(var item in propertyToDisplay.OrderBy((left) => left.Item1.Order)) { componentViewModel.Children.Add(item.Item2); } }
/// <summary> /// Display components that are tagged with the <see cref="DisplayAttribute"/>. /// </summary> /// <param name="context">Context of the view model.</param> /// <param name="viewModel">The current view model</param> /// <param name="component">The entity component to display</param> private void AutoDisplayComponent(ViewModelContext context, IViewModelNode viewModel, EntityComponent component) { var displayComp = DisplayAttribute.GetDisplay(component.GetType()); if (displayComp == null) { return; } var componentViewModel = viewModel.GetChildrenByName("Component"); if (componentViewModel == null) { return; } // Change the name of the component being displayed if (!string.IsNullOrEmpty(displayComp.Name)) { var componentName = viewModel.GetChildrenByName("PropertyKeyName"); if (componentName != null) { componentName.Value = displayComp.Name; } } var propertyToDisplay = new List <Tuple <DisplayAttribute, ViewModelNode> >(); var memberInfos = new List <MemberInfo>(); memberInfos.AddRange(component.GetType().GetProperties()); memberInfos.AddRange(component.GetType().GetFields()); // Process fields and properties foreach (var property in memberInfos) { var display = DisplayAttribute.GetDisplay(property); if (display == null) { continue; } IViewModelContent modelContent = null; object modelValue = null; var propertyInfo = property as PropertyInfo; if (propertyInfo != null) { if (typeof(ParameterCollection).IsAssignableFrom(propertyInfo.PropertyType)) { modelValue = propertyInfo.GetValue(component, null); } else { modelContent = new PropertyInfoViewModelContent(new ParentNodeValueViewModelContent(), propertyInfo); } } var fieldInfo = property as FieldInfo; if (fieldInfo != null) { if (typeof(ParameterCollection).IsAssignableFrom(fieldInfo.FieldType)) { modelValue = fieldInfo.GetValue(component); } else { modelContent = new FieldInfoViewModelContent(new ParentNodeValueViewModelContent(), fieldInfo); } } var propertyViewModel = modelValue != null ? new ViewModelNode(display.Name ?? property.Name, modelValue) : new ViewModelNode(display.Name ?? property.Name, modelContent); propertyViewModel.GenerateChildren(context); propertyToDisplay.Add(new Tuple <DisplayAttribute, ViewModelNode>(display, propertyViewModel)); } foreach (var item in propertyToDisplay.OrderBy((left) => left.Item1.Order)) { componentViewModel.Children.Add(item.Item2); } }