public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            var layoutDropdown = new Rect(r.x + 5, r.y + 5, r.width - 10, 20);
            var layoutImage    = new Rect(r.x, layoutDropdown.yMax + 5, r.width,
                                          r.yMax - layoutDropdown.yMax - 5);
            var currentChannel = _currentPreviewChannels[target];

            currentChannel = SirenixEditorFields.Dropdown(layoutDropdown,
                                                          currentChannel, target.GetChannelIdentifiers());
            if (target.GetImage(currentChannel)?.Texture != null)
            {
                var image = target.GetImage(currentChannel);
                var s     = new GUIStyle {
                    normal    = { textColor = Color.white },
                    alignment = TextAnchor.MiddleCenter,
                    fontSize  = r.width > 200 ? 12 : 10
                };
                GUI.DrawTexture(layoutImage.SubYMax(s.fontSize * 1.5f), image.Texture,
                                ScaleMode.ScaleToFit, true);
                GUI.Box(layoutImage.AlignBottom(s.fontSize * 1.5f),
                        $"{image.Width} x {image.Height} {image.Format} Image", s);
            }
            else
            {
                var s = new GUIStyle {
                    normal    = { textColor = Color.gray },
                    alignment = TextAnchor.MiddleCenter
                };
                GUI.Box(layoutImage, "Empty Image", s);
            }
            _currentPreviewChannels[target] = currentChannel;
        }
    protected override void DrawPropertyLayout(GUIContent label)
    {
        SirenixEditorGUI.BeginBox();
        {
            SirenixEditorGUI.BeginToolbarBoxHeader();
            {
                var rect = GUILayoutUtility.GetRect(0, 19);
                var unityObjectFieldRect = rect.Padding(2).AlignLeft(rect.width / 2);
                var methodSelectorRect   = rect.Padding(2).AlignRight(rect.width / 2 - 5);
                var dInfo    = this.GetDelegateInfo();
                var intlabel = new GUIContent("some type");

                EditorGUI.BeginChangeCheck();
                var someType = SirenixEditorFields.Dropdown(intlabel, tempType, SupportedReturnTypes);
                /*public static bool Dropdown<T>(Rect rect, IList<int> selected, IList<T> items, bool multiSelection)*/
                var newTarget = SirenixEditorFields.UnityObjectField(unityObjectFieldRect, dInfo.Target, typeof(UnityEngine.Object), true);
                if (EditorGUI.EndChangeCheck())
                {
                    this.tmpTarget = newTarget;
                    this.tempType  = someType;
                }

                EditorGUI.BeginChangeCheck();
                var selectorText = (dInfo.Method == null || this.tmpTarget) ? "Select a method" : dInfo.Method.Name;
                var newMethod    = MethodSelector.DrawSelectorDropdown(methodSelectorRect, selectorText, this.CreateSelector);
                if (EditorGUI.EndChangeCheck())
                {
                    this.CreateAndAssignNewDelegate(newMethod.FirstOrDefault());
                    this.tmpTarget = null;
                }
            }
            SirenixEditorGUI.EndToolbarBoxHeader();

            // Draws the rest of the ICustomEvent, and since we've drawn the label, we simply pass along null.
            for (int i = 0; i < this.Property.Children.Count; i++)
            {
                var child = this.Property.Children[i];
                if (child.Name == "Result")
                {
                    continue;
                }
                child.Draw();
            }
        }
        SirenixEditorGUI.EndBox();
    }
        protected override void DrawPropertyLayout(GUIContent label)
        {
            Rect rect = EditorGUILayout.GetControlRect();

            string[] possible = (from type in TypeCache.GetTypesWithAttribute <TelegraphAttribute>()
                                 from propertyTuple in type.GetPropertiesWithAttribute <TelegramAttribute>()
                                 select string.IsNullOrEmpty(propertyTuple.Item1.NameOverride)
                    ? propertyTuple.Item2.Name
                    : propertyTuple.Item1.NameOverride).ToArray();

            string current  = ValueEntry.SmartValue;
            int    selected = SirenixEditorFields.Dropdown(rect, label, Array.IndexOf(possible, current), possible);

            if (selected > -1)
            {
                ValueEntry.SmartValue = possible[selected];
            }
        }
