devNotes 2-8-2017 Sliders – Buttons – Dial in UI

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

public class Slider_VU : MonoBehaviour
{
    //FOUR MODES 
    //WAITING - Normal 
    //TOUCHED - RADIUS OF ACTION - DYNAMIC INDICATOR
    //FINE TUNE - GRIP - TOGGLE DYNAMIC INDICATOR - Runble Controller
    //DISABLED - INDICATE GREYED OUT

    public enum Mode
    {
        NORMAL,
        TOUCHED,
        FINE_TUNE,
        DISABLED,
    }

    public Mode mode;

    public float sld_VAL = 0f;
    public Slider Target_Slider;
    public Text Display_VAL_Txt;

    [Space]

    public Color bar_Norm_col;
    public Color bar_Touching_col;
    public Color bar_FineTune_col;
    public Color bar_Disabled_col;
    [Space]
    public Color handle_Norm_col;
    public Color handle_Touched_1_col;
    public Color handle_Touched_2_col;
    public Color handle_FineTune_1_col;
    public Color handle_FineTune_2_col;
    public Color handle_Disabled_col;
    [Space]

    public Material handle_in_Use_Material;
    private Material handle_base_Material;

    public GameObject cylinder;
    public GameObject handle;	
    public GameObject left_Marker;
    public GameObject right_Marker;
    public GameObject up_Marker;

    public float col_cycleTime = 0.7f;
    public float fresnel_cycleTime = 0.7f;

    private VRTK_InteractableObject interactHandle;
    private int myIDX = 0;
    private VU_UI_MANAGER vu_UI_Manager;

    private ArgosMeshDraft amd;
    private Vector3 vnLen;
    private Vector3 vnWid;
    private float sld_Length;
    private float sld_H_Width;
    private Vector3 vLeft;
    public float dampSlidNorm = 1f;

    public float sld_Fill_Width = 3f;

    private GameObject ctrl_from_e;
    private Vector3 vHandle_to_Tip;

    float col_timer = 0f;
    float fres_timer = 0f;
    Mode lastMode = Mode.NORMAL;
    Color col1_touched;
    Color col2_touched;
    Color col1_fine;
    Color col2_fine;

    bool bSwitchCol = false;
    bool bSwitchFres = false;

    void Start ()
    {
        interactHandle = handle.GetComponent<VRTK_InteractableObject>();
        interactHandle.InteractableObjectTouched += new InteractableObjectEventHandler(DoHandleTouched);
        interactHandle.InteractableObjectUntouched += new InteractableObjectEventHandler(DoHandleUnTouched);

        vnLen = right_Marker.transform.localPosition - left_Marker.transform.localPosition;
        sld_Length = vnLen.magnitude;
        vnLen.Normalize();

        vnWid = up_Marker.transform.localPosition - left_Marker.transform.localPosition;
        sld_H_Width = vnWid.magnitude;
        vnWid.Normalize();

        amd = new ArgosMeshDraft();
        init_Slider();
        setColor(bar_Norm_col);
        GetComponent<MeshFilter>().mesh = amd.ToMeshInternal();

        vu_UI_Manager = VU_UI_MANAGER.Instance;

        myIDX = vu_UI_Manager.register_Slider_VU(this);

        handle_base_Material = handle.GetComponent<Renderer>().material;
    }

    public void setColor(Color c)
    {
        GetComponent<Renderer>().material.SetColor("_TintColor", c);
    }

    public GameObject getMySliderHandle()
    {
        return handle;
    }

    private void DoHandleTouched(object sender, InteractableObjectEventArgs e)
    {
        if (mode != Mode.TOUCHED)
        {
            mode = Mode.TOUCHED;
            ctrl_from_e = e.interactingObject;
            Vector3 vCtrlTipPos = ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().Get_Tip_Pointer_Location();
            ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().SetActiveSlider(this, true);
            ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().SliderTouchedHaptic(0.7f);

            handle.GetComponent<Renderer>().material = handle_in_Use_Material;
        }
    }

    public void Do_Slider_Pointed_To()
    {

    }

    private void DoHandleUnTouched(object sender, InteractableObjectEventArgs e)
    {

    }

