Example #1
0
    /// <summary>
    /// A function to implement the spinner behaviour.
    /// </summary>
    /// <param name="mgr">The application manager.</param>
    /// <param name="actor">The actor that owns the parameter the widget is being setup for.</param>
    /// <param name="btn">The spinner button.</param>
    /// <param name="ev">The parameter value.</param>
    /// <param name="ve">The parameter's widget.</param>
    public static void SetupWidgetDrag(Main mgr, SceneActor actor, UnityEngine.UI.Button btn, EditValue ev, ValueEditor_Base ve)
    {
        UnityEngine.EventSystems.EventTrigger et =
            btn.gameObject.AddComponent <UnityEngine.EventSystems.EventTrigger>();

        et.triggers = new List <UnityEngine.EventSystems.EventTrigger.Entry>();

        UnityEngine.EventSystems.EventTrigger.Entry aBeginDrag = new UnityEngine.EventSystems.EventTrigger.Entry();
        aBeginDrag.eventID = UnityEngine.EventSystems.EventTriggerType.BeginDrag;
        aBeginDrag.callback.AddListener(
            (x) =>
        {
            UnityEngine.EventSystems.PointerEventData ped = x as UnityEngine.EventSystems.PointerEventData;

            if (ve.escapeCheck != null)
            {
                ve.StopCoroutine(ve.escapeCheck);
            }

            ve.escapeCheck = ve.StartCoroutine(EndDragOnEscape(ped, mgr, actor, ve));

            startingDragValue = ev.val.Clone();
            startDrag         = ped.position;
        });

        UnityEngine.EventSystems.EventTrigger.Entry aEndDrag = new UnityEngine.EventSystems.EventTrigger.Entry();
        aEndDrag.eventID = UnityEngine.EventSystems.EventTriggerType.EndDrag;
        aEndDrag.callback.AddListener(
            (x) =>
        {
            UnityEngine.EventSystems.PointerEventData ped = x as UnityEngine.EventSystems.PointerEventData;

            ValFloat diff = new ValFloat(-(startDrag.y - ped.position.y));
            Val vb        = ev.Offset(startingDragValue, diff);
            ev.val.SetValue(vb);

            mgr.NotifyActorModified(actor, ev.name);
            if (ve != null && ve.escapeCheck != null)
            {
                ve.StopCoroutine(ve.escapeCheck);
                ve.escapeCheck = null;
            }
        });

        UnityEngine.EventSystems.EventTrigger.Entry aDrag = new UnityEngine.EventSystems.EventTrigger.Entry();
        aDrag.eventID = UnityEngine.EventSystems.EventTriggerType.Drag;
        aDrag.callback.AddListener(
            (x) =>
        {
            UnityEngine.EventSystems.PointerEventData ped = x as UnityEngine.EventSystems.PointerEventData;

            ValFloat diff = new ValFloat(-(startDrag.y - ped.position.y));
            Val vb        = ev.Offset(startingDragValue, diff);
            ev.val.SetValue(vb);

            mgr.NotifyActorModified(actor, ev.name);
        });

        et.triggers.Add(aBeginDrag);
        et.triggers.Add(aEndDrag);
        et.triggers.Add(aDrag);
    }