diff --git a/Editor/EditorAddons/Utility/ValidationAttributeUtility.cs b/Editor/EditorAddons/Utility/ValidationAttributeUtility.cs index 8cc06a8..0923bd7 100644 --- a/Editor/EditorAddons/Utility/ValidationAttributeUtility.cs +++ b/Editor/EditorAddons/Utility/ValidationAttributeUtility.cs @@ -19,6 +19,8 @@ protected override BaseWrappersTypeCollection GenerateCollection() { typeof(SceneReferenceAttribute), typeof(SceneReferenceWrapper) }, { typeof(FindAttribute), typeof(RequireComponentWrapper) }, { typeof(DataValidationAttribute), typeof(DataValidationWrapper) }, + { typeof(MaxAttribute), typeof(MaxWrapper) }, + { typeof(ClampAttribute), typeof(ClampWrapper) }, }; } diff --git a/Editor/EditorAddons/Wrappers/ClampWrapper.cs b/Editor/EditorAddons/Wrappers/ClampWrapper.cs new file mode 100644 index 0000000..116b3c8 --- /dev/null +++ b/Editor/EditorAddons/Wrappers/ClampWrapper.cs @@ -0,0 +1,51 @@ +using System; +using Better.Commons.EditorAddons.Drawers.Caching; +using Better.Commons.EditorAddons.Extensions; +using Better.Commons.EditorAddons.Utility; +using Better.Validation.Runtime.Attributes; +using UnityEditor; +using UnityEngine; + +namespace Better.Validation.EditorAddons.Wrappers +{ + public class ClampWrapper : PropertyValidationWrapper + { + public override bool IsSupported() + { + return Property.propertyType is SerializedPropertyType.Integer or SerializedPropertyType.Float; + } + + public override CacheValue Validate() + { + var clampAttribute = (ClampAttribute)Attribute; + var minValue = clampAttribute.Min; + var maxValue = clampAttribute.Max; + var value = Property.propertyType switch + { + SerializedPropertyType.Float => Property.floatValue, + SerializedPropertyType.Integer => Property.intValue, + _ => throw new ArgumentOutOfRangeException() + }; + + if (value >= minValue && value <= maxValue) + { + return GetClearCache(); + } + + value = Mathf.Clamp(value, minValue, maxValue); + switch (Property.propertyType) + { + case SerializedPropertyType.Float: + Property.SetValue(value); + break; + case SerializedPropertyType.Integer: + Property.SetValue((int)value); + break; + } + + EditorUtility.SetDirty(Property.serializedObject.targetObject); + Property.serializedObject.ApplyModifiedProperties(); + return GetClearCache(); + } + } +} \ No newline at end of file diff --git a/Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta b/Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta new file mode 100644 index 0000000..ea48a0b --- /dev/null +++ b/Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a7c8db3cd48b4e84a5654cf0f99086f8 +timeCreated: 1714589033 \ No newline at end of file diff --git a/Editor/EditorAddons/Wrappers/MaxWrapper.cs b/Editor/EditorAddons/Wrappers/MaxWrapper.cs new file mode 100644 index 0000000..9edc112 --- /dev/null +++ b/Editor/EditorAddons/Wrappers/MaxWrapper.cs @@ -0,0 +1,48 @@ +using System; +using Better.Commons.EditorAddons.Drawers.Caching; +using Better.Commons.EditorAddons.Extensions; +using Better.Commons.EditorAddons.Utility; +using Better.Validation.Runtime.Attributes; +using UnityEditor; + +namespace Better.Validation.EditorAddons.Wrappers +{ + public class MaxWrapper : PropertyValidationWrapper + { + public override bool IsSupported() + { + return Property.propertyType is SerializedPropertyType.Integer or SerializedPropertyType.Float; + } + + public override CacheValue Validate() + { + var maxAttribute = (MaxAttribute)Attribute; + var maxValue = maxAttribute.Max; + var isValid = Property.propertyType switch + { + SerializedPropertyType.Float => Property.floatValue <= maxValue, + SerializedPropertyType.Integer => Property.intValue <= maxValue, + _ => throw new ArgumentOutOfRangeException() + }; + + if (isValid) + { + return GetClearCache(); + } + + switch (Property.propertyType) + { + case SerializedPropertyType.Float: + Property.SetValue(maxValue); + break; + case SerializedPropertyType.Integer: + Property.SetValue((int)maxValue); + break; + } + + EditorUtility.SetDirty(Property.serializedObject.targetObject); + Property.serializedObject.ApplyModifiedProperties(); + return GetClearCache(); + } + } +} \ No newline at end of file diff --git a/Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta b/Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta new file mode 100644 index 0000000..3ac83d7 --- /dev/null +++ b/Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 18228140e07b49a49ce7f2d7677b69ca +timeCreated: 1714586383 \ No newline at end of file diff --git a/Runtime/Attributes/ClampAttribute.cs b/Runtime/Attributes/ClampAttribute.cs new file mode 100644 index 0000000..e74ee9d --- /dev/null +++ b/Runtime/Attributes/ClampAttribute.cs @@ -0,0 +1,20 @@ +using System; +using System.Diagnostics; +using Better.Internal.Core.Runtime; + +namespace Better.Validation.Runtime.Attributes +{ + [Conditional(Defines.Editor)] + [AttributeUsage(AttributeTargets.Field)] + public class ClampAttribute : ValidationAttribute + { + public float Min { get; } + public float Max { get; } + + public ClampAttribute(float min, float max) + { + Min = min; + Max = max; + } + } +} \ No newline at end of file diff --git a/Runtime/Attributes/ClampAttribute.cs.meta b/Runtime/Attributes/ClampAttribute.cs.meta new file mode 100644 index 0000000..13a61d3 --- /dev/null +++ b/Runtime/Attributes/ClampAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 60c97c1b8f4742a28249f028f12ade22 +timeCreated: 1714588974 \ No newline at end of file diff --git a/Runtime/Attributes/MaxAttribute.cs b/Runtime/Attributes/MaxAttribute.cs new file mode 100644 index 0000000..25fa846 --- /dev/null +++ b/Runtime/Attributes/MaxAttribute.cs @@ -0,0 +1,18 @@ +using System; +using System.Diagnostics; +using Better.Internal.Core.Runtime; + +namespace Better.Validation.Runtime.Attributes +{ + [Conditional(Defines.Editor)] + [AttributeUsage(AttributeTargets.Field)] + public class MaxAttribute : ValidationAttribute + { + public float Max { get; } + + public MaxAttribute(float max) + { + Max = max; + } + } +} \ No newline at end of file diff --git a/Runtime/Attributes/MaxAttribute.cs.meta b/Runtime/Attributes/MaxAttribute.cs.meta new file mode 100644 index 0000000..22f5079 --- /dev/null +++ b/Runtime/Attributes/MaxAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ab5da13133494d3883761e219721a17f +timeCreated: 1714585888 \ No newline at end of file diff --git a/package.json b/package.json index dd04c2d..f5f1100 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.uurha.bettervalidation", "displayName": "Better Validation", - "version": "1.6.0", + "version": "1.6.1", "unity": "2021.3", "description": " ", "dependencies": {