private void HandleNormal(Bounds portalBounds, GridPortalComponent p)
        {
            var grid = GridManager.instance.GetGridComponent(_portalRectStart);

            if (grid == null)
            {
                grid = GridManager.instance.GetGridComponent(_portalRectEnd);
                if (grid == null)
                {
                    return;
                }
            }

            portalBounds = EditorUtilities.SnapToGrid(grid, portalBounds, _gridConnectMode);

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

            if (_shiftDown)
            {
                p.portalTwo = portalBounds;
            }
            else
            {
                p.portalOne = portalBounds;
            }
        }
Пример #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 (!EditorUtilities.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();
                _inPortalMode = false;
                this.Repaint();

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

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

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

                _portalRectStart.y = _portalRectEnd.y = Mathf.Max(_portalRectStart.y, _portalRectEnd.y);

                var grid = GridManager.instance.GetGridComponent(_portalRectStart);
                if (grid == null)
                {
                    grid = GridManager.instance.GetGridComponent(_portalRectEnd);
                    if (grid == null)
                    {
                        return;
                    }
                }

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

                _gridConnectMode = _gridConnectMode || (evt.modifiers & EventModifiers.Control) > 0 || (evt.modifiers & EventModifiers.Command) > 0;
                portalBounds = EditorUtilities.SnapToGrid(grid, portalBounds, _gridConnectMode);

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

                _shiftDown = _shiftDown || (evt.modifiers & EventModifiers.Shift) > 0;
                if (_shiftDown)
                {
                    p.portalTwo = portalBounds;
                }
                else
                {
                    p.portalOne = portalBounds;
                }

                EditorUtility.SetDirty(p);
            }
            else if (evt.type == EventType.Repaint)
            {
                Handles.color = _shiftDown ? p.portalTwoColor : p.portalOneColor;
                var c1 = new Vector3(_portalRectStart.x, _portalRectStart.y, _portalRectEnd.z);
                var c2 = new Vector3(_portalRectEnd.x, _portalRectEnd.y, _portalRectStart.z);
                Handles.DrawDottedLine(_portalRectStart, c1, 10f);
                Handles.DrawDottedLine(c1, _portalRectEnd, 10f);
                Handles.DrawDottedLine(_portalRectEnd, c2, 10f);
                Handles.DrawDottedLine(c2, _portalRectStart, 10f);
            }
        }