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 Color runtimeCol; private VRTK_InteractableObject interact; 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; void Start () { interact = handle.GetComponent<VRTK_InteractableObject>(); interact.InteractableObjectTouched += new InteractableObjectEventHandler(DoSliderTouched); interact.InteractableObjectTouched += new InteractableObjectEventHandler(DoSliderUnTouched); 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) { runtimeCol = c; GetComponent<Renderer>().material.SetColor("_TintColor", c); } public GameObject getMySliderHandle() { return handle; } private void DoSliderTouched(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>().SliderTouchedHaptic(0.7f); handle.GetComponent<Renderer>().material = handle_in_Use_Material; } } public void Do_Slider_Pointed_To() { } private void DoSliderUnTouched(object sender, InteractableObjectEventArgs e) { //StopCoroutine(WaitToUnTouch()); //StartCoroutine(WaitToUnTouch()); } 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(); } 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; 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; } 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>().SliderFinishedEditing(0.7f); StartCoroutine(Blink_for_Done()); } Vector3 vLeft_to_Tip = vCtrlTipPos - left_Marker.transform.position; Vector3 vTrnLen = right_Marker.transform.position - left_Marker.transform.position; Vector3 vnTrnLen = Vu_Normalize(vTrnLen); Vector3 vnLeft_to_Tip = Vu_Normalize(vLeft_to_Tip); float sld_Trnlen = Vu_Length(vTrnLen); //Transform[] tList = new Transform[10]; //int count = 0; //Transform t = transform; //tList[0] = t; //count++; //while(t.parent!=null) //{ // tList[count] = t.parent; // t = t.parent; // count++; //} //Vector3 v = vLeft_to_Tip; //for (int i = count-1; 1 >-1; i--) //{ // v = tList[i].InverseTransformDirection(v); //} //Vector3 vLeft_to_Tip_Trans = transform.InverseTransformDirection(vLeft_to_Tip); float mag = Vu_Dot(vnTrnLen, vLeft_to_Tip);//may have to transform here mag = Mathf.Clamp(mag, 0, sld_Trnlen); handle.transform.position = Vector3.Lerp(handle.transform.position, left_Marker.transform.position + vnTrnLen * mag, dampSlidNorm*Time.deltaTime); sld_VAL = mag / sld_Trnlen;// (handle.transform.localPosition - left_Marker.transform.localPosition).magnitude / sld_Length; SetVal_Norm(sld_VAL); if(Target_Slider != null) Target_Slider.value = sld_VAL;//Add TARGET SLIDER Scaling Method Here if(Display_VAL_Txt != null) Display_VAL_Txt.text = sld_VAL.ToString("F4"); } //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(); } }