Add InfoBox attribute

This commit is contained in:
VladV 2022-05-19 17:31:43 +03:00
parent 801b793124
commit 9f2610d171
7 changed files with 68 additions and 24 deletions

View File

@ -1,19 +0,0 @@
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
[assembly: RegisterTriAttributeDrawer(typeof(InfoBoxDrawer), TriDrawerOrder.System)]
namespace TriInspector.Drawers
{
public class InfoBoxDrawer : TriAttributeDrawer<InfoBoxAttribute>
{
public override TriElement CreateElement(TriProperty property, TriElement next)
{
var container = new TriElement();
container.AddChild(new TriInfoBoxElement(Attribute.Text, Attribute.MessageType));
container.AddChild(next);
return container;
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 511918057d2f46238db086d6471d660e
timeCreated: 1652897510

View File

@ -0,0 +1,35 @@
using TriInspector;
using TriInspector.Resolvers;
using TriInspector.Validators;
[assembly: RegisterTriAttributeValidator(typeof(InfoBoxValidator))]
namespace TriInspector.Validators
{
public class InfoBoxValidator : TriAttributeValidator<InfoBoxAttribute>
{
private ValueResolver<string> _resolver;
private ValueResolver<bool> _visibleIfResolver;
public override void Initialize(TriPropertyDefinition propertyDefinition)
{
base.Initialize(propertyDefinition);
_resolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Text);
_visibleIfResolver = Attribute.VisibleIf != null
? ValueResolver.Resolve<bool>(propertyDefinition, Attribute.VisibleIf)
: null;
}
public override TriValidationResult Validate(TriProperty property)
{
if (_visibleIfResolver != null && !_visibleIfResolver.GetValue(property))
{
return TriValidationResult.Valid;
}
var message = _resolver.GetValue(property, "");
return new TriValidationResult(false, message, Attribute.MessageType);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d6a852249c1149edbbc1bd0e997726ad
timeCreated: 1652968861

View File

@ -127,6 +127,32 @@ private TriValidationResult ValidateTexture()
```
#### InfoBox
![InfoBox](https://user-images.githubusercontent.com/26966368/169318171-d1a02212-48f1-41d1-b0aa-e2e1b25df262.png)
```csharp
[Title("InfoBox Message Types")]
[InfoBox("Default info box")]
public int a;
[InfoBox("None info box", TriMessageType.None)]
public int b;
[InfoBox("Warning info box", TriMessageType.Warning)]
public int c;
[InfoBox("Error info box", TriMessageType.Error)]
public int d;
[InfoBox("$" + nameof(DynamicInfo), visibleIf: nameof(VisibleInEditMode))]
public Vector3 vec;
private string DynamicInfo => "Dynamic info box: " + DateTime.Now.ToLongTimeString();
private bool VisibleInEditMode => !Application.isPlaying;
```
### Styling
#### Title

View File

@ -9,11 +9,13 @@ namespace TriInspector
{
public string Text { get; }
public TriMessageType MessageType { get; }
public string VisibleIf { get; }
public InfoBoxAttribute(string text, TriMessageType messageType = TriMessageType.Info)
public InfoBoxAttribute(string text, TriMessageType messageType = TriMessageType.Info, string visibleIf = null)
{
Text = text;
MessageType = messageType;
VisibleIf = visibleIf;
}
}
}

View File

@ -4,7 +4,7 @@
{
public static TriValidationResult Valid => new TriValidationResult(true, null, TriMessageType.None);
private TriValidationResult(bool valid, string message, TriMessageType messageType)
public TriValidationResult(bool valid, string message, TriMessageType messageType)
{
IsValid = valid;
Message = message;