Exemple #1
0
            public static void OnSceneGui(Plane[] frustum, BGCurve curve, BGCurveSettings settings, BGCurveEditorPointsSelection editorSelection)
            {
                Array.Resize(ref visiblePoints, curve.PointsCount);
                curve.ForEach((point, i, count) => visiblePoints[i] = GeometryUtility.TestPlanesAABB(frustum, new Bounds(point.PositionWorld, Vector3.one)));


                var fieldsCount       = curve.FieldsCount;
                var fields            = curve.Fields;
                var showPointsNumbers = settings.ShowLabels;

                var fieldsWithHandlesCount = 0;
                var fieldsWithLabelCount   = 0;

                if (fieldsCount > 0)
                {
                    fieldsWithHandlesCount = fields.Count(FieldWithHandlesPredicate);
                    if (fieldsWithHandlesCount > 0)
                    {
                        Array.Resize(ref handlesColor, fieldsWithHandlesCount);
                        var cursor = 0;
                        for (var i = 0; i < fieldsCount; i++)
                        {
                            var f = fields[i];
                            if (!FieldWithHandlesPredicate(f))
                            {
                                continue;
                            }

                            if (FieldWithLabelPredicate(f))
                            {
                                fieldsWithLabelCount++;
                            }
                            handlesColor[cursor++] = BGPrivateField.GetHandlesColor(f);
                        }
                    }
                }

                // nothing to show
                if (!showPointsNumbers && fieldsWithHandlesCount == 0)
                {
                    return;
                }


                if (fieldsWithHandlesCount > 0)
                {
                    //not a label
                    curve.ForEach((point, i, length) =>
                    {
                        if (!visiblePoints[i] || !settings.RestrictGizmozSettings.IsShowing(i))
                        {
                            return;
                        }

                        var pos = point.PositionWorld;

                        var quanterionShown = false;
                        var fieldCursor     = 0;
                        for (var j = 0; j < fields.Length; j++)
                        {
                            var field       = fields[j];
                            var handlesType = (HandlesType)BGPrivateField.GetHandlesType(field);

                            if (handlesType == 0)
                            {
                                continue;
                            }

                            if (handlesType == HandlesType.Label)
                            {
                                fieldCursor++;
                                continue;
                            }

                            var color = handlesColor[fieldCursor++];
                            switch (handlesType)
                            {
                            case HandlesType.DistanceFromPoint:
                                BGEditorUtility.SwapHandlesColor(color, () =>
                                {
#if UNITY_5_6_OR_NEWER
                                    Handles.CircleHandleCap(0, pos, Quaternion.LookRotation(SceneView.currentDrawingSceneView.camera.transform.position - pos),
                                                            point.GetField <float>(field.FieldName), EventType.Repaint);
#else
                                    Handles.CircleCap(0, pos, Quaternion.LookRotation(SceneView.currentDrawingSceneView.camera.transform.position - pos),
                                                      point.GetField <float>(field.FieldName));
#endif
                                }
                                                                 );
                                break;

                            case HandlesType.BoundsAroundPoint:
                                Bounds bounds;
                                switch (field.Type)
                                {
                                case BGCurvePointField.TypeEnum.Bounds:
                                    bounds        = point.GetField <Bounds>(field.FieldName);
                                    bounds.center = pos;
                                    break;

                                default:
                                    //vector3
                                    var vector3 = point.GetField <Vector3>(field.FieldName);
                                    bounds      = new Bounds(pos, vector3);
                                    break;
                                }
                                BGEditorUtility.DrawBound(bounds, new Color(color.r, color.g, color.b, 0.05f), color);
                                break;

                            case HandlesType.Bounds:
                                var boundsValue = point.GetField <Bounds>(field.FieldName);
                                if (boundsValue.extents != Vector3.zero)
                                {
                                    BGEditorUtility.DrawBound(boundsValue, new Color(color.r, color.g, color.b, 0.05f), color);
                                    BGEditorUtility.SwapHandlesColor(color, () => Handles.DrawDottedLine(boundsValue.center, pos, 4));
                                }
                                break;

                            case HandlesType.Direction:
                                var vector3Value = point.GetField <Vector3>(field.FieldName);
                                if (vector3Value != Vector3.zero)
                                {
                                    BGEditorUtility.SwapHandlesColor(color, () =>
                                    {
#if UNITY_5_6_OR_NEWER
                                        Handles.ArrowHandleCap(0, pos, Quaternion.LookRotation(vector3Value),
                                                               vector3Value.magnitude, EventType.Repaint);
#else
                                        Handles.ArrowCap(0, pos, Quaternion.LookRotation(vector3Value), vector3Value.magnitude);
#endif
                                    });
                                }
                                break;

                            case HandlesType.Rotation:
                                if (quanterionShown)
                                {
                                    break;
                                }

                                quanterionShown = true;

                                var quaternionValue = point.GetField <Quaternion>(field.FieldName);
                                if (quaternionValue.x < BGCurve.Epsilon && quaternionValue.y < BGCurve.Epsilon && quaternionValue.z < BGCurve.Epsilon && quaternionValue.w < BGCurve.Epsilon)
                                {
                                    quaternionValue = Quaternion.identity;
                                }

                                var newValue = Handles.RotationHandle(quaternionValue, pos);
                                point.SetField(field.FieldName, newValue);


                                BGEditorUtility.SwapHandlesColor(color, () =>
                                {
                                    var rotated = newValue * Vector3.forward * BGEditorUtility.GetHandleSize(pos, 2);
                                    var toPos   = pos + rotated;
#if UNITY_5_6_OR_NEWER
                                    Handles.ArrowHandleCap(0, toPos, newValue, 1, EventType.Repaint);
#else
                                    Handles.ArrowCap(0, toPos, newValue, 1);
#endif
                                    Handles.DrawDottedLine(pos, toPos, 10);
                                });
                                break;

                            case HandlesType.Link:
                                switch (field.Type)
                                {
                                case BGCurvePointField.TypeEnum.GameObject:
                                    var go = point.GetField <GameObject>(field.FieldName);
                                    if (go != null)
                                    {
                                        BGEditorUtility.SwapHandlesColor(color, () => Handles.DrawDottedLine(go.transform.position, pos, 4));
                                    }
                                    break;

                                case BGCurvePointField.TypeEnum.BGCurve:
                                    var bgCurve = point.GetField <BGCurve>(field.FieldName);
                                    if (bgCurve != null)
                                    {
                                        BGEditorUtility.SwapHandlesColor(color, () => Handles.DrawDottedLine(bgCurve.transform.position, pos, 4));
                                    }
                                    break;

                                case BGCurvePointField.TypeEnum.BGCurvePointComponent:
                                    var pointComponent = point.GetField <BGCurvePointComponent>(field.FieldName);
                                    if (pointComponent != null)
                                    {
                                        BGEditorUtility.SwapHandlesColor(color, () => Handles.DrawDottedLine(pointComponent.PositionWorld, pos, 4));
                                    }
                                    break;

                                case BGCurvePointField.TypeEnum.BGCurvePointGO:
                                    var pointGO = point.GetField <BGCurvePointGO>(field.FieldName);
                                    if (pointGO != null)
                                    {
                                        BGEditorUtility.SwapHandlesColor(color, () => Handles.DrawDottedLine(pointGO.PositionWorld, pos, 4));
                                    }
                                    break;
                                }
                                break;
                            }
                        }
                    });
                }

                // nothing more to show
                if (!showPointsNumbers && fieldsWithLabelCount == 0)
                {
                    return;
                }

                //=============================== Labels

                //styles
                var labelColor    = settings.LabelColor;
                var selectedColor = settings.LabelColorSelected;
                var backColor     = BGCurveSettingsForEditor.I.Get <Color32>(BGCurveSettingsForEditor.ColorForLabelBackgroundKey);

                if (labelStyle == null || labelStyle.normal.textColor != labelColor || labelStyle.normal.background == null ||
                    latestLabelBackColor.r != backColor.r || latestLabelBackColor.g != backColor.g || latestLabelBackColor.b != backColor.b || latestLabelBackColor.a != backColor.a)
                {
                    latestLabelBackColor = backColor;
                    labelStyle           = new GUIStyle("Label")
                    {
                        richText = true,
                        border   = new RectOffset(2, 2, 2, 2),
                        clipping = TextClipping.Overflow,
                        wordWrap = false,
                        normal   =
                        {
                            background = BGEditorUtility.TextureWithBorder(8, 1, backColor, new Color32(backColor.r, backColor.g, backColor.b, 255)),
                            textColor  = labelColor
                        }
                    };

                    selectedlabelStyle = new GUIStyle(labelStyle)
                    {
                        normal =
                        {
                            background = BGEditorUtility.TextureWithBorder(8, 1, new Color(selectedColor.r, selectedColor.g, selectedColor.b, .1f), selectedColor),
                            textColor  = selectedColor
                        }
                    };
                }


                curve.ForEach((point, i, length) =>
                {
                    if (!visiblePoints[i])
                    {
                        return;
                    }

                    var pos = point.PositionWorld;

                    var style = !editorSelection.Contains(point) ? labelStyle : selectedlabelStyle;
                    var text  = "";

                    //point numbers and pos
                    if (showPointsNumbers)
                    {
                        text += "# : " + i + "\r\n";
                        if (settings.ShowPositions)
                        {
                            text += "P : " + pos + "\r\n";
                        }
                    }

                    //fields
                    if (fieldsWithLabelCount > 0)
                    {
                        for (var j = 0; j < fieldsCount; j++)
                        {
                            var field = fields[j];
                            if (!FieldWithHandlesPredicate(field) || !FieldWithLabelPredicate(field))
                            {
                                continue;
                            }

                            text += BGEditorUtility.ColorIt(field.FieldName + " : " + point.GetField(field.FieldName, BGCurvePoint.FieldTypes.GetType(field.Type)),
                                                            BGEditorUtility.ToHex(BGPrivateField.GetHandlesColor(field))) + "\r\n";
                        }
                    }

                    var normalized  = (SceneView.currentDrawingSceneView.camera.transform.position - pos).normalized;
                    var handleSize  = BGEditorUtility.GetHandleSize(pos, .25f);
                    var shiftLeft   = -Vector3.Cross(normalized, Vector3.up);
                    var shiftBottom = Vector3.Cross(normalized, Vector3.right) * .3f;
                    Handles.Label(pos + handleSize * shiftLeft + handleSize * shiftBottom, text.Substring(0, text.Length - 2), style);
                });
            }