using System;
using Unity.Properties;
using UnityEngine.UIElements;

/// 
/// This example is taken from the Unity 6 documentation examples and is here for an example of a working UXML object
/// 
[UxmlObject]
public partial class CurrentTimeBinding : CustomBinding
{
    [UxmlAttribute]
    public string TimeFormat = "HH:mm:ss";

    /// 
    /// By default, the binding system updates a CustomBinding instance on every frame.
    /// To define update triggers, use the following methods:

/// MarkDirty: Sets the binding object as dirty so that it gets updated during the next cycle.
/// updateTrigger: Use this enum property to change how the binding is updated.
/// BindingResult: Use this method to customize the update process.

/// The BindingResult return is a struct that tells you whether the update was successful.
/// It contains a status and a message. ///
/// BindingContext /// BindingResult struct that tells you whether the update was successful /// public CurrentTimeBinding() { updateTrigger = BindingUpdateTrigger.EveryUpdate; } protected override BindingResult Update(in BindingContext context) { var timeNow = DateTime.Now.ToString(TimeFormat); var element = context.targetElement; if (ConverterGroups.TrySetValueGlobal(ref element, context.bindingId, timeNow, out var errorCode)) return new BindingResult(BindingStatus.Success); // Error handling var bindingTypename = TypeUtility.GetTypeDisplayName(typeof(CurrentTimeBinding)); var bindingId = $"{TypeUtility.GetTypeDisplayName(element.GetType())}.{context.bindingId}"; return errorCode switch { VisitReturnCode.InvalidPath => new BindingResult(BindingStatus.Failure, $"{bindingTypename}: Binding id `{bindingId}` is either invalid or contains a `null` value."), VisitReturnCode.InvalidCast => new BindingResult(BindingStatus.Failure, $"{bindingTypename}: Invalid conversion from `string` for binding id `{bindingId}`"), VisitReturnCode.AccessViolation => new BindingResult(BindingStatus.Failure, $"{bindingTypename}: Trying set value for binding id `{bindingId}`, but it is read-only."), _ => throw new ArgumentOutOfRangeException() }; } }