代码改变世界

Windows phone 手势,鼠标-学习笔记

2010-11-13 19:29  Aga.J  阅读(1656)  评论(2编辑  收藏  举报

  1 首先是最原始的鼠标2维位置获取

              Mouse.GetState(); 

  返回原始的触摸位置信息,当然这里的触摸指的是MouseState.LeftButton在被按下的状态,获取到的值会放在MouseState.X和MouseState .Y中,注意到这个函数获取的只是单一的触摸点击信息,要同时获取多个不同点击点的信息(也就是多点触控的操作),要使用TouchPanel.GetState(这个后面会说到)

  代码例子

MouseState ms=Mouse.GetState();

If(canDrop && ms.LeftButton == ButtonState.Pressed)     //wp里是leftButton

{

       foodLocations.Add(new Vector2(ms.X,ms.Y));

       canDrop=false;                                                       

}

else if(ms.LeftButton == ButtonState.Released)

{

       canDrop=true;

}

  当然有get就会有set,这里提供的是SetPosition(x,y);

  2 TouchPanel,提供多点触控的操作的类,功能有三:

  而在第1点说到的Mouse类,则只能获得多点触控里的第一点信息,如果仅仅需要简单的触控信息,那么使用第一种方法会方便点

  下面的例子是判断触摸板是否接收触摸,并且接收多少种触摸信息

TouchPanelCapabilities tc = TouchPanel.GetCapabilities();

if(tc.IsConnected)          //这里判断当前触摸板是否可以读取触摸信息

{

       return tc.MaxinumTouchCount;

}

  接下来时获取多点触控的信息,使用TouchPanel.GetState获取当前状态,注意它会返回一个TouchCollection的集合,里面保存了多点触控的每个点的信息,包括TouchLocation等相关信息,如下例:

TouchCollection touchCollection = TouchPanel.GetState();

Foreach(TouchLocation tl in touchCollection)

{

       If ((tl.State==TouchLocationState.Pressd) || (tl.State==TouchLocationState.Moved)

{

       Sparkles.Add(new Sparkle(tl.Postition.X,tl.Position.Y,ttms));

}

}

  3 介绍第2点中的TouchCollection的信息。

   这是一个实现多个接口的类,其中就包含了IList,所以这个类可以保存多个触摸点的信息TouchLocation,也就是TouchLocation对象的集合。

下面是其公有成员(为了避免又被一些无聊的人说我照抄msdn,所以在这里解释下,这里的一些东西不解释,英语很浅,而且我列在这里是为了以后查找起来方便而已)

Count

Gets the current number of touch locations for the touch screen.

IsConnected

Indicates if the touch screen is available for use.

IsReadOnly

Determines if the touch location array is read-only.

Item

Gets or sets the information of the specified touch location.

  还有这个类相应的成员函数,包含了大部分集合类型的类所有的成员函数,比如add等

  4 和TouchCollection搭配使用的有TouchCollection.Enumerator。顾名思义,之前说TouchCollection是个集合类型的类,所以这里有个TouchCollection.Enumerator来作为迭代子也很正常。主要能获取TouchCollection的当前元素,下一个元素等

  5接下来是TouchLocation,之前说过是TouchCollection的元素类型

  主要属性是

Name

Description

Id

Gets the ID of the touch location.

Position

Gets the position of the touch location.

State

Gets the state of the touch location.  

  值得注意的是它有几个比较好用的成员函数

   op_Equality op_Inequality TryGetPreviousLocation(看名字就知道是什么意思了,微软那班人真是厉害。)

       6 TouchPanelCapabilities就下面两个属性

Name

Description

IsConnected

Indicates if the touch panel device is available for use.

MaximumTouchCount

Gets the maximum number of touch locations that can be tracked by the touch pad device.

       7 最后一个是TouchLocationState枚举,枚举Touch Location的类型

Member name

Description

Invalid

This touch location position is invalid. Typically, you will encounter this state when a new touch location attempts to get the previous state of itself.

Moved

This touch location position was updated or pressed at the same position.

Pressed

This touch location position is new.

Released

This touch location position was released.

  8 TouchPanel 使用它的封装好的手势信息。比较第2,这是更高层次上使用touchPanel

  首先需要再game初始化时先确定touchPanel所能接收的手势类型

  TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.Flick;

  在update里面,我们在每次游戏循环都检查目前有什么样的手势输入,每次用户输入手势,都会存入一个手势队列中,IsGestureAvailable就是检查队列中是否有手势存在

while(TouchPanel.IsGestureAvailable)        

{

       gestureSample gs =TouchPanel.ReadGesture();

       switch(gs.GestureType)

{

  case GestureType.VerticalDrag:

       poem.offset.Y -= gs.Delta.Y;

       break;

   case GestureType.Flick:

       poem.velocity.Y +=gs.Delta.Y;

       break;

}

}

8.1 在8中提到的GestureType是一个枚举类型,有以下几种类型

Member name

Description

None

Represents no gestures.

Tap

The user briefly touched a single point on the screen.

DoubleTap

The user tapped the screen twice in quick succession. This always is preceded by a Tap gesture.

If the time between taps is too great to be considered a DoubleTap, two Tap gestures will be generated instead.

Hold

The user touched a single point on the screen for approximately one second. This is a single event, and not continuously generated while the user is holding the touchpoint.

HorizontalDrag

The user touched the screen, and then performed a horizontal (left to right or right to left) gesture.

VerticalDrag

The user touched the screen, and then performed a vertical (top to bottom or bottom to top) gesture.

FreeDrag

The user touched the screen, and then performed a free-form drag gesture.

Pinch

The user touched two points on the screen, and then converged or diverged them. Pinch behaves like a two-finger drag. When this gesture is enabled, it takes precedence over drag gestures while two fingers are down.

Flick

The user performed a touch combined with a quick swipe of the screen. Flicks are positionless. The velocity of the flick can be retrieved by reading the Delta member of GestureSample.

DragComplete

A drag gesture (VerticalDrag, HorizontalDrag, or FreeDrag) was completed. This signals only completion. No position or delta data is valid for this sample.

PinchComplete

A pinch operation was completed. This signals only completion. No position or delta data is valid for this sample.

  8.2 在8中提到的GestureSample里面保存了获取到的高层手势类型的详细内部信息

  包括

A)    Delta 手势中的第一点delta信息

B)     Delta 手势中的第二点delta信息

C)     GestureType 手势类型

D)    Position       手势第一点的位置信息

E)     Position2    手势第二点的位置信息

F)     Timestamp   手势的开始时间

(注意到GestureType的Flick并没有方向的信息,也就是说不指定Flick是往哪个方向的,通过GestureSample提供的Delta,我们可以得到这方面的信息—值的正负而已。)


作者:Aga.J
出处:http://www.cnblogs.com/aga-j
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
个人学习笔记仅供本人记录知识所用,不属发表性文章。