Beispiel #1
0
        public static bool GetInfoFromMember <T>(this BaseMightyMember mightyMember, object target, string memberName,
                                                 out MightyInfo <T> info, GetValuePredicate <T> predicate = null, bool neverInWrapper = false)
        {
            if (string.IsNullOrWhiteSpace(memberName))
            {
                info = null;
                return(false);
            }

            if (predicate != null && predicate(memberName, out var outValue))
            {
                info = new MightyInfo <T>(null, null, outValue);
                return(true);
            }

            if (target is BaseWrapperAttribute wrapper)
            {
                if (neverInWrapper)
                {
                    return(mightyMember.GetInfoFromMember(mightyMember.GetWrapperTarget(wrapper), memberName, out info, predicate));
                }
                if (GetCallbackName(target, memberName, out var callbackName))
                {
                    return(mightyMember.GetInfoFromMember(mightyMember.GetWrapperTarget(wrapper), callbackName, out info, predicate));
                }
            }

            if (InternalGetInfoFromMember(target, memberName, true, out info))
            {
                return(true);
            }

            if (!(mightyMember is MightySerializedField serializedField))
            {
                return(false);
            }

            target = serializedField.Property.GetPropertyTargetReference();
            if (target.GetType().GetCustomAttributes(typeof(SerializableAttribute), true).Length > 0 &&
                InternalGetInfoFromMember(target, memberName, true, out info))
            {
                return(true);
            }


            if (!TypeUtilities.TryConvertStringToType(memberName, out T value))
            {
                return(false);
            }

            info = new MightyInfo <T>(target, null, value);
            return(true);
        }
 protected override void DrawLabel(BaseMightyMember mightyMember, LargeLabelAttribute attribute, string prefix, string label)
 {
     if (string.IsNullOrEmpty(prefix))
     {
         EditorGUILayout.LabelField(label, EditorStyles.largeLabel, GUILayout.Height(20),
                                    GUILayout.Width(MightyGUIUtilities.TextWidth(label) + MightyGUIUtilities.TAB_SIZE));
     }
     else
     {
         EditorGUILayout.LabelField(prefix, label, EditorStyles.largeLabel, GUILayout.Height(20));
     }
 }
Beispiel #3
0
        public void RefreshDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute)
        {
            var gameObject = ((MightyComponent)mightyMember).ComponentContext.GameObject;

            if (!m_priorityCache.ContainsKey(gameObject))
            {
                EnableDrawer(mightyMember, mightyAttribute);
                return;
            }

            m_priorityCache[gameObject].RefreshValue();
        }
        public override void InitDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute)
        {
            var attribute = (InfoBoxAttribute)mightyAttribute;
            var target    = mightyMember.InitAttributeTarget <InfoBoxAttribute>();

            if (!mightyMember.Property.GetBoolInfo(target, attribute.VisibleIf, out var visibleInfo))
            {
                visibleInfo = new MightyInfo <bool>(true);
            }

            m_infoBoxCache[mightyMember] = visibleInfo;
        }
Beispiel #5
0
        public static T[] GetWrappedAttributes <T>(this BaseMightyMember mightyMember) where T : BaseMightyAttribute
        {
            var wrappedAttributes = new List <T>();

            var wrappers = GetAttributes <BaseWrapperAttribute>(mightyMember.GetMemberInfo());

            if (wrappers.Length > 0)
            {
                GetWrappedAttributes(wrappers, wrappedAttributes, mightyMember, mightyMember.Context.Target);
            }

            return(wrappedAttributes.ToArray());
        }
Beispiel #6
0
        public override void InitDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute)
        {
            var attribute = (LayoutSpaceAttribute)mightyAttribute;
            var target    = mightyMember.InitAttributeTarget <LayoutSpaceAttribute>();
            var property  = mightyMember.Property;

            if (!property.GetInfoFromMember <DecoratorPosition>(target, attribute.PositionName, out var positionInfo, Enum.TryParse))
            {
                positionInfo = new MightyInfo <DecoratorPosition>(attribute.Position);
            }

            m_flexibleSpaceCache[mightyMember] = positionInfo;
        }
        public void RefreshDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute)
        {
            if (!m_buttonCache.Contains(mightyMember))
            {
                InitDrawer(mightyMember, mightyAttribute);
                return;
            }

            if (((ButtonAttribute)mightyAttribute).ExecuteInPlayMode || !EditorApplication.isPlaying)
            {
                m_buttonCache[mightyMember].RefreshValue();
            }
        }
        public override void EndDraw(BaseMightyMember mightyMember, BaseDecoratorAttribute attribute)
        {
            if (((ScriptFieldAttribute)attribute).Position != FieldPosition.After)
            {
                return;
            }

            var enabled = GUI.enabled;

            GUI.enabled = false;
            EditorGUILayout.PropertyField(mightyMember.Property.serializedObject.FindProperty("m_Script"));
            GUI.enabled = enabled;
        }
