Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.6.1 #28

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<string> 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();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Assets/BetterValidation/Runtime/Attributes/ClampAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Assets/BetterValidation/Runtime/Attributes/MaxAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/BetterValidation/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.uurha.bettervalidation",
"displayName": "Better Validation",
"version": "1.6.0",
"version": "1.6.1",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down
Loading