Esempio n. 1
0
        public void Draw(Rect position)
        {
            GUI.Label(new Rect(0f, 0f, position.width, position.height),
                      "WorkDir: " + BTEditorManager.SelectAsset.GetFullpath());

            // 缩放
            Rect zoomedPosition = EditorZoomArea.Begin(zoomScale, position);

            // 滚动
            mScrollPoint = GUI.BeginScrollView(new Rect(0f, 0f,
                                                        zoomedPosition.width, zoomedPosition.height), mScrollPoint, mCanvas);

            #region Core logic

            // 输入处理
            HandleMouseEvents(zoomedPosition, BTEditorManager.SelectTree.nodeDic);

            // 绘制背景网格
            if (mGridRenderer == null)
            {
                mGridRenderer = new GridRenderer();
            }
            mGridRenderer.Draw(mCanvas);

            // 绘制节点
            DrawNodes(BTEditorManager.SelectTree.nodeDic);
            if (mCurMode == Mode.ConnectChild || mCurMode == Mode.ConnectParent)
            {
                DrawConnectionLine();
            }

            // 绘制鼠标框选范围
            DrawPickupRect();

            #endregion Core logic

            GUI.EndScrollView();
            EditorZoomArea.End();
        }
Esempio n. 2
0
        // Returns true if needs a repaint
        void HandleMouseEvents(Rect position, Dictionary <int, BTNode> nodeDic)
        {
            Event e = Event.current;

            // MouseDown //
            // Identify the control being clicked
            if (Event.current.type == EventType.MouseDown)
            {
                // Do nothing for MouseDown on the scrollbar, if present
                if (e.mousePosition.x >= position.width + mScrollPoint.x ||
                    e.mousePosition.y >= position.height + mScrollPoint.y)
                {
                    mCurMode = Mode.None;
                }
                // MouseDown in the canvas, check if in a node or on background
                else
                {
                    // Store the mouse position
                    mMouseStartPos = e.mousePosition;

                    // Loop through nodes and check if their rects contain the mouse position
                    BTNode contextNode = null;
                    BTNode tempNode    = null;
                    foreach (var kvp in nodeDic)
                    {
                        tempNode = kvp.Value;
                        if (tempNode != null && mNodeRenderer.rectForNode(tempNode).Contains(mMouseStartPos))
                        {
                            // Connect a parent to a child
                            if (mCurMode == Mode.ConnectChild)
                            {
                                BTEditorManager.Connect(owner.connectOrigin,
                                                        tempNode.GetInstanceID());
                                // editorWindow.wantsMouseMove = false;
                                mCurMode = Mode.None;
                                break;
                            }
                            // Connect a child to a parent
                            else if (mCurMode == Mode.ConnectParent)
                            {
                                BTEditorManager.Connect(tempNode.GetInstanceID(),
                                                        owner.connectOrigin);
                                // editorWindow.wantsMouseMove = false;
                                mCurMode = Mode.None;
                                break;
                            }
                            // Perform a node action at key up
                            else
                            {
                                mCurMode    = Mode.NodeAction;
                                contextNode = tempNode;
                            }
                        }
                    }

                    // Cancel the connection
                    if (mCurMode == Mode.ConnectParent || mCurMode == Mode.ConnectChild)
                    {
                        // editorWindow.wantsMouseMove = false;
                        mCurMode = Mode.None;
                    }

                    // MouseDown on the canvas background enables panning the view
                    if (mCurMode == Mode.None && contextNode == null)
                    {
                        mCurMode = Mode.CanvasAction;
                    }

                    SelectNode(contextNode);
                }

                return;
            }

            // Mouse Up //
            // MouseUp resets the current interaction mode to None
            if (e.type == EventType.MouseUp)
            {
                mPickingup = false;

                // Context Menu
                if (IsRightMouseBtn())
                {
                    if (mCurMode == Mode.NodeAction)
                    {
                        owner.ShowContextMenu(e.mousePosition,
                                              EditorZoomArea.GetScrollOffset(zoomScale, mScrollPoint), mSelectedNodes);
                    }
                    else if (mCurMode == Mode.CanvasAction)
                    {
                        owner.ShowContextMenu(e.mousePosition,
                                              EditorZoomArea.GetScrollOffset(zoomScale, mScrollPoint), null);
                    }
                }
                // Resize canvas after a drag
                else if (mCurMode == Mode.DragNode)
                {
                    BTEditorManager.Dirty();
                }

                mCurMode = Mode.None;

                return;
            }

            // Mouse Drag //
            if (e.type == EventType.MouseDrag)
            {
                if (IsLeftMouseBtn())
                {
                    if (mCurMode == Mode.CanvasAction)
                    {
                        if (e.modifiers == EventModifiers.Alt)
                        {
                            mPickingup      = true;
                            mPickupStartPos = mMouseStartPos;
                        }
                        else if (e.modifiers == EventModifiers.None)
                        {
                            mScrollPoint -= (e.delta / zoomScale);
                        }
                    }
                    // Switch to node dragging mode
                    else if (mCurMode == Mode.NodeAction)
                    {
                        if (mSelectedNodes.Count > 0 && e.modifiers != EventModifiers.Control)
                        {
                            Vector2 mousePos = e.mousePosition;
                            float   deltaX   = Mathf.Abs(mousePos.x - mMouseStartPos.x);
                            float   deltaY   = Mathf.Abs(mousePos.y - mMouseStartPos.y);

                            // Ignore mouse drags inside nodes lesser than the grid step. These would be rounded,
                            // and make selecting a node slightly more difficult.
                            if (deltaX >= GridRenderer.step.x || deltaY >= GridRenderer.step.y)
                            {
                                mCurMode = Mode.DragNode;
                            }
                        }
                    }
                    // Drag a node
                    if (mCurMode == Mode.DragNode)
                    {
                        // 距离Node中心位置的移动距离
                        DragNodes(mSelectedNodes);
                    }
                }

                return;
            }

            // 鼠标移出窗口
            if (e.type == EventType.MouseLeaveWindow)
            {
                if (mPickingup)
                {
                    mPickingup = false;
                }

                return;
            }

            // Zoom
            if (e.type == EventType.ScrollWheel)
            {
                if (e.modifiers == EventModifiers.Alt)
                {
                    Vector2 delta     = e.delta;
                    float   zoomDelta = -delta.y / 150.0f;
                    zoomScale += zoomDelta;
                    zoomScale  = Mathf.Clamp(zoomScale, ZoomMin, ZoomMax);

                    e.Use();
                }

                return;
            }

            // 按键操作
            if (e.type == EventType.KeyDown)
            {
                if (e.control)
                {
                    // 复制
                    if (e.keyCode == KeyCode.C)
                    {
                        BTEditorManager.Copy(mSelectedNodes.ToArray());
                        return;
                    }

                    // 粘贴
                    if (e.keyCode == KeyCode.V)
                    {
                        Vector2 to = BTEditorManager.CopyFromPos;
                        to.x += 100f;
                        BTEditorManager.Paste(to);

                        return;
                    }
                }

                // 删除
                if (e.keyCode == KeyCode.Delete)
                {
                    BTEditorManager.Delete(mSelectedNodes.ToArray());
                    return;
                }
            }
        }