private void OnSceneGUI()
        {
            var evt = Event.current;
            var p   = this.target as PatrolPointsComponent;

            if (evt.type == EventType.Repaint)
            {
                if (_emphasize && GUIUtility.hotControl != _id)
                {
                    DrawActiveIndication(p);
                }

                DrawNumberLabels(p);
                return;
            }

            if (!_inPlacementMode)
            {
                return;
            }

            var points     = p.points;
            var groundRect = new Plane(Vector3.up, Vector3.zero);

            if (evt.type == EventType.KeyDown && evt.keyCode == _addKey)
            {
                GUIUtility.hotControl = _id;

                evt.Use();
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == _addKey)
            {
                evt.Use();

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    GUIUtility.hotControl = 0;
                    return;
                }

                if (p.relativeToTransform)
                {
                    point = p.transform.InverseTransformPoint(point);
                }

                int idx = points.Length;

                var tmp = new Vector3[idx + 1];
                Array.Copy(points, tmp, points.Length);
                points   = tmp;
                p.points = tmp;

                points[idx] = point;
                EditorUtility.SetDirty(p);
                GUIUtility.hotControl = 0;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == _removeKey)
            {
                GUIUtility.hotControl = _id;

                evt.Use();
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == _removeKey)
            {
                evt.Use();

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    GUIUtility.hotControl = 0;
                    return;
                }

                var removeIdx  = -1;
                var lineOrigin = Camera.current.transform.position;

                for (int i = 0; i < points.Length; i++)
                {
                    //If Gizmos change so should this
                    var sphereCenter = points[i] + Vector3.up;
                    if (Geometry.DoesLineIntersectSphere(lineOrigin, point, sphereCenter, 0.3f))
                    {
                        removeIdx = i;
                        break;
                    }
                }

                if (removeIdx >= 0)
                {
                    var tmp = new Vector3[points.Length - 1];
                    Array.Copy(points, tmp, removeIdx);
                    Array.Copy(points, removeIdx + 1, tmp, removeIdx, points.Length - (removeIdx + 1));
                    points   = tmp;
                    p.points = tmp;

                    EditorUtility.SetDirty(p);
                }

                GUIUtility.hotControl = 0;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = _id;
                evt.Use();
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = 0;
                evt.Use();
                _inPlacementMode = false;
                this.Repaint();
            }
            else if (evt.control)
            {
                return;
            }
            else if (evt.type == EventType.MouseDown && evt.button == MouseButton.left)
            {
                GUIUtility.hotControl = _id;
                evt.Use();
                _movingIdx = -1;

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    return;
                }

                var lineOrigin = Camera.current.transform.position;

                for (int i = 0; i < points.Length; i++)
                {
                    //If Gizmos change so should this
                    var sphereCenter = points[i] + Vector3.up;
                    if (Geometry.DoesLineIntersectSphere(lineOrigin, point, sphereCenter, 0.3f))
                    {
                        _movingIdx = i;
                        break;
                    }
                }
            }
            else if (evt.type == EventType.MouseUp && evt.button == MouseButton.left)
            {
                GUIUtility.hotControl = 0;
                evt.Use();

                if (_movingIdx >= 0)
                {
                    EditorUtility.SetDirty(p);
                    _movingIdx = -1;
                }
            }
            else if (evt.type == EventType.MouseDrag && evt.button == MouseButton.left && _movingIdx >= 0)
            {
                evt.Use();

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    return;
                }

                points[_movingIdx] = point;
            }
        }
