protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (IsClicked) { // Button was clicked with mouse. No need for more checks. return; } if (!IsLoaded) { return; } var inputService = InputService; // Repeat button behavior and mouse. if (IsRepeatButton) { if (IsMouseOver && IsDown && inputService.IsPressed(MouseButtons.Left, true)) { inputService.IsMouseOrTouchHandled = true; IsClicked = true; } // Repeat button behavior and keyboard. if (IsFocusWithin) { if (!inputService.IsKeyboardHandled && (inputService.IsPressed(Keys.Enter, true) || inputService.IsPressed(Keys.Space, true))) { inputService.IsKeyboardHandled = true; IsClicked = true; } // Repeat button behavior and gamepad. if (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsPressed(Buttons.A, true, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); IsClicked = true; } } } bool isDefault = IsDefault; bool isCancel = IsCancel; if (isDefault || isCancel) { // Handling IsDefault, IsCancel and keyboard. if (!inputService.IsKeyboardHandled) { if ((isDefault && inputService.IsPressed(Keys.Enter, false)) || (isDefault && inputService.IsPressed(Keys.Space, false)) || (isCancel && inputService.IsPressed(Keys.Escape, false))) { inputService.IsKeyboardHandled = true; IsClicked = true; } } // Handling IsDefault, IsCancel and gamepad. if (!inputService.IsGamePadHandled(context.AllowedPlayer)) { if ((isDefault && inputService.IsPressed(Buttons.A, false, context.AllowedPlayer)) || (isDefault && inputService.IsPressed(Buttons.Start, false, context.AllowedPlayer)) || (isCancel && inputService.IsPressed(Buttons.B, false, context.AllowedPlayer)) || (isCancel && inputService.IsPressed(Buttons.Back, false, context.AllowedPlayer))) { inputService.SetGamePadHandled(context.AllowedPlayer, true); IsClicked = true; } } } }
protected override void OnHandleInput(InputContext context) { var inputService = InputService; if (_isTouchDevice) { // Scrolling on phone/tablet has priority over the actions of the visual children. // Therefore base.OnHandleInput() is called after this code section. Vector2F mousePosition = inputService.MousePosition; float t = (float)context.DeltaTime.TotalSeconds; if (_isDragging) { if (inputService.IsMouseOrTouchHandled || inputService.IsUp(MouseButtons.Left)) { // Dragging ends. _isDragging = false; // Check flick gesture. foreach (var gesture in inputService.Gestures) { if (gesture.GestureType == GestureType.Flick) { // Flick detected. // --> Set a scroll velocity proportional to the flick delta. _scrollVelocity = (Vector2F)(-gesture.Delta * FlickScrollVelocityFactor / t); _scrollVelocity = Vector2F.Clamp(_scrollVelocity, -MaxScrollVelocity, MaxScrollVelocity); inputService.IsMouseOrTouchHandled = true; break; } } } else { // Dragging continues. bool canScrollHorizontally = (ExtentWidth > ViewportWidth); bool canScrollVertically = (ExtentHeight > ViewportHeight); if (!_scrollToleranceExceeded) { // Check if drag tolerance has been exceeded. if (canScrollHorizontally && Math.Abs(mousePosition.X - _scrollStartPosition.X) > _scrollThreshold || canScrollVertically && Math.Abs(mousePosition.Y - _scrollStartPosition.Y) > _scrollThreshold) { // Start dragging. (Use current mouse position to avoid a "jump".) _scrollStartPosition = mousePosition; _scrollToleranceExceeded = true; } } if (_scrollToleranceExceeded) { // Drag content. if (canScrollHorizontally && inputService.MousePositionDelta.X != 0 || canScrollVertically && inputService.MousePositionDelta.Y != 0) { inputService.IsMouseOrTouchHandled = true; Vector2F minOffset = new Vector2F(0, 0); Vector2F maxOffset = new Vector2F(Math.Max(ExtentWidth - ViewportWidth, 0), Math.Max(ExtentHeight - ViewportHeight, 0)); Vector2F minVirtualOffset = minOffset - new Vector2F(SpringLength); Vector2F maxVirtualOffset = maxOffset + new Vector2F(SpringLength); Vector2F newOffset = _scrollStartOffset + _scrollStartPosition - mousePosition; if (canScrollHorizontally) { HorizontalOffset = MathHelper.Clamp(newOffset.X, minOffset.X, maxOffset.X); _virtualOffset.X = MathHelper.Clamp(newOffset.X, minVirtualOffset.X, maxVirtualOffset.X); } if (canScrollVertically) { VerticalOffset = MathHelper.Clamp(newOffset.Y, minOffset.Y, maxOffset.Y); _virtualOffset.Y = MathHelper.Clamp(newOffset.Y, minVirtualOffset.Y, maxVirtualOffset.Y); } _scrollVelocity = -inputService.MousePositionDelta / t; _scrollVelocity = Vector2F.Clamp(_scrollVelocity, -MaxScrollVelocity, MaxScrollVelocity); } } } } else { if (!inputService.IsMouseOrTouchHandled && inputService.IsPressed(MouseButtons.Left, false) && IsMouseOver) { // Dragging starts. _isDragging = true; // Remember the mouse position. _scrollStartPosition = mousePosition; _scrollStartOffset = _virtualOffset; _scrollToleranceExceeded = false; } } if (!inputService.IsMouseOrTouchHandled && inputService.IsDown(MouseButtons.Left)) _scrollVelocity = Vector2F.Zero; } base.OnHandleInput(context); if (!IsLoaded) return; // Mouse wheel scrolls vertically when the mouse cursor is over the scroll viewer. if (!inputService.IsMouseOrTouchHandled && IsMouseOver) { if (inputService.MouseWheelDelta != 0 && VerticalScrollBarVisibility != ScrollBarVisibility.Disabled && _verticalScrollBar != null) { inputService.IsMouseOrTouchHandled = true; var screen = Screen; float offset = inputService.MouseWheelDelta / screen.MouseWheelScrollDelta * screen.MouseWheelScrollLines; offset *= _verticalScrollBar.SmallChange; offset = VerticalOffset - offset; if (offset < 0) offset = 0; if (offset > ExtentHeight - ViewportHeight) offset = ExtentHeight - ViewportHeight; VerticalOffset = offset; } } // Scroll with game pad right stick. if (!inputService.IsGamePadHandled(context.AllowedPlayer)) { var gamePadState = inputService.GetGamePadState(context.AllowedPlayer); Vector2 rightStick = gamePadState.ThumbSticks.Right; float x = rightStick.X; float y = rightStick.Y; if (!Numeric.IsZero(x + y) && IsInActiveWindow()) { if (_horizontalScrollBar != null) { float offset = HorizontalOffset + 0.5f * x * _horizontalScrollBar.SmallChange; offset = MathHelper.Clamp(offset, 0, ExtentWidth - ViewportWidth); HorizontalOffset = offset; } if (_verticalScrollBar != null) { float offset = VerticalOffset - 0.5f * y * _verticalScrollBar.SmallChange; offset = MathHelper.Clamp(offset, 0, ExtentHeight - ViewportHeight); VerticalOffset = offset; } inputService.SetGamePadHandled(context.AllowedPlayer, true); } } }
protected override void OnHandleInput(InputContext context) { if (GlobalSettings.PlatformID != PlatformID.WindowsPhone8) { ContinueDraggingSelection(context); } base.OnHandleInput(context); if (!IsLoaded) return; var inputService = InputService; if (GlobalSettings.PlatformID != PlatformID.WindowsStore && GlobalSettings.PlatformID != PlatformID.WindowsPhone8 && GlobalSettings.PlatformID != PlatformID.Android && GlobalSettings.PlatformID != PlatformID.iOS) { var screen = Screen; bool isMouseOver = IsMouseOver; if (isMouseOver && !inputService.IsMouseOrTouchHandled) { if (inputService.IsDoubleClick(MouseButtons.Left) && !_mouseDownPosition.IsNaN && (_mouseDownPosition - context.MousePosition).LengthSquared() < MinDragDistanceSquared) { // Double-click with left mouse button --> Select word or white-space. inputService.IsMouseOrTouchHandled = true; int index = GetIndex(context.MousePosition, screen); SelectWordOrWhiteSpace(index); StartDraggingSelection(context); } else if (inputService.IsPressed(MouseButtons.Left, false)) { // Left mouse button pressed --> Position caret. inputService.IsMouseOrTouchHandled = true; int index = GetIndex(context.MousePosition, screen); _selectionStart = index; CaretIndex = index; StartDraggingSelection(context); } else { // Check for other mouse interactions. _isDraggingSelection = false; if (inputService.MouseWheelDelta != 0 && IsMultiline) { // Mouse wheel over the text box --> Scroll vertically. inputService.IsMouseOrTouchHandled = true; float delta = inputService.MouseWheelDelta / screen.MouseWheelScrollDelta * screen.MouseWheelScrollLines; delta *= _verticalScrollBar.SmallChange; VisualOffset = MathHelper.Clamp(VisualOffset - delta, 0, _verticalScrollBar.Maximum); _verticalScrollBar.Value = VisualOffset; InvalidateArrange(); } } } if (!_isDraggingSelection && IsFocusWithin) HandleKeyboardInput(); } else { // Windows phone: The guide is shown when the touch is released over the box. bool isMouseOver = IsMouseOver; if (inputService.IsMouseOrTouchHandled) { _isPressed = false; } else if (_isPressed && isMouseOver && inputService.IsReleased(MouseButtons.Left)) { ShowGuide(inputService.GetLogicalPlayer(context.AllowedPlayer)); inputService.IsMouseOrTouchHandled = true; _isPressed = false; } else if (_isPressed && (!isMouseOver || inputService.IsUp(MouseButtons.Left))) { _isPressed = false; } else if (isMouseOver && inputService.IsPressed(MouseButtons.Left, false)) { _isPressed = true; inputService.IsMouseOrTouchHandled = true; } } // Xbox: Guide is shown when gamepad A is pressed. if (IsFocusWithin) { if (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsPressed(Buttons.A, false, context.AllowedPlayer)) { ShowGuide(inputService.GetLogicalPlayer(context.AllowedPlayer)); inputService.SetGamePadHandled(context.AllowedPlayer, true); } } }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) { return; } var inputService = InputService; float change = 0; float value = Value; float minimum = Minimum; float maximum = Maximum; float range = maximum - minimum; Vector4 padding = Padding; if (IsFocusWithin) { // Change the value if Keyboard left/right/home/end is pressed. if (!inputService.IsKeyboardHandled) { if (inputService.IsPressed(Keys.Left, true)) { inputService.IsKeyboardHandled = true; change -= Math.Sign(range) * SmallChange; } if (inputService.IsPressed(Keys.Right, true)) { inputService.IsKeyboardHandled = true; change += Math.Sign(range) * SmallChange; } if (inputService.IsPressed(Keys.Home, true)) { inputService.IsKeyboardHandled = true; Value = minimum; } if (inputService.IsPressed(Keys.End, true)) { inputService.IsKeyboardHandled = true; Value = maximum; } } // Change value if left thumb stick or DPad is pressed. if (!inputService.IsGamePadHandled(context.AllowedPlayer)) { if ((inputService.IsPressed(Buttons.LeftThumbstickLeft, true, context.AllowedPlayer)) || (inputService.IsPressed(Buttons.DPadLeft, true, context.AllowedPlayer))) { inputService.SetGamePadHandled(context.AllowedPlayer, true); change -= Math.Sign(range) * SmallChange; } if ((inputService.IsPressed(Buttons.LeftThumbstickRight, true, context.AllowedPlayer)) || (inputService.IsPressed(Buttons.DPadRight, true, context.AllowedPlayer))) { inputService.SetGamePadHandled(context.AllowedPlayer, true); change += Math.Sign(range) * SmallChange; } } } if (!inputService.IsMouseOrTouchHandled) { // Handle mouse clicks. // Remember real physical mouse button presses on slider. if (IsMouseDirectlyOver && inputService.IsPressed(MouseButtons.Left, false)) { _isPressed = true; } // While pressed, the slider "captures" mouse input. if (_isPressed) { inputService.IsMouseOrTouchHandled = true; } // If the slider was pressed, virtual key presses are registered so that the slider // works like a repeat button. if (_isPressed && IsMouseDirectlyOver && inputService.IsPressed(MouseButtons.Left, true)) { float thumbPosition = ActualX + (ActualWidth - padding.X - padding.Z) * (value - minimum) / range; if (context.MousePosition.X < thumbPosition) { change -= Math.Sign(range) * LargeChange; } else { change += Math.Sign(range) * LargeChange; } } else if (inputService.IsUp(MouseButtons.Left)) { _isPressed = false; } } else { _isPressed = false; } // Handle thumb dragging. if (_thumb != null && _thumb.IsDragging) { float contentWidth = ActualWidth - padding.X - padding.Z - _thumb.ActualWidth; change += _thumb.DragDelta.X / contentWidth * range; } if (change != 0.0f) { // Set new value. Value = value + change; } }
private void HandleReleaseMode(InputContext context) { var inputService = InputService; if (!IsDown) { // Check if button gets pressed down. if (IsMouseOver && !inputService.IsMouseOrTouchHandled && inputService.IsPressed(MouseButtons.Left, false)) { inputService.IsMouseOrTouchHandled = true; IsDown = true; } if (IsFocusWithin && !inputService.IsKeyboardHandled && (inputService.IsPressed(Keys.Enter, false) || inputService.IsPressed(Keys.Space, false))) { inputService.IsKeyboardHandled = true; IsDown = true; } if (IsFocusWithin && !inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsPressed(Buttons.A, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); IsDown = true; } } else { if ((!inputService.IsMouseOrTouchHandled && inputService.IsDown(MouseButtons.Left)) || (!inputService.IsKeyboardHandled && (inputService.IsDown(Keys.Enter) || inputService.IsDown(Keys.Space))) || (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsDown(Buttons.A, context.AllowedPlayer)) ) { // IsDown stays true. } else { // Released! IsDown = false; // A click is created only if the release comes from the mouse over the control, or if // the release comes from a button/key when the control is focused. if (IsMouseOver && !inputService.IsMouseOrTouchHandled && inputService.IsReleased(MouseButtons.Left) || IsFocusWithin && !inputService.IsKeyboardHandled && (inputService.IsReleased(Keys.Enter) || inputService.IsReleased(Keys.Space)) || IsFocusWithin && !inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsReleased(Buttons.A, context.AllowedPlayer) ) { IsClicked = true; } } // Input is still captured for this frame. inputService.IsMouseOrTouchHandled = true; inputService.IsKeyboardHandled = true; inputService.SetGamePadHandled(context.AllowedPlayer, true); } }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) return; var screen = Screen; var inputService = InputService; var uiService = UIService; // Limit the console content. LimitText(); // Scroll with mouse wheel when mouse is over. if (IsMouseOver && !inputService.IsMouseOrTouchHandled) { if (inputService.MouseWheelDelta != 0) { inputService.IsMouseOrTouchHandled = true; LineOffset += (int)inputService.MouseWheelDelta / screen.MouseWheelScrollDelta * screen.MouseWheelScrollLines; } } // Move caret with left stick and d-pad. // Move through history with left stick and d-pad. // Scroll with right stick. if (!inputService.IsGamePadHandled(context.AllowedPlayer)) { if (inputService.IsPressed(Buttons.DPadLeft, true, context.AllowedPlayer) || inputService.IsPressed(Buttons.LeftThumbstickLeft, true, context.AllowedPlayer)) { inputService.IsGamePadHandled(context.AllowedPlayer); CaretIndex--; } if (inputService.IsPressed(Buttons.DPadRight, true, context.AllowedPlayer) || inputService.IsPressed(Buttons.LeftThumbstickRight, true, context.AllowedPlayer)) { inputService.IsGamePadHandled(context.AllowedPlayer); CaretIndex++; } if (inputService.IsPressed(Buttons.DPadUp, true, context.AllowedPlayer) || inputService.IsPressed(Buttons.LeftThumbstickUp, true, context.AllowedPlayer)) { inputService.IsGamePadHandled(context.AllowedPlayer); HistoryUp(); } if (inputService.IsPressed(Buttons.DPadDown, true, context.AllowedPlayer) || inputService.IsPressed(Buttons.LeftThumbstickDown, true, context.AllowedPlayer)) { inputService.IsGamePadHandled(context.AllowedPlayer); HistoryDown(); } if (inputService.IsPressed(Buttons.RightThumbstickUp, true, context.AllowedPlayer)) { inputService.IsGamePadHandled(context.AllowedPlayer); LineOffset++; } if (inputService.IsPressed(Buttons.RightThumbstickDown, true, context.AllowedPlayer)) { inputService.IsGamePadHandled(context.AllowedPlayer); LineOffset--; } } if (!inputService.IsKeyboardHandled && IsFocusWithin && inputService.PressedKeys.Count > 0) { int numberOfPressedKeys = inputService.PressedKeys.Count; // Handle ChatPadOrange/Green. if (inputService.IsPressed(Keys.ChatPadOrange, false)) { if (_chatPadOrangeIsActive) _chatPadOrangeIsActive = false; // ChatPadOrange is pressed a second time to disable the mode. else if (numberOfPressedKeys == 1) _chatPadOrangeIsActive = true; // ChatPadOrange is pressed alone to enable the mode. } if (inputService.IsPressed(Keys.ChatPadGreen, false)) { if (_chatPadGreenIsActive) _chatPadOrangeIsActive = false; // ChatPadGreen is pressed a second time to disable the mode. else if (numberOfPressedKeys == 1) _chatPadGreenIsActive = true; // ChatPadGreen is pressed alone to enable the mode. } // Check which modifier keys are pressed. We check this manually to detect ChatPadOrange/Green. ModifierKeys modifierKeys = ModifierKeys.None; if (inputService.IsDown(Keys.LeftShift) || inputService.IsDown(Keys.RightShift)) modifierKeys = modifierKeys | ModifierKeys.Shift; if (inputService.IsDown(Keys.LeftControl) || inputService.IsDown(Keys.RightControl)) modifierKeys = modifierKeys | ModifierKeys.Control; if (inputService.IsDown(Keys.LeftAlt) || inputService.IsDown(Keys.RightAlt)) modifierKeys = modifierKeys | ModifierKeys.Alt; if (_chatPadGreenIsActive || inputService.IsDown(Keys.ChatPadGreen)) modifierKeys = modifierKeys | ModifierKeys.ChatPadGreen; if (_chatPadOrangeIsActive || inputService.IsDown(Keys.ChatPadOrange)) modifierKeys = modifierKeys | ModifierKeys.ChatPadOrange; #else if (inputService.IsDown(Keys.Shift)) modifierKeys = modifierKeys | ModifierKeys.Shift; if (inputService.IsDown(Keys.Ctrl)) modifierKeys = modifierKeys | ModifierKeys.Control; if (inputService.IsDown(Keys.Alt)) modifierKeys = modifierKeys | ModifierKeys.Alt; if (inputService.IsPressed(Keys.Enter, true)) { // ----- Enter --> Add current text to TextLines and raise CommandEntered. string text = Text.ToString(); Text.Clear(); WriteLine(Prompt + text); if (!string.IsNullOrEmpty(text)) { // Add history entry. History.Remove(text); // If the entry exists, we want to move it to the front. History.Add(text); } // Raise CommandExecuted event. string[] args = ParseCommand(text); if (args.Length > 0) OnCommandEntered(new ConsoleCommandEventArgs(args)); LineOffset = 0; _historyIndex = -1; CaretIndex = 0; InvalidateArrange(); inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Back, true)) { // ----- Backspace --> Delete single character. if (Text.Length > 0 && CaretIndex > 0) { Text.Remove(CaretIndex - 1, 1); CaretIndex--; InvalidateArrange(); } inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Delete, true)) { // ----- Delete --> Delete single character. if (CaretIndex < Text.Length) { Text.Remove(CaretIndex, 1); InvalidateArrange(); } inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Left, true)) { // ----- Caret Left CaretIndex--; inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Right, true)) { // ----- Caret Right CaretIndex++; inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Home, true)) { // ----- Home CaretIndex = 0; inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.End, true)) { // ----- End CaretIndex = Text.Length; inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Up, true)) { // ----- Up --> Get history entry. HistoryUp(); inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.Down, true)) { // ----- Down --> Get history entry. HistoryDown(); inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.PageUp, true)) { // ----- PageUp --> Scroll up. PageUp(); inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.PageDown, true)) { // ----- PageDown --> Scroll down. PageDown(); inputService.IsKeyboardHandled = true; } // Cut/copy/paste if (modifierKeys == ModifierKeys.Control) { if (inputService.IsPressed(Keys.X, true)) { Cut(); inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.C, true)) { Copy(); inputService.IsKeyboardHandled = true; } if (inputService.IsPressed(Keys.V, true)) { Paste(); inputService.IsKeyboardHandled = true; } } for (int i = 0; i < numberOfPressedKeys; i++) { // Add characters to current text. var key = inputService.PressedKeys[i]; char c = uiService.KeyMap[key, modifierKeys]; if (c == 0 || char.IsControl(c)) continue; Text.Insert(CaretIndex, c.ToString()); CaretIndex++; InvalidateArrange(); inputService.IsKeyboardHandled = true; } // Handle ChatPadOrange/Green. if (_chatPadOrangeIsActive && inputService.PressedKeys.Any(k => k != Keys.ChatPadOrange)) _chatPadOrangeIsActive = false; // Any other key is pressed. This disables the ChatPadOrangeMode. if (_chatPadGreenIsActive && inputService.PressedKeys.Any(k => k != Keys.ChatPadGreen)) _chatPadGreenIsActive = false; // Any other key is pressed. This disables the ChatPadGreenMode. } }