コード例 #1
0
ファイル: KeyboardGraphNavigator.cs プロジェクト: zparr/ATF
        /// <summary>
        /// Finds the nearest node to a starting node, in the desired direction</summary>
        /// <param name="startNode">Node to measure distance from</param>
        /// <param name="arrow">Direction (Keys.Up, Keys.Right, Keys.Down, or Keys.Left)</param>
        /// <param name="nearestRect">Nearest node's rectangle, or an empty rectangle if no
        /// nearest node is found</param>
        /// <returns>The nearest node or null if there is none in that direction</returns>
        protected virtual TNode FindNearestElement(TNode startNode, Keys arrow, out Rectangle nearestRect)
        {
            TNode nearest        = null;
            int   bestDist       = int.MaxValue;
            var   pickingAdapter = AdaptedControl.Cast <IPickingAdapter2>();

            nearestRect = new Rectangle();

            Rectangle startRect = pickingAdapter.GetBounds(new[] { startNode });

            foreach (TNode node in m_graph.Nodes)
            {
                if (node != startNode)
                {
                    Rectangle targetRect = pickingAdapter.GetBounds(new[] { node });
                    int       dist       = WinFormsUtil.CalculateDistance(startRect, arrow, targetRect);
                    if (dist < bestDist)
                    {
                        bestDist    = dist;
                        nearest     = node;
                        nearestRect = targetRect;
                    }
                }
            }

            return(nearest);
        }
コード例 #2
0
        /// <summary>
        /// Finds the nearest node to a starting node, in the desired direction</summary>
        /// <param name="startNode">Node to measure distance from</param>
        /// <param name="key">Key press (e.g., Keys.Up, Keys.Right, Keys.Down, or Keys.Left),
        /// without modifier keys</param>
        /// <param name="modifiers">Modifier keys that were pressed</param>
        /// <returns>The nearest node or null if there is none in that direction</returns>
        protected virtual TNode FindNearestNode(TNode startNode, Keys key, Keys modifiers)
        {
            TNode nearest  = null;
            int   bestDist = int.MaxValue;

            Rectangle startRect = m_pickingAdapter.GetBounds(new[] { startNode });

            foreach (TNode node in m_graph.Nodes)
            {
                if (node != startNode)
                {
                    Rectangle targetRect = m_pickingAdapter.GetBounds(new[] { node });
                    int       dist       = WinFormsUtil.CalculateDistance(startRect, key, targetRect);
                    if (dist < bestDist)
                    {
                        bestDist = dist;
                        nearest  = node;
                    }
                }
            }

            return(nearest);
        }