/// <summary>
        /// Returns the collection of available styles for a given type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override Dictionary <string, MemberDescriptor> GetStyleDescriptors(Type type)
        {
            // we should reflect a given component type and get properties and fields
            //return EditorReflector.GetStyleProperties(type, true); // restrict

            Dictionary <string, MemberDescriptor> output = new Dictionary <string, MemberDescriptor>();

            // 1. getting all public fields
            var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);

            foreach (var fieldInfo in fields)
            {
                var    name            = fieldInfo.Name;
                object @default        = null;
                var    styleAttributes = CoreReflector.GetMemberAttributes <StyleAttribute>(fieldInfo);
                if (styleAttributes.Count > 0)
                {
                    @default = styleAttributes[0].GetDefault();

                    /*if (!string.IsNullOrEmpty(styleAttributes[0].Name))
                     *  name = styleAttributes[0].Name; // attribute value overrides the name*/
                }

                var prop = new MemberDescriptor(name, fieldInfo.FieldType, GetStyleIcon(fieldInfo.FieldType), @default);// StyleProperty.CreateProperty(fieldInfo.FieldType);
                output[fieldInfo.Name] = prop;
            }

            // 2. getting all public properties having a *setter*
            var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var propertyInfo in properties)
            {
                //if (propertyInfo.GetSetMethod() == null)
                if (!propertyInfo.CanWrite)
                {
                    continue; // get only writeable propertis
                }
                var    name            = propertyInfo.Name;
                object @default        = null;
                var    styleAttributes = CoreReflector.GetMemberAttributes <StyleAttribute>(propertyInfo);
                if (styleAttributes.Count > 0)
                {
                    @default = styleAttributes[0].GetDefault();

                    /*if (!string.IsNullOrEmpty(styleAttributes[0].Name))
                     *  name = styleAttributes[0].Name; // attribute value overrides the name*/
                }

                var prop = new MemberDescriptor(name, propertyInfo.PropertyType, GetStyleIcon(propertyInfo.PropertyType), @default);// StyleProperty.CreateProperty(fieldInfo.FieldType);
                output[propertyInfo.Name] = prop;
            }

            return(output);
        }
