XNA游戏:手势触控

在XNA游戏中使用到手势触控操作时,需要引入using Microsoft.Xna.Framework.Input.Touch; 

空间,在该空间下下面两个类在触控编程中会用到。
TouchLocation 用来保存某一个触摸点的状态信息。
TouchCollection 是保存了当前所有触控状态(TouchLocation)的集合。

当我们把一个指头在屏幕上操作,可能会有这样三种动作:按,移动,移开。那么这三个操作在WP7的XNA里如何获取呢?我们就需要了解XNA里的TouchPanel和TouchCollection这两个类

 

 
 
  1. TouchCollection touchStateTouchPanel.GetState();  
  2.        Foreach(TouchLocation location in touchState)  
  3.        {  
  4.                switch(location.State)  
  5.                {  
  6.                    case TouchLocationState.Pressed://按下  
  7.                     ……  
  8.                    break;  
  9.  
  10.        case TouchLocationState.Moved://移动  
  11.                     ……  
  12.                    break;  
  13.  
  14.        case TouchLocationState.Released://释放  
  15.                     ……  
  16.                    break;  
  17.  
  18.        }  
  19.        } 

TouchLocation :
State 触摸状态,包含4个状态
> TouchLocationState.Pressed 表示屏幕被触摸时手指按下的一瞬间
> TouchLocationState.Moved 表示手指按下后正在移动,经过测试可知,在手指按下的一瞬间State为Pressed ,在手指按下后抬起前这段时间内的状态均是Moved
> TouchLocationState.Invalid 无效状态
> TouchLocationState.Released 表示手指抬起的一瞬间
ID 表示当前触摸事件的ID,一个完成的触控事件的过程应该是“Pressed -> Moved -> Released ”在这个过程中ID是一致的,用来在多点触摸时区分触摸的每个点。
Position 触摸位置,包含两个属性
> X 当前触摸位置的X轴坐标
> Y 当前触摸位置的Y轴坐标
(横屏全屏情况下,屏幕的左上角坐标为(0,0)右下角坐标为(800,480))