Exemple #2
0
        private void HandlePortalDefinition()
        {
            if (!_inPortalMode)
            {
                return;
            }

            var p          = this.target as GridPortalComponent;
            int id         = GUIUtility.GetControlID(_idHash, FocusType.Passive);
            var groundRect = new Plane(Vector3.up, new Vector3(0f, 0f, 0f));

            var evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0)
            {
                GUIUtility.hotControl = id;

                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _portalRectStart))
                {
                    GUIUtility.hotControl = 0;
                }

                _shiftDown       = (evt.modifiers & EventModifiers.Shift) > 0;
                _gridConnectMode = (evt.modifiers & EventModifiers.Control) > 0 || (evt.modifiers & EventModifiers.Command) > 0;
                _portalRectEnd   = _portalRectStart;
                return;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = id;
                evt.Use();
                return;
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = 0;
                evt.Use();
                ToggleEditMode(false);
                this.Repaint();

                return;
            }
            else if (GUIUtility.hotControl != id)
            {
                if (evt.type == EventType.Repaint)
                {
                    DrawActiveIndication(p);
                }

                return;
            }

            if (evt.type == EventType.MouseDrag)
            {
                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _portalRectEnd))
                {
                    _portalRectEnd = _portalRectStart;
                }
            }
            else if (evt.type == EventType.MouseUp)
            {
                GUIUtility.hotControl = 0;
                evt.Use();

                var startToEnd   = (_portalRectEnd - _portalRectStart);
                var portalBounds = new Bounds(
                    _portalRectStart + (startToEnd * 0.5f),
                    new Vector3(Mathf.Abs(startToEnd.x), Mathf.Max(Mathf.Abs(startToEnd.y), 0.1f), Mathf.Abs(startToEnd.z)));

                _gridConnectMode = _gridConnectMode || (evt.modifiers & EventModifiers.Control) > 0 || (evt.modifiers & EventModifiers.Command) > 0;
                _shiftDown       = _shiftDown || (evt.modifiers & EventModifiers.Shift) > 0;

                if (p.type == PortalType.Connector)
                {
                    HandleConnector(portalBounds, p);
                }
                else
                {
                    HandleNormal(portalBounds, p);
                }

                EditorUtility.SetDirty(p);
            }
            else if (evt.type == EventType.Repaint)
            {
                Handles.color = _shiftDown ? p.portalTwoColor : p.portalOneColor;
                var y  = Mathf.Max(_portalRectStart.y, _portalRectEnd.y);
                var c1 = new Vector3(_portalRectStart.x, y, _portalRectEnd.z);
                var c2 = new Vector3(_portalRectEnd.x, y, _portalRectStart.z);
                Handles.DrawDottedLine(_portalRectStart, c1, 10f);
                Handles.DrawDottedLine(c1, _portalRectEnd, 10f);
                Handles.DrawDottedLine(_portalRectEnd, c2, 10f);
                Handles.DrawDottedLine(c2, _portalRectStart, 10f);
            }
        }
Exemple #3
0
        private void HandleFieldDefinition()
        {
            if (!_inDrawMode)
            {
                return;
            }

            var p          = this.target as SimpleCostField;
            int id         = GUIUtility.GetControlID(_idHash, FocusType.Passive);
            var groundRect = new Plane(Vector3.up, new Vector3(0f, 0f, 0f));

            var evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0)
            {
                GUIUtility.hotControl = id;

                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _boundsRectStart))
                {
                    GUIUtility.hotControl = 0;
                }

                _boundsRectEnd = _boundsRectStart;
                return;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = id;
                evt.Use();
                return;
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = 0;
                evt.Use();
                _inDrawMode = false;
                this.Repaint();

                return;
            }
            else if (GUIUtility.hotControl != id)
            {
                return;
            }

            if (evt.type == EventType.MouseDrag)
            {
                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _boundsRectEnd))
                {
                    _boundsRectEnd = _boundsRectStart;
                }
            }
            else if (evt.type == EventType.MouseUp)
            {
                GUIUtility.hotControl = 0;
                evt.Use();

                _boundsRectStart.y = _boundsRectEnd.y = Mathf.Max(_boundsRectStart.y, _boundsRectEnd.y);

                var grid = GridManager.instance.GetGridComponent(_boundsRectStart);
                if (grid == null)
                {
                    grid = GridManager.instance.GetGridComponent(_boundsRectEnd);
                    if (grid == null)
                    {
                        return;
                    }
                }

                var startToEnd  = (_boundsRectEnd - _boundsRectStart);
                var fieldBounds = new Bounds(
                    _boundsRectStart + (startToEnd * 0.5f),
                    new Vector3(Mathf.Abs(startToEnd.x), Mathf.Abs(startToEnd.y) + 0.1f, Mathf.Abs(startToEnd.z)));

                fieldBounds = EditorUtilitiesInternal.SnapToGrid(grid, fieldBounds, false);

                if (p.relativeToTransform)
                {
                    fieldBounds.center = p.transform.InverseTransformPoint(fieldBounds.center);
                }

                p.bounds = fieldBounds;

                EditorUtility.SetDirty(p);
            }
            else if (evt.type == EventType.Repaint)
            {
                Handles.color = p.gizmoColor;
                var c1 = new Vector3(_boundsRectStart.x, _boundsRectStart.y, _boundsRectEnd.z);
                var c2 = new Vector3(_boundsRectEnd.x, _boundsRectEnd.y, _boundsRectStart.z);
                Handles.DrawDottedLine(_boundsRectStart, c1, 10f);
                Handles.DrawDottedLine(c1, _boundsRectEnd, 10f);
                Handles.DrawDottedLine(_boundsRectEnd, c2, 10f);
                Handles.DrawDottedLine(c2, _boundsRectStart, 10f);
            }
        }