Beispiel #9
0
        public static GUIStyle GetStyle(this BaseMightyMember mightyMember, object target, string styleName, out bool exception)
        {
            var ex = false;

            if (string.IsNullOrEmpty(styleName) || !mightyMember.GetValueFromMember(target, styleName, out var guiStyle,
                                                                                    (string name, out GUIStyle value) => GetStyle(name, out value, out ex)))
            {
                exception = ex;
                return(null);
            }

            exception = ex;
            return(guiStyle);
        }
        public float GetElementHeight(BaseMightyMember mightyMember, int index, BaseDrawerAttribute baseAttribute)
        {
            if (string.IsNullOrWhiteSpace(((CustomDrawerAttribute)baseAttribute).ElementHeightCallback))
            {
                return(0);
            }

            if (!GetDrawerForMember(mightyMember, m_elementCallback, out var callback, baseAttribute))
            {
                return(0);
            }

            return((float)callback.Invoke());
        }
        public override void BeginDrawElement(BaseMightyMember mightyMember, int index, BaseElementDecoratorAttribute attribute)
        {
            var position = PositionByMember(mightyMember, attribute);

            if (position.Contains(DecoratorPosition.BeforeElements))
            {
                DrawSpace();
            }

            if (index != 0 && position.Contains(DecoratorPosition.BetweenElements))
            {
                DrawSpace();
            }
        }
        public override void InitDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute)
        {
            var buttonAttribute = (ButtonAttribute)mightyAttribute;

            if (mightyMember.Target.GetBoolInfo(buttonAttribute.EnabledCallback, out var enabledInfo))
            {
                enabledInfo.Value = enabledInfo.Value;
            }
            else
            {
                enabledInfo = new MightyInfo <bool>(GUI.enabled && buttonAttribute.ExecuteInPlayMode || !EditorApplication.isPlaying);
            }

            m_buttonCache[mightyMember] = enabledInfo;
        }
Beispiel #13
0
        public static void CacheAnywhereDecoratorsForMember(this BaseMightyMember mightyMember, MemberInfo memberInfo,
                                                            IEnumerable <BaseMightyAttribute> wrappedAttributes)
        {
            var decoratorAttributes = new List <BaseGlobalDecoratorAttribute>();

            var any = PopulateAttributesListWithException(decoratorAttributes,
                                                          memberInfo.GetCustomAttributes <BaseGlobalDecoratorAttribute>(true), typeof(IDrawAnywhereAttribute), true);

            any = PopulateAttributesListWithException(decoratorAttributes, wrappedAttributes, typeof(IDrawAnywhereAttribute), true) || any;

            if (any)
            {
                mightyMember.SetAttributes(decoratorAttributes.ToArray());
            }
        }
Beispiel #14
0
        public static MightyInfo <GUIStyle> GetStyleInfo(this BaseMightyMember mightyMember, object target, string styleName,
                                                         out bool exception,
                                                         bool editorStyle)
        {
            var ex = false;

            if (string.IsNullOrEmpty(styleName) || !mightyMember.GetInfoFromMember(target, styleName, out var styleInfo,
                                                                                   (string name, out GUIStyle value) => GetStyle(name, out value, out ex, editorStyle)))
            {
                exception = ex;
                return(null);
            }

            exception = ex;
            return(styleInfo);
        }
Beispiel #15
0
        public override Rect BeginDrawElement(Rect rect, BaseMightyMember mightyMember, int index, BaseElementDecoratorAttribute attribute)
        {
            var position = PositionByMember(mightyMember, attribute);
            var size     = ((LayoutSpaceAttribute)attribute).Size;

            if (position.Contains(DecoratorPosition.BeforeElements))
            {
                rect = DrawSpace(rect, size);
            }

            if (index != 0 && position.Contains(DecoratorPosition.BetweenElements))
            {
                rect = DrawSpace(rect, size);
            }

            return(rect);
        }
        public override void InitDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute)
        {
            var attribute = (CustomDrawerAttribute)mightyAttribute;
            var target    = mightyMember.InitAttributeTarget <CustomDrawerAttribute>();

            if (!mightyMember.Property.isArray || mightyMember.Property.propertyType == SerializedPropertyType.String)
            {
                InitCallback(mightyMember, target, attribute, m_propertyCallback);
            }
            else
            {
                InitCallback(mightyMember, target, attribute, m_elementCallback);
                InitCallback(mightyMember, target, attribute, m_labelledElementCallback);
                InitCallback(mightyMember, target, attribute, m_rectElementCallback);
                InitCallback(mightyMember, target, attribute, m_elementHeightCallback);
            }
        }
        public bool CanDraw(BaseMightyMember mightyMember, BaseConditionalGroupAttribute baseAttribute)
        {
            _previousBackgroundColor = GUI.backgroundColor;
            _previousContentColor    = GUI.contentColor;

            if (!m_colorsCache.Contains(mightyMember))
            {
                InitDrawer(mightyMember, baseAttribute);
            }
            var(background, content) = m_colorsCache[mightyMember];

            GUI.backgroundColor = background.Value ?? _previousBackgroundColor;
            GUI.contentColor    = content.Value ?? _previousContentColor;

            var canDraw = CanDrawImpl(mightyMember, baseAttribute.DrawName, IndentLevelForMember(mightyMember));

            return(canDraw);
        }
        public override void ApplyPropertyMeta(BaseMightyMember mightyMember, BaseMetaAttribute metaAttribute)
        {
            var infoBoxAttribute = (InfoBoxAttribute)metaAttribute;

            if (!m_infoBoxCache.Contains(mightyMember))
            {
                InitDrawer(mightyMember, metaAttribute);
            }
            var visibleInfo = m_infoBoxCache[mightyMember];

            if (visibleInfo.Value)
            {
                DrawInfoBox(infoBoxAttribute.Text, infoBoxAttribute.Type);
            }
            else
            {
                EditorDrawUtility.DrawHelpBox($"{typeof(InfoBoxAttribute).Name} needs a valid boolean condition to work");
            }
        }
Beispiel #19
0
        public override void BeginDraw(BaseMightyMember mightyMember, BaseElementDecoratorAttribute baseAttribute,
                                       Action <BaseMightyMember, SerializedProperty, BaseDrawerAttribute> propertyDrawCallback,
                                       BaseDrawerAttribute drawerAttribute = null)
        {
            var property = mightyMember.Property;

            if (!property.isArray)
            {
                propertyDrawCallback?.Invoke(mightyMember, property, drawerAttribute);
                return;
            }

            if (!EditorDrawUtility.DrawFoldout(property))
            {
                return;
            }

            EditorGUI.indentLevel++;
            EditorDrawUtility.DrawArraySizeField(property);
        }
Beispiel #20
0
        private void DrawMember(BaseMightyMember mightyMember, bool canDraw, ref bool valueChanged)
        {
            switch (mightyMember)
            {
            case MightySerializedField serializedField when canDraw:
                DrawSerializedField(serializedField);
                break;

            case MightyNonSerializedField nonSerializedField when canDraw:
                DrawNonSerializedField(nonSerializedField);
                break;

            case MightyNativeProperty nativeProperty when canDraw:
                DrawNativeProperty(nativeProperty);
                break;

            case MightyMethod method:
                DrawMethod(method, canDraw, ref valueChanged);
                break;
            }
        }
Beispiel #21
0
        public static object GetCallbackTarget(BaseMightyMember mightyMember, object target, string callbackName,
                                               CallbackSignature callbackSignature, out MemberInfo outMember)
        {
            outMember = null;
            if (string.IsNullOrWhiteSpace(callbackName))
            {
                return(null);
            }
            if (target.GetType().GetMemberInfo(callbackName, callbackSignature, out outMember))
            {
                return(target);
            }

            if (target is BaseWrapperAttribute wrapper && GetCallbackName(target, callbackName, out var callbackNameValue))
            {
                return(GetCallbackTarget(mightyMember, mightyMember.GetWrapperTarget(wrapper), callbackNameValue,
                                         callbackSignature, out outMember));
            }

            return(null);
        }
Beispiel #22
0
        public override void EndDraw(BaseMightyMember mightyMember, BaseDecoratorAttribute baseAttribute)
        {
            if (!m_styleCache.Contains(mightyMember))
            {
                InitDrawer(mightyMember, baseAttribute);
            }
            var style = m_styleCache[mightyMember].Value;

            if (style == null)
            {
                return;
            }

            var attribute = (StyleAttribute)baseAttribute;

            if (attribute.Indent)
            {
                EditorGUI.indentLevel--;
            }
            GUILayout.EndVertical();
        }
        public void BeginGroup(BaseMightyMember mightyMember, BaseGroupAttribute baseAttribute)
        {
            _previousBackgroundColor = GUI.backgroundColor;
            _previousContentColor    = GUI.contentColor;

            if (!m_colorsCache.Contains(mightyMember))
            {
                InitDrawer(mightyMember, baseAttribute);
            }
            var(background, content) = m_colorsCache[mightyMember];

            GUI.backgroundColor = background.Value ?? _previousBackgroundColor;
            GUI.contentColor    = content.Value ?? _previousContentColor;

            BeginDrawGroup(mightyMember.GroupName, baseAttribute.DrawName, IndentLevelForMember(mightyMember));

            if (baseAttribute.DrawLine)
            {
                DrawLine(baseAttribute.LineColor);
            }
        }
