Skip to content

Commit

Permalink
Merge pull request #28 from techno-dwarf-works/dev
Browse files Browse the repository at this point in the history
Version 1.6.1
  • Loading branch information
OpOpYaDev committed May 1, 2024
1 parent 2672b9d commit 3d8a3e4
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Editor/EditorAddons/Utility/ValidationAttributeUtility.cs
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
51 changes: 51 additions & 0 deletions Editor/EditorAddons/Wrappers/ClampWrapper.cs
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();
}
}
}
3 changes: 3 additions & 0 deletions Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta

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

48 changes: 48 additions & 0 deletions 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();
}
}
}
3 changes: 3 additions & 0 deletions Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta

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

20 changes: 20 additions & 0 deletions 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;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/ClampAttribute.cs.meta

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

18 changes: 18 additions & 0 deletions 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;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/MaxAttribute.cs.meta

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

2 changes: 1 addition & 1 deletion 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

0 comments on commit 3d8a3e4

Please sign in to comment.