using System;
namespace Yarn.Unity.Attributes
{
#nullable enable
///
/// The abstract base class for all Yarn Editor attributes.
///
public abstract class YarnEditorAttribute : Attribute { }
///
/// Indents a property in the Unity Inspector.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class IndentAttribute : YarnEditorAttribute
{
///
/// The amount to indent this property by.
///
public int indentLevel = 1;
///
///
public IndentAttribute(int indentLevel = 1)
{
if (this.indentLevel < 0)
{
indentLevel = 0;
}
this.indentLevel = indentLevel;
}
}
///
/// Controls whether a property is visible or not in the Unity Inspector.
///
public abstract class VisibilityAttribute : YarnEditorAttribute
{
///
/// The type of test represented by .
///
public enum AttributeMode
{
///
/// is the name of a
/// variable or a reference to a ,
/// and the test passes when the variable is
/// (if bool) or non-null (if an object).
///
BooleanCondition,
///
/// is the name of an enum variable, and the
/// test passes when the variable's value is equal to .
///
EnumEquality,
}
///
/// The type of test that represents.
///
public AttributeMode Mode { get; protected set; }
///
/// Controls whether the property appears when the condition passes
/// (), or fails ().
///
public bool Invert { get; protected set; }
///
/// The name of another property on the object that determines whether
/// this property is visible or not.
///
public string? Condition { get; protected set; }
///
/// The value that the variable indicated by is
/// compared to.
///
/// This value is only used when is .
public int EnumValue { get; protected set; }
}
///
/// Shows this property only when a condition is true.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class ShowIfAttribute : VisibilityAttribute
{
///
///
public ShowIfAttribute(string condition)
{
this.Invert = false;
this.Condition = condition;
this.Mode = AttributeMode.BooleanCondition;
this.EnumValue = default;
}
///
/// This variable must be an enum.
///
public ShowIfAttribute(string condition, object value)
{
this.Invert = false;
this.Condition = condition;
this.Mode = AttributeMode.EnumEquality;
this.EnumValue = (int)value;
}
}
///
/// Hides this property when a condition is true.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class HideIfAttribute : VisibilityAttribute
{
///
///
public HideIfAttribute(string condition)
{
this.Invert = true;
this.Condition = condition;
}
///
/// This variable must be an enum.
///
public HideIfAttribute(string condition, object value)
{
this.Invert = true;
this.Condition = condition;
this.Mode = AttributeMode.EnumEquality;
this.EnumValue = (int)value;
}
}
///
/// Shows a header above this property and the following properties that
/// have the same group name, optionally as a foldout.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class GroupAttribute : YarnEditorAttribute
{
///
/// The name of the group.
///
public string GroupName { get; }
///
/// Whether to show this group as a fold-out.
///
public bool FoldOut { get; }
///
///
///
public GroupAttribute(string groupName, bool foldOut = false)
{
this.GroupName = groupName;
this.FoldOut = foldOut;
}
}
///
/// Overrides the displayed label of the property in the Unity Inspector.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class LabelAttribute : YarnEditorAttribute
{
///
/// The label to show for this property.
///
public string Label { get; }
///
///
public LabelAttribute(string label)
{
this.Label = label;
}
}
///
/// Overrides the displayed label of the property in the Unity Inspector by
/// getting a label from a named method.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class LabelFromAttribute : YarnEditorAttribute
{
///
/// The method to invoke that will return the label to display. The
/// method must be an instance method, take no parameters, and return a
/// .
///
public string SourceMethod { get; }
///
///
public LabelFromAttribute(string methodName)
{
this.SourceMethod = methodName;
}
}
///
/// Shows an error message box if this property is null.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class MustNotBeNullAttribute : YarnEditorAttribute
{
///
/// The text of the error message to show if the property is null. If
/// not provided, a generic error message is shown.
///
public string? Label { get; }
///
///
public MustNotBeNullAttribute(string? label = null)
{
this.Label = label;
}
}
///
/// Shows an error message box if this property is null and the variable
/// indicated by is false.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class MustNotBeNullWhenAttribute : YarnEditorAttribute
{
///
/// The name of another property on this object to compare to. This
/// variable must be either a boolean value, or a reference.
///
public string Condition { get; }
///
/// The text of the error message to show if the property is null and
/// the condition is met. If not provided, a generic error message is
/// shown.
///
public string? Label { get; }
///
///
///
public MustNotBeNullWhenAttribute(string condition, string? label = null)
{
this.Condition = condition;
this.Label = label;
}
}
///
/// Shows a message box on the property.
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class MessageBoxAttribute : YarnEditorAttribute
{
///
/// The name of a method that will be called to determine the contents
/// of the message box. The method must return a .
///
public string SourceMethod { get; }
///
/// The type of the message box to display.
///
///
public enum Type
{
///
/// Do not show an icon in the message box.
///
///
None,
///
/// Show an information icon in the message box.
///
///
Info,
///
/// Show a warning icon in the message box.
///
///
Warning,
///
/// Show an error icon in the message box.
///
///
Error
};
///
/// A description for a message box to show in the Unity Inspector.
///
public struct Message
{
///
/// The type of the message to show.
///
public Type type;
///
/// The text to show in the message box. If this value is , no message box is displayed.
///
public string? text;
///
/// Creates a new message with the given string.
///
///
public static implicit operator Message(string? text)
{
return new Message
{
type = Type.Error,
text = text,
};
}
}
///
///
public MessageBoxAttribute(string sourceMethod)
{
this.SourceMethod = sourceMethod;
}
///
/// Creates a new error using the provided text.
///
///
/// A new .
public static Message Error(string message)
{
return new Message { type = Type.Error, text = message };
}
///
/// Creates a new warning using the provided text.
///
///
///
public static Message Warning(string message)
{
return new Message { type = Type.Warning, text = message };
}
///
/// Creates a new information using the provided text.
///
///
///
public static Message Info(string message)
{
return new Message { type = Type.Info, text = message };
}
///
/// Creates a new neutral using the provided text.
///
///
///
public static Message Neutral(string message)
{
return new Message { type = Type.None, text = message };
}
///
/// Returns a new that represents an instruction
/// to not draw a message box at all.
///
public static Message NoMessage => new Message { type = Type.None, text = null };
}
}