示例#2
0
        /// <summary>
        /// Describes mulricast delegates
        /// </summary>
        /// <param name="componentType"></param>
        /// <returns></returns>
        public static string GetMulticastDelegates(Type componentType)
        {
            var memberNames = CoreReflector.GetFieldAndPropertyNames(componentType);
            //memberNames.Sort(MemberInfoSort);
            List <string> data = new List <string>();

            foreach (var name in memberNames)
            {
                MemberWrapper mw = new MemberWrapper(componentType, name);
                if (mw.MemberType == typeof(Core.Events.MulticastDelegate))
                {
                    var output = mw.MemberInfo.Name;

                    /*var clazz = mw.MemberInfo.DeclaringType;
                     * if (null != clazz)
                     * {
                     *  var types =
                     *
                     *
                     *  var instance = Activator.CreateInstance(clazz);
                     *  var value = mw.GetValue(instance);
                     *  if (null != value)
                     *      output = string.Format(@"{0} [{1}]", output, value.GetType().FullName);
                     * }*/

                    var attributes = CoreReflector.GetMemberAttributes <EventAttribute>(mw.MemberInfo);
                    if (attributes.Count > 0)
                    {
                        var eventAttribute = attributes[0];
                        output = string.Format(@"{0} → {1}", output, eventAttribute);
                    }

                    data.Add(output);
                }
            }

            data.Sort();

            StringBuilder sb = new StringBuilder();

            foreach (var item in data)
            {
                sb.AppendLine(item);
            }

            return(string.Format(@"Multicast delegates ({0}):
{1}
{2}", data.Count, Line, sb) + NewLine /* + NewLine*/);
        }
        public override void UpdateStyles(Selector selector, DictionaryDelta delta)
        {
            // nothing yet
            //Debug.Log("UpdateStyles: " + delta);
            var components = GetComponentsMatchingSelector(selector);

            delta.Process();

            foreach (Component component in components)
            {
                // TODO: find out which property changed and its value
                //var changedProp = _modifiedPropertyName;
                //component.SetStyle("paddingLeft", 30);

                // 1. for removals, clear the style
                foreach (string removal in delta.Removals.Keys)
                {
                    // TODO: set the default value (if exists)
                    // (default values should be implemented via the attribute)
                    //Debug.Log("Removing -> " + removal);
                    MemberWrapper wrapper = new MemberWrapper(component.GetType(), removal);

                    object value      = null;
                    var    attributes = CoreReflector.GetMemberAttributes <StyleAttribute>(wrapper.MemberInfo);
                    if (attributes.Count > 0)
                    {
                        value = attributes[0].GetDefault();
                    }

                    wrapper.SetValue(component, value);
                }
                // 2. for additions, set the style
                foreach (KeyValuePair <string, object> addition in delta.Additions)
                {
                    MemberWrapper wrapper = new MemberWrapper(component.GetType(), addition.Key);
                    wrapper.SetValue(component, addition.Value);
                }

                // 3. for updates, set the style
                foreach (KeyValuePair <string, object> update in delta.Updates)
                {
                    MemberWrapper wrapper = new MemberWrapper(component.GetType(), update.Key);
                    wrapper.SetValue(component, update.Value);
                }

                UnityComponentStylingGizmo.Show(components);
            }
        }
示例#4
0
        public override Dictionary <string, bool> Get(Type key)
        {
            //Debug.Log("Key: " + key);
            var partDict = base.Get(key);

            if (null == partDict)
            {
                partDict = new Dictionary <string, bool>();

                MemberInfo[] proxyMembers = key.GetMembers(BindingFlags.Public | BindingFlags.GetField | BindingFlags.Instance /*BindingFlags.NonPublic | BindingFlags.Instance*/); //BindingFlags.Public | BindingFlags.GetField | BindingFlags.GetProperty);

                foreach (MemberInfo proxyMemberInfo in proxyMembers)
                {
                    //var skinPartAttributes = proxyMemberInfo.GetCustomAttributes(typeof(SkinPartAttribute), true);
                    var skinPartAttributes = CoreReflector.GetMemberAttributes <SkinPartAttribute>(proxyMemberInfo);

                    foreach (SkinPartAttribute attribute in skinPartAttributes)
                    {
                        if (null != attribute)
                        {
                            string id = attribute.Id;
                            if (string.IsNullOrEmpty(id)) // Id is optional
                            {
                                // If Id not defined, lookup by member name
                                // Skin should contain a member having the same name
                                id = proxyMemberInfo.Name;
                            }

                            partDict.Add(id, attribute.Required);
                            //Debug.Log("    -> " + id + ": " + skinPartAttribute.Required);
                        }
                    }
                }

                Instance.Put(key, partDict);
#if DEBUG
                if (DebugMode)
                {
                    Debug.Log("Type added to SkinPartCache: " + key);
                }
#endif
            }

            return(partDict);
        }
示例#5
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="target">The target</param>
        internal PersistedComponent(Object target)
        {
            /**
             * 1. Save metadata
             * */
            _target     = target;
            _type       = _target.GetType();
            _instanceId = _target.GetInstanceID();

            var adapter = _target as ComponentAdapter;

            if (null != adapter)
            {
                ComponentRegistry.Instance.Register(_instanceId, adapter);
            }

#if DEBUG
            if (DebugMode)
            {
                _sb = new StringBuilder();
                foreach (MemberInfo memberInfo in _type.GetMembers())
                {
                    var attributes = CoreReflector.GetMemberAttributes <SaveableAttribute>(memberInfo);
                    //foreach (Attribute attr in memberInfo.GetCustomAttributes(typeof(SaveableAttribute), true))
                    foreach (var attribute in attributes)
                    {
                        if (attribute.IsSaveable)
                        {
                            _sb.AppendLine(string.Format("    - {0}; {1}", memberInfo.Name, memberInfo));
                        }
                    }
                }

                Debug.Log(string.Format(@"[{0}] SaveableMembers: {1}
{2}", _target, SaveableMembers.Count(_type), _sb), _target);
            }
#endif

            /**
             * 2. Adding the new member info to SaveableMembers dictionary
             * This is done for each saveable type in the component
             * Each member that must be persisted should be decorated with [Saveable]
             * */
            foreach (MemberInfo memberInfo in _type.GetMembers())
            {
                var attributes = CoreReflector.GetMemberAttributes <SaveableAttribute>(memberInfo);
                foreach (SaveableAttribute attribute in attributes)
                {
                    if (attribute.IsSaveable)
                    {
                        // doesn't add anything if it already exists
                        SaveableMembers.Put(_type, memberInfo);
                    }
                }
            }

            /**
             * 3. Take a snapshot of the original values
             * */
            TakeSnapshot(_originalValues);
        }