using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace UIWidgets {
///
/// Bridge to EasyLayout class.
///
public class EasyLayoutBridge : ILayoutBridge {
bool isHorizontal;
///
/// Gets or sets a value indicating whether this instance is horizontal.
///
/// true if this instance is horizontal; otherwise, false.
public bool IsHorizontal {
get {
return isHorizontal;
}
set {
isHorizontal = value;
UpdateDirection();
}
}
///
/// Gets or sets a value indicating whether this update content size fitter.
///
/// true if update content size fitter; otherwise, false.
public bool UpdateContentSizeFitter {
get; set;
}
EasyLayout.EasyLayout Layout;
RectTransform DefaultItem;
ContentSizeFitter fitter;
///
/// Initializes a new instance of the class.
///
/// Layout.
/// Default item.
/// Update ContentSizeFitter on direction change.
public EasyLayoutBridge(EasyLayout.EasyLayout layout, RectTransform defaultItem, bool updateContentSizeFitter=true)
{
Layout = layout;
DefaultItem = defaultItem;
UpdateContentSizeFitter = updateContentSizeFitter;
fitter = Layout.GetComponent();
}
void UpdateDirection()
{
Layout.Stacking = isHorizontal ? EasyLayout.Stackings.Vertical : EasyLayout.Stackings.Horizontal;
if (UpdateContentSizeFitter)
{
if (fitter!=null)
{
fitter.horizontalFit = (IsHorizontal) ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained;
fitter.verticalFit = (!IsHorizontal) ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained;
}
}
var layout_rect_transform = Layout.transform as RectTransform;
layout_rect_transform.pivot = new Vector2(0, 1);
if (isHorizontal)
{
layout_rect_transform.anchorMin = new Vector2(0, 0);
layout_rect_transform.anchorMax = new Vector2(0, 1);
}
else
{
layout_rect_transform.anchorMin = new Vector2(0, 1);
layout_rect_transform.anchorMax = new Vector2(1, 1);
}
layout_rect_transform.sizeDelta = new Vector2(0, 0);
}
///
/// Updates the layout.
///
public void UpdateLayout()
{
Layout.UpdateLayout();
if (fitter!=null)
{
fitter.SetLayoutHorizontal();
fitter.SetLayoutVertical();
}
}
///
/// Sets the filler.
///
/// First.
/// Last.
public void SetFiller(float first, float last)
{
var padding = IsHorizontal
? new EasyLayout.Padding(first, last, 0, 0)
: new EasyLayout.Padding(0, 0, first, last);
Layout.PaddingInner = padding;
}
///
/// Gets the size of the item.
///
/// The item size.
public Vector2 GetItemSize()
{
return new Vector2(DefaultItem.rect.width, DefaultItem.rect.height);
}
///
/// Gets the left or top margin.
///
/// The margin.
public float GetMargin()
{
return IsHorizontal ? Layout.GetMarginLeft() : Layout.GetMarginTop();
}
///
/// Gets the full margin.
///
/// The full margin.
public float GetFullMargin()
{
return IsHorizontal ? Layout.GetMarginLeft() + Layout.GetMarginRight() : Layout.GetMarginTop() + Layout.GetMarginBottom();
}
///
/// Gets the spacing between items.
///
/// The spacing.
public float GetSpacing()
{
return IsHorizontal ? Layout.Spacing.x : Layout.Spacing.y;
}
}
}