public EyeExpressionState(Eye eyeType, EyeTracker eyeTracker)
 {
     this.Eye         = eyeType;
     this.PupilCenter = new Vector2(eyeTracker.GetEyeExpression(this.Eye, EyeExpression.PupilCenterX), eyeTracker.GetEyeExpression(this.Eye, EyeExpression.PupilCenterY));
     this.Openness    = eyeTracker.GetEyeExpression(this.Eye, EyeExpression.Openness);
     this.Blink       = (eyeTracker.GetEyeExpression(this.Eye, EyeExpression.Blink) != 0.0f);
 }
        public EyeState(Eye eyeType, EyeTracker eyeTracker)
        {
            this.Eye                    = eyeType;
            this.Gaze                   = new Vector2(eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeX), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeY));
            this.GazeRaw                = new Vector2(eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeRawX), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeRawY));
            this.GazeSmooth             = new Vector2(eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeSmoothX), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeSmoothY));
            this.GazeOrigin             = new Vector3(eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeOriginX), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeOriginY), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeOriginZ));
            this.GazeDirection          = new Vector3(eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeDirectionX), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeDirectionY), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeDirectionZ));
            this.GazeReliability        = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.GazeReliability);
            this.PupilDistance          = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilDistance);
            this.PupilMajorDiameter     = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilMajorDiameter);
            this.PupilMajorUnitDiameter = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilMajorUnitDiameter);
            this.PupilMinorDiameter     = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilMinorDiameter);
            this.PupilMinorUnitDiameter = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilMinorUnitDiameter);
            this.Blink                  = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.Blink);
            this.UpperEyelid            = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.UpperEyelid);
            this.LowerEyelid            = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.LowerEyelid);
            this.Openness               = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.Openness);
            this.PupilCenter            = new Vector2(eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilCenterX), eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilCenterY));
            this.Expression             = new EyeExpressionState(eyeType, eyeTracker);

            // Convert range from 0...1 to -1...1, defaulting eyes to center (0) when unavailable
            //float x = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilCenterX);
            //float y = eyeTracker.GetEyeParameter(this.Eye, EyeParameter.PupilCenterY);
            //this.PupilCenter = new Vector2(x <= float.Epsilon ? 0.0f : (x * 2.0f - 1.0f), y <= float.Epsilon ? 0.0f : (y * 2.0f - 1.0f));
            //this.Openness = (x <= float.Epsilon && y <= float.Epsilon) ? 0.0f : 1.0f;
        }
        public override void OnInspectorGUI()
        {
            base.DrawDefaultInspector();

            eyeTracker = ((PimaxEyeTracker)target).EyeTracker;
            if (eyeTracker == null)
            {
                return;
            }

            if (eyeTracker.Active)
            {
                Action DrawEyePreview = () => {
                    var eyePreviewSize        = 200.0f;
                    var eyeBlinkPreviewHeight = 50.0f;
                    var eyeSize = 20.0f;

                    GUILayout.BeginHorizontal();
                    Rect rtBackgroundLeft = GUILayoutUtility.GetRect(GUIContent.none, GUI.skin.box, GUILayout.MinWidth(eyePreviewSize + eyeSize), GUILayout.MinHeight(eyePreviewSize + eyeSize));
                    GUILayout.Space(80);
                    Rect rtBackgroundRight = GUILayoutUtility.GetRect(GUIContent.none, GUI.skin.box, GUILayout.MinWidth(eyePreviewSize + eyeSize), GUILayout.MinHeight(eyePreviewSize + eyeSize));
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();
                    Rect rtBlinkBackgroundLeft = GUILayoutUtility.GetRect(GUIContent.none, GUI.skin.box, GUILayout.MinWidth(eyePreviewSize + eyeSize), GUILayout.MinHeight(eyeBlinkPreviewHeight));
                    GUILayout.Space(80);
                    Rect rtBlinkBackgroundRight = GUILayoutUtility.GetRect(GUIContent.none, GUI.skin.box, GUILayout.MinWidth(eyePreviewSize + eyeSize), GUILayout.MinHeight(eyeBlinkPreviewHeight));
                    GUILayout.EndHorizontal();

                    Rect rtEyeLeft  = new Rect(rtBackgroundLeft.x + (Mathf.InverseLerp(-1.0f, 1.0f, eyeTracker.LeftEye.Expression.PupilCenter.x) * eyePreviewSize), rtBackgroundLeft.y + (Mathf.InverseLerp(-1.0f, 1.0f, -eyeTracker.LeftEye.Expression.PupilCenter.y) * eyePreviewSize), 20, 20);
                    Rect rtEyeRight = new Rect(rtBackgroundRight.x + (Mathf.InverseLerp(-1.0f, 1.0f, eyeTracker.RightEye.Expression.PupilCenter.x) * eyePreviewSize), rtBackgroundRight.y + (Mathf.InverseLerp(-1.0f, 1.0f, -eyeTracker.RightEye.Expression.PupilCenter.y) * eyePreviewSize), 20, 20);

                    //Rect rtEyeLeft = new Rect(rtBackgroundLeft.x + (eyeTracker.LeftEye.PupilCenter.x * eyePreviewSize), rtBackgroundLeft.y + (eyeTracker.LeftEye.PupilCenter.y * eyePreviewSize), 20, 20);
                    //Rect rtEyeRight = new Rect(rtBackgroundRight.x + (eyeTracker.RightEye.PupilCenter.x * eyePreviewSize), rtBackgroundRight.y + (eyeTracker.RightEye.PupilCenter.y * eyePreviewSize), 20, 20);

                    var oldColor = GUI.backgroundColor;
                    GUI.backgroundColor = Color.white;

                    GUI.Box(rtBackgroundLeft, "");
                    GUI.Box(rtBackgroundRight, "");

                    GUI.backgroundColor = Color.red;

                    GUI.Box(rtEyeLeft, "");
                    GUI.Box(rtEyeRight, "");

                    if (eyeTracker.LeftEye.Expression.Openness <= 0.2f)
                    {
                        GUI.backgroundColor = Color.green;
                    }
                    else if (eyeTracker.LeftEye.Expression.Openness >= 0.8f)
                    {
                        GUI.backgroundColor = Color.grey;
                    }
                    else
                    {
                        GUI.backgroundColor = Color.yellow;
                    }

                    GUI.Box(rtBlinkBackgroundLeft, "");

                    if (eyeTracker.RightEye.Expression.Openness <= 0.2f)
                    {
                        GUI.backgroundColor = Color.green;
                    }
                    else if (eyeTracker.RightEye.Expression.Openness >= 0.8f)
                    {
                        GUI.backgroundColor = Color.grey;
                    }
                    else
                    {
                        GUI.backgroundColor = Color.yellow;
                    }

                    GUI.Box(rtBlinkBackgroundRight, "");

                    GUI.backgroundColor = oldColor;
                };

                if (GUILayout.Button("Stop Eye Tracking"))
                {
                    eyeTracker.Stop();
                }

                EditorGUILayout.LabelField("Eye Tracking Preview", EditorStyles.boldLabel);
                DrawEyePreview();
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Expression Metrics", EditorStyles.boldLabel);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Center", EditorStyles.boldLabel);
                EditorGUILayout.Vector2Field("Left", eyeTracker.LeftEye.Expression.PupilCenter);
                EditorGUILayout.Vector2Field("Right", eyeTracker.RightEye.Expression.PupilCenter);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Blink", EditorStyles.boldLabel);
                EditorGUILayout.Toggle("Left", eyeTracker.LeftEye.Expression.Blink);
                EditorGUILayout.Toggle("Right", eyeTracker.RightEye.Expression.Blink);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Openness", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.Expression.Openness);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.Expression.Openness);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Raw Metrics", EditorStyles.boldLabel);
                EditorGUILayout.Space();

                EditorGUILayout.TextField("Timestamp", eyeTracker.Timestamp.ToString());
                EditorGUILayout.TextField("Recommended Eye", eyeTracker.RecommendedEye.Eye.ToString());
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Gaze", EditorStyles.boldLabel);
                EditorGUILayout.Vector2Field("Left", eyeTracker.LeftEye.Gaze);
                EditorGUILayout.Vector2Field("Right", eyeTracker.RightEye.Gaze);
                EditorGUILayout.Vector2Field("Recommended", eyeTracker.RecommendedEye.Gaze);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Gaze (Raw)", EditorStyles.boldLabel);
                EditorGUILayout.Vector2Field("Left", eyeTracker.LeftEye.GazeRaw);
                EditorGUILayout.Vector2Field("Right", eyeTracker.RightEye.GazeRaw);
                EditorGUILayout.Vector2Field("Recommended", eyeTracker.RecommendedEye.GazeRaw);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Gaze (Smooth)", EditorStyles.boldLabel);
                EditorGUILayout.Vector2Field("Left", eyeTracker.LeftEye.GazeSmooth);
                EditorGUILayout.Vector2Field("Right", eyeTracker.RightEye.GazeSmooth);
                EditorGUILayout.Vector2Field("Recommended", eyeTracker.RecommendedEye.GazeSmooth);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Gaze Origin", EditorStyles.boldLabel);
                EditorGUILayout.Vector3Field("Left", eyeTracker.LeftEye.GazeOrigin);
                EditorGUILayout.Vector3Field("Right", eyeTracker.RightEye.GazeOrigin);
                EditorGUILayout.Vector3Field("Recommended", eyeTracker.RecommendedEye.GazeSmooth);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Gaze Direction", EditorStyles.boldLabel);
                EditorGUILayout.Vector3Field("Left", eyeTracker.LeftEye.GazeDirection);
                EditorGUILayout.Vector3Field("Right", eyeTracker.RightEye.GazeDirection);
                EditorGUILayout.Vector3Field("Recommended", eyeTracker.RecommendedEye.GazeDirection);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Gaze Reliability", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.GazeReliability);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.GazeReliability);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.GazeReliability);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Center", EditorStyles.boldLabel);
                EditorGUILayout.Vector2Field("Left", eyeTracker.LeftEye.PupilCenter);
                EditorGUILayout.Vector2Field("Right", eyeTracker.RightEye.PupilCenter);
                EditorGUILayout.Vector2Field("Recommended", eyeTracker.RecommendedEye.PupilCenter);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Distance", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.PupilDistance);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.PupilDistance);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.PupilDistance);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Diameter (Major)", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.PupilMajorDiameter);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.PupilMajorDiameter);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.PupilMajorDiameter);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Unit Diameter (Major)", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.PupilMajorUnitDiameter);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.PupilMajorUnitDiameter);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.PupilMajorUnitDiameter);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Diameter (Minor)", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.PupilMinorDiameter);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.PupilMinorDiameter);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.PupilMinorDiameter);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Pupil Unit Diameter (Minor)", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.PupilMinorUnitDiameter);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.PupilMinorUnitDiameter);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.PupilMinorUnitDiameter);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Blink", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.Blink);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.Blink);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.Blink);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Openness", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.Openness);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.Openness);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.Openness);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Upper Eyelid", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.UpperEyelid);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.UpperEyelid);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.UpperEyelid);
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Lower Eyelid", EditorStyles.boldLabel);
                EditorGUILayout.FloatField("Left", eyeTracker.LeftEye.LowerEyelid);
                EditorGUILayout.FloatField("Right", eyeTracker.RightEye.LowerEyelid);
                EditorGUILayout.FloatField("Recommended", eyeTracker.RecommendedEye.LowerEyelid);
            }
            else
            {
                if (GUILayout.Button("Start Eye Tracking"))
                {
                    eyeTracker.Start();
                }
            }
        }
 void Awake()
 {
     EyeTracker = new EyeTracker();
 }