-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Version 1.6.2
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,51 @@ | ||
using System.Reflection; | ||
using Better.Commons.EditorAddons.Drawers.Attributes; | ||
using Better.Commons.EditorAddons.Drawers.Base; | ||
using Better.Commons.EditorAddons.Drawers.Caching; | ||
using Better.Commons.EditorAddons.Drawers; | ||
using Better.Commons.EditorAddons.Drawers.Container; | ||
using Better.Commons.EditorAddons.Extensions; | ||
using Better.Commons.EditorAddons.Utility; | ||
using Better.Commons.Runtime.Drawers.Attributes; | ||
using Better.Commons.Runtime.Extensions; | ||
using Better.Validation.EditorAddons.Handlers; | ||
using Better.Validation.EditorAddons.Utility; | ||
using Better.Validation.EditorAddons.Wrappers; | ||
using Better.Validation.Runtime.Attributes; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Better.Validation.EditorAddons.Drawers | ||
{ | ||
[MultiCustomPropertyDrawer(typeof(ValidationAttribute))] | ||
public class ValidationDrawer : MultiFieldDrawer<PropertyValidationWrapper> | ||
[CustomPropertyDrawer(typeof(ValidationAttribute))] | ||
public class ValidationDrawer : BasePropertyDrawer<PropertyValidationHandler, ValidationAttribute> | ||
{ | ||
private CacheValue<MutableTuple<string, ValidationType>> _validationResult = new CacheValue<MutableTuple<string, ValidationType>>(); | ||
|
||
public ValidationDrawer(FieldInfo fieldInfo, MultiPropertyAttribute attribute) : base(fieldInfo, attribute) | ||
protected override void PopulateContainer(ElementsContainer container) | ||
{ | ||
UpdateDrawer(container); | ||
container.SerializedPropertyChanged += UpdateDrawer; | ||
} | ||
|
||
protected override bool PreDraw(ref Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
var cache = ValidateCachedProperties(property, ValidationAttributeUtility.Instance); | ||
var validationWrapper = cache.Value; | ||
var wrapper = validationWrapper.Wrapper; | ||
if (!cache.IsValid) | ||
{ | ||
if (cache.Value == null) | ||
{ | ||
return false; | ||
} | ||
|
||
wrapper.SetProperty(property, (ValidationAttribute)_attribute); | ||
} | ||
private void UpdateDrawer(ElementsContainer container) | ||
{ | ||
var handler = GetHandler(container.SerializedProperty); | ||
handler.Setup(container.SerializedProperty, FieldInfo, Attribute); | ||
|
||
if (wrapper.IsSupported()) | ||
if (handler.IsSupported()) | ||
{ | ||
var validation = wrapper.Validate(); | ||
_validationResult.Set(validation.IsValid, new MutableTuple<string, ValidationType>(validation.Value, wrapper.Type)); | ||
} | ||
var validation = handler.Validate(); | ||
|
||
return true; | ||
} | ||
HelpBox helpBox; | ||
if (!container.TryGetByTag(container.SerializedProperty, out var element)) | ||
{ | ||
helpBox = new HelpBox(); | ||
element = container.CreateElementFrom(helpBox); | ||
element.AddTag(container.SerializedProperty); | ||
} | ||
else | ||
{ | ||
helpBox = element.Q<HelpBox>(); | ||
} | ||
|
||
protected override Rect PreparePropertyRect(Rect original) | ||
{ | ||
return original; | ||
} | ||
helpBox.text = validation.Result; | ||
helpBox.messageType = handler.Type.GetMessageType(); | ||
|
||
protected override void PostDraw(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
if (!_validationResult.IsValid) | ||
{ | ||
var (value, type) = _validationResult.Value; | ||
ExtendedGUIUtility.HelpBox(value, type.GetIconType()); | ||
helpBox.style.SetVisible(!validation.State); | ||
} | ||
} | ||
|
||
protected override WrapperCollection<PropertyValidationWrapper> GenerateCollection() | ||
{ | ||
return new WrapperCollection<PropertyValidationWrapper>(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.