Ejemplo n.º 1
0
    private void OnEnable()
    {
        _partConsole    = (PartConsole)target;
        _lastPartTarget = _partTarget;

        UpdatePropertyList();
    }
Ejemplo n.º 2
0
    /*/EditorGUILayout.PropertyField(_property);
     *
     * /SerializedProperty iterator = serializedObject.GetIterator();
     *
     * bool next = iterator.NextVisible(true);
     * while(next)
     * {
     *  if (iterator.propertyType == SerializedPropertyType.Float)
     *  {
     *      EditorGUILayout.PropertyField(iterator);
     *  }
     *  next = iterator.NextVisible(true);
     * }
     */
    #endregion


    public void UpdatePropertyList()
    {
        Debug.Log("Updating property list");
        _lastPartTarget = _partTarget;

        //Where we hold the values before checking them against the actual console's values
        disppropinfolist = new List <PropertyInfo>(); //For properties that are readable
        contpropinfolist = new List <PropertyInfo>(); //For properties that are writable

        Type parttype = _partConsole.PartType;

        if (parttype != null)
        {
            //You can't call reflection on null, so update with the blank lists

            PropertyInfo[] properties = parttype.GetProperties();
            foreach (PropertyInfo info in properties)
            {
                if (info.DeclaringType == typeof(SomePart) || info.DeclaringType.IsSubclassOf(typeof(SomePart))) //SomePart would be the base part class in Hyperfusion
                {
                    //Assign its getter to display properties, if there is one
                    if (info.CanRead)
                    {
                        disppropinfolist.Add(info);
                    }

                    //Assign its setter to control properties, if there is one
                    if (info.CanWrite)
                    {
                        contpropinfolist.Add(info);
                    }
                }
            }
        }

        //Preserve the old settings so they don't get wiped out if they're common to a new part.
        //Doing this separately for readables and writables. Maybe there's a better way but I haven't thought of it yet.
        List <DisplayPropBinding> newdispbindings = new List <DisplayPropBinding>();

        foreach (PropertyInfo info in disppropinfolist)
        {
            DisplayPropBinding binding = new DisplayPropBinding
            {
                PropertyName       = info.Name,
                PropertyTypeString = info.PropertyType.ToString()
            };

            newdispbindings.Add(binding);
        }

        //Now combine the old and new lists, so we can carry over old but valid properties.
        //_partConsole.DisplayBindings = newbindings;
        List <DisplayPropBinding> finaldispbindings  = new List <DisplayPropBinding>();
        List <DisplayPropBinding> badnewdispbindings = new List <DisplayPropBinding>();

        foreach (DisplayPropBinding oldbinding in _partConsole.DisplayBindings)
        {
            foreach (DisplayPropBinding newbinding in newdispbindings)
            {
                if (newbinding.PropertyName == oldbinding.PropertyName)
                {
                    //Take the old one, not the new one
                    finaldispbindings.Add(oldbinding);
                    //Add to the bad list to avoid adding the new version
                    badnewdispbindings.Add(newbinding);
                }
            }
        }

        //Add the new ones that didn't have old counterparts added
        foreach (DisplayPropBinding newbinding in newdispbindings)
        {
            if (!badnewdispbindings.Contains(newbinding))
            {
                finaldispbindings.Add(newbinding);
            }
        }

        _partConsole.DisplayBindings = finaldispbindings;

        //Now writable properties.
        List <ControlPropBinding> newcontbindings = new List <ControlPropBinding>();

        foreach (PropertyInfo info in contpropinfolist)
        {
            ControlPropBinding binding = new ControlPropBinding
            {
                PropertyName       = info.Name,
                PropertyTypeString = info.PropertyType.ToString()
            };

            newcontbindings.Add(binding);
        }

        //Now combine the old and new lists, so we can carry over old but valid properties.
        //_partConsole.DisplayBindings = newbindings;
        List <ControlPropBinding> finalcontbindings  = new List <ControlPropBinding>();
        List <ControlPropBinding> badnewcontbindings = new List <ControlPropBinding>();

        foreach (ControlPropBinding oldbinding in _partConsole.ControlBindings)
        {
            foreach (ControlPropBinding newbinding in newcontbindings)
            {
                if (newbinding.PropertyName == oldbinding.PropertyName)
                {
                    //Take the old one, not the new one
                    finalcontbindings.Add(oldbinding);
                    //Add to the bad list to avoid adding the new version
                    badnewcontbindings.Add(newbinding);
                }
            }
        }

        //Add the new ones that didn't have old counterparts added
        foreach (ControlPropBinding newbinding in newcontbindings)
        {
            if (!badnewcontbindings.Contains(newbinding))
            {
                finalcontbindings.Add(newbinding);
            }
        }

        _partConsole.ControlBindings = finalcontbindings;


        serializedObject.ApplyModifiedProperties();
    }