GestureDefinition.cs

usingSystem; usingMicrosoft.Xna.Framework; usingMicrosoft.Xna.Framework.Input.Touch; namespaceInputHandlerDemo.Inputs { ///<summary>///手势定义 ///</summary>classGestureDefinition { publicGestureTypeType; publicRectangleCollisionArea; publicGestureSampleGesture; publicVector2Delta; publicVector2Delta2; publicVector2Position; publicVector2Position2; publicGestureDefinition(GestureTypetheGestureType,RectangletheGestureArea) { Gesture=newGestureSample(theGestureType,newTimeSpan(0), Vector2.Zero,Vector2.Zero, Vector2.Zero,Vector2.Zero); Type=theGestureType; CollisionArea=theGestureArea; } publicGestureDefinition(GestureSampletheGestureSample) { Gesture=theGestureSample; Type=theGestureSample.GestureType; CollisionArea=newRectangle((int)theGestureSample.Position.X, (int)theGestureSample.Position.Y,5,5); Delta=theGestureSample.Delta; Delta2=theGestureSample.Delta2; Position=theGestureSample.Position; Position2=theGestureSample.Position2; } } }

Input.cs

usingSystem; usingSystem.Collections.Generic; usingMicrosoft.Xna.Framework.Input; usingMicrosoft.Xna.Framework.Input.Touch; usingMicrosoft.Xna.Framework; usingMicrosoft.Devices.Sensors; namespaceInputHandlerDemo.Inputs { ///<summary>///输入操作类 ///</summary>classInput { Dictionary<Keys,bool>keyboardInputs=newDictionary<Keys,bool>(); Dictionary<Buttons,bool>gamepadInputs=newDictionary<Buttons,bool>(); Dictionary<Rectangle,bool>touchTapInputs=newDictionary<Rectangle,bool>(); Dictionary<Direction,float>touchSlideInputs=newDictionary<Direction,float>(); Dictionary<int,GestureDefinition>gestureInputs=newDictionary<int,GestureDefinition>(); Dictionary<Direction,float>accelerometerInputs=newDictionary<Direction,float>(); staticpublicDictionary<PlayerIndex,GamePadState>CurrentGamePadState//当前玩家的控制状态 =newDictionary<PlayerIndex,GamePadState>(); staticpublicDictionary<PlayerIndex,GamePadState>PreviousGamePadState//上一个玩家的控制状态 =newDictionary<PlayerIndex,GamePadState>(); staticpublicTouchCollectionCurrentTouchLocationState;//当前的触控集合 staticpublicTouchCollectionPreviousTouchLocationState;//上一个触控集合 staticpublicKeyboardStateCurrentKeyboardState;//当前的键盘状态 staticpublicKeyboardStatePreviousKeyboardState;//上一个键盘状态 staticpublicDictionary<PlayerIndex,bool>GamepadConnectionState=newDictionary<PlayerIndex,bool>(); staticprivateList<GestureDefinition>detectedGestures=newList<GestureDefinition>(); staticprivateAccelerometeraccelerometerSensor; staticprivateVector3currentAccelerometerReading; //方向 publicenumDirection { Up, Down, Left, Right } publicInput() { if(CurrentGamePadState.Count==0) { CurrentGamePadState.Add(PlayerIndex.One,GamePad.GetState(PlayerIndex.One)); CurrentGamePadState.Add(PlayerIndex.Two,GamePad.GetState(PlayerIndex.Two)); CurrentGamePadState.Add(PlayerIndex.Three,GamePad.GetState(PlayerIndex.Three)); CurrentGamePadState.Add(PlayerIndex.Four,GamePad.GetState(PlayerIndex.Four)); PreviousGamePadState.Add(PlayerIndex.One,GamePad.GetState(PlayerIndex.One)); PreviousGamePadState.Add(PlayerIndex.Two,GamePad.GetState(PlayerIndex.Two)); PreviousGamePadState.Add(PlayerIndex.Three,GamePad.GetState(PlayerIndex.Three)); PreviousGamePadState.Add(PlayerIndex.Four,GamePad.GetState(PlayerIndex.Four)); GamepadConnectionState.Add(PlayerIndex.One, CurrentGamePadState[PlayerIndex.One].IsConnected); GamepadConnectionState.Add(PlayerIndex.Two, CurrentGamePadState[PlayerIndex.Two].IsConnected); GamepadConnectionState.Add(PlayerIndex.Three, CurrentGamePadState[PlayerIndex.Three].IsConnected); GamepadConnectionState.Add(PlayerIndex.Four, CurrentGamePadState[PlayerIndex.Four].IsConnected); } //添加重力感应 if(accelerometerSensor==null) { accelerometerSensor=newAccelerometer(); accelerometerSensor.ReadingChanged +=newEventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged); } } ///<summary>///开始更新 ///</summary>staticpublicvoidBeginUpdate() { //PlayerIndex游戏玩家的索引,添加4个玩家 CurrentGamePadState[PlayerIndex.One]=GamePad.GetState(PlayerIndex.One); CurrentGamePadState[PlayerIndex.Two]=GamePad.GetState(PlayerIndex.Two); CurrentGamePadState[PlayerIndex.Three]=GamePad.GetState(PlayerIndex.Three); CurrentGamePadState[PlayerIndex.Four]=GamePad.GetState(PlayerIndex.Four); //当前触摸的地方 CurrentTouchLocationState=TouchPanel.GetState(); //玩家1的状态 CurrentKeyboardState=Keyboard.GetState(PlayerIndex.One); detectedGestures.Clear(); while(TouchPanel.IsGestureAvailable) { GestureSamplegesture=TouchPanel.ReadGesture(); detectedGestures.Add(newGestureDefinition(gesture)); } } ///<summary>///结束更新 ///</summary>staticpublicvoidEndUpdate() { //PlayerIndex游戏玩家的索引,日安家 PreviousGamePadState[PlayerIndex.One]=CurrentGamePadState[PlayerIndex.One]; PreviousGamePadState[PlayerIndex.Two]=CurrentGamePadState[PlayerIndex.Two]; PreviousGamePadState[PlayerIndex.Three]=CurrentGamePadState[PlayerIndex.Three]; PreviousGamePadState[PlayerIndex.Four]=CurrentGamePadState[PlayerIndex.Four]; PreviousTouchLocationState=CurrentTouchLocationState; PreviousKeyboardState=CurrentKeyboardState; } privatevoidAccelerometerReadingChanged(objectsender,AccelerometerReadingEventArgse) { currentAccelerometerReading.X=(float)e.X; currentAccelerometerReading.Y=(float)e.Y; currentAccelerometerReading.Z=(float)e.Z; } //添加一个键盘输入 publicvoidAddKeyboardInput(KeystheKey,boolisReleasedPreviously) { if(keyboardInputs.ContainsKey(theKey)) { keyboardInputs[theKey]=isReleasedPreviously; return; } keyboardInputs.Add(theKey,isReleasedPreviously); } //添加一个游戏按钮输入 publicvoidAddGamepadInput(ButtonstheButton,boolisReleasedPreviously) { if(gamepadInputs.ContainsKey(theButton)) { gamepadInputs[theButton]=isReleasedPreviously; return; } gamepadInputs.Add(theButton,isReleasedPreviously); } //添加一个手势单击输入 publicvoidAddTouchTapInput(RectangletheTouchArea,boolisReleasedPreviously) { if(touchTapInputs.ContainsKey(theTouchArea)) { touchTapInputs[theTouchArea]=isReleasedPreviously; return; } touchTapInputs.Add(theTouchArea,isReleasedPreviously); } //添加一个手势滑动输入 publicvoidAddTouchSlideInput(DirectiontheDirection,floatslideDistance) { if(touchSlideInputs.ContainsKey(theDirection)) { touchSlideInputs[theDirection]=slideDistance; return; } touchSlideInputs.Add(theDirection,slideDistance); } publicboolPinchGestureAvailable=false;//手势是否可用 publicvoidAddTouchGesture(GestureTypetheGesture,RectangletheTouchArea) { TouchPanel.EnabledGestures=theGesture|TouchPanel.EnabledGestures; gestureInputs.Add(gestureInputs.Count,newGestureDefinition(theGesture,theTouchArea)); if(theGesture==GestureType.Pinch) { PinchGestureAvailable=true; } } staticprivateboolisAccelerometerStarted=false;//重力加速是否可用 publicvoidAddAccelerometerInput(Directiondirection,floattiltThreshold) { if(!isAccelerometerStarted) { try { accelerometerSensor.Start(); isAccelerometerStarted=true; } catch(AccelerometerFailedExceptione) { isAccelerometerStarted=false; System.Diagnostics.Debug.WriteLine(e.Message); } } accelerometerInputs.Add(direction,tiltThreshold); } publicvoidRemoveAccelerometerInputs() { if(isAccelerometerStarted) { try { accelerometerSensor.Stop(); isAccelerometerStarted=false; } catch(AccelerometerFailedExceptione) { //Thesensorcouldn'tbestopped. System.Diagnostics.Debug.WriteLine(e.Message); } } accelerometerInputs.Clear(); } staticpublicboolIsConnected(PlayerIndexthePlayerIndex) { returnCurrentGamePadState[thePlayerIndex].IsConnected; } //是否选中玩家 publicboolIsPressed(PlayerIndexthePlayerIndex) { returnIsPressed(thePlayerIndex,null); } publicboolIsPressed(PlayerIndexthePlayerIndex,Rectangle?theCurrentObjectLocation) { if(IsKeyboardInputPressed()) { returntrue; } if(IsGamepadInputPressed(thePlayerIndex)) { returntrue; } if(IsTouchTapInputPressed()) { returntrue; } if(IsTouchSlideInputPressed()) { returntrue; } //点钟矩形区域 if(IsGestureInputPressed(theCurrentObjectLocation)) { returntrue; } returnfalse; } privateboolIsKeyboardInputPressed() { foreach(KeysaKeyinkeyboardInputs.Keys) { if(keyboardInputs[aKey] &&CurrentKeyboardState.IsKeyDown(aKey) &&!PreviousKeyboardState.IsKeyDown(aKey)) { returntrue; } elseif(!keyboardInputs[aKey] &&CurrentKeyboardState.IsKeyDown(aKey)) { returntrue; } } returnfalse; } privateboolIsGamepadInputPressed(PlayerIndexthePlayerIndex) { foreach(ButtonsaButtoningamepadInputs.Keys) { if(gamepadInputs[aButton] &&CurrentGamePadState[thePlayerIndex].IsButtonDown(aButton) &&!PreviousGamePadState[thePlayerIndex].IsButtonDown(aButton)) { returntrue; } elseif(!gamepadInputs[aButton] &&CurrentGamePadState[thePlayerIndex].IsButtonDown(aButton)) { returntrue; } } returnfalse; } privateboolIsTouchTapInputPressed() { foreach(RectangletouchAreaintouchTapInputs.Keys) { if(touchTapInputs[touchArea] &&touchArea.Intersects(CurrentTouchRectangle) &&PreviousTouchPosition()==null) { returntrue; } elseif(!touchTapInputs[touchArea] &&touchArea.Intersects(CurrentTouchRectangle)) { returntrue; } } returnfalse; } privateboolIsTouchSlideInputPressed() { foreach(DirectionslideDirectionintouchSlideInputs.Keys) { if(CurrentTouchPosition()!=null&&PreviousTouchPosition()!=null) { switch(slideDirection) { caseDirection.Up: { if(CurrentTouchPosition().Value.Y+touchSlideInputs[slideDirection] <PreviousTouchPosition().Value.Y) { returntrue; } break; } caseDirection.Down: { if(CurrentTouchPosition().Value.Y-touchSlideInputs[slideDirection] >PreviousTouchPosition().Value.Y) { returntrue; } break; } caseDirection.Left: { if(CurrentTouchPosition().Value.X+touchSlideInputs[slideDirection] <PreviousTouchPosition().Value.X) { returntrue; } break; } caseDirection.Right: { if(CurrentTouchPosition().Value.X-touchSlideInputs[slideDirection] >PreviousTouchPosition().Value.X) { returntrue; } break; } } } } returnfalse; } privateboolIsGestureInputPressed(Rectangle?theNewDetectionLocation) { currentGestureDefinition=null; if(detectedGestures.Count==0)returnfalse; //ChecktoseeifanyoftheGesturesdefinedinthegestureInputs //dictionaryhavebeenperformedanddetected. foreach(GestureDefinitionuserDefinedGestureingestureInputs.Values) { foreach(GestureDefinitiondetectedGestureindetectedGestures) { if(detectedGesture.Type==userDefinedGesture.Type) { //IfaRectangleareatocheckagainsthasbeenpassedin,then //usethatone,otherwiseusetheoneoriginallydefined RectangleareaToCheck=userDefinedGesture.CollisionArea; if(theNewDetectionLocation!=null) areaToCheck=(Rectangle)theNewDetectionLocation; //Ifthegesturedetectedwasmadeintheareawhereuserswere //interestedinInput(theyintersect),thenagestureinputis //considereddetected. if(detectedGesture.CollisionArea.Intersects(areaToCheck)) { if(currentGestureDefinition==null) { currentGestureDefinition=newGestureDefinition(detectedGesture.Gesture); } else { //SomegestureslikeFreeDragandFlickareregisteredmany, //manytimesinasingleUpdateframe.Sincethereisonly //onevariabletostorethegestureinfo,youmustaddon //anyadditionalgesturevaluessothereisacombination //ofallthegestureinformationincurrentGesture currentGestureDefinition.Delta+=detectedGesture.Delta; currentGestureDefinition.Delta2+=detectedGesture.Delta2; currentGestureDefinition.Position+=detectedGesture.Position; currentGestureDefinition.Position2+=detectedGesture.Position2; } } } } } if(currentGestureDefinition!=null)returntrue; returnfalse; } privateboolIsAccelerometerInputPressed() { foreach(KeyValuePair<Direction,float>inputinaccelerometerInputs) { switch(input.Key) { caseDirection.Up: { if(Math.Abs(currentAccelerometerReading.Y)>input.Value &&currentAccelerometerReading.Y<0) { returntrue; } break; } caseDirection.Down: { if(Math.Abs(currentAccelerometerReading.Y)>input.Value &&currentAccelerometerReading.Y>0) { returntrue; } break; } caseDirection.Left: { if(Math.Abs(currentAccelerometerReading.X)>input.Value &&currentAccelerometerReading.X<0) { returntrue; } break; } caseDirection.Right: { if(Math.Abs(currentAccelerometerReading.X)>input.Value &&currentAccelerometerReading.X>0) { returntrue; } break; } } } returnfalse; } GestureDefinitioncurrentGestureDefinition; publicVector2CurrentGesturePosition() { if(currentGestureDefinition==null) returnVector2.Zero; returncurrentGestureDefinition.Position; } publicVector2CurrentGesturePosition2() { if(currentGestureDefinition==null) returnVector2.Zero; returncurrentGestureDefinition.Position2; } publicVector2CurrentGestureDelta() { if(currentGestureDefinition==null) returnVector2.Zero; returncurrentGestureDefinition.Delta; } publicVector2CurrentGestureDelta2() { if(currentGestureDefinition==null) returnVector2.Zero; returncurrentGestureDefinition.Delta2; } publicVector2?CurrentTouchPosition() { foreach(TouchLocationlocationinCurrentTouchLocationState) { switch(location.State) { caseTouchLocationState.Pressed: returnlocation.Position; caseTouchLocationState.Moved: returnlocation.Position; } } returnnull; } privateVector2?PreviousTouchPosition() { foreach(TouchLocationlocationinPreviousTouchLocationState) { switch(location.State) { caseTouchLocationState.Pressed: returnlocation.Position; caseTouchLocationState.Moved: returnlocation.Position; } } returnnull; } privateRectangleCurrentTouchRectangle { get { Vector2?touchPosition=CurrentTouchPosition(); if(touchPosition==null) returnRectangle.Empty; returnnewRectangle((int)touchPosition.Value.X-5, (int)touchPosition.Value.Y-5, 10, 10); } } publicVector3CurrentAccelerometerReading { get { returncurrentAccelerometerReading; } } } }