public static void Update(GameTime gameTime) { TouchPanel.EnabledGestures = GestureType.Tap | GestureType.Pinch | GestureType.HorizontalDrag | GestureType.VerticalDrag; _previousMouseState = _currentMouseState; _currentMouseState = Mouse.GetState(); if (TouchPanel.IsGestureAvailable) _currentGestureSample = TouchPanel.ReadGesture(); else _currentGestureSample = null; }
/// <summary> /// Turn off dragging state and reset drag related variables /// </summary> public void ResetDragState() { firstSample = null; prevSample = null; isDragging = false; arrowScale = 0; Catapult.ShotStrength = 0; }
public void HandleInput(GestureSample gestureSample) { // Process input only if in Human's turn if (IsActive) { // Process any Drag gesture if (gestureSample.GestureType == GestureType.FreeDrag) { // If drag just began, save the sample for future // calculations and start Aim "animation" if (null == firstSample) { firstSample = gestureSample; Catapult.CurrentState = CatapultState.Aiming; } // save the current gesture sample prevSample = gestureSample; // calculate the delta between first sample and current // sample to present visual sound on screen Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotStrength = delta.Length() / maxDragDelta; float baseScale = 0.001f; arrowScale = baseScale * delta.Length(); isDragging = true; } else if (gestureSample.GestureType == GestureType.DragComplete) { // calculate velocity based on delta between first and last // gesture samples if (null != firstSample) { Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotVelocity = MinShotStrength + Catapult.ShotStrength * (MaxShotStrength - MinShotStrength); Catapult.Fire(Catapult.ShotVelocity); Catapult.CurrentState = CatapultState.Firing; } // turn off dragging state ResetDragState(); } } }
/// <summary> /// Turn off dragging state and reset drag related variables /// </summary> public void ResetDragState() { firstSample = null; prevSample = null; isDragging = false; arrowScale = 0; Catapult.ShotStrength = 0; deltaSum = Vector2.Zero; }
/// <summary> /// Turn off dragging state and reset drag related variables /// </summary> public void ResetDragState () { firstSample = null; prevSample = null; firstMouseSample = null; prevMouseSample = null; isDragging = false; arrowScale = 0; Catapult.ShotStrength = 0; }
public void ResetDragState() { firstSample = null; prevSample = null; isDragging = false; }
/// <summary> /// Reads the latest state of the keyboard and gamepad. /// </summary> public void Update() { for (int i = 0; i < MaxInputs; i++) { LastKeyboardStates[i] = CurrentKeyboardStates[i]; LastGamePadStates[i] = CurrentGamePadStates[i]; CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i); CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i); // Keep track of whether a gamepad has ever been // connected, so we can detect if it is unplugged. if (CurrentGamePadStates[i].IsConnected) { GamePadWasConnected[i] = true; } } LastMouseState = CurrentMouseState; CurrentMouseState = Mouse.GetState(); //TouchState = TouchPanel.GetState(); Gestures.Clear(); while (TouchPanel.IsGestureAvailable) { Gestures.Add(TouchPanel.ReadGesture()); } TapGesture = null; DragGesture = null; PinchGesture = null; foreach (GestureSample gest in Gestures) { if (gest.GestureType == GestureType.Tap) TapGesture = gest; if (gest.GestureType == GestureType.FreeDrag) DragGesture = gest; if (gest.GestureType == GestureType.Pinch) PinchGesture = gest; } MouseDelta = new Vector2(CurrentMouseState.X - LastMouseState.X, CurrentMouseState.Y - LastMouseState.Y); MouseLeftClick = false; MouseDragging = false; if (LastMouseState.LeftButton == ButtonState.Pressed && CurrentMouseState.LeftButton == ButtonState.Pressed) { mouseHoldCount++; if (mouseHoldCount > 0 && MouseDelta.Length() > 5f) MouseDragging = true; if (mouseHoldCount > 30) MouseDragging = true; } else { if (mouseHoldCount > 0 && mouseHoldCount < 10) MouseLeftClick = true; mouseHoldCount = 0; } TapPosition = null; if (TapGesture.HasValue) TapPosition = TapGesture.Value.Position; if (MouseLeftClick) TapPosition = new Vector2(CurrentMouseState.X, CurrentMouseState.Y); }
/// <summary> /// Function processes the user input /// </summary> /// <param name="gestureSample"></param> public void HandleInput(GestureSample gestureSample) { // Process input only if in Human's turn if (IsActive) { // Process any Drag gesture if (gestureSample.GestureType == GestureType.FreeDrag) { // If drag just began save the sample for future // calculations and start Aim "animation" if (null == firstSample) { firstSample = gestureSample; if (App.g_isTwoHumanPlayers && GlobalContext.IsUDPEnableOnNetwork) { GlobalContext.currentUDPPacketNumber = 1; if (GlobalContext.IsUDPEnableOnNetwork) { WarpClient.GetInstance().SendUDPUpdatePeers(MoveMessage.buildDragingMessageBytes(GlobalContext.currentUDPPacketNumber, gestureSample.Position.X.ToString(), gestureSample.Position.Y.ToString())); } } Catapult.CurrentState = CatapultState.Aiming; } if (App.g_isTwoHumanPlayers && GlobalContext.IsUDPEnableOnNetwork) { tempSample = gestureSample; if (prevSample != null) { Vector2 tempDelta = tempSample.Value.Position - prevSample.Value.Position; if (tempDelta.Length() != 0) { if (skipCounter == 5) { skipCounter = 0; GlobalContext.currentUDPPacketNumber++; WarpClient.GetInstance().SendUDPUpdatePeers(MoveMessage.buildDragingMessageBytes(GlobalContext.currentUDPPacketNumber, gestureSample.Position.X.ToString(), gestureSample.Position.Y.ToString())); } else { skipCounter++; } } } } // save the current gesture sample prevSample = gestureSample; // calculate the delta between first sample and current // sample to present visual sound on screen Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotStrength = delta.Length() / maxDragDelta; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) { Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); } else { Catapult.ShotAngle = MinShotAngle; } float baseScale = 0.001f; arrowScale = baseScale * delta.Length(); isDragging = true; } else if (gestureSample.GestureType == GestureType.DragComplete) { // calc velocity based on delta between first and last // gesture samples if (null != firstSample) { Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) { Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); } else { Catapult.ShotAngle = MinShotAngle; } if (App.g_isTwoHumanPlayers) { //Since Drag is Completed so now we will send SHOT strength and SHOT velocity to remote user //It is the final data for current fire,thats why Here I am also updating property "fireNumber" //so if remote user will get any UDP packet after getting TCP data for current fire,It will ignore them GlobalContext.fireNumber = Convert.ToInt32(GlobalContext.tableProperties["fireNumber"]); GlobalContext.fireNumber++; Dictionary <string, object> fireProperties = new Dictionary <string, object>(); fireProperties.Add("fireNumber", GlobalContext.fireNumber); WarpClient.GetInstance().UpdateRoomProperties(GlobalContext.GameRoomId, fireProperties, null); WarpClient.GetInstance().SendUpdatePeers(MoveMessage.buildSHOTMessageBytes(Catapult.ShotVelocity.ToString(), Catapult.ShotAngle.ToString())); //Here i am resetting this counter because host user has sent his final shot via TCP so in next //step remote user will send his data,before receive remote user UDP packets we need to reset this counter GlobalContext.prevUDPPacketNumber = 0; } Catapult.CurrentState = CatapultState.Firing; } // turn off dragging state ResetDragState(); } } }
public void ResetPinchState() { prevSample = null; currentSample = null; }
/// <summary> /// Input helper method provided by GameScreen. Packages up the various input /// values for ease of use. /// </summary> /// <param name="input">The state of the gamepads</param> public override void HandleInput(InputState input) { if (input == null) { throw new ArgumentNullException("input"); } if (gameOver) { if (input.IsPauseGame(null)) { FinishCurrentGame(); } foreach (GestureSample gestureSample in input.Gestures) { if (gestureSample.GestureType == GestureType.Tap) { FinishCurrentGame(); } } return; } if (input.IsPauseGame(null)) { PauseCurrentGame(); } else if (isCameraMoving) // Handle camera movement { // Read all available gestures foreach (GestureSample gestureSample in input.Gestures) { switch (gestureSample.GestureType) { case GestureType.FreeDrag: if (CatapultTapped(gestureSample.Position)) { // Allow player to fire isCameraMoving = false; CenterOnPosition(player.Catapult.Position + catapultCenterOffset); } else { isDragging = true; // Move screen according to delta Vector2 newOffset = DrawOffset; newOffset += gestureSample.Delta; DrawOffset = ClampDrawOffset(newOffset); } break; case GestureType.DragComplete: // turn off dragging state ResetDragState(); break; case GestureType.Tap: if (isCameraMoving) { isFlying = false; } break; case GestureType.Flick: // Ignore flicks which appear as part of a pinch ending if (lastGestureType != GestureType.PinchComplete) { FlyToPositionNoScale( ScreenCenter - gestureSample.Delta); } break; case GestureType.Pinch: // Store last drag location if (null == prevSample) { prevSample = gestureSample; } else { prevSample = currentSample; } // save the current gesture sample currentSample = gestureSample; float currentLength = (currentSample.Value.Position - currentSample.Value.Position2).Length(); float previousLength = (prevSample.Value.Position - prevSample.Value.Position2).Length(); float scaleChange = (currentLength - previousLength) * 0.05f; Vector2 previousCenter = ScreenCenter; float previousScale = DrawScale; DrawScale += scaleChange; DrawScale = MathHelper.Clamp(DrawScale, MinScale, MaxScale); CenterOnPositionNoScale(previousCenter * DrawScale / previousScale); break; case GestureType.PinchComplete: ResetPinchState(); break; default: break; } lastGestureType = gestureSample.GestureType; } } else if (isHumanTurn && (player.Catapult.CurrentState == CatapultState.Idle || player.Catapult.CurrentState == CatapultState.Aiming)) { // Read all available gestures foreach (GestureSample gestureSample in input.Gestures) { if (gestureSample.GestureType == GestureType.FreeDrag) { isDragging = true; } else if (gestureSample.GestureType == GestureType.DragComplete) { isDragging = false; } player.HandleInput(gestureSample); } } }
public XnaTouchState(TouchCollection touchCollection, TouchPanelCapabilities capabilities, bool isGestureAvailable, GestureSample? gestureSample) { this.touchCollection = touchCollection; this.capabilities = capabilities; this.isGestureAvailable = isGestureAvailable; this.gestureSample = gestureSample; }
/// <summary> /// Function processes the user input /// </summary> /// <param name="gestureSample"></param> public void HandleInput(GestureSample gestureSample) { // Process input only if in Human's turn if (IsActive) { // Process any Drag gesture if (gestureSample.GestureType == GestureType.FreeDrag) { // If drag just began save the sample for future // calculations and start Aim "animation" if (null == firstSample) { firstSample = gestureSample; if (App.g_isTwoHumanPlayers && GlobalContext.IsUDPEnableOnNetwork) { GlobalContext.currentUDPPacketNumber = 1; if(GlobalContext.IsUDPEnableOnNetwork) WarpClient.GetInstance().SendUDPUpdatePeers(MoveMessage.buildDragingMessageBytes(GlobalContext.currentUDPPacketNumber, gestureSample.Position.X.ToString(), gestureSample.Position.Y.ToString())); } Catapult.CurrentState = CatapultState.Aiming; } if (App.g_isTwoHumanPlayers&&GlobalContext.IsUDPEnableOnNetwork) { tempSample = gestureSample; if (prevSample != null) { Vector2 tempDelta = tempSample.Value.Position - prevSample.Value.Position; if (tempDelta.Length() != 0) { if (skipCounter == 5) { skipCounter = 0; GlobalContext.currentUDPPacketNumber++; WarpClient.GetInstance().SendUDPUpdatePeers(MoveMessage.buildDragingMessageBytes(GlobalContext.currentUDPPacketNumber, gestureSample.Position.X.ToString(), gestureSample.Position.Y.ToString())); } else { skipCounter++; } } } } // save the current gesture sample prevSample = gestureSample; // calculate the delta between first sample and current // sample to present visual sound on screen Vector2 delta = prevSample.Value.Position- firstSample.Value.Position; Catapult.ShotStrength = delta.Length() / maxDragDelta; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); else Catapult.ShotAngle = MinShotAngle; float baseScale = 0.001f; arrowScale = baseScale * delta.Length(); isDragging = true; } else if (gestureSample.GestureType == GestureType.DragComplete) { // calc velocity based on delta between first and last // gesture samples if (null != firstSample) { Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); else Catapult.ShotAngle = MinShotAngle; if (App.g_isTwoHumanPlayers) { //Since Drag is Completed so now we will send SHOT strength and SHOT velocity to remote user //It is the final data for current fire,thats why Here I am also updating property "fireNumber" //so if remote user will get any UDP packet after getting TCP data for current fire,It will ignore them GlobalContext.fireNumber = Convert.ToInt32(GlobalContext.tableProperties["fireNumber"]); GlobalContext.fireNumber++; Dictionary<string, object> fireProperties = new Dictionary<string, object>(); fireProperties.Add("fireNumber", GlobalContext.fireNumber); WarpClient.GetInstance().UpdateRoomProperties(GlobalContext.GameRoomId, fireProperties, null); WarpClient.GetInstance().SendUpdatePeers(MoveMessage.buildSHOTMessageBytes(Catapult.ShotVelocity.ToString(), Catapult.ShotAngle.ToString())); //Here i am resetting this counter because host user has sent his final shot via TCP so in next //step remote user will send his data,before receive remote user UDP packets we need to reset this counter GlobalContext.prevUDPPacketNumber = 0; } Catapult.CurrentState = CatapultState.Firing; } // turn off dragging state ResetDragState(); } } }
public IEnumerable<GestureSample> GetSamples() { var events = _touchEventProvider.Events; if (events == null || events.Count == 0) { return null; } var gestures = new List<GestureSample>(); // First capture all the down touches so movement is easier to track // ReSharper disable once ForCanBeConvertedToForeach for (var index = 0; index < events.Count; index++) { var touchEvent = events[index]; if (touchEvent.Action == TouchEvent.TouchEventAction.Down && !_touchPoints.ContainsKey(touchEvent.Id)) { _touchPoints[touchEvent.Id] = touchEvent; events.RemoveAt(index); --index; } } // ReSharper disable once ForCanBeConvertedToForeach // ReSharper disable once LoopCanBeConvertedToQuery for (var index = 0; index < events.Count; index++) { var touchEvent = events[index]; if (touchEvent.Action == TouchEvent.TouchEventAction.Move) { var originalEvent = _touchPoints[touchEvent.Id]; if (originalEvent.TranslatedPosition != touchEvent.TranslatedPosition) { _holdEvent = null; _movingPoints[touchEvent.Id] = touchEvent; #region pinch if (_touchPoints.Count > 1) { // This was a pinch. Figure out if both points moved so we combine gestures var pinchStart = new TouchEvent(); TouchEvent pinchMove = null; foreach (var touchPoint in _touchPoints) { if (touchPoint.Key != touchEvent.Id) { pinchStart = touchPoint.Value; break; } } for (int index2 = index; index2 < events.Count; index2++) { var touchEvent2 = events[index2]; if (touchEvent2.Action == TouchEvent.TouchEventAction.Move && touchEvent2.Id == pinchStart.Id) { pinchMove = touchEvent2; _movingPoints[touchEvent2.Id] = touchEvent2; events.RemoveAt(index2); break; } } if (pinchMove == null) { pinchMove = pinchStart; } _pinchPoints[touchEvent.Id] = _pinchPoints[pinchMove.Id] = new PinchEventGroup { One = touchEvent, Two = pinchMove }; gestures.Add(new GestureSample(GestureType.Pinch, touchEvent.TimeStamp - _startTime, touchEvent.TranslatedPosition, pinchMove.TranslatedPosition, touchEvent.TranslatedPosition - originalEvent.TranslatedPosition, pinchMove.TranslatedPosition - pinchStart.TranslatedPosition, touchEvent.NonTranslatedPosition, pinchMove.NonTranslatedPosition, touchEvent.NonTranslatedPosition - originalEvent.NonTranslatedPosition, pinchMove.NonTranslatedPosition - pinchStart.NonTranslatedPosition)); var originalCenter = (pinchStart.TranslatedPosition - originalEvent.TranslatedPosition)/2.0f; var newCenter = (pinchMove.TranslatedPosition - touchEvent.TranslatedPosition)/2.0f; var originalCenterNontranslated = (pinchStart.TranslatedPosition - originalEvent.TranslatedPosition)/2.0f; var newCenterNontranslated = (pinchMove.TranslatedPosition - touchEvent.TranslatedPosition)/ 2.0f; gestures.Add(new GestureSample(GestureType.FreeDrag, touchEvent.TimeStamp - _startTime, newCenter, Vector2.Zero, newCenter - originalCenter, Vector2.Zero, newCenterNontranslated, Vector2.Zero, newCenterNontranslated - originalCenterNontranslated, Vector2.Zero)); } #endregion #region drag else { gestures.Add(new GestureSample(GestureType.FreeDrag, touchEvent.TimeStamp - _startTime, touchEvent.TranslatedPosition, Vector2.Zero, touchEvent.TranslatedPosition - originalEvent.TranslatedPosition, Vector2.Zero, touchEvent.NonTranslatedPosition, Vector2.Zero, touchEvent.NonTranslatedPosition - originalEvent.NonTranslatedPosition, Vector2.Zero)); } #endregion } else if (_holdEvent == null && touchEvent.TimeStamp - originalEvent.TimeStamp >= TimeSpan.FromSeconds(1)) { gestures.Add(new GestureSample(GestureType.Hold, touchEvent.TimeStamp - _startTime, touchEvent.TranslatedPosition, Vector2.Zero, Vector2.Zero, Vector2.Zero, touchEvent.NonTranslatedPosition, Vector2.Zero, Vector2.Zero, Vector2.Zero)); _holdEvent = touchEvent; } } else if (touchEvent.Action == TouchEvent.TouchEventAction.Up) { _touchPoints.Remove(touchEvent.Id); if (_holdEvent != null) { _holdEvent = null; continue; } if (!_movingPoints.ContainsKey(touchEvent.Id)) { var tapGesture = new GestureSample(GestureType.Tap, touchEvent.TimeStamp - _startTime, touchEvent.TranslatedPosition, Vector2.Zero, Vector2.Zero, Vector2.Zero, touchEvent.NonTranslatedPosition, Vector2.Zero, Vector2.Zero, Vector2.Zero); if (!_lastTap.HasValue) { _lastTap = tapGesture; } else { if (tapGesture.Timestamp - _lastTap.Value.Timestamp > TimeSpan.FromMilliseconds(500)) { _lastTap = tapGesture; } else { tapGesture.GestureType = GestureType.DoubleTap; } } gestures.Add(tapGesture); } else { // The touch was moving, so it's a dragcomplete, but we only want to fire it once // It could be a pinchcomplete too, so maybe i need to keep track of pinching points. _movingPoints.Remove(touchEvent.Id); if (_pinchPoints.ContainsKey(touchEvent.Id)) { var pinchPoint = _pinchPoints[touchEvent.Id]; _pinchPoints.Remove(touchEvent.Id); if (!_partialPinchComplete.Contains(touchEvent, TouchEvent.IdComparer)) { var otherPoint = pinchPoint.One.Id == touchEvent.Id ? pinchPoint.Two : pinchPoint.One; _partialPinchComplete.Add(otherPoint); gestures.Add(new GestureSample(GestureType.PinchComplete, touchEvent.TimeStamp - _startTime, pinchPoint.One.TranslatedPosition, pinchPoint.Two.TranslatedPosition, Vector2.Zero, Vector2.Zero, pinchPoint.One.NonTranslatedPosition, pinchPoint.Two.NonTranslatedPosition, Vector2.Zero, Vector2.Zero)); } } else { gestures.Add(new GestureSample(GestureType.DragComplete, touchEvent.TimeStamp - _startTime, touchEvent.TranslatedPosition, Vector2.Zero, Vector2.Zero, Vector2.Zero, touchEvent.NonTranslatedPosition, Vector2.Zero, Vector2.Zero, Vector2.Zero)); } } } } return gestures; }