private void RemoveComponent(ObjectComponent component, bool checkAttributes) { component.Removed(); this.components.Remove(component); this.componentReferences.Remove(component.GetType().FullName); this.componentValues.Remove(component.GetType().FullName); if (checkAttributes) { CheckAllAttributes(); } }
private bool CheckAttributes(ObjectComponent component) { #if WINDOWS System.Reflection.MemberInfo info = component.GetType(); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i] is RequireComponent) { bool found = false; int foundCount = 0; string[] typeNames = (attributes[i] as RequireComponent).ComponentsTypeNames.Split('|'); foreach (string typeName in typeNames) { string typeNameTrim = typeName.Trim(); foreach (var cmp in components) { if (cmp.GetType().Name.Equals(typeNameTrim)) { foundCount++; break; } } } if ((foundCount == typeNames.Count() && (attributes[i] as RequireComponent).requireAll) || (foundCount > 0 && !(attributes[i] as RequireComponent).requireAll)) { found = true; } if (!found && this.componentValues.ContainsKey(component.GetType().FullName)) { RemoveComponent(component, false); } if (!found) { return(false); } } } #endif return(true); }
private void LoadComponentValues(ObjectComponent component) { // The component is assigned? if (!componentValues.ContainsKey(component.GetType().FullName)) return; #if WINDOWS List<PropertyInfo> props = new List<PropertyInfo>(component.GetType().GetProperties()); //foreach (PropertyInfo pinfo in props) //{ // Console.WriteLine("c:" + component.Transform.gameObject + " b: " + pinfo.Name); //} #elif WINRT List<PropertyInfo> props = new List<PropertyInfo>(component.GetType().GetRuntimeProperties()); //foreach (PropertyInfo pinfo in props) //{ // Debug.WriteLine("cc: " + component.GetType().Name + " c:" + component.Transform.gameObject + " b: " + pinfo.Name); // Debug.WriteLine("VALUE: " + pinfo.GetValue(component, null)); //} #endif foreach (PropertyInfo propInfo in props) { try { #if WINRT bool found = false; foreach (var item in componentValues[component.GetType().FullName]) { if (item.Key.Name == propInfo.Name && item.Key.TypeName == propInfo.PropertyType.FullName) { propInfo.SetValue(component, componentValues[component.GetType().FullName][item.Key], null); found = true; break; } } if (!found) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } #elif WINDOWS // dummy label PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); // There is a place to store the component value? if (!componentValues[component.GetType().FullName].ContainsKey(label)) { this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } else { propInfo.SetValue(component, componentValues[component.GetType().FullName][label], null); } #endif } catch (Exception ex) { Debug.WriteLine("Property not loaded: " + ex.ToString()); } } }
private void RemoveComponent(ObjectComponent component, bool checkAttributes) { component.Removed(); this.components.Remove(component); this.componentReferences.Remove(component.GetType().FullName); this.componentValues.Remove(component.GetType().FullName); if (checkAttributes) CheckAllAttributes(); }
/// <summary> /// Adds a component to this object. /// </summary> /// <param name="component"></param> public bool AddComponent(ObjectComponent component) { #if WINDOWS if (!CheckAttributes(component)) return false; // Check existing components attributes: foreach (var cmp in components) { System.Reflection.MemberInfo info = cmp.GetType(); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i] is Unique) { if ((attributes[i] as Unique).Options == Unique.UniqueOptions.Explicit) { if (component.GetType() == cmp.GetType()) return false; } else { if (component.GetType().IsAssignableFrom(cmp.GetType()) || cmp.GetType().IsAssignableFrom(component.GetType())) return false; var baseA = component.GetType().BaseType; var baseB = cmp.GetType().BaseType; while (true) { if (baseA == typeof(ExtendedObjectComponent) || baseB == typeof(ExtendedObjectComponent) || baseA == typeof(ObjectComponent) || baseB == typeof(ObjectComponent)) { break; } else if (baseA == baseB) { return false; } else if (baseA.IsAssignableFrom(baseB) || baseB.IsAssignableFrom(baseA)) { return false; } baseA = baseA.BaseType; baseB = baseB.BaseType; } } } } } #endif // This component is already assigned? if (!this.componentValues.ContainsKey(component.GetType().FullName)) { this.componentValues[component.GetType().FullName] = new Dictionary<PropertyLabel, object>(new PropertyLabel.EqualityComparer()); } else { // Component already added, nothing to do here, return. return false; } component.Transform = this.transform; component.Name = component.GetType().Name; this.components.Add(component); this.componentReferences.Add(component.GetType().FullName); // Get through all the properties in the component and assign them #if WINDOWS List<PropertyInfo> props = new List<PropertyInfo>(component.GetType().GetProperties()); #elif WINRT List<PropertyInfo> props = new List<PropertyInfo>(component.GetType().GetRuntimeProperties()); #endif foreach (PropertyInfo propInfo in props) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } if (!SceneManager.IsEditor) { component.Initialize(); } else if (SceneManager.IsEditor && component is ExtendedObjectComponent) { component.Initialize(); } return true; }
private bool CheckAttributes(ObjectComponent component) { #if WINDOWS System.Reflection.MemberInfo info = component.GetType(); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i] is RequireComponent) { bool found = false; int foundCount = 0; string[] typeNames = (attributes[i] as RequireComponent).ComponentsTypeNames.Split('|'); foreach (string typeName in typeNames) { string typeNameTrim = typeName.Trim(); foreach (var cmp in components) { if (cmp.GetType().Name.Equals(typeNameTrim)) { foundCount++; break; } } } if ((foundCount == typeNames.Count() && (attributes[i] as RequireComponent).requireAll) || (foundCount > 0 && !(attributes[i] as RequireComponent).requireAll)) found = true; if (!found && this.componentValues.ContainsKey(component.GetType().FullName)) RemoveComponent(component, false); if (!found) return false; } } #endif return true; }
private void LoadComponentValues(ObjectComponent component) { // The component is assigned? if (!componentValues.ContainsKey(component.GetType().FullName)) { return; } #if WINDOWS List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetProperties()); //foreach (PropertyInfo pinfo in props) //{ // Console.WriteLine("c:" + component.Transform.gameObject + " b: " + pinfo.Name); //} #elif WINRT List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetRuntimeProperties()); //foreach (PropertyInfo pinfo in props) //{ // Debug.WriteLine("cc: " + component.GetType().Name + " c:" + component.Transform.gameObject + " b: " + pinfo.Name); // Debug.WriteLine("VALUE: " + pinfo.GetValue(component, null)); //} #endif foreach (PropertyInfo propInfo in props) { try { #if WINRT bool found = false; foreach (var item in componentValues[component.GetType().FullName]) { if (item.Key.Name == propInfo.Name && item.Key.TypeName == propInfo.PropertyType.FullName) { propInfo.SetValue(component, componentValues[component.GetType().FullName][item.Key], null); found = true; break; } } if (!found) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } #elif WINDOWS // dummy label PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); // There is a place to store the component value? if (!componentValues[component.GetType().FullName].ContainsKey(label)) { this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } else { propInfo.SetValue(component, componentValues[component.GetType().FullName][label], null); } #endif } catch (Exception ex) { Debug.WriteLine("Property not loaded: " + ex.ToString()); } } }
/// <summary> /// Adds a component to this object. /// </summary> /// <param name="component"></param> public bool AddComponent(ObjectComponent component) { #if WINDOWS if (!CheckAttributes(component)) { return(false); } // Check existing components attributes: foreach (var cmp in components) { System.Reflection.MemberInfo info = cmp.GetType(); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i] is Unique) { if ((attributes[i] as Unique).Options == Unique.UniqueOptions.Explicit) { if (component.GetType() == cmp.GetType()) { return(false); } } else { if (component.GetType().IsAssignableFrom(cmp.GetType()) || cmp.GetType().IsAssignableFrom(component.GetType())) { return(false); } var baseA = component.GetType().BaseType; var baseB = cmp.GetType().BaseType; while (true) { if (baseA == typeof(ExtendedObjectComponent) || baseB == typeof(ExtendedObjectComponent) || baseA == typeof(ObjectComponent) || baseB == typeof(ObjectComponent)) { break; } else if (baseA == baseB) { return(false); } else if (baseA.IsAssignableFrom(baseB) || baseB.IsAssignableFrom(baseA)) { return(false); } baseA = baseA.BaseType; baseB = baseB.BaseType; } } } } } #endif // This component is already assigned? if (!this.componentValues.ContainsKey(component.GetType().FullName)) { this.componentValues[component.GetType().FullName] = new Dictionary <PropertyLabel, object>(new PropertyLabel.EqualityComparer()); } else { // Component already added, nothing to do here, return. return(false); } component.Transform = this.transform; component.Name = component.GetType().Name; this.components.Add(component); this.componentReferences.Add(component.GetType().FullName); // Get through all the properties in the component and assign them #if WINDOWS List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetProperties()); #elif WINRT List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetRuntimeProperties()); #endif foreach (PropertyInfo propInfo in props) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } if (!SceneManager.IsEditor) { component.Initialize(); } else if (SceneManager.IsEditor && component is ExtendedObjectComponent) { component.Initialize(); } return(true); }
private void objectContextMenuStrip_Opening(object sender, CancelEventArgs e) { addComponentToolStripMenuItem.DropDownItems.Clear(); if (SceneManager.ScriptsAssembly == null) { addComponentToolStripMenuItem.Visible = false; return; } else { addComponentToolStripMenuItem.Visible = true; ObjectComponent dummy = new ObjectComponent(); foreach (Type type in SceneManager.ScriptsAssembly.GetTypes()) { if (type.IsSubclassOf(dummy.GetType())) { string fullname = type.FullName; ToolStripMenuItem lastItem = addComponentToolStripMenuItem; if (fullname != type.Name && fullname.Contains('.')) { string[] splitted = fullname.Split('.'); int scount = splitted.Count() - 1; for (int i = 0; i < scount; i++) { ToolStripMenuItem item = null; string camelCaseFix = GibboHelper.SplitCamelCase(splitted[i]); foreach(ToolStripMenuItem _item in lastItem.DropDownItems) { if (_item.Text.Equals(camelCaseFix)) { item = _item; break; } } if(item == null) item = new ToolStripMenuItem(camelCaseFix); lastItem.DropDownItems.Insert(0, item); lastItem = item; } } ToolStripMenuItem newItem = new ToolStripMenuItem(GibboHelper.SplitCamelCase(type.Name)); newItem.Tag = type; newItem.Image = Properties.Resources.component_item; newItem.Click += new EventHandler(component_Click); lastItem.DropDownItems.Add(newItem); } } } }