    public void DoCylinderTouched_From_Collider_Trigger(GameObject ctrl)
    {
        if(mode != Mode.TOUCHED)
        {

            handle.GetComponent<Renderer>().material = handle_in_Use_Material;
            Vector3 vCtrlTipPos = ctrl.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().Get_Tip_Pointer_Location();
            Vector3 vLM_to_Tip = vCtrlTipPos - left_Marker.transform.position;
            Vector3 vLM_to_RM = right_Marker.transform.position - left_Marker.transform.position;

            Vector3 vN_LM_to_RM = Vu_Normalize(vLM_to_RM);

            float proj = Vu_Dot(vN_LM_to_RM, vLM_to_Tip);

            float slidelen = Vu_Length(vLM_to_RM);

            proj = Mathf.Clamp(proj, 0, slidelen);

            float mag = proj / Vu_Length(vLM_to_RM);

            SetVal_Norm(mag);

            handle.transform.position = left_Marker.transform.position + proj * vN_LM_to_RM;

            if (Target_Slider != null)
                Target_Slider.value = mag;//Add TARGET SLIDER Scaling Method Here
            if (Display_VAL_Txt != null)
                Display_VAL_Txt.text = mag.ToString("F4");

            StartCoroutine(Blink_for_Done());
            ctrl.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().SliderTouchedHaptic(0.7f);
        }
    }

    private void DoCylinderUnTouched(object sender, InteractableObjectEventArgs e)
    {


    }

    private IEnumerator WaitToUnTouch()
    {
        yield return new WaitForSeconds(2f);
        mode = Mode.NORMAL;
    }

    private void init_Slider()
    {
        vLeft = left_Marker.transform.localPosition;
        Vector3 v1 = Vector3.zero;
        Vector3 v2 = Vector3.zero;
        Vector3 v3 = Vector3.zero;
        Vector3 v4 = Vector3.zero;

        Color selColor;

        selColor.r = 1f;
        selColor.g = 1f;
        selColor.b = 1f;
        selColor.a = 1f;

        MeshDraft md;

        v1 = vLeft + sld_H_Width * vnWid * sld_Fill_Width;
        v2 = vLeft + sld_Length * vnLen + sld_H_Width * vnWid * sld_Fill_Width;
        v3 = vLeft + sld_Length * vnLen - sld_H_Width * vnWid * sld_Fill_Width;
        v4 = vLeft - sld_H_Width * vnWid * sld_Fill_Width;

        md = MeshDraft.Quad(v1, v2, v3, v4, selColor);
        amd.Add(md);
        GetComponent<MeshFilter>().mesh = amd.ToMeshInternal();
    }

    private IEnumerator Blink_for_Done()
    {

        float elps = 0f;
        while (elps < 0.5f)
        {
            elps += Time.deltaTime;
            float s = 0.5f*(1f + Mathf.Cos(9f * Mathf.PI * elps));

            Color colorTemp = Color.Lerp(handle_FineTune_1_col, handle_Norm_col, s);
            handle.GetComponent<Renderer>().sharedMaterial.SetColor("_RimColor", colorTemp);

            yield return true;
        }
        handle.GetComponent<Renderer>().material = handle_base_Material;
    }


    public Vector3 Vu_Normalize(Vector3 vToNorm)
    {
        float length =  Mathf.Sqrt(vToNorm.x * vToNorm.x + vToNorm.y * vToNorm.y + vToNorm.z * vToNorm.z);
        return vToNorm / length;
    }

    public float Vu_Length(Vector3 vToNorm)
    {
        return Mathf.Sqrt(vToNorm.x * vToNorm.x + vToNorm.y * vToNorm.y + vToNorm.z * vToNorm.z);
    }

    public float Vu_Dot(Vector3 v1, Vector3 v2)
    {
        return v1.x * v2.x + v1.y * v2.y + v1.z*v2.z;

    }

    public void Set_Fine_Tune_On()
    {
        mode = Mode.FINE_TUNE;
    }

    public void Set_Fine_Tune_Off()
    {
        mode = Mode.NORMAL;
        ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().Haptic_Single(0.7f);
        ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().SetActiveSlider(this, false);
        StartCoroutine(Blink_for_Done());
    }

    int count = 0;
    void Update ()
    {
        if(mode == Mode.TOUCHED)
        {
            Vector3 vCtrlTipPos = ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().Get_Tip_Pointer_Location();
            vHandle_to_Tip = vCtrlTipPos - handle.transform.position;
            float dist = vHandle_to_Tip.magnitude;

            if(dist > sld_Length/2 || ctrl_from_e.GetComponent<VRTK_ControllerEvents>().triggerPressed)
            {
                mode = Mode.NORMAL;
                ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().Haptic_Single(0.7f);
                ctrl_from_e.GetComponent<ArgosVU_VRTK_ControllerPointerEvents_Listener>().SetActiveSlider(this, false);
                StartCoroutine(Blink_for_Done());
            }

            Vector3 vLM_to_Tip = vCtrlTipPos - left_Marker.transform.position;
            Vector3 vLM_to_RM = right_Marker.transform.position - left_Marker.transform.position;

            Vector3 vN_LM_to_RM = Vu_Normalize(vLM_to_RM);

            float proj = Vu_Dot(vN_LM_to_RM, vLM_to_Tip);

            float slidelen = Vu_Length(vLM_to_RM);

            proj = Mathf.Clamp(proj, 0, slidelen);

            float mag = proj / Vu_Length(vLM_to_RM);

            SetVal_Norm(mag);

            handle.transform.position = left_Marker.transform.position + proj * vN_LM_to_RM;

            if(Target_Slider != null)
                Target_Slider.value = mag;//Add TARGET SLIDER Scaling Method Here
            if(Display_VAL_Txt != null)
                Display_VAL_Txt.text = mag.ToString("F4");

        }
        if(mode == Mode.FINE_TUNE)
        {

        }

        //TEST SLIDER MOVEMENT//
        //float val = 0.5f + 0.5f * Mathf.Sin(Time.unscaledTime);
        //SetVal_Norm(val);
        ////////////////////////

        col_timer  += Time.deltaTime;
        fres_timer += Time.deltaTime;

        if (col_timer > col_cycleTime)
        {
            bSwitchCol = !bSwitchCol;

            if(bSwitchCol)
            {
                col1_touched = handle_Touched_1_col;
                col2_touched = handle_Touched_2_col;
                col1_fine = handle_FineTune_1_col;
                col2_fine = handle_FineTune_2_col;
            }
            else
            {
                col1_touched = handle_Touched_2_col;
                col2_touched = handle_Touched_1_col;
                col1_fine = handle_FineTune_2_col;
                col2_fine = handle_FineTune_1_col;
            }
            col_timer = 0.0f;
        }

        if (fres_timer > fresnel_cycleTime)
        {
            bSwitchFres = !bSwitchFres;
            fres_timer = 0.0f;
        }
        if(mode == Mode.NORMAL)
        {
            if (lastMode != Mode.TOUCHED)
            {
                setColor(bar_Norm_col);
                handle.GetComponent<Renderer>().sharedMaterial.SetFloat("_RimFresnel", 1.2f);
                handle.GetComponent<Renderer>().sharedMaterial.SetColor("_RimColor", handle_Norm_col);
            }
        }

        if (mode == Mode.TOUCHED)
        {
            if (lastMode != Mode.TOUCHED)
            {
                setColor(bar_Touching_col);
            }

            Color colorTemp = Color.Lerp(col1_touched, col2_touched, col_timer / col_cycleTime);
            handle.GetComponent<Renderer>().sharedMaterial.SetColor("_RimColor", colorTemp);

            if (bSwitchFres)
            {
                float floatTemp = Mathf.Lerp(0.3f, 3.3f, fres_timer / fresnel_cycleTime);
                handle.GetComponent<Renderer>().sharedMaterial.SetFloat("_RimFresnel", floatTemp);
            }
            else
            {
                float floatTemp = Mathf.Lerp(3.3f, 0.3f, fres_timer / fresnel_cycleTime);
                handle.GetComponent<Renderer>().sharedMaterial.SetFloat("_RimFresnel", floatTemp);
            }
        }
        if (mode == Mode.FINE_TUNE)
        {
            if (lastMode != Mode.FINE_TUNE)
            {
                setColor(bar_FineTune_col);
            }

            Color colorTemp = Color.Lerp(col1_fine, col2_fine, col_timer / col_cycleTime);
            handle.GetComponent<Renderer>().sharedMaterial.SetColor("_RimColor", colorTemp);

            if (bSwitchFres)
            {
                float floatTemp = Mathf.Lerp(0.3f, 3.3f, fres_timer / fresnel_cycleTime);
                handle.GetComponent<Renderer>().sharedMaterial.SetFloat("_RimFresnel", floatTemp);
            }
            else
            {
                float floatTemp = Mathf.Lerp(3.3f, 0.3f, fres_timer / fresnel_cycleTime);
                handle.GetComponent<Renderer>().sharedMaterial.SetFloat("_RimFresnel", floatTemp);
            }
        }
        if (mode == Mode.DISABLED)
        {
            if (lastMode != Mode.DISABLED)
            {
                setColor(bar_Disabled_col);
                handle.GetComponent<Renderer>().sharedMaterial.SetFloat("_RimFresnel", 1.2f);
                handle.GetComponent<Renderer>().sharedMaterial.SetColor("_RimColor", handle_Disabled_col);
            }
        }
        lastMode = mode;
    }


    void SetVal_Norm(float val)
    {
        float len = sld_Length * val;
        Vector3 rightPos = vLeft + len * vnLen;

        amd.vertices[0] = vLeft + sld_H_Width * vnWid * sld_Fill_Width;
        amd.vertices[1] = vLeft + len * vnLen + sld_H_Width * vnWid * sld_Fill_Width;
        amd.vertices[2] = vLeft + len * vnLen - sld_H_Width * vnWid * sld_Fill_Width;
        amd.vertices[3] = vLeft - sld_H_Width * vnWid * sld_Fill_Width;

        GetComponent<MeshFilter>().mesh = amd.ToMeshInternal();
    }
}

dfghghgh4

 

sdfdsfdf-27