Beispiel #1
0
        //----------------------------------------------------------------------
        public TreeView( Screen _screen )
            : base(_screen)
        {
            Nodes = new ObservableList<TreeViewNode>();

            Nodes.ListCleared += delegate {
                SelectedNode = null;
                HoveredNode = null;
                FocusedNode = null;
                mHoveredActionButton = null;
                mbIsHoveredActionButtonDown = false;
            };

            Nodes.ListChanged += delegate( object _source, ObservableList<TreeViewNode>.ListChangedEventArgs _args )
            {
                if( ! _args.Added )
                {
                    if( _args.Item == SelectedNode )
                    {
                        SelectedNode = null;
                    }

                    if( _args.Item == HoveredNode )
                    {
                        UpdateHoveredNode();
                    }

                    if( _args.Item == FocusedNode )
                    {
                        FocusedNode = null;
                        IsDragging = false;
                    }
                }
            };

            Scrollbar = new UI.Scrollbar( _screen );
            Scrollbar.Parent = this;

            ActionButtons = new List<Button>();
        }
Beispiel #2
0
        internal void UpdateHoveredNode()
        {
            TreeViewNode oldHoveredNode = HoveredNode;
            HoveredNode = null;

            if( mbIsHovered )
            {
                int iNodeY = ( mHoverPoint.Y - ( LayoutRect.Y + 10 ) + (int)Scrollbar.LerpOffset );

                int iOffset = iNodeY % ( NodeHeight + NodeSpacing );

                if( iOffset < ( NodeHeight + NodeSpacing ) / 4 )
                {
                    InsertMode = NodeInsertMode.Before;
                }
                else
                if( iOffset > ( NodeHeight + NodeSpacing ) * 3 / 4 )
                {
                    InsertMode = NodeInsertMode.After;
                }
                else
                {
                    InsertMode = NodeInsertMode.Over;
                }

                int iNodeIndex = iNodeY / ( NodeHeight + NodeSpacing );

                HoveredNode = FindHoveredNode( Nodes, iNodeIndex, 0 );

                if( HoveredNode == null )
                {
                    InsertMode = iNodeY < ( NodeHeight + NodeSpacing ) / 4 ? NodeInsertMode.Before : NodeInsertMode.After;
                }
                else
                if( HoverHandler != null && oldHoveredNode != HoveredNode )
                {
                    HoverHandler( this );
                }

                if( oldHoveredNode != HoveredNode )
                {
                    if( mHoveredActionButton != null )
                    {
                        if( mbIsHoveredActionButtonDown )
                        {
                            mHoveredActionButton.ResetPressState();
                            mbIsHoveredActionButtonDown = false;
                        }
                        mHoveredActionButton = null;
                    }
                }

                if( ! IsDragging && HoveredNode != null )
                {
                    if( mHoveredActionButton != null )
                    {
                        if( mHoveredActionButton.HitTest( mHoverPoint ) != null )
                        {
                            mHoveredActionButton.OnMouseMove( mHoverPoint );
                        }
                        else
                        {
                            mHoveredActionButton.OnMouseOut( mHoverPoint );

                            if( mbIsHoveredActionButtonDown )
                            {
                                mHoveredActionButton.ResetPressState();
                                mbIsHoveredActionButtonDown = false;
                            }

                            mHoveredActionButton = null;
                        }
                    }

                    if( mHoveredActionButton == null )
                    {
                        mbIsHoveredActionButtonDown = false;

                        foreach( Button button in ActionButtons )
                        {
                            if( button.HitTest( mHoverPoint ) != null )
                            {
                                mHoveredActionButton = button;
                                mHoveredActionButton.OnMouseEnter( mHoverPoint );
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if( mbIsHoveredActionButtonDown )
                    {
                        mHoveredActionButton.ResetPressState();
                        mbIsHoveredActionButtonDown = false;
                    }
                    mHoveredActionButton = null;
                }
            }
            else
            if( mHoveredActionButton != null )
            {
                if( mbIsHoveredActionButtonDown )
                {
                    mHoveredActionButton.ResetPressState();
                    mbIsHoveredActionButtonDown = false;
                }
                mHoveredActionButton = null;
            }
        }
Beispiel #3
0
        protected internal override void OnMouseUp( Point _hitPoint, int _iButton )
        {
            if( _iButton != Screen.Game.InputMgr.PrimaryMouseButton ) return;

            mbIsMouseDown = false;

            if( mHoveredActionButton != null )
            {
                if( mbIsHoveredActionButtonDown )
                {
                    mHoveredActionButton.OnMouseUp( _hitPoint, _iButton );
                    mbIsHoveredActionButtonDown = false;
                }
            }
            else
            if( IsDragging )
            {
                Debug.Assert( FocusedNode != null );

                TreeViewNode draggedNode = FocusedNode;
                TreeViewNode currentParentNode = (TreeViewNode)draggedNode.Parent;

                int iIndex = ( HoveredNode != null ) ? HoveredNode.Children.Count : Nodes.Count;
                TreeViewNode targetParentNode = HoveredNode;

                if( HoveredNode != null )
                {
                    switch( InsertMode )
                    {
                        case NodeInsertMode.Before:
                            targetParentNode = (TreeViewNode)HoveredNode.Parent;
                            iIndex = ( targetParentNode != null ) ? targetParentNode.Children.IndexOf( HoveredNode ) : Nodes.IndexOf( HoveredNode );
                            break;
                        case NodeInsertMode.After:
                            if( ! HoveredNode.DisplayAsContainer && HoveredNode.Children.Count == 0 )
                            {
                                targetParentNode = (TreeViewNode)HoveredNode.Parent;
                                iIndex = 1 + ( targetParentNode != null ? targetParentNode.Children.IndexOf( HoveredNode ) : Nodes.IndexOf( HoveredNode ) );
                            }
                            else
                            {
                                iIndex = 0;
                            }
                            break;
                    }
                }

                if( HitBox.Contains( _hitPoint ) && targetParentNode != draggedNode && DragNDropHandler != null )
                {
                    TreeViewNode ancestorNode = targetParentNode;

                    bool bIsCycle = false;
                    while( ancestorNode != null )
                    {
                        if( ancestorNode == draggedNode )
                        {
                            bIsCycle = true;
                            break;
                        }

                        ancestorNode = (TreeViewNode)ancestorNode.Parent;
                    }

                    if( ! bIsCycle )
                    {
                        // Offset index if the node is moving inside the same parent
                        if( targetParentNode == currentParentNode )
                        {
                            if( currentParentNode != null )
                            {
                                if( currentParentNode.Children.IndexOf( draggedNode ) < iIndex )
                                {
                                    iIndex--;
                                }
                            }
                            else
                            {
                                if( Nodes.IndexOf( draggedNode ) < iIndex )
                                {
                                    iIndex--;
                                }
                            }
                        }

                        if( DragNDropHandler( draggedNode, targetParentNode, iIndex ) )
                        {

                            if( draggedNode.Parent != null )
                            {
                                ( (TreeViewNode)(draggedNode.Parent) ).Children.Remove( draggedNode );
                            }
                            else
                            {
                                Nodes.Remove( draggedNode );
                            }

                            if( targetParentNode != null )
                            {
                                targetParentNode.Children.Insert( iIndex, draggedNode );
                            }
                            else
                            {
                                Nodes.Insert( iIndex, draggedNode );
                            }

                            FocusedNode = draggedNode;
                        }
                    }
                }
                else
                {
                    // Drag'n'Drop cancelled
                }

                IsDragging = false;
                mfScrollRepeatTimer = sfScrollRepeatDelay;
            }
            else
            {
                SelectHoveredNode( true );
            }
        }
Beispiel #4
0
        //----------------------------------------------------------------------
        protected internal override void OnPadMove( Direction _direction )
        {
            ObservableList<TreeViewNode> lNodes = null;
            TreeViewNode parentNode = null;
            int iIndex = -1;

            if( FocusedNode != null )
            {
                parentNode = ( FocusedNode.Parent != null ) ? ( (TreeViewNode)FocusedNode.Parent ) : null;
                lNodes = ( parentNode != null ) ? parentNode.Children : Nodes;
                iIndex = lNodes.IndexOf( FocusedNode );
            }

            if( _direction == Direction.Up )
            {
                if( lNodes == null ) return;

                if( iIndex > 0 )
                {
                    TreeViewNode node = lNodes[ iIndex - 1 ];

                    while( node.Children.Count > 0 && ! node.Collapsed )
                    {
                        node = node.Children[ node.Children.Count - 1 ];
                    }

                    FocusedNode = node;
                }
                else
                if( parentNode != null )
                {
                    FocusedNode = parentNode;
                }
            }
            else
            if( _direction == Direction.Down )
            {
                if( lNodes == null ) return;

                if( FocusedNode.Children.Count > 0 && ! FocusedNode.Collapsed )
                {
                    FocusedNode = FocusedNode.Children[0];
                }
                else
                {
                    TreeViewNode node = FocusedNode;

                    while( true )
                    {
                        if( iIndex < lNodes.Count - 1 )
                        {
                            FocusedNode = lNodes[ iIndex + 1 ];
                            break;
                        }
                        else
                        if( parentNode != null )
                        {
                            node = parentNode;
                            parentNode = ( node.Parent != null ) ? ( (TreeViewNode)node.Parent ) : null;
                            lNodes = ( parentNode != null ) ? parentNode.Children : Nodes;
                            iIndex = lNodes.IndexOf( node );
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                base.OnPadMove( _direction );
            }
        }
Beispiel #5
0
        //----------------------------------------------------------------------
        protected internal override bool OnMouseDown( Point _hitPoint, int _iButton )
        {
            if( _iButton != Screen.Game.InputMgr.PrimaryMouseButton ) return false;

            if( mHoveredActionButton != null )
            {
                mHoveredActionButton.OnActivateDown();
                mbIsHoveredActionButtonDown = true;
            }
            else
            {
                mbIsMouseDown = true;
                mMouseDownPoint = new Point( _hitPoint.X, _hitPoint.Y + (int)Scrollbar.LerpOffset );

                Screen.Focus( this );
                FocusedNode = HoveredNode;
            }

            return true;
        }