// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
///
/// Customizes editor of or .
///
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class BehaviorKnowledgeSelectorAttribute : Attribute
{
///
/// Changes selector editor to allow to pick only whole goals.
///
public bool IsGoalSelector;
///
/// Initializes a new instance of the structure.
///
/// Changes selector editor to allow to pick only whole goals.
public BehaviorKnowledgeSelectorAttribute(bool isGoalSelector = false)
{
IsGoalSelector = isGoalSelector;
}
}
#if FLAX_EDITOR
[CustomEditor(typeof(FlaxEditor.CustomEditors.Editors.BehaviorKnowledgeSelectorEditor))]
#endif
partial struct BehaviorKnowledgeSelectorAny : IComparable, IComparable
{
///
/// Initializes a new instance of the structure.
///
/// The selector path.
public BehaviorKnowledgeSelectorAny(string path)
{
Path = path;
}
///
/// Initializes a new instance of the structure.
///
/// The other selector.
public BehaviorKnowledgeSelectorAny(BehaviorKnowledgeSelectorAny other)
{
Path = other.Path;
}
///
/// Implicit cast operator from selector to string.
///
/// Selector
/// Path
public static implicit operator string(BehaviorKnowledgeSelectorAny value)
{
return value.Path;
}
///
/// Implicit cast operator from string to selector.
///
/// Path
/// Selector
public static implicit operator BehaviorKnowledgeSelectorAny(string value)
{
return new BehaviorKnowledgeSelectorAny(value);
}
///
/// Sets the selected knowledge value.
///
/// The knowledge container to access.
/// The value to set.
/// True if set value value, otherwise false.
public bool Set(BehaviorKnowledge knowledge, object value)
{
return knowledge != null && knowledge.Set(Path, value);
}
///
/// Gets the selected knowledge value.
///
/// The knowledge container to access.
/// The output value or null (if cannot read it - eg. missing goal or no blackboard entry of that name).
public object Get(BehaviorKnowledge knowledge)
{
object value = null;
if (knowledge != null)
knowledge.Get(Path, out value);
return value;
}
///
/// Tries to get the selected knowledge value. Returns true if got value, otherwise false.
///
/// The knowledge container to access.
/// The output value.
/// True if got value, otherwise false.
public bool TryGet(BehaviorKnowledge knowledge, out object value)
{
value = null;
return knowledge != null && knowledge.Get(Path, out value);
}
///
public override string ToString()
{
return Path;
}
///
public override int GetHashCode()
{
return Path?.GetHashCode() ?? 0;
}
///
public int CompareTo(object obj)
{
if (obj is BehaviorKnowledgeSelectorAny other)
return CompareTo(other);
return 0;
}
///
public int CompareTo(BehaviorKnowledgeSelectorAny other)
{
return string.Compare(Path, other.Path, StringComparison.Ordinal);
}
}
///
/// Behavior knowledge value selector that can reference blackboard item, behavior goal or sensor values.
///
#if FLAX_EDITOR
[CustomEditor(typeof(FlaxEditor.CustomEditors.Editors.BehaviorKnowledgeSelectorEditor))]
#endif
public struct BehaviorKnowledgeSelector : IComparable, IComparable, IComparable>
{
///
/// Selector path that redirects to the specific knowledge value.
///
public string Path;
///
/// Initializes a new instance of the structure.
///
/// The selector path.
public BehaviorKnowledgeSelector(string path)
{
Path = path;
}
///
/// Initializes a new instance of the structure.
///
/// The other selector.
public BehaviorKnowledgeSelector(BehaviorKnowledgeSelectorAny other)
{
Path = other.Path;
}
///
/// Implicit cast operator from selector to string.
///
/// Selector
/// Path
public static implicit operator string(BehaviorKnowledgeSelector value)
{
return value.Path;
}
///
/// Implicit cast operator from string to selector.
///
/// Path
/// Selector
public static implicit operator BehaviorKnowledgeSelector(string value)
{
return new BehaviorKnowledgeSelector(value);
}
///
/// Sets the selected knowledge value.
///
/// The knowledge container to access.
/// The value to set.
/// True if set value value, otherwise false.
public bool Set(BehaviorKnowledge knowledge, T value)
{
return knowledge != null && knowledge.Set(Path, value);
}
///
/// Gets the selected knowledge value.
///
/// The knowledge container to access.
/// The output value or null (if cannot read it - eg. missing goal or no blackboard entry of that name).
public T Get(BehaviorKnowledge knowledge)
{
if (knowledge != null && knowledge.Get(Path, out var value))
return Utilities.VariantUtils.Cast(value);
return default;
}
///
/// Tries to get the selected knowledge value. Returns true if got value, otherwise false.
///
/// The knowledge container to access.
/// The output value.
/// True if got value, otherwise false.
public bool TryGet(BehaviorKnowledge knowledge, out T value)
{
value = default;
object tmp = null;
bool result = knowledge != null && knowledge.Get(Path, out tmp);
if (result)
value = Utilities.VariantUtils.Cast(tmp);
return result;
}
///
public override string ToString()
{
return Path;
}
///
public override int GetHashCode()
{
return Path?.GetHashCode() ?? 0;
}
///
public int CompareTo(object obj)
{
if (obj is BehaviorKnowledgeSelectorAny otherAny)
return CompareTo(otherAny);
if (obj is BehaviorKnowledgeSelector other)
return CompareTo(other);
return 0;
}
///
public int CompareTo(BehaviorKnowledgeSelectorAny other)
{
return string.Compare(Path, other.Path, StringComparison.Ordinal);
}
///
public int CompareTo(BehaviorKnowledgeSelector other)
{
return string.Compare(Path, other.Path, StringComparison.Ordinal);
}
}
}