using UnityEngine;
using UnityEngine.Events;
namespace Rellac.Windows
{
///
/// Script to handle moving windows
///
public class GUIWindowMover : GUIPointerObject
{
///
/// Window to move
///
[Tooltip("Window to move")]
[SerializeField] private RectTransform parentWindow = null;
///
/// Mover is locked and unusable
///
[Tooltip("Mover is locked and unusable")]
[SerializeField] private bool isLocked = false;
///
/// Fires when a window has been moved
///
[Tooltip("Fires when a window has been moved")]
[SerializeField] private UnityEvent onWindowMoved = null;
private Vector2 mouseOffset;
private bool isGrabbed = false;
void Start()
{
onPointerDown.AddListener(SetIsGrabbed);
}
void Update()
{
if (!isGrabbed || isLocked) return;
parentWindow.position = (Vector2)GUIWindowUtils.MousePosition() + mouseOffset;
if (Input.GetMouseButtonUp(0))
{
isGrabbed = false;
if (onWindowMoved != null)
{
onWindowMoved.Invoke();
}
}
}
///
/// Toggle interactivity of handle
///
/// is interactive
public void SetIsLocked(bool input)
{
isLocked = input;
isGrabbed = false;
}
///
/// Trigger that window has started to be moved
///
public void SetIsGrabbed()
{
mouseOffset = parentWindow.position - GUIWindowUtils.MousePosition();
isGrabbed = true;
parentWindow.SetAsLastSibling();
}
}
}