天天看點

XNA遊戲:各種輸入測試 下

TouchIndicator.cs

using System;  

using System.Collections.Generic;  

using Microsoft.Xna.Framework;  

using Microsoft.Xna.Framework.Graphics;  

using Microsoft.Xna.Framework.Input.Touch;  

using Microsoft.Xna.Framework.Content;  

namespace InputHandlerDemo.Inputs  

{  

    /// <summary> 

    /// 觸控軌迹  

    /// </summary> 

    class TouchIndicator  

    {  

        int alphaValue = 255;  

        public int TouchID;  

        Texture2D touchCircleIndicatorTexture;  

        Texture2D touchCrossHairIndicatorTexture;  

        //軌迹集合  

        List<Vector2> touchPositions = new List<Vector2>();  

        /// <summary> 

        /// 初始化觸摸軌迹  

        /// </summary> 

        /// <param name="touchID"></param> 

        /// <param name="content"></param> 

        public TouchIndicator(int touchID, ContentManager content)  

        {  

            TouchID = touchID;  

            touchCircleIndicatorTexture = content.Load<Texture2D>("Circle");  

            touchCrossHairIndicatorTexture = content.Load<Texture2D>("Crosshair");  

        }  

        private Vector2? TouchPosition(TouchCollection touchLocationState)  

            TouchLocation touchLocation;  

            if (touchLocationState.FindById(TouchID, out touchLocation))  

            {  

                return touchLocation.Position;  

            }  

            return null;  

        public void Update(TouchCollection touchLocationState)  

            Vector2? currentPosition = TouchPosition(touchLocationState);  

            if (currentPosition == null)  

                if (touchPositions.Count > 0)  

                {  

                    alphaValue -= 20;  

                    if (alphaValue <= 0)  

                    {  

                        touchPositions.Clear();  

                        alphaValue = 255;  

                    }  

                }  

            else  

                if (alphaValue != 255)  

                    touchPositions.Clear();  

                    alphaValue = 255;  

                touchPositions.Add((Vector2)currentPosition);  

        /// 繪制界面  

        /// <param name="batch"></param> 

        public void Draw(SpriteBatch batch)  

            if (touchPositions.Count != 0)  

                Vector2 previousPosition = touchPositions[0];  

                Vector2 offsetForCenteringTouchPosition = new Vector2(-25, 0);  

                //繪制觸摸的軌迹  

                foreach (Vector2 aPosition in touchPositions)  

                    DrawLine(batch,  

                             touchCircleIndicatorTexture,  

                             touchCrossHairIndicatorTexture,  

                             previousPosition + offsetForCenteringTouchPosition,  

                             aPosition + offsetForCenteringTouchPosition,  

                             new Color(0, 0, 255, alphaValue));  

                    previousPosition = aPosition;  

        /// 畫線  

        /// <param name="lineTexture"></param> 

        /// <param name="touchTexture"></param> 

        /// <param name="startingPoint"></param> 

        /// <param name="endingPoint"></param> 

        /// <param name="lineColor"></param> 

        void DrawLine(SpriteBatch batch, Texture2D lineTexture, Texture2D touchTexture,  

              Vector2 startingPoint, Vector2 endingPoint, Color lineColor)  

            batch.Draw(touchTexture, startingPoint, lineColor);  

            Vector2 difference = startingPoint - endingPoint;  

            float lineLength = difference.Length() / 8;  

            for (int i = 0; i < lineLength; i++)  

                batch.Draw(lineTexture, startingPoint, lineColor);  

                startingPoint.X -= difference.X / lineLength;  

                startingPoint.Y -= difference.Y / lineLength;  

            batch.Draw(touchTexture, endingPoint, lineColor);  

    }  

TouchIndicatorCollection.cs

    /// 觸控軌迹集合  

    class TouchIndicatorCollection  

        List<TouchIndicator> touchPositions = new List<TouchIndicator>();  

        /// 更新遊戲界面  

        /// <param name="gameTime"></param> 

        public void Update(GameTime gameTime, ContentManager content)  

            //在XNA中,TouchPanel可以向我們提供大量關于觸控操作的資訊  擷取觸控的集合  

            TouchCollection currentTouchLocationState = TouchPanel.GetState();  

            foreach (TouchLocation location in currentTouchLocationState)  

            {//擷取觸控的位置  

                bool isTouchIDAlreadyStored = false;  

                foreach (TouchIndicator indicator in touchPositions)  

                    //如果觸控的位置id存在則跳出整個循環  

                    if (location.Id == indicator.TouchID)  

                        isTouchIDAlreadyStored = true;  

                        break;  

                if (!isTouchIDAlreadyStored)  

                    //加入一個觸控的位置  

                    TouchIndicator indicator = new TouchIndicator(location.Id, content);  

                    touchPositions.Add(indicator);  

            foreach (TouchIndicator indicator in touchPositions)  

                indicator.Update(currentTouchLocationState);  

                indicator.Draw(batch);  

本文轉自linzheng 51CTO部落格,原文連結:http://blog.51cto.com/linzheng/1078376

繼續閱讀