Exemple #4
0
        private void DoProperty <TBase>(IPropertyValueEntry weakEntry, ValueDropdownAttribute attribute, GUIContent label)
        {
            var entry  = (IPropertyValueEntry <TBase>)weakEntry;
            var config = entry.Property.Context.Get(this, "Config", (PropertyConfig <TBase>)null);

            if (config.Value == null)
            {
                config.Value = new PropertyConfig <TBase>();

                Type parentType;

                if (entry.ValueCategory == PropertyValueCategory.Member)
                {
                    parentType = entry.ParentType;
                    config.Value.GetParentValuesFromProperty = entry.Property;
                }
                else
                {
                    parentType = entry.Property.Parent.ParentType;
                    config.Value.GetParentValuesFromProperty = entry.Property.Parent;
                }

                MemberInfo memberInfo = parentType.FindMember()
                                        .HasReturnType <IList <TBase> >(true)
                                        .HasNoParameters()
                                        .IsNamed(attribute.MemberName)
                                        .GetMember <MemberInfo>(out config.Value.ErrorMessage);

                if (config.Value.ErrorMessage == null)
                {
                    string memberName = attribute.MemberName + ((memberInfo is MethodInfo) ? "()" : "");
                    if (memberInfo.IsStatic())
                    {
                        config.Value.StaticValueDropdownGetter = DeepReflection.CreateValueGetter <IList <TBase> >(parentType, memberName);
                    }
                    else
                    {
                        config.Value.InstanceValueDropdownGetter = DeepReflection.CreateWeakInstanceValueGetter <IList <TBase> >(parentType, memberName);
                    }
                    config.Value.IsValueDropdown = false;
                }
                else
                {
                    string errorMessage;

                    memberInfo = parentType.FindMember()
                                 .HasReturnType <IList <ValueDropdownItem <TBase> > >(true)
                                 .HasNoParameters()
                                 .IsNamed(attribute.MemberName)
                                 .GetMember <MemberInfo>(out errorMessage);

                    if (errorMessage == null)
                    {
                        string memberName = attribute.MemberName + ((memberInfo is MethodInfo) ? "()" : "");
                        if (memberInfo.IsStatic())
                        {
                            config.Value.ValueDropdownStaticValueDropdownGetter = DeepReflection.CreateValueGetter <IList <ValueDropdownItem <TBase> > >(parentType, memberName);
                        }
                        else
                        {
                            config.Value.ValueDropdownInstanceValueDropdownGetter = DeepReflection.CreateWeakInstanceValueGetter <IList <ValueDropdownItem <TBase> > >(parentType, memberName);
                        }
                        config.Value.ErrorMessage    = null;
                        config.Value.IsValueDropdown = true;
                    }
                    else
                    {
                        if (config.Value.ErrorMessage != errorMessage)
                        {
                            config.Value.ErrorMessage += " or\n" + errorMessage;
                        }

                        if (IsStrongList)
                        {
                            memberInfo = parentType.FindMember()
                                         .HasReturnType(typeof(IList <>).MakeGenericType(StrongListElementType), true)
                                         .HasNoParameters()
                                         .IsNamed(attribute.MemberName)
                                         .GetMember <MemberInfo>(out errorMessage);

                            if (errorMessage != null)
                            {
                                config.Value.ErrorMessage += " or\n" + errorMessage;

                                Type valueDropdown = typeof(ValueDropdownItem <>).MakeGenericType(StrongListElementType);

                                memberInfo = parentType.FindMember()
                                             .HasReturnType(typeof(IList <>).MakeGenericType(valueDropdown), true)
                                             .HasNoParameters()
                                             .IsNamed(attribute.MemberName)
                                             .GetMember <MemberInfo>(out errorMessage);

                                if (errorMessage != null)
                                {
                                    config.Value.ErrorMessage += " or\n" + errorMessage;
                                }
                                else
                                {
                                    config.Value.ErrorMessage   = null;
                                    config.Value.CallNextDrawer = true;
                                }
                            }
                            else
                            {
                                config.Value.ErrorMessage   = null;
                                config.Value.CallNextDrawer = true;
                            }
                        }
                    }
                }
            }

            if (config.Value.ErrorMessage != null)
            {
                if (entry.ValueCategory == PropertyValueCategory.Member)
                {
                    AllEditorGUI.ErrorMessageBox(config.Value.ErrorMessage);
                }

                this.CallNextDrawer(entry, label);
            }
            else if (config.Value.CallNextDrawer)
            {
                this.CallNextDrawer(entry, label);
            }
            else
            {
                if (config.Value.IsValueDropdown)
                {
                    IList <ValueDropdownItem <TBase> > selectList = config.Value.ValueDropdownStaticValueDropdownGetter != null?
                                                                    config.Value.ValueDropdownStaticValueDropdownGetter() :
                                                                        config.Value.ValueDropdownInstanceValueDropdownGetter(config.Value.GetParentValuesFromProperty.ParentValues[0]);

                    selectedValuesBuffer.Clear();

                    if (selectList != null && selectList.Count > 0)
                    {
                        for (int i = 0; i < entry.Values.Count; i++)
                        {
                            var val = entry.Values[i];
                            for (int j = 0; j < selectList.Count; j++)
                            {
                                if (EqualityComparer <TBase> .Default.Equals((TBase)val, selectList[j].Value))
                                {
                                    selectedValuesBuffer.Add(j);
                                }
                            }
                        }

                        //if (Event.current.type == EventType.Repaint && selectList.Count > 0 && selectedValuesBuffer.Count == 0 && entry.Values.Count == 1)
                        //{
                        //    entry.SmartValue = selectList[0].Value;
                        //}
                    }

                    if (SirenixEditorFields.Dropdown <ValueDropdownItem <TBase> >(label, selectedValuesBuffer, selectList, false))
                    {
                        if (selectedValuesBuffer.Count > 0)
                        {
                            entry.SmartValue = selectList[selectedValuesBuffer[0]].Value;
                        }
                    }

                    //IList<ValueDropdownItem<TBase>> selectList = config.Value.ValueDropdownStaticValueDropdownGetter != null ?
                    //    config.Value.ValueDropdownStaticValueDropdownGetter() :
                    //    config.Value.ValueDropdownInstanceValueDropdownGetter(config.Value.GetParentValuesFromProperty.ParentValues[0]);

                    //ValueDropdownItem<TBase> selected = new ValueDropdownItem<TBase>(null, entry.SmartValue);
                    //entry.SmartValue = SirenixEditorFields.Dropdown(label, selected, selectList).Value;
                }
                else
                {
                    IList <TBase> selectList = config.Value.StaticValueDropdownGetter != null?
                                               config.Value.StaticValueDropdownGetter() :
                                                   config.Value.InstanceValueDropdownGetter(config.Value.GetParentValuesFromProperty.ParentValues[0]);

                    entry.SmartValue = SirenixEditorFields.Dropdown(label, entry.SmartValue, selectList);
                }
            }
        }
        private void DoProperty <TBase>(IPropertyValueEntry weakEntry, ValueDropdownAttribute attribute, GUIContent label)
        {
            var entry  = (IPropertyValueEntry <TBase>)weakEntry;
            var config = entry.Property.Context.Get(this, "Config", (PropertyConfig <TBase>)null);

            if (config.Value == null)
            {
                config.Value = new PropertyConfig <TBase>();

                Type parentType;

                if (entry.ValueCategory == PropertyValueCategory.Member)
                {
                    parentType = entry.ParentType;
                    config.Value.GetParentValuesFromProperty = entry.Property;
                }
                else
                {
                    parentType = entry.Property.Parent.ParentType;
                    config.Value.GetParentValuesFromProperty = entry.Property.Parent;
                }

                MemberInfo memberInfo = parentType.FindMember()
                                        .HasReturnType <IList <TBase> >(true)
                                        .HasNoParameters()
                                        .IsNamed(attribute.MemberName)
                                        .GetMember <MemberInfo>(out config.Value.ErrorMessage);

                if (config.Value.ErrorMessage == null)
                {
                    string memberName = attribute.MemberName + ((memberInfo is MethodInfo) ? "()" : "");
                    if (memberInfo.IsStatic())
                    {
                        config.Value.StaticValueDropdownGetter = DeepReflection.CreateValueGetter <IList <TBase> >(parentType, memberName);
                    }
                    else
                    {
                        config.Value.InstanceValueDropdownGetter = DeepReflection.CreateWeakInstanceValueGetter <IList <TBase> >(parentType, memberName);
                    }
                    config.Value.IsValueDropdown = false;
                }
                else
                {
                    string errorMessage;

                    memberInfo = parentType.FindMember()
                                 .HasReturnType <IList <ValueDropdownItem <TBase> > >(true)
                                 .HasNoParameters()
                                 .IsNamed(attribute.MemberName)
                                 .GetMember <MemberInfo>(out errorMessage);

                    if (errorMessage == null)
                    {
                        string memberName = attribute.MemberName + ((memberInfo is MethodInfo) ? "()" : "");
                        if (memberInfo.IsStatic())
                        {
                            config.Value.ValueDropdownStaticValueDropdownGetter = DeepReflection.CreateValueGetter <IList <ValueDropdownItem <TBase> > >(parentType, memberName);
                        }
                        else
                        {
                            config.Value.ValueDropdownInstanceValueDropdownGetter = DeepReflection.CreateWeakInstanceValueGetter <IList <ValueDropdownItem <TBase> > >(parentType, memberName);
                        }
                        config.Value.ErrorMessage    = null;
                        config.Value.IsValueDropdown = true;
                    }
                    else
                    {
                        if (config.Value.ErrorMessage != errorMessage)
                        {
                            config.Value.ErrorMessage += " or\n" + errorMessage;
                        }

                        if (IsStrongList)
                        {
                            memberInfo = parentType.FindMember()
                                         .HasReturnType(typeof(IList <>).MakeGenericType(StrongListElementType), true)
                                         .HasNoParameters()
                                         .IsNamed(attribute.MemberName)
                                         .GetMember <MemberInfo>(out errorMessage);

                            if (errorMessage != null)
                            {
                                config.Value.ErrorMessage += " or\n" + errorMessage;

                                Type valueDropdown = typeof(ValueDropdownItem <>).MakeGenericType(StrongListElementType);

                                memberInfo = parentType.FindMember()
                                             .HasReturnType(typeof(IList <>).MakeGenericType(valueDropdown), true)
                                             .HasNoParameters()
                                             .IsNamed(attribute.MemberName)
                                             .GetMember <MemberInfo>(out errorMessage);

                                if (errorMessage != null)
                                {
                                    config.Value.ErrorMessage += " or\n" + errorMessage;
                                }
                                else
                                {
                                    config.Value.ErrorMessage   = null;
                                    config.Value.CallNextDrawer = true;
                                }
                            }
                            else
                            {
                                config.Value.ErrorMessage   = null;
                                config.Value.CallNextDrawer = true;
                            }
                        }
                    }
                }
            }

            if (config.Value.ErrorMessage != null)
            {
                if (entry.ValueCategory == PropertyValueCategory.Member)
                {
                    SirenixEditorGUI.ErrorMessageBox(config.Value.ErrorMessage);
                }

                this.CallNextDrawer(entry, label);
            }
            else if (config.Value.CallNextDrawer)
            {
                this.CallNextDrawer(entry, label);
            }
            else
            {
                if (config.Value.IsValueDropdown)
                {
                    IList <ValueDropdownItem <TBase> > selectList = config.Value.ValueDropdownStaticValueDropdownGetter != null?
                                                                    config.Value.ValueDropdownStaticValueDropdownGetter() :
                                                                        config.Value.ValueDropdownInstanceValueDropdownGetter(config.Value.GetParentValuesFromProperty.ParentValues[0]);

                    selectedValuesBuffer.Clear();

                    if (selectList != null && selectList.Count > 0)
                    {
                        for (int i = 0; i < entry.Values.Count; i++)
                        {
                            var val = entry.Values[i];
                            for (int j = 0; j < selectList.Count; j++)
                            {
                                if (EqualityComparer <TBase> .Default.Equals((TBase)val, selectList[j].Value))
                                {
                                    selectedValuesBuffer.Add(j);
                                }
                            }
                        }
                    }

                    if (SirenixEditorFields.Dropdown <ValueDropdownItem <TBase> >(label, selectedValuesBuffer, selectList, false))
                    {
                        if (selectedValuesBuffer.Count > 0)
                        {
                            entry.SmartValue = selectList[selectedValuesBuffer[0]].Value;
                        }
                    }
                }
                else
                {
                    IList <TBase> selectList = config.Value.StaticValueDropdownGetter != null?
                                               config.Value.StaticValueDropdownGetter() :
                                                   config.Value.InstanceValueDropdownGetter(config.Value.GetParentValuesFromProperty.ParentValues[0]);

                    if (GeneralDrawerConfig.Instance.UseImprovedEnumDropDown)
                    {
                        int  id;
                        bool hasFocus;
                        Rect rect;
                        Action <OdinSelector <TBase> > bindSelector;
                        Func <IEnumerable <TBase> >    getResult;

                        TempFeatureRichControlRect(label, out id, out hasFocus, out rect);

                        if (GenericSelector <TBase> .DrawSelectorButton(rect, entry.SmartValue + "", EditorStyles.popup, id, out bindSelector, out getResult))
                        {
                            var selector = new GenericSelector <TBase>(selectList);
                            selector.SetSelection(entry.SmartValue);
                            selector.ShowInPopup(new Vector2(rect.xMin, rect.yMax));
                            bindSelector(selector);
                        }

                        if (getResult != null)
                        {
                            entry.SmartValue = getResult().FirstOrDefault();
                        }
                    }
                    else
                    {
                        entry.SmartValue = SirenixEditorFields.Dropdown(label, entry.SmartValue, selectList);
                    }
                }
            }
        }