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);
- }
- }
- /// <summary>
- /// 绘制界面
- /// </summary>
- /// <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;
- }
- }
- }
- /// <summary>
- /// 画线
- /// </summary>
- /// <param name="batch"></param>
- /// <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
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Input.Touch;
- using Microsoft.Xna.Framework.Graphics;
- namespace InputHandlerDemo.Inputs
- {
- /// <summary>
- /// 触控轨迹集合
- /// </summary>
- class TouchIndicatorCollection
- {
- List<TouchIndicator> touchPositions = new List<TouchIndicator>();
- /// <summary>
- /// 更新游戏界面
- /// </summary>
- /// <param name="gameTime"></param>
- /// <param name="content"></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);
- }
- }
- public void Draw(SpriteBatch batch)
- {
- foreach (TouchIndicator indicator in touchPositions)
- {
- indicator.Draw(batch);
- }
- }
- }
- }
本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078376