コード例 #1
0
        /// <summary>
        /// Walks the visual tree to find a <see cref="GamePieceVisual"/> from a
        /// <see cref="PlanarText"/> object.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        static GamePieceVisual GetGamePieceVisualFromText(PlanarText text)
        {
            bool             flag         = false;
            DependencyObject currentChild = text;
            GamePieceVisual  foundPiece   = null;
            int max   = 100;
            int count = 0;

            while (flag == false)
            {
                DependencyObject parent      = VisualTreeHelper.GetParent(currentChild);
                GamePieceVisual  parentPiece = parent as GamePieceVisual;
                if (parentPiece != null)
                {
                    foundPiece = parentPiece;
                    flag       = true;
                }
                else
                {
                    if (parent is BoardAxes)
                    {
                        return(null);
                    }
                    currentChild = parent;
                }

                count = count + 1;
                if (count > max)
                {
                    throw new GameException("A parent game piece on a text model could not be found.");
                }
            }

            return(foundPiece);
        }
コード例 #2
0
        /// <summary>
        /// A helper method used to find a <see cref="GamePieceVisual"/> object by either direct
        /// casting or walking the visual tree.
        /// </summary>
        /// <param name="hitTestResult"></param>
        /// <returns></returns>
        static GamePieceVisual ConvertHitTestResultToGamePieceVisual(ModelVisual3D hitTestResult)
        {
            if (hitTestResult == null)
            {
                return(null);
            }

            PlanarText textResult = hitTestResult as PlanarText;

            if (textResult != null)
            {
                return(GameBoardController.GetGamePieceVisualFromText(textResult));
            }

            GamePieceVisual pieceResult = hitTestResult as GamePieceVisual;

            if (pieceResult != null)
            {
                return(pieceResult);
            }

            return(null);
        }