和触控操作类似的还有叫“手势”的,也算复杂的触控吧。

 

 
 
  1. TouchPanel.EnabledGestures = GestureType.FreeDrag;//用来指定手势,必须要先设定,否则  
  2.  
  3. 报错  
  4.  
  5. if (TouchPanel.EnabledGestures != GestureType.None)  
  6. {  
  7. switch (TouchPanel.ReadGesture())  
  8. {  
  9. case GestureType.Tap: //单击  
  10. break;  
  11. case GestureType.DoubleTap://双击  
  12. break;  
  13. case GestureType.FreeDrag://自由拖动  
  14. break;  
  15. case GestureType.DragComplete://拖动完成  
  16. break;  
  17. case GestureType.Flick://轻弹  
  18. break;  
  19. case GestureType.Hold://按住不动  
  20. break;  
  21. case GestureType.HorizontalDrag://横向拖动  
  22. break;  
  23. case GestureType.None://无手势  
  24. break;  
  25. case GestureType.Pinch://捏  
  26. break;  
  27. case GestureType.PinchComplete://捏完  
  28. break;  
  29. case GestureType.VerticalDrag://纵向拖动  
  30. break;  
  31. }  

示例一各种手势的测试:

 

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Microsoft.Xna.Framework;  
  5. using Microsoft.Xna.Framework.Audio;  
  6. using Microsoft.Xna.Framework.Content;  
  7. using Microsoft.Xna.Framework.GamerServices;  
  8. using Microsoft.Xna.Framework.Graphics;  
  9. using Microsoft.Xna.Framework.Input;  
  10. using Microsoft.Xna.Framework.Input.Touch;  
  11. using Microsoft.Xna.Framework.Media;  
  12.  
  13. namespace Gestures  
  14. {  
  15.     /// <summary> 
  16.     /// This is the main type for your game  
  17.     /// </summary> 
  18.     public class Game1 : Microsoft.Xna.Framework.Game  
  19.     {  
  20.         GraphicsDeviceManager graphics;  
  21.         SpriteBatch spriteBatch;  
  22.  
  23.         SpriteFont spriteFont;  
  24.  
  25.         String message = "Do something";  
  26.         Vector2 messagePos = Vector2.Zero;  
  27.         Color color = Color.Black;  
  28.  
  29.         public Game1()  
  30.         {  
  31.             graphics = new GraphicsDeviceManager(this);  
  32.             Content.RootDirectory = "Content";  
  33.  
  34.             // Frame rate is 30 fps by default for Windows Phone.  
  35.             TargetElapsedTime = TimeSpan.FromTicks(333333);  
  36.         }  
  37.  
  38.         /// <summary> 
  39.         /// Allows the game to perform any initialization it needs to before starting to run.  
  40.         /// This is where it can query for any required services and load any non-graphic  
  41.         /// related content.  Calling base.Initialize will enumerate through any components  
  42.         /// and initialize them as well.  
  43.         /// </summary> 
  44.         protected override void Initialize()  
  45.         {  
  46.             //添加各种手势的支持  
  47.             TouchPanel.EnabledGestures = GestureType.Tap | GestureType.DoubleTap | GestureType.Hold | GestureType.HorizontalDrag  
  48.                 | GestureType.VerticalDrag | GestureType.FreeDrag | GestureType.DragComplete | GestureType.Pinch  
  49.                 | GestureType.PinchComplete | GestureType.Flick;  
  50.  
  51.             base.Initialize();  
  52.         }  
  53.  
  54.         /// <summary> 
  55.         /// LoadContent will be called once per game and is the place to load  
  56.         /// all of your content.  
  57.         /// </summary> 
  58.         protected override void LoadContent()  
  59.         {  
  60.             // Create a new SpriteBatch, which can be used to draw textures.  
  61.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  62.  
  63.             //记载字体资源  
  64.             spriteFont = Content.Load<SpriteFont>("SpriteFont1");  
  65.         }  
  66.  
  67.         /// <summary> 
  68.         /// UnloadContent will be called once per game and is the place to unload  
  69.         /// all content.  
  70.         /// </summary> 
  71.         protected override void UnloadContent()  
  72.         {  
  73.             // TODO: Unload any non ContentManager content here  
  74.         }  
  75.  
  76.         /// <summary> 
  77.         /// Allows the game to run logic such as updating the world,  
  78.         /// checking for collisions, gathering input, and playing audio.  
  79.         /// </summary> 
  80.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  81.         protected override void Update(GameTime gameTime)  
  82.         {  
  83.             // Allows the game to exit  
  84.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)  
  85.                 this.Exit();  
  86.  
  87.             //判断手势的类别  
  88.             if (TouchPanel.IsGestureAvailable)  
  89.             {  
  90.                 GestureSample gesture = TouchPanel.ReadGesture();  
  91.                 switch (gesture.GestureType)  
  92.                 {  
  93.                     case GestureType.Tap:  
  94.                         message = "That was a Tap";  
  95.                         color = Color.Red;  
  96.                         break;  
  97.  
  98.                     case GestureType.DoubleTap:  
  99.                         message = "That was a Double Tap";  
  100.                         color = Color.Orange;  
  101.                         break;  
  102.  
  103.                     case GestureType.Hold:  
  104.                         message = "That was a Hold";  
  105.                         color = Color.Yellow;  
  106.                         break;  
  107.  
  108.                     case GestureType.HorizontalDrag:  
  109.                         message = "That was a Horizontal Drag";  
  110.                         color = Color.Blue;  
  111.                         break;  
  112.  
  113.                     case GestureType.VerticalDrag:  
  114.                         message = "That was a Vertical Drag";  
  115.                         color = Color.Indigo;  
  116.                         break;  
  117.  
  118.                     case GestureType.FreeDrag:  
  119.                         message = "That was a Free Drag";  
  120.                         color = Color.Green;  
  121.                         break;  
  122.  
  123.                     case GestureType.DragComplete:  
  124.                         message = "Drag gesture complete";  
  125.                         color = Color.Gold;  
  126.                         break;  
  127.  
  128.                     case GestureType.Flick:  
  129.                         message = "That was a Flick";  
  130.                         color = Color.Violet;  
  131.                         break;  
  132.  
  133.                     case GestureType.Pinch:  
  134.                         message = "That was a Pinch";  
  135.                         color = Color.Violet;  
  136.                         break;  
  137.  
  138.                     case GestureType.PinchComplete:  
  139.                         message = "Pinch gesture complete";  
  140.                         color = Color.Silver;  
  141.                         break;  
  142.                 }  
  143.  
  144.                 messagePos = gesture.Position;  
  145.             }  
  146.  
  147.             base.Update(gameTime);  
  148.         }  
  149.  
  150.         /// <summary> 
  151.         /// This is called when the game should draw itself.  
  152.         /// </summary> 
  153.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  154.         protected override void Draw(GameTime gameTime)  
  155.         {  
  156.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  157.  
  158.             //绘制屏幕的文字  
  159.             spriteBatch.Begin();  
  160.             spriteBatch.DrawString(spriteFont, message, messagePos, color);  
  161.             spriteBatch.End();  
  162.  
  163.             base.Draw(gameTime);  
  164.         }  
  165.     }  

示例二多点触控的测试:

 

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Microsoft.Xna.Framework;  
  5. using Microsoft.Xna.Framework.Audio;  
  6. using Microsoft.Xna.Framework.Content;  
  7. using Microsoft.Xna.Framework.GamerServices;  
  8. using Microsoft.Xna.Framework.Graphics;  
  9. using Microsoft.Xna.Framework.Input;  
  10. using Microsoft.Xna.Framework.Input.Touch;  
  11. using Microsoft.Xna.Framework.Media;  
  12.  
  13. namespace MultiTouchMe  
  14. {  
  15.     /// <summary> 
  16.     /// This is the main type for your game  
  17.     /// </summary> 
  18.     public class Game1 : Microsoft.Xna.Framework.Game  
  19.     {  
  20.         GraphicsDeviceManager graphics;  
  21.         SpriteBatch spriteBatch;  
  22.  
  23.         SpriteFont spriteFont;  
  24.         TouchCollection touchCollection;  
  25.  
  26.         public Game1()  
  27.         {  
  28.             graphics = new GraphicsDeviceManager(this);  
  29.             Content.RootDirectory = "Content";  
  30.  
  31.             // Frame rate is 30 fps by default for Windows Phone.  
  32.             TargetElapsedTime = TimeSpan.FromTicks(333333);  
  33.         }  
  34.  
  35.         /// <summary> 
  36.         /// Allows the game to perform any initialization it needs to before starting to run.  
  37.         /// This is where it can query for any required services and load any non-graphic  
  38.         /// related content.  Calling base.Initialize will enumerate through any components  
  39.         /// and initialize them as well.  
  40.         /// </summary> 
  41.         protected override void Initialize()  
  42.         {  
  43.             // TODO: Add your initialization logic here  
  44.  
  45.             base.Initialize();  
  46.         }  
  47.  
  48.         /// <summary> 
  49.         /// LoadContent will be called once per game and is the place to load  
  50.         /// all of your content.  
  51.         /// </summary> 
  52.         protected override void LoadContent()  
  53.         {  
  54.             // Create a new SpriteBatch, which can be used to draw textures.  
  55.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  56.  
  57.             // TODO: use this.Content to load your game content here  
  58.             spriteFont = Content.Load<SpriteFont>("SpriteFont1");  
  59.         }  
  60.  
  61.         /// <summary> 
  62.         /// UnloadContent will be called once per game and is the place to unload  
  63.         /// all content.  
  64.         /// </summary> 
  65.         protected override void UnloadContent()  
  66.         {  
  67.             // TODO: Unload any non ContentManager content here  
  68.         }  
  69.  
  70.         /// <summary> 
  71.         /// Allows the game to run logic such as updating the world,  
  72.         /// checking for collisions, gathering input, and playing audio.  
  73.         /// </summary> 
  74.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  75.         protected override void Update(GameTime gameTime)  
  76.         {  
  77.             // Allows the game to exit  
  78.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)  
  79.                 this.Exit();  
  80.  
  81.             // TODO: Add your update logic here  
  82.             touchCollection = TouchPanel.GetState();  
  83.  
  84.             base.Update(gameTime);  
  85.         }  
  86.  
  87.         /// <summary> 
  88.         /// This is called when the game should draw itself.  
  89.         /// </summary> 
  90.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  91.         protected override void Draw(GameTime gameTime)  
  92.         {  
  93.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  94.  
  95.             // TODO: Add your drawing code here  
  96.             spriteBatch.Begin();  
  97.  
  98.             foreach (TouchLocation touch in touchCollection)  
  99.                 spriteBatch.DrawString(spriteFont, "ID: " + touch.Id.ToString() + " (" +  
  100.                     (int)touch.Position.X + "," + (int)touch.Position.Y + ")", touch.Position, Color.White);  
  101.  
  102.             spriteBatch.End();  
  103.  
  104.             base.Draw(gameTime);  
  105.         }  
  106.     }  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078389


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧农业是一种结合了现代信息技术,包括物联网、大数据、云计算等,对农业生产过程进行智能化管理和监控的新模式。它通过各种传感器和设备采集农业生产中的关键数据,如大气、土壤和水质参数,以及生物生长状态等,实现远程诊断和精准调控。智慧农业的核心价值在于提高农业生产效率,保障食品安全,实现资源的可持续利用,并为农业产业的转型升级提供支持。 智慧农业的实现依赖于多个子系统,包括但不限于设施蔬菜精细化种植管理系统、农业技术资料库、数据采集系统、防伪防串货系统、食品安全与质量追溯系统、应急追溯系统、灾情疫情防控系统、农业工作管理系统、远程诊断系统、监控中心、环境监测系统、智能环境控制系统等。这些系统共同构成了一个综合的信息管理和服务平台,使得农业生产者能够基于数据做出更加科学的决策。 数据采集是智慧农业的基础。通过手工录入、传感器自动采集、移动端录入、条码/RFID扫描录入、拍照录入以及GPS和遥感技术等多种方式,智慧农业系统能够全面收集农业生产过程中的各种数据。这些数据不仅包括环境参数,还涵盖了生长状态、加工保存、检验检疫等环节,为农业生产提供了全面的数据支持。 智慧农业的应用前景广阔,它不仅能够提升农业生产的管理水平,还能够通过各种应用系统,如库房管理、无公害监控、物资管理、成本控制等,为农业生产者提供全面的服务。此外,智慧农业还能够支持政府监管,通过发病报告、投入品报告、死亡报告等,加强农业产品的安全管理和质量控制。 面对智慧农业的建设和发展,存在一些挑战,如投资成本高、生产过程标准化难度大、数据采集和监测的技术难题等。为了克服这些挑战,需要政府、企业和相关机构的共同努力,通过政策支持、技术创新和教育培训等手段,推动智慧农业的健康发展。智慧农业的建设需要明确建设目的,选择合适的系统模块,并制定合理的设备布署方案,以实现农业生产的智能化、精准化和高效化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值