using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections.Generic;
using System;
namespace UIWidgets
{
///
/// Resizable event.
///
[Serializable]
public class ResizableEvent : UnityEvent
{
}
///
/// Resizable.
/// N - north (top).
/// S - south (bottom).
/// E - east (right).
/// W - west (left).
///
[AddComponentMenu("UI/UIWidgets/Resizable")]
public class Resizable : MonoBehaviour,
IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler,
IPointerEnterHandler, IPointerExitHandler
{
///
/// Resize directions.
///
[Serializable]
public struct Directions {
///
/// Allow resize from top.
///
public bool Top;
///
/// Allow resize from bottom.
///
public bool Bottom;
///
/// Allow resize from left.
///
public bool Left;
///
/// Allow resize from right.
///
public bool Right;
///
/// Initializes a new instance of the struct.
///
/// If set to true allow resize from top.
/// If set to true allow resize from bottom.
/// If set to true allow resize from left.
/// If set to true allow resize from right.
public Directions(bool top, bool bottom, bool left, bool right)
{
Top = top;
Bottom = bottom;
Left = left;
Right = right;
}
}
///
/// Active resize region.
///
public struct Regions {
///
/// The top.
///
public bool Top;
///
/// The bottom.
///
public bool Bottom;
///
/// The left.
///
public bool Left;
///
/// The right.
///
public bool Right;
///
/// NWSE
///
/// true if cursor mode is NWSE; otherwise, false.
public bool NWSE {
get {
return (Top && Left) || (Bottom && Right);
}
}
///
/// NESW.
///
/// true if cursor mode is NESW; otherwise, false.
public bool NESW {
get {
return (Top && Right) || (Bottom && Left);
}
}
///
/// NS
///
/// true if cursor mode is NS; otherwise, false.
public bool NS {
get {
return (Top && !Right) || (Bottom && !Left);
}
}
///
/// EW.
///
/// true if cursor mode is EW; otherwise, false.
public bool EW {
get {
return (!Top && Right) || (!Bottom && Left);
}
}
///
/// Is any region active.
///
/// true if any region active; otherwise, false.
public bool Active {
get {
return Top || Bottom || Left || Right;
}
}
///
/// Reset this instance.
///
public void Reset()
{
Top = false;
Bottom = false;
Left = false;
Right = false;
}
///
/// Returns a string that represents the current object.
///
/// A string that represents the current object.
public override string ToString()
{
return String.Format("Top: {0}; Bottom: {1}; Left: {2}; Right: {3}", Top, Bottom, Left, Right);
}
}
///
/// Is need to update RectTransform on Resize.
///
[SerializeField]
public bool UpdateRectTransform = true;
///
/// Is need to update LayoutElement on Resize.
///
[SerializeField]
public bool UpdateLayoutElement = true;
///
/// The active region in points from left or right border where resize allowed.
///
[SerializeField]
[Tooltip("Maximum padding from border where resize active.")]
public float ActiveRegion = 5;
///
/// The minimum size.
///
[SerializeField]
public Vector2 MinSize;
///
/// The maximum size.
///
[SerializeField]
[Tooltip("Set 0 to unlimit.")]
public Vector2 MaxSize;
///
/// The keep aspect ratio.
/// Aspect ratio applied after MinSize and MaxSize, so if RectTransform aspect ratio not equal MinSize and MaxSize aspect ratio then real size may be outside limit with one of axis.
///
[SerializeField]
public bool KeepAspectRatio;
///
/// Resize directions.
///
[SerializeField]
public Directions ResizeDirections = new Directions(true, true, true, true);
///
/// The current camera. For Screen Space - Overlay let it empty.
///
[SerializeField]
public Camera CurrentCamera;
///
/// The cursor EW texture.
///
[SerializeField]
public Texture2D CursorEWTexture;
///
/// The cursor EW hot spot.
///
[SerializeField]
public Vector2 CursorEWHotSpot = new Vector2(16, 16);
///
/// The cursor NS texture.
///
[SerializeField]
public Texture2D CursorNSTexture;
///
/// The cursor NS hot spot.
///
[SerializeField]
public Vector2 CursorNSHotSpot = new Vector2(16, 16);
///
/// The cursor NESW texture.
///
[SerializeField]
public Texture2D CursorNESWTexture;
///
/// The cursor NESW hot spot.
///
[SerializeField]
public Vector2 CursorNESWHotSpot = new Vector2(16, 16);
///
/// The cursor NWSE texture.
///
[SerializeField]
public Texture2D CursorNWSETexture;
///
/// The cursor NWSE hot spot.
///
[SerializeField]
public Vector2 CursorNWSEHotSpot = new Vector2(16, 16);
///
/// The default cursor texture.
///
[SerializeField]
public Texture2D DefaultCursorTexture;
///
/// The default cursor hot spot.
///
[SerializeField]
public Vector2 DefaultCursorHotSpot;
///
/// OnStartResize event.
///
public ResizableEvent OnStartResize = new ResizableEvent();
///
/// OnEndResize event.
///
public ResizableEvent OnEndResize = new ResizableEvent();
RectTransform rectTransform;
///
/// Gets the RectTransform.
///
/// RectTransform.
public RectTransform RectTransform {
get {
if (rectTransform==null)
{
rectTransform = transform as RectTransform;
}
return rectTransform;
}
}
LayoutElement layoutElement;
///
/// Gets the LayoutElement.
///
/// LayoutElement.
public LayoutElement LayoutElement {
get {
if (layoutElement==null)
{
layoutElement = GetComponent();
if (layoutElement==null)
{
layoutElement = gameObject.AddComponent();
}
}
return layoutElement;
}
}
Regions regions;
Regions dragRegions;
Canvas canvas;
RectTransform canvasRect;
bool processDrag;
void Start()
{
var layout = GetComponent();
if (layout)
{
Utilites.UpdateLayout(layout);
}
Init();
}
///
/// Raises the initialize potential drag event.
///
/// Event data.
public void OnInitializePotentialDrag(PointerEventData eventData)
{
Init();
}
///
/// Init this instance.
///
public void Init()
{
canvasRect = Utilites.FindTopmostCanvas(transform) as RectTransform;
canvas = canvasRect.GetComponent