Beispiel #24
0
        public override float GetElementHeight(BaseMightyMember mightyMember, int index, BaseElementDecoratorAttribute attribute)
        {
            var element = mightyMember.GetElement(index);

            if (element.propertyType != SerializedPropertyType.ObjectReference || element.objectReferenceValue == null)
            {
                return(50);
            }

            var previewTexture = AssetPreview.GetAssetPreview(element.objectReferenceValue);

            if (previewTexture == null)
            {
                return(50);
            }

            var showAssetAttribute = (ShowAssetPreviewAttribute)attribute;

            var textureRatio = (float)previewTexture.width / previewTexture.height;

            return((int)Mathf.Clamp(showAssetAttribute.Size / textureRatio, 0, previewTexture.height) + 15);
        }
Beispiel #25
0
        protected override void Enable(BaseMightyMember mightyMember, T attribute)
        {
            if (!(mightyMember is MightyComponent mightyComponent))
            {
                return;
            }

            var gameObject = mightyComponent.ComponentContext.GameObject;

            if (!mightyMember.GetInfoFromMember <int>(attribute.Target, attribute.PriorityCallback, out var priorityInfo))
            {
                priorityInfo = new MightyInfo <int>(attribute.Priority);
            }

            if (m_priorityCache.TryGetValue(gameObject, out var priority) && priority.Value >= priorityInfo.Value)
            {
                return;
            }

            m_priorityCache[gameObject] = priorityInfo;
            EnableDrawerImpl(mightyComponent, attribute);
        }
Beispiel #26
0
        public static bool GetValueFromMember <T>(this BaseMightyMember mightyMember, object target, string memberName, out T value,
                                                  GetValuePredicate <T> predicate = null)
        {
            if (string.IsNullOrWhiteSpace(memberName))
            {
                value = default;
                return(false);
            }

            if (predicate != null && predicate(memberName, out value))
            {
                return(true);
            }

            if (target is BaseWrapperAttribute wrapper && GetCallbackName(target, memberName, out var callbackName))
            {
                return(mightyMember.GetValueFromMember(mightyMember.GetWrapperTarget(wrapper), callbackName, out value, predicate));
            }

            if (InternalGetValueFromMember(target, memberName, true, out value))
            {
                return(true);
            }

            if (!(mightyMember is MightySerializedField serializedField))
            {
                return(false);
            }

            target = serializedField.Property.GetPropertyTargetReference();
            if (target.GetType().GetCustomAttributes(typeof(SerializableAttribute), true).Length > 0 &&
                InternalGetValueFromMember(target, memberName, true, out value))
            {
                return(true);
            }

            return(TypeUtilities.TryConvertStringToType(memberName, out value));
        }
Beispiel #27
0
        public override void BeginDraw(BaseMightyMember mightyMember, BaseElementDecoratorAttribute baseAttribute,
                                       Action <BaseMightyMember, SerializedProperty, BaseDrawerAttribute> propertyDrawCallback,
                                       BaseDrawerAttribute drawerAttribute = null)
        {
            var property = mightyMember.Property;

            if (!property.isArray || property.propertyType == SerializedPropertyType.String)
            {
                if (baseAttribute is BeginHorizontalAttribute || baseAttribute is HorizontalAttribute)
                {
                    GUILayout.BeginHorizontal();
                }

                propertyDrawCallback?.Invoke(mightyMember, property, drawerAttribute);
                return;
            }

            if (!EditorDrawUtility.DrawFoldout(property))
            {
                EndDrawHeader(mightyMember, baseAttribute);
                EndDrawArray(mightyMember, baseAttribute);
                return;
            }

            EditorGUI.indentLevel++;
            EditorDrawUtility.DrawArraySizeField(property);

            EditorDrawUtility.DrawArrayBody(property, index =>
            {
                BeginDrawElement(mightyMember, index, baseAttribute);
                propertyDrawCallback?.Invoke(mightyMember, property.GetArrayElementAtIndex(index), drawerAttribute);
                EndDrawElement(mightyMember, index, baseAttribute);
            });

            EditorGUI.indentLevel--;
        }
 public void EndGroup(BaseMightyMember mightyMember)
 {
     EndDrawGroup(IndentLevelForMember(mightyMember));
     GUI.backgroundColor = _previousBackgroundColor;
     GUI.contentColor    = _previousContentColor;
 }
Beispiel #29
0
 public void BeginDrawAnywhere(BaseMightyMember mightyMember, IDrawAnywhereAttribute baseAttribute) =>
 BeginDraw(mightyMember, (LineAttribute)baseAttribute);
Beispiel #30
0
        protected override void Enable(BaseMightyMember mightyMember, LineAttribute attribute)
        {
            base.Enable(mightyMember, attribute);

            m_colorCache[mightyMember] = mightyMember.GetColorInfo(attribute.Target, attribute.ColorName, attribute.Color);
        }