using UnityEngine;
namespace Flexalon
{
///
/// Adapters determine how Flexalon measures other Unity components.
/// See [adapters](/docs/adapters) documentation.
///
public interface Adapter
{
/// Measure the size of this node.
/// The node to measure.
/// The size set by the Flexalon Object Component. The adapter should update any axis set to SizeType.Component.
/// The maximum size, determined by the MinSizeType.
/// The maximum size, determined by the MaxSizeType and the parent layout.
/// The measured bounds to use in layout.
Bounds Measure(FlexalonNode node, Vector3 size, Vector3 min, Vector3 max);
///
/// Return what the gameObject's scale should be in local space.
///
/// The node to update.
/// The desired scale.
/// True if the scale should be modified.
bool TryGetScale(FlexalonNode node, out Vector3 scale);
/// Return what the rect transform size should be.
/// The node to update.
/// The desired rect size.
/// True if the rect size should be modified.
bool TryGetRectSize(FlexalonNode node, out Vector2 rectSize);
}
internal interface InternalAdapter : Adapter
{
bool IsValid();
bool SizeChanged();
}
internal class DefaultAdapter : Adapter
{
private InternalAdapter _adapter;
public DefaultAdapter(GameObject gameObject)
{
CheckComponent(gameObject);
// Prevent detecting a change immediately after creating the adapter.
_adapter?.SizeChanged();
}
public bool CheckComponent(GameObject gameObject)
{
if (_adapter == null)
{
CreateAdapter(gameObject);
return _adapter != null;
}
if (!_adapter.IsValid())
{
CreateAdapter(gameObject);
return true;
}
if (_adapter != null && _adapter.SizeChanged())
{
return true;
}
return false;
}
public void CreateAdapter(GameObject gameObject)
{
_adapter = null;
#if UNITY_TMPRO
if (gameObject.TryGetComponent(out var text))
{
_adapter = new TextAdapter(text);
} else
#endif
#if UNITY_UI
if (gameObject.TryGetComponent