devNotes 9-23-2016 waist level ui – panel layout

 

 

 

 

 
—————————————

using UnityEngine;
using System.Collections;
using VRTK;
using UnityEngine.UI;

public class Attractor_Show_Position : VRTK_InteractableObject
{
    private VRTK_InteractableObject interact;
    private Vector3 myPosition;
    private Text posText;
    private bool isTouched;
    private Transform center;
    private MD_Measure_Cube md_Measure_Cube;
    private bool wasTouched = false;
    private bool bSnapModeOn = false;
    private Rigidbody rb;

    private VRTK_ControllerActions controllerActions;
    private VRTK_ControllerEvents controllerEvents;

    private Vector3 vRounded;

    protected override void Awake()
    {
        base.Awake();
        rb = GetComponent<Rigidbody>();
        rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
    }

    void Start()
    {
        interact = GetComponent<VRTK_InteractableObject>();
        interact.InteractableObjectTouched += new InteractableObjectEventHandler(DoObjectTouched);
        interact.InteractableObjectUntouched += new InteractableObjectEventHandler(DoObjectUntouched);
        interact.InteractableObjectGrabbed += new InteractableObjectEventHandler(DoObjectGrabbed);
        interact.InteractableObjectUngrabbed += new InteractableObjectEventHandler(DoObjectUnGrabbed);

        rb = GetComponent<Rigidbody>();

        Text[] t = GetComponentsInChildren<Text>();
        posText = t[0];

        center = GetComponentInChildren<Inner_Sphere>().transform;

        posText.text = transform.position.ToString("F2");
        isTouched = false;
        HidePosition();

        md_Measure_Cube = FindObjectOfType<MD_Measure_Cube>();
    }

    private void Init()
    {
        //interact = GetComponent<VRTK_InteractableObject>();
        //interact.InteractableObjectTouched += new InteractableObjectEventHandler(DoObjectTouched);
        //Text[] t = GetComponentsInChildren<Text>();
        //posText = t[0];

        //posText.text = transform.position.ToString("F2");
        //isTouched = true;
    }

    //public void SetPosition(int NoteIx)
    //{
    //    Text[] t = GetComponentsInChildren<Text>();
    //    t[0].text = transform.position.ToString("F2");
    //}

    public void SetText(string s)
    {
        Text[] t = GetComponentsInChildren<Text>();
        t[0].text = s;
    }

    void OnCollisionEnter(Collision collisionInfo)
    {
        //argos_SoundGen.playNote(NoteIdx);
        //dbg_Text.text = " DoObjectTouched_PlayNote called";
    }

    private void DoObjectGrabbed(object sender, InteractableObjectEventArgs e)
    {

    }

    private void DoObjectUnGrabbed(object sender, InteractableObjectEventArgs e)
    {
        if (md_Measure_Cube.isTriggerPressed())
        {
            vRounded.y += 80f;
            rb.position = vRounded;
        }
    }
    //StartTouching(GameObject currentTouchingObject)
    public override void StartTouching(GameObject touchingObject)
    {
        base.StartTouching(touchingObject);
        controllerActions = touchingObject.GetComponent<VRTK_ControllerActions>();
        controllerEvents = touchingObject.GetComponent<VRTK_ControllerEvents>();
        controllerActions.TriggerHapticPulse(3333, 0.5f, 0.01f);
    }


    private void DoObjectTouched(object sender, InteractableObjectEventArgs e)
    {
        isTouched = true;
        rb.constraints = RigidbodyConstraints.FreezeAll;
        ShowPosition();
        md_Measure_Cube.show_Grid();

    }

    private void DoObjectUntouched(object sender, InteractableObjectEventArgs e)
    {
        isTouched = false;
        HidePosition();
        md_Measure_Cube.Update_To_Position(Vector3.zero);
        md_Measure_Cube.hide_Grid();
        StartCoroutine(WaitToUnConstrain());

    }

    private IEnumerator WaitToUnConstrain()
    {
        yield return new WaitForSeconds(0.2f);
        rb.constraints = RigidbodyConstraints.None;
        rb.velocity = Vector3.zero;
    }

    private float round_to_Point_25(float val)
    {
        float doub = val * (4f/5f);
        doub = Mathf.Round(doub);
        return doub * (5f/4f);
    }

    float waitForUpdate = 0;
    Vector3 vLast;
    void Update()
    {
        waitForUpdate -= Time.deltaTime;
        
        if (waitForUpdate < 0)
        {
            if (isTouched)
            {
                Vector3 v = rb.position;
                v.y -= 80;

                if (md_Measure_Cube.isTriggerPressed()) //snap mode
                {
                    v.x = round_to_Point_25(v.x);
                    v.y = round_to_Point_25(v.y);
                    v.z = round_to_Point_25(v.z);
                    vRounded = v;

                    if(vLast != v)
                    {
                        SteamVR_Controller.Input(0).TriggerHapticPulse();
                    }
                    bSnapModeOn = true;
                }
                else
                {
                    if (bSnapModeOn)
                    {
                        this.GetComponent<VRTK_InteractableObject>().ForceStopInteracting();
                        vRounded.y += 80f;
                        rb.position = vRounded;
                    }
                    bSnapModeOn = false;
                }

                md_Measure_Cube.Update_To_Position(v);

                Vector3 vScaled = v / 5f;

                posText.text = vScaled.ToString("F2");

                if (Camera.main != null)
                {
                    center.rotation = Camera.main.transform.rotation;
                }
                vLast = v;
            }
        }
    }

    public void ShowPosition()
    {

        StopCoroutine("TweenPosTextScale");
        StartCoroutine("TweenPosTextScale", true);
    }

    public void HidePosition()
    {
        StopCoroutine("TweenPosTextScale");
        StartCoroutine("TweenPosTextScale", false);
    }

    //Simple tweening for menu, scales linearly from 0 to 1 and 1 to 0
    private IEnumerator TweenPosTextScale(bool show)
    {
        float targetScale = 0;
        Vector3 Dir = -1 * Vector3.one;
        if (show)
        {
            targetScale = 1;
            Dir = Vector3.one;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && center.localScale.x < targetScale) || (!show && center.localScale.x > targetScale)))
        {
            center.localScale += Dir * Time.deltaTime * 2f; //Tweening function - currently 0.25 second linear
            yield return true;
            i++;
        }
        center.localScale = Dir * targetScale;
        StopCoroutine("TweenPosTextScale");
    }

    //private void TriggerNote(float volume)
    //{
    //    argos_SoundGen.playNote(NoteIdx);
    //}
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using VRTK;
using UnityEngine.EventSystems;
using NewtonVR;

public class ShowHide_UI : MonoBehaviour
{
    public static ShowHide_UI Instance;
    bool isShown = true;
    public VRTK_ControllerEvents ctrl_1_events;
    public VRTK_ControllerEvents ctrl_2_events;
    public HMD_Ctrl_Tracking hmd_Track;

    private Slider activeSlider;
    private float activeSld_Delta;
    private float activeSld_Adj;

    [Space(10)]
    [Header("Show Floor")]
    [Space(10)]
    public bool bFloor_isShown = true;
    public GameObject Floor;
    public float fadeFloorTime = 0.7f;
    private CanvasGroup floor_CG;

    [Space(10)]
    [Header("Show Grid")]
    [Space(10)]
    public GameObject Grid;
    public bool bGrid_isShown = false;
    private CanvasGroup grid_CG;
    public FadeAxes AxesParent;
    public float fadeGridTime = 0.7f;
    public GameObject Platonic_Butt_3d_Group;

    [Space(10)]
    [Header("UI Offsets")]
    [Space(10)]
    public float ui_dist;
    public float ui_vert_offset;

    [Space(10)]
    [Header("Platonic Scaling Interaction")]
    [Space(10)]
    public GameObject basePlatonicSolids_GO;
    public bool platonics_On = false;
    private bool platonics_tweening = false;

    [Space(10)]
    [Header("Grabbable Scaling Interaction")]
    [Space(10)]
    public GameObject baseGrabbables_GO;
    public NVRButton nvrGrababbleShow_Button;
    public bool grabbables_On = false;
    private bool grabbables_tweening = false;

    [Space(10)]
    [Header("ActiveSlider")]
    [Space(10)]

    public Text ActiveSlider_CTRL_Txt;

    private bool isA_Trigger_Pressed;

    // Use this for initialization
    void Start()
    {
        Instance = this;

        ctrl_1_events.ApplicationMenuPressed += new ControllerInteractionEventHandler(DoApplicationMenuPressed);
        ctrl_2_events.ApplicationMenuPressed += new ControllerInteractionEventHandler(DoApplicationMenuPressed);

        ctrl_1_events.TouchpadTouchStart += new ControllerInteractionEventHandler(DoTouchPadTouchStart);
        ctrl_2_events.TouchpadTouchStart += new ControllerInteractionEventHandler(DoTouchPadTouchStart);

        ctrl_1_events.TouchpadTouchEnd += new ControllerInteractionEventHandler(DoTouchPadTouchEnd);
        ctrl_2_events.TouchpadTouchEnd += new ControllerInteractionEventHandler(DoTouchPadTouchEnd);
        ctrl_1_events.TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged);
        ctrl_2_events.TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged);

        //GetComponent<VRTK_ControllerEvents>().TouchpadPressed += new ControllerInteractionEventHandler(DoTouchpadPressed);
        ctrl_1_events.TouchpadPressed += new ControllerInteractionEventHandler(DoTouchpadPressed);
        ctrl_2_events.TouchpadPressed += new ControllerInteractionEventHandler(DoTouchpadPressed);

        ctrl_1_events.TriggerPressed += new ControllerInteractionEventHandler(DoTriggerPressed);
        ctrl_2_events.TriggerPressed += new ControllerInteractionEventHandler(DoTriggerPressed);

        ctrl_1_events.TriggerReleased += new ControllerInteractionEventHandler(DoTriggerReleased);
        ctrl_2_events.TriggerReleased += new ControllerInteractionEventHandler(DoTriggerReleased);

        floor_CG = Floor.GetComponent<CanvasGroup>();
        grid_CG  = Grid.GetComponent<CanvasGroup>();

        TogglePlatonics();
        ToggleGrabbables();
    }

    void onApplicationQuit()
    {
        ctrl_1_events.ApplicationMenuPressed -= new ControllerInteractionEventHandler(DoApplicationMenuPressed);
        ctrl_2_events.ApplicationMenuPressed -= new ControllerInteractionEventHandler(DoApplicationMenuPressed);

        ctrl_1_events.TouchpadTouchStart -= new ControllerInteractionEventHandler(DoTouchPadTouchStart);
        ctrl_2_events.TouchpadTouchStart -= new ControllerInteractionEventHandler(DoTouchPadTouchStart);

        ctrl_1_events.TouchpadTouchEnd -= new ControllerInteractionEventHandler(DoTouchPadTouchEnd);
        ctrl_2_events.TouchpadTouchEnd -= new ControllerInteractionEventHandler(DoTouchPadTouchEnd);
        ctrl_1_events.TouchpadAxisChanged -= new ControllerInteractionEventHandler(DoTouchpadAxisChanged);
        ctrl_2_events.TouchpadAxisChanged -= new ControllerInteractionEventHandler(DoTouchpadAxisChanged);

    }

    private void DoTouchpadPressed(object sender, ControllerInteractionEventArgs e)
    {
        activeSlider = null;

    }

    private void DoTouchPadTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        if(activeSlider != null)
        {
            float min = activeSlider.minValue;
            float max = activeSlider.maxValue;

            activeSld_Delta = (max - min) / 24f;

        }
    }

    private void DoTouchPadTouchEnd(object sender, ControllerInteractionEventArgs e)
    {
        activeSld_Adj = 0f;

    }

    private void DoTouchpadAxisChanged(object sender, ControllerInteractionEventArgs e)
    {
        activeSld_Adj = activeSld_Delta * e.touchpadAxis.y;
    }

    private void DoTriggerPressed(object sender, ControllerInteractionEventArgs e)
    {
        isA_Trigger_Pressed = true;
    }

    private void DoTriggerReleased(object sender, ControllerInteractionEventArgs e)
    {
        isA_Trigger_Pressed = false;
    }

    public bool isOne_of_the_Triggers_Pressed()
    {
        return isA_Trigger_Pressed;
    }

    void Update ()
    {
        //if(aPlatonicShow_Button.ButtonDown && !platonics_tweening)
        //{
        //    TogglePlatonics();
        //}

        if (isA_Trigger_Pressed)
        {
            print("A Trigger is Pressed");
        }

        //if (nvrGrababbleShow_Button.ButtonDown && !grabbables_tweening)
        //{
        //    ToggleGrabbables();
        //}

        if (activeSlider != null)
        {
            ActiveSlider_CTRL_Txt.text = activeSlider.transform.gameObject.name.ToString();
            //activeSlider.value += activeSld_Adj * Time.deltaTime;
        }
        else
        {
            ActiveSlider_CTRL_Txt.text = "NONE SELECTED";
        }
    }



    private void DoApplicationMenuPressed(object sender, ControllerInteractionEventArgs e)
    {

        if (!isShown)
        {
            ShowMenu();
        }
        else
        {
            HideMenu();
        }
    }

    public void Toggle_Show_Floor()
    {
        if(bFloor_isShown)
        {
            Hide_Floor();
        }
        else
        {
            Show_Floor();
        }
    }

    public void grabSlider(Slider sld)
    {
        activeSlider = sld;
    }

    private void Hide_Floor()
    {
        bFloor_isShown = false;
        StopCoroutine("TweenFloorAlpha");
        StartCoroutine("TweenFloorAlpha", bFloor_isShown);
    }

    private void Show_Floor()
    {
        bFloor_isShown = true;
        StopCoroutine("TweenFloorAlpha");
        StartCoroutine("TweenFloorAlpha", bFloor_isShown);
    }

    private IEnumerator TweenFloorAlpha(bool show)
    {
        float targetAlpha = 0;
        float Dir = -1;
        if (show)
        {
            targetAlpha = 1;
            Dir = 1;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && floor_CG.alpha < targetAlpha) || (!show && floor_CG.alpha > targetAlpha)))
        {
            floor_CG.alpha += Dir * Time.deltaTime * fadeFloorTime; //Tweening function
            yield return true;
            i++;
        }
        floor_CG.alpha = Dir * targetAlpha;
        StopCoroutine("TweenFloorAlpha");
    }

    public void Toggle_Show_Grid()
    {
        if (bGrid_isShown)
        {
            Hide_Grid();
        }
        else
        {
            Show_Grid();
        }
    }

    private void Hide_Grid()
    {
        bGrid_isShown = false;
        Platonic_Butt_3d_Group.SetActive(false);
        StopCoroutine("TweenGridAlpha");
        StartCoroutine("TweenGridAlpha", bGrid_isShown);
    }

    private void Show_Grid()
    {
        bGrid_isShown = true;
        Platonic_Butt_3d_Group.SetActive(true);
        StopCoroutine("TweenGridAlpha");
        StartCoroutine("TweenGridAlpha", bGrid_isShown);
    }

    private IEnumerator TweenGridAlpha(bool show)
    {
        float targetAlpha = 0;
        float Dir = -1;
        if (show)
        {
            targetAlpha = 1;
            Dir = 1;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && grid_CG.alpha < targetAlpha) || (!show && grid_CG.alpha > targetAlpha)))
        {
            grid_CG.alpha += Dir * Time.deltaTime * fadeGridTime; //Tweening function
            AxesParent.setAlpha(grid_CG.alpha);
            yield return true;
            i++;
        }
        grid_CG.alpha = Dir * targetAlpha;
        StopCoroutine("TweenGridAlpha");
    }

    private void Set_UI_Transform()
    {
        Transform hmd_trans = hmd_Track.get_HMD_Transform();

        if (hmd_trans != null)
        {
            Vector3 fwd = hmd_trans.forward;
            fwd.y = 0f;

            fwd.Normalize();

            Quaternion q = Quaternion.LookRotation(fwd, Vector3.up);

            transform.rotation = q;
            transform.transform.position = hmd_trans.position + fwd * ui_dist + Vector3.up*ui_vert_offset;
        }
    }

    public void ShowMenu()
    {
        Set_UI_Transform();
        isShown = true;
        StopCoroutine("TweenMenuScale");
        StartCoroutine("TweenMenuScale", isShown);
    }

    public void HideMenu()
    {
        isShown = false;
        StopCoroutine("TweenMenuScale");
        StartCoroutine("TweenMenuScale", isShown);
    }

    //Simple tweening for menu, scales linearly from 0 to 1 and 1 to 0
    private IEnumerator TweenMenuScale(bool show)
    {
        float targetScale = 0;
        Vector3 Dir = -1 * Vector3.one;
        if (show)
        {
            targetScale = 1;
            Dir = Vector3.one;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && transform.localScale.x < targetScale) || (!show && transform.localScale.x > targetScale)))
        {
            transform.localScale += Dir * Time.deltaTime * 2f; //Tweening function - currently 0.25 second linear
            yield return true;
            i++;
        }
        transform.localScale = Dir * targetScale;
        StopCoroutine("TweenMenuScale");
    }

    //PLATONICS
    public void TogglePlatonics()
    {
        platonics_On = !platonics_On;
        if(platonics_On)
        {
            ShowPlatonics();
        }
        else
        {
            HidePlatonics();
        }
    }

    public void ShowPlatonics()
    {
        StopCoroutine("TweenPlatonicScale");
        StartCoroutine("TweenPlatonicScale", platonics_On);
    }

    public void HidePlatonics()
    {
        StopCoroutine("TweenPlatonicScale");
        StartCoroutine("TweenPlatonicScale", platonics_On);
    }

    //Simple tweening for menu, scales linearly from 0 to 1 and 1 to 0
    private IEnumerator TweenPlatonicScale(bool show)
    {
        float targetScale = 0;
        platonics_tweening = true;
        Vector3 Dir = -1 * Vector3.one;
        if (show)
        {
            targetScale = 1;
            Dir = Vector3.one;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && basePlatonicSolids_GO.transform.localScale.x < targetScale) || (!show && basePlatonicSolids_GO.transform.localScale.x > targetScale)))
        {
            basePlatonicSolids_GO.transform.localScale += Dir * Time.deltaTime * 2f; //Tweening function - currently 0.25 second linear
            yield return true;
            i++;
        }
        basePlatonicSolids_GO.transform.localScale = Dir * targetScale;
        platonics_tweening = false;
        StopCoroutine("TweenPlatonicScale");
    }

    //Grabbables

    public void ToggleGrabbables()
    {
        grabbables_On = !grabbables_On;
        if (grabbables_On)
        {
            ShowGrabbable();
        }
        else
        {
            HideGrabbable();
        }
    }

    public void ShowGrabbable()
    {
        StopCoroutine("TweenGrabbableScale");
        StartCoroutine("TweenGrabbableScale", grabbables_On);
    }

    public void HideGrabbable()
    {
        StopCoroutine("TweenGrabbableScale");
        StartCoroutine("TweenGrabbableScale", grabbables_On);
    }

    //Simple tweening for menu, scales linearly from 0 to 1 and 1 to 0
    private IEnumerator TweenGrabbableScale(bool show)
    {
        float targetScale = 0;
        grabbables_tweening = true;
        Vector3 Dir = -1 * Vector3.one;
        if (show)
        {
            targetScale = 1;
            Dir = Vector3.one;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && baseGrabbables_GO.transform.localScale.x < targetScale) || (!show && baseGrabbables_GO.transform.localScale.x > targetScale)))
        {
            baseGrabbables_GO.transform.localScale += Dir * Time.deltaTime * 2f; //Tweening function - currently 0.25 second linear
            yield return true;
            i++;
        }
        baseGrabbables_GO.transform.localScale = Dir * targetScale;
        grabbables_tweening = false;
        StopCoroutine("TweenGrabbableScale");
    }
}

ghjmjhgm