Example #1
0
        void OnSceneGUI()
        {
            //if (Event.current != null)
            //{
            //    Event e = Event.current;
            //    if (e.isMouse && e.type == EventType.mouseDown && e.button == 0)
            //        _GridPoints.SelectedIndex = CheckSelectedIndexByMouse(e.mousePosition);
            //}

            if (_CurvePoints != null && _Path != null)
            {
                Handles.color = _Path.Color;
                if (_RenderPoints != null)
                {
                    Handles.DrawAAPolyLine(3, _RenderPoints);
                }

                if (_LabelStyle == null)
                {
                    _LabelStyle           = new GUIStyle(EditorStyles.largeLabel);
                    _LabelStyle.fontSize  = 12;
                    _LabelStyle.fontStyle = FontStyle.Bold;
                }

                if (_Path.ShowPoints)
                {
                    _LabelStyle.normal.textColor = _Path.Color;
                    for (int i = 1; i < _CurvePoints.Length - 1; i++)
                    {
                        Vector3 point      = _CurvePoints[i];
                        float   handleSize = HandleUtility.GetHandleSize(point);
                        point.y += _Path.PointRadius + handleSize * 0.25f;
                        Handles.Label(point, (i - 1).ToString(), _LabelStyle);
                    }
                }
            }

            int index = _Path.SelectedIndex;

            if (index >= 0)
            {
                bool    changed = _Path.transform.hasChanged;
                Vector3 point   = _Path.Keys[index];
                if (!_Path.UseWorldSpace)
                {
                    point = _Path.transform.TransformPoint(point);
                }
                Vector3 newPos = Handles.PositionHandle(point, Quaternion.identity);
                if (point != newPos)
                {
                    if (!_Path.UseWorldSpace)
                    {
                        newPos = _Path.transform.InverseTransformPoint(newPos);
                    }
                    _VFValue.Value = _Path.Keys[index] = newPos;
                    changed        = true;
                }

                if (changed)
                {
                    EditorUtility.SetDirty(_Path);
                    RebuildPath();
                    ReloadProperties();
                }
            }

            // ********************************* Simulation *************************************

            if (_SimulationStarted)
            {
                if (_CurvePoints != null)
                {
                    _SimulationCurrectTime += _SimulationSpeed / 120.0f;
                    if (_SimulationCurrectTime >= _Path.TimeLength)
                    {
                        StopSimulation();
                    }
                    else
                    {
                        Vector3 pos = CRSpline3D.Interpolate(_CurvePoints, _Path.ConvertToInterpolationTime(_SimulationCurrectTime));
                        Handles.color = Color.red;
                        Handles.SphereCap(0, pos, Quaternion.identity, HandleUtility.GetHandleSize(pos) * 0.2f);
                        SceneView.focusedWindow.Repaint();
                    }
                }
            }

            // ********************************* GUI *************************************
            Handles.BeginGUI();
            GUILayout.BeginArea(new Rect(Screen.width - 200, Screen.height - 90, 190, 120));

            GUILayout.BeginVertical();
            if (_SimulationStarted)
            {
                if (GUILayout.Button("Stop Simulation"))
                {
                    StopSimulation();
                }
            }
            else
            {
                if (GUILayout.Button("Start Simulation"))
                {
                    StartSimulation();
                }
            }

            GUILayout.BeginHorizontal();
            GUILayout.Label(string.Format("Speed : {0:F2}", _SimulationSpeed), GUILayout.MaxWidth(80));
            _SimulationSpeed = GUILayout.HorizontalSlider(_SimulationSpeed, 0.1f, 2.0f);
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();


            GUILayout.EndArea();
            Handles.EndGUI();
        }