This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
generated from techno-dwarf-works/unity-package-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from techno-dwarf-works/dev
Version 1.6.1
- Loading branch information
Showing
10 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
Assets/BetterValidation/Runtime/Attributes/ClampAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
Assets/BetterValidation/Runtime/Attributes/ClampAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
Assets/BetterValidation/Runtime/Attributes/MaxAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
Assets/BetterValidation/Runtime/Attributes/MaxAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters