protected override void OnHandleInput(InputContext context) { base.OnHandleInput(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; //IsClicked = true; } if (IsFocusWithin && !inputService.IsKeyboardHandled) { inputService.IsKeyboardHandled = true; IsDown = true; //IsClicked = true; } } else { if ((!inputService.IsMouseOrTouchHandled && inputService.IsDown(MouseButtons.Left))) { // IsDown stays true. } else { IsDown = false; } // Input is still captured for this frame. inputService.IsMouseOrTouchHandled = true; inputService.IsKeyboardHandled = true; } }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); IsClicked = false; if (InputService.IsDown(MouseButtons.Left) && IsMouseOver && IsDown == false) { InputService.IsMouseOrTouchHandled = true; IsClicked = true; } IsDown = InputService.IsDown(MouseButtons.Left); if (IsClicked && Clicked != null) Clicked(this, EventArgs.Empty); if (InputService.IsDoubleClick(MouseButtons.Left) && DoubleClicked != null) DoubleClicked(this, EventArgs.Empty); }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) return; var screen = Screen; var inputService = InputService; // ESC --> Close. // Note: We do not check the InputService.IsHandled flags because the popup closes // when ESC is pressed - even if the ESC was caught by another game component. if (inputService.IsDown(Keys.Escape)) { inputService.IsKeyboardHandled = true; Close(); } #if !SILVERLIGHT // Same for BACK or B on gamepad. if (inputService.IsPressed(Buttons.Back, false, context.AllowedPlayer) || inputService.IsPressed(Buttons.B, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); Close(); } #endif // If another control is opened above this popup, then this popup closes. // Exception: Tooltips are okay above the popup. if (screen.Children[screen.Children.Count - 1] != this) { if (screen.Children[screen.Children.Count - 1] != screen.ToolTipManager.ToolTipControl || screen.Children[screen.Children.Count - 2] != this) { Close(); } } // If mouse is pressed somewhere else or if GamePad.A is pressed, we close the context menu. if (!IsMouseOver // If mouse is pressed over context menu, we still have to wait for MouseUp. && (inputService.IsPressed(MouseButtons.Left, false) || inputService.IsPressed(MouseButtons.Right, false))) { Close(); } // Like a normal window: mouse does not act through this popup. if (IsMouseOver) inputService.IsMouseOrTouchHandled = true; }
// Called when this UI control should process input. protected override void OnHandleInput(InputContext context) { // Call base method to update the contained controls first. base.OnHandleInput(context); if (!IsLoaded) return; var inputService = InputService; // We call OnOk() when START is pressed and OnCancel() when BACK is pressed. Before that // we must check if the gamepad input hasn't already been handled by a control inside // this window. (If the drop down button is opened, the BACK button is handled by the // drop down button, and must not close the window!). if (!inputService.IsGamePadHandled(context.AllowedPlayer)) { if (inputService.IsPressed(Buttons.Start, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); OnOK(null, null); } else if (inputService.IsPressed(Buttons.Back, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); OnCancel(null, null); } } }
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; Vector4F padding = Padding; if (!inputService.IsMouseOrTouchHandled) { // Check if "empty" space between thumbs and buttons is clicked. if (IsMouseDirectlyOver && inputService.IsPressed(MouseButtons.Left, false)) _isPressed = true; if (_isPressed) inputService.IsMouseOrTouchHandled = true; if (_isPressed && IsMouseDirectlyOver && inputService.IsPressed(MouseButtons.Left, true)) { // The area between the outer repeat buttons and the thumb acts as repeat button that // causes a LargeChange. if (Orientation == Orientation.Horizontal) { 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 { float thumbPosition = ActualY + (ActualHeight - padding.Y - padding.W) * (value - minimum) / range; if (context.MousePosition.Y < thumbPosition) change -= Math.Sign(range) * LargeChange; else change += Math.Sign(range) * LargeChange; } } else if (inputService.IsUp(MouseButtons.Left)) { _isPressed = false; } } else { _isPressed = false; } if (_thumb != null) { // Handle thumb dragging. if (_thumb.IsDragging && !Numeric.AreEqual(minimum, maximum)) { if (Orientation == Orientation.Horizontal) { float contentWidth = ActualWidth - padding.X - padding.Z - _thumb.ActualWidth; change += _thumb.DragDelta.X / contentWidth * range; } else { float contentHeight = ActualHeight - padding.Y - padding.W - _thumb.ActualHeight; change += _thumb.DragDelta.Y / contentHeight * range; } } } if (change != 0.0f) { // Set new value. Value = value + change; } }
protected override void OnHandleInput(InputContext context) { #if !WP7 && !XBOX #if PORTABLE if (GlobalSettings.PlatformID != PlatformID.WindowsPhone8) #endif { ContinueDraggingSelection(context); } #endif base.OnHandleInput(context); if (!IsLoaded) return; var inputService = InputService; #if !WP7 && !XBOX #if PORTABLE if (GlobalSettings.PlatformID != PlatformID.WindowsStore && GlobalSettings.PlatformID != PlatformID.WindowsPhone8 && GlobalSettings.PlatformID != PlatformID.Android && GlobalSettings.PlatformID != PlatformID.iOS) #endif { 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(); } #endif #if WP7 || PORTABLE #if PORTABLE else #endif { // 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; } } #endif #if XBOX // 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); } } #endif }
private void ContinueDraggingSelection(InputContext context) { if (!_isDraggingSelection) return; if (!IsLoaded) return; var screen = Screen; var inputService = InputService; // The user is dragging the caret (end of selection). if (!inputService.IsMouseOrTouchHandled) { inputService.IsMouseOrTouchHandled = true; if (inputService.IsDown(MouseButtons.Left)) { // Only update the caret position if the mouse has moved. // (This check is necessary because we don't want to clear a selection // created by a double-click.) if ((_mouseDownPosition - context.MousePosition).LengthSquared > MinDragDistanceSquared) { // Update the caret index (= end of selection). CaretIndex = GetIndex(context.MousePosition, screen); } } else { // The user has released the mouse button. StopDraggingSelection(); } } else { // Mouse input has been intercepted. StopDraggingSelection(); } }
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; Vector4F padding = Padding; if (!inputService.IsMouseOrTouchHandled) { // Check if "empty" space between thumbs and buttons is clicked. if (IsMouseDirectlyOver && inputService.IsPressed(MouseButtons.Left, false)) { _isPressed = true; } if (_isPressed) { inputService.IsMouseOrTouchHandled = true; } if (_isPressed && IsMouseDirectlyOver && inputService.IsPressed(MouseButtons.Left, true)) { // The area between the outer repeat buttons and the thumb acts as repeat button that // causes a LargeChange. if (Orientation == Orientation.Horizontal) { 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 { float thumbPosition = ActualY + (ActualHeight - padding.Y - padding.W) * (value - minimum) / range; if (context.MousePosition.Y < thumbPosition) { change -= Math.Sign(range) * LargeChange; } else { change += Math.Sign(range) * LargeChange; } } } else if (inputService.IsUp(MouseButtons.Left)) { _isPressed = false; } } else { _isPressed = false; } if (_thumb != null) { // Handle thumb dragging. if (_thumb.IsDragging && !Numeric.AreEqual(minimum, maximum)) { if (Orientation == Orientation.Horizontal) { float contentWidth = ActualWidth - padding.X - padding.Z - _thumb.ActualWidth; change += _thumb.DragDelta.X / contentWidth * range; } else { float contentHeight = ActualHeight - padding.Y - padding.W - _thumb.ActualHeight; change += _thumb.DragDelta.Y / contentHeight * range; } } } if (change != 0.0f) { // Set new value. Value = value + change; } }
private void ContinueResizeAndDrag(InputContext context) { if (!_isResizing && !_isDragging) { // Nothing to do. return; } var screen = Screen; var inputService = InputService; // ----- Stop dragging/resizing bool cancel = inputService.IsDown(Keys.Escape); if (cancel) { inputService.IsKeyboardHandled = true; RestoreBounds(); } if (cancel // <ESC> cancels dragging/resizing. || inputService.IsUp(MouseButtons.Left) // Mouse button is up. || inputService.IsMouseOrTouchHandled // Mouse was handled by another control. || _isResizing && !CanResize // CanResize has been reset by user during resizing. || _isDragging && !CanDrag) // CanDrag has been reset by user during dragging. { screen.UIService.Cursor = null; _setSpecialCursor = false; _isResizing = false; _isDragging = false; return; } // Clamp mouse position to screen. (Only relevant if game runs in windowed-mode.) Vector2F screenMousePosition = context.ScreenMousePosition; float left = screen.ActualX; float right = left + screen.ActualWidth; float top = screen.ActualY; float bottom = top + screen.ActualHeight; screenMousePosition.X = MathHelper.Clamp(screenMousePosition.X, left, right); screenMousePosition.Y = MathHelper.Clamp(screenMousePosition.Y, top, bottom); Vector2F delta = screenMousePosition - _mouseStartPosition; // Undo render transform of screen. if (screen.HasRenderTransform) delta = screen.RenderTransform.FromRenderDirection(delta); if (delta != Vector2F.Zero) { // ----- Handle ongoing resizing operation. if (_isResizing) { // Resizing only works when there is no render transform or the render // transform origin is the top, left corner. Otherwise, it does not work // correct because resizing the window simultaneously changes the render // transform! // Restore original render transform. var transform = new Rendering.RenderTransform(_startPosition, _startSize.X, _startSize.Y, RenderTransformOrigin, RenderScale, RenderRotation, RenderTranslation); // Apply delta in local space of window. delta = transform.FromRenderDirection(delta); // Ensure limits. if ((_resizeDirection & ResizeDirection.E) != 0) { float width = _startSize.X + delta.X; width = MathHelper.Clamp(width, MinWidth, MaxWidth); delta.X = width - _startSize.X; } else if ((_resizeDirection & ResizeDirection.W) != 0) { float width = _startSize.X - delta.X; width = MathHelper.Clamp(width, MinWidth, MaxWidth); delta.X = _startSize.X - width; } else { delta.X = 0; } if ((_resizeDirection & ResizeDirection.S) != 0) { float height = _startSize.Y + delta.Y; height = MathHelper.Clamp(height, MinHeight, MaxHeight); delta.Y = height - _startSize.Y; } else if ((_resizeDirection & ResizeDirection.N) != 0) { float height = _startSize.Y - delta.Y; height = MathHelper.Clamp(height, MinHeight, MaxHeight); delta.Y = _startSize.Y - height; } else { delta.Y = 0; } Vector2F topLeft = _startPosition; Vector2F bottomRight = _startPosition + _startSize; switch (_resizeDirection) { case ResizeDirection.N: case ResizeDirection.W: case ResizeDirection.NW: topLeft += delta; break; case ResizeDirection.NE: bottomRight.X += delta.X; topLeft.Y += delta.Y; break; case ResizeDirection.E: case ResizeDirection.SE: case ResizeDirection.S: bottomRight += delta; break; case ResizeDirection.SW: topLeft.X += delta.X; bottomRight.Y += delta.Y; break; } topLeft = transform.ToRenderPosition(topLeft); bottomRight = transform.ToRenderPosition(bottomRight); X = topLeft.X; // Note: Setting X, Y changes the render transform! Y = topLeft.Y; transform = RenderTransform; topLeft = transform.FromRenderPosition(topLeft); bottomRight = transform.FromRenderPosition(bottomRight); Width = bottomRight.X - topLeft.X; Height = bottomRight.Y - topLeft.Y; InvalidateArrange(); inputService.IsMouseOrTouchHandled = true; return; } // ----- Handle ongoing dragging operation. if (_isDragging) { X = _startPosition.X + delta.X; Y = _startPosition.Y + delta.Y; InvalidateArrange(); inputService.IsMouseOrTouchHandled = 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; } } #if !SILVERLIGHT // 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--; } } #endif if (!inputService.IsKeyboardHandled && IsFocusWithin && inputService.PressedKeys.Count > 0) { int numberOfPressedKeys = inputService.PressedKeys.Count; #if !SILVERLIGHT // 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. } } #endif // Check which modifier keys are pressed. We check this manually to detect ChatPadOrange/Green. ModifierKeys modifierKeys = ModifierKeys.None; #if !SILVERLIGHT 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; } #endif 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; } #if !SILVERLIGHT // 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. } #endif } }
protected override void OnHandleInput(InputContext context) { var inputService = InputService; #if WP7 || PORTABLE 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; } #endif base.OnHandleInput(context); if (!IsLoaded) return; #if !WP7 && !XBOX // 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; } } #endif // 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); } } }
private void HandlePressMode(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; IsClicked = true; } if (IsFocusWithin && !inputService.IsKeyboardHandled && (inputService.IsPressed(Keys.Enter, false) || inputService.IsPressed(Keys.Space, false))) { inputService.IsKeyboardHandled = true; IsDown = true; IsClicked = true; } #if !SILVERLIGHT if (IsFocusWithin && !inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsPressed(Buttons.A, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); IsDown = true; IsClicked = true; } #endif } else { if ((!inputService.IsMouseOrTouchHandled && inputService.IsDown(MouseButtons.Left)) || (!inputService.IsKeyboardHandled && (inputService.IsDown(Keys.Enter) || inputService.IsDown(Keys.Space))) #if !SILVERLIGHT || (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsDown(Buttons.A, context.AllowedPlayer)) #endif ) { // IsDown stays true. } else { IsDown = false; } // Input is still captured for this frame. inputService.IsMouseOrTouchHandled = true; inputService.IsKeyboardHandled = true; #if !SILVERLIGHT inputService.SetGamePadHandled(context.AllowedPlayer, true); #endif } }
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 !SILVERLIGHT if (IsFocusWithin && !inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsPressed(Buttons.A, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); IsDown = true; } #endif } else { if ((!inputService.IsMouseOrTouchHandled && inputService.IsDown(MouseButtons.Left)) || (!inputService.IsKeyboardHandled && (inputService.IsDown(Keys.Enter) || inputService.IsDown(Keys.Space))) #if !SILVERLIGHT || (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsDown(Buttons.A, context.AllowedPlayer)) #endif ) { // 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)) #if !SILVERLIGHT || IsFocusWithin && !inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsReleased(Buttons.A, context.AllowedPlayer) #endif ) { IsClicked = true; } } // Input is still captured for this frame. inputService.IsMouseOrTouchHandled = true; inputService.IsKeyboardHandled = true; #if !SILVERLIGHT inputService.SetGamePadHandled(context.AllowedPlayer, true); #endif } }
/// <inheritdoc/> protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); IsClicked = false; if (!IsLoaded) return; if (ClickMode == ClickMode.Press) HandlePressMode(context); else HandleReleaseMode(context); }
// Called when the window should handle device input. protected override void OnHandleInput(InputContext context) { // Call base implementation to update window content. base.OnHandleInput(context); if (!IsLoaded) return; var inputService = InputService; // If gamepad was not handled (e.g. by a child control of the window) then we close // the window if the BACK button is pressed. if (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsDown(Buttons.Back, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); Close(); } }
protected override void OnHandleInput(InputContext context) { var screen = Screen; var inputService = InputService; var uiService = UIService; bool mouseOrTouchWasHandled = inputService.IsMouseOrTouchHandled; UIControl oldFocusedControl = screen.FocusManager.FocusedControl; if (mouseOrTouchWasHandled && _setSpecialCursor) { // This window has changed the cursor in the last frame, but in this frame another // window has the mouse. // Minor problem: If the other window has also changed the cursor, then we remove // its special cursor. But this case should be rare. uiService.Cursor = null; _setSpecialCursor = false; } // Continue resizing and dragging if already in progress. ContinueResizeAndDrag(context); base.OnHandleInput(context); if (!IsLoaded) return; // Handling of activation is very complicated. For example: The window is clicked, but // a context menu is opened by this click. And there are other difficult cases... Horror! if (!mouseOrTouchWasHandled) { // The mouse was not handled by any control that handled input before this window; if (!screen.IsFocusWithin // Nothing in window is focused. || IsFocusWithin // This window is focused. || oldFocusedControl == screen.FocusManager.FocusedControl) // The focus was not changed by a visual child. (Don't { // activate window if focus moved to a new context menu or other popup!!!) // Mouse must be over the window and left or right mouse button must be pressed. if (IsMouseOver) { if ((inputService.IsPressed(MouseButtons.Left, false) || inputService.IsPressed(MouseButtons.Right, false))) { Activate(); } } } } // If the focus moves into this window, it should become activated. if (IsFocusWithin && !IsActive) Activate(); // Check whether to start resizing or dragging. StartResizeAndDrag(context); // Update mouse cursor. if ((uiService.Cursor == null || _setSpecialCursor) // Cursor of UIService was set by this window. && (!inputService.IsMouseOrTouchHandled || _isResizing)) // Mouse was not yet handled or is currently resizing. { switch (_resizeDirection) { case ResizeDirection.N: case ResizeDirection.S: uiService.Cursor = screen.Renderer.GetCursor("SizeNS"); _setSpecialCursor = true; break; case ResizeDirection.E: case ResizeDirection.W: uiService.Cursor = screen.Renderer.GetCursor("SizeWE"); _setSpecialCursor = true; break; case ResizeDirection.NE: case ResizeDirection.SW: uiService.Cursor = screen.Renderer.GetCursor("SizeNESW"); _setSpecialCursor = true; break; case ResizeDirection.NW: case ResizeDirection.SE: uiService.Cursor = screen.Renderer.GetCursor("SizeNWSE"); _setSpecialCursor = true; break; default: uiService.Cursor = null; _setSpecialCursor = false; break; } } // Mouse cannot act through a window. if (IsMouseOver) inputService.IsMouseOrTouchHandled = true; if (IsModal) { // Modal windows absorb all input. inputService.IsMouseOrTouchHandled = true; #if !SILVERLIGHT inputService.SetGamePadHandled(context.AllowedPlayer, true); #endif inputService.IsKeyboardHandled = true; } }
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; } #if !SILVERLIGHT // Repeat button behavior and gamepad. if (!inputService.IsGamePadHandled(context.AllowedPlayer) && inputService.IsPressed(Buttons.A, true, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); IsClicked = true; } #endif } } 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; } } #if !SILVERLIGHT // 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; } } #endif } }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) { return; } var screen = Screen; var inputService = InputService; // ESC --> Close drop-down. // Note: We do not check the InputService.IsHandled flags because the popup closes // when ESC is pressed - even if the ESC was caught by another game component. if (inputService.IsDown(Keys.Escape)) { inputService.IsKeyboardHandled = true; Close(); } #if !SILVERLIGHT // BACK on gamepad --> Close drop-down. if (inputService.IsPressed(Buttons.Back, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); #if WP7 || PORTABLE // Special: The SelectedIndex needs to be set to the item that has focus. // (Only on Windows Phone.) #if PORTABLE if (GlobalSettings.PlatformID == PlatformID.WindowsPhone8) #endif { var focusedControl = Screen.FocusManager.FocusedControl; int index = _itemsPanel.Children.IndexOf(focusedControl); if (index >= 0) { Owner.SelectedIndex = index; } } #endif Close(); } // B on gamepad --> Close drop-down. if (inputService.IsPressed(Buttons.B, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); Close(); } #endif // If another control is opened above this popup, then this popup closes. // Exception: Tooltips are okay above the popup. if (screen.Children[screen.Children.Count - 1] != this) { if (screen.Children[screen.Children.Count - 1] != screen.ToolTipManager.ToolTipControl || screen.Children[screen.Children.Count - 2] != this) { Close(); } } // If mouse is pressed somewhere else, we close the drop-down. if (!IsMouseOver && // If mouse is pressed over drop-down, we still have to wait for MouseUp. (inputService.IsPressed(MouseButtons.Left, false) || inputService.IsPressed(MouseButtons.Right, false))) { Close(); } // Like a normal window: mouse does not act through this popup. if (IsMouseOver) { inputService.IsMouseOrTouchHandled = 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; Vector4F 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; } } #if !SILVERLIGHT // 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; } } #endif } 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; } }
protected override void OnHandleInput(InputContext context) { if (_cameraObject.CameraNode == null) return; // The input context contains the mouse position that is used by the UI controls of this // screen. The mouse position is stored in the following properties: // - context.ScreenMousePosition // - context.ScreenMousePositionDelta // - context.MousePosition // - context.MousePositionDelta // // Currently, these properties contain the mouse position relative to the game window. // But the mouse position of the in-game screen is determined by the reticle of the // game camera. We need to make a ray-cast to see which part of the screen is hit and // override the properties. bool screenHit = false; // Get the camera position and the view direction in world space. Vector3F cameraPosition = _cameraObject.CameraNode.PoseWorld.Position; Vector3F cameraDirection = _cameraObject.CameraNode.PoseWorld.ToWorldDirection(Vector3F.Forward); // Create a ray (ideally this shape should be cached and reused). var ray = new RayShape(cameraPosition, cameraDirection, 1000); // We are only interested in the first object that is hit by the ray. ray.StopsAtFirstHit = true; // Create a collision object for this shape. var rayCollisionObject = new CollisionObject(new GeometricObject(ray, Pose.Identity)); // Use the CollisionDomain of the physics simulation to perform a ray cast. ContactSet contactSet = _simulation.CollisionDomain.GetContacts(rayCollisionObject).FirstOrDefault(); if (contactSet != null && contactSet.Count > 0) { // We have hit something :-) // Get the contact information of the ray hit. Contact contact = contactSet[0]; // Get the hit object (one object in the contact set is the ray and the other object is the hit object). CollisionObject hitCollisionObject = (contactSet.ObjectA == rayCollisionObject) ? contactSet.ObjectB : contactSet.ObjectA; RigidBody hitBody = hitCollisionObject.GeometricObject as RigidBody; if (hitBody != null && hitBody.UserData is string && (string)hitBody.UserData == "TV") { // We have hit a dynamic rigid body of a TV object. // Get the normal vector of the contact. var normal = (contactSet.ObjectA == rayCollisionObject) ? -contact.Normal : contact.Normal; // Convert the normal vector to the local space of the TV box. normal = hitBody.Pose.ToLocalDirection(normal); // The InGameUIScreen texture is only mapped onto the -Y sides of the boxes. If the user // looks onto another side, he cannot interact with the game screen. if (normal.Y < 0.5f) { // The user looks onto the TV's front side. Now, we have to map the ray hit position // to the texture coordinate of the InGameUIScreen render target/texture. var localHitPosition = (contactSet.ObjectA == rayCollisionObject) ? contact.PositionBLocal : contact.PositionALocal; var normalizedPosition = GetTextureCoordinate(localHitPosition); // The texture coordinate is in the range [0, 0] to [1, 1]. If we multiply it with the // screen extent to the position in pixels. var inGameScreenMousePosition = normalizedPosition * new Vector2F(ActualWidth, ActualHeight); var inGameScreenMousePositionDelta = inGameScreenMousePosition - _lastMousePosition; // Finally, we can set the mouse positions that are relative to the InGame screen. Hurray! context.ScreenMousePosition = inGameScreenMousePosition; context.ScreenMousePositionDelta = inGameScreenMousePositionDelta; context.MousePosition = inGameScreenMousePosition; context.MousePositionDelta = inGameScreenMousePositionDelta; // Store the mouse position so that we can compute MousePositionDelta in the next frame. _lastMousePosition = context.MousePosition; screenHit = true; } } } if (screenHit) { // Call base class to call HandleInput for all child controls. The child controls will // use the overridden mouse positions. base.OnHandleInput(context); } }
protected override void OnHandleInput(InputContext context) { #if !WP7 && !XBOX #if PORTABLE if (GlobalSettings.PlatformID != PlatformID.WindowsPhone8) #endif { ContinueDraggingSelection(context); } #endif base.OnHandleInput(context); if (!IsLoaded) { return; } var inputService = InputService; #if !WP7 && !XBOX #if PORTABLE if (GlobalSettings.PlatformID != PlatformID.WindowsStore && GlobalSettings.PlatformID != PlatformID.WindowsPhone8 && GlobalSettings.PlatformID != PlatformID.Android && GlobalSettings.PlatformID != PlatformID.iOS) #endif { 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(); } } #endif #if WP7 || PORTABLE #if PORTABLE else #endif { // 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; } } #endif #if XBOX // 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); } } #endif }
private void StartResizeAndDrag(InputContext context) { if (_isResizing || _isDragging) return; bool canDrag = CanDrag; bool canResize = CanResize; if (!canDrag && !canResize) return; var inputService = InputService; // ----- Find out if mouse position is over the border _resizeDirection = ResizeDirection.None; if (!inputService.IsMouseOrTouchHandled // Mouse is available for resizing. && canResize) // Window allows resizing. { // Position relative to window: Vector2F mousePosition = context.MousePosition - new Vector2F(ActualX, ActualY); // Find resize direction. if (IsMouseDirectlyOver) { bool isWest = (0 <= mousePosition.X && mousePosition.X <= ResizeBorder.X); bool isEast = (ActualWidth - ResizeBorder.Z < mousePosition.X && mousePosition.X < ActualWidth); bool isNorth = (0 <= mousePosition.Y && mousePosition.Y < ResizeBorder.Y); bool isSouth = (ActualHeight - ResizeBorder.W <= mousePosition.Y && mousePosition.Y < ActualHeight); if (isSouth && isEast) _resizeDirection = ResizeDirection.SE; else if (isSouth && isWest) _resizeDirection = ResizeDirection.SW; else if (isNorth && isEast) _resizeDirection = ResizeDirection.NE; else if (isNorth && isWest) _resizeDirection = ResizeDirection.NW; else if (isSouth) _resizeDirection = ResizeDirection.S; else if (isEast) _resizeDirection = ResizeDirection.E; else if (isWest) _resizeDirection = ResizeDirection.W; else if (isNorth) _resizeDirection = ResizeDirection.N; } } // ----- Start resizing. if (canResize) { if (_resizeDirection != ResizeDirection.None && inputService.IsPressed(MouseButtons.Left, false)) { _isResizing = true; inputService.IsMouseOrTouchHandled = true; _mouseStartPosition = context.ScreenMousePosition; _startPosition = new Vector2F(ActualX, ActualY); _startSize = new Vector2F(ActualWidth, ActualHeight); BackupBounds(); return; } } // ----- Start dragging. if (canDrag) { // The window can be grabbed on any point that is not a visual child // (except for icon and title). bool isOverDragArea = IsMouseDirectlyOver || (_icon != null && _icon.IsMouseOver) || (_caption != null && _caption.IsMouseOver); if (isOverDragArea && inputService.IsPressed(MouseButtons.Left, false)) { _isDragging = true; inputService.IsMouseOrTouchHandled = true; _mouseStartPosition = context.ScreenMousePosition; _startPosition = new Vector2F(ActualX, ActualY); BackupBounds(); return; } } }
//-------------------------------------------------------------- #region Methods //-------------------------------------------------------------- /// <inheritdoc/> protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) return; var inputService = InputService; // When clicked, call TabControl.Select(). if (IsMouseOver && !inputService.IsMouseOrTouchHandled && inputService.IsPressed(MouseButtons.Left, false)) { inputService.IsMouseOrTouchHandled = true; if (TabControl != null) TabControl.Select(this); } }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) return; var inputService = InputService; if (IsDragging) { if (inputService.IsMouseOrTouchHandled || inputService.IsUp(MouseButtons.Left)) { // Dragging ends. DragDelta = Vector2F.Zero; IsDragging = false; } else { // Dragging continues. Vector2F newOffset = context.MousePosition - new Vector2F(ActualX + ActualWidth / 2, ActualY + ActualHeight / 2); DragDelta = newOffset - _offset; } // Mouse or touch input is "captured" while dragging. inputService.IsMouseOrTouchHandled = true; } else { if (IsMouseOver && inputService.IsPressed(MouseButtons.Left, false)) { inputService.IsMouseOrTouchHandled = true; // Dragging starts. IsDragging = true; DragDelta = Vector2F.Zero; // Store the mouse position offset relative to the control center. _offset = context.MousePosition - new Vector2F(ActualX + ActualWidth / 2, ActualY + ActualHeight / 2); } } }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) return; var screen = Screen; var inputService = InputService; // ESC --> Close drop-down. // Note: We do not check the InputService.IsHandled flags because the popup closes // when ESC is pressed - even if the ESC was caught by another game component. if (inputService.IsDown(Keys.Escape)) { inputService.IsKeyboardHandled = true; Close(); } #if !SILVERLIGHT // BACK on gamepad --> Close drop-down. if (inputService.IsPressed(Buttons.Back, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); #if WP7 || PORTABLE // Special: The SelectedIndex needs to be set to the item that has focus. // (Only on Windows Phone.) #if PORTABLE if (GlobalSettings.PlatformID == PlatformID.WindowsPhone8) #endif { var focusedControl = Screen.FocusManager.FocusedControl; int index = _itemsPanel.Children.IndexOf(focusedControl); if (index >= 0) Owner.SelectedIndex = index; } #endif Close(); } // B on gamepad --> Close drop-down. if (inputService.IsPressed(Buttons.B, false, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); Close(); } #endif // If another control is opened above this popup, then this popup closes. // Exception: Tooltips are okay above the popup. if (screen.Children[screen.Children.Count - 1] != this) { if (screen.Children[screen.Children.Count - 1] != screen.ToolTipManager.ToolTipControl || screen.Children[screen.Children.Count - 2] != this) { Close(); } } // If mouse is pressed somewhere else, we close the drop-down. if (!IsMouseOver // If mouse is pressed over drop-down, we still have to wait for MouseUp. && (inputService.IsPressed(MouseButtons.Left, false) || inputService.IsPressed(MouseButtons.Right, false))) { Close(); } // Like a normal window: mouse does not act through this popup. if (IsMouseOver) inputService.IsMouseOrTouchHandled = true; }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); if (!IsLoaded) return; var screen = Screen; var inputService = InputService; #if !SILVERLIGHT // Gamepad shoulder buttons switches tab items. if (!inputService.IsGamePadHandled(context.AllowedPlayer)) { if (inputService.IsPressed(Buttons.RightShoulder, true, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); // Select next item. if (SelectedIndex < Items.Count - 1) Select(SelectedIndex + 1); // If focus was in the old item, then the focus moves to the new item. if (!IsFocusWithin) screen.FocusManager.Focus(this); } else if (inputService.IsPressed(Buttons.LeftShoulder, true, context.AllowedPlayer)) { inputService.SetGamePadHandled(context.AllowedPlayer, true); // Select previous item. if (SelectedIndex > 0) Select(SelectedIndex - 1); // If focus was in the old item, then the focus moves to the new item. if (!IsFocusWithin) screen.FocusManager.Focus(this); } } #endif }
private void StartDraggingSelection(InputContext context) { if (_selectionStart < 0) { // Start index of selection is not set. return; } _isDraggingSelection = true; _mouseDownPosition = context.MousePosition; }
/// <inheritdoc/> protected override void OnHandleInput(InputContext context) { if (!InputEnabled) return; base.OnHandleInput(context); }
protected override void OnHandleInput(InputContext context) { base.OnHandleInput(context); }
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; } } #if !SILVERLIGHT // 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--; } } #endif if (!inputService.IsKeyboardHandled && IsFocusWithin && inputService.PressedKeys.Count > 0) { int numberOfPressedKeys = inputService.PressedKeys.Count; #if !SILVERLIGHT // 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. } #endif // Check which modifier keys are pressed. We check this manually to detect ChatPadOrange/Green. ModifierKeys modifierKeys = ModifierKeys.None; #if !SILVERLIGHT 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; #endif 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; } #if !SILVERLIGHT // 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. #endif } }
// Called when this control should handle input. protected override void OnHandleInput(InputContext context) { // Call base class. This will automatically handle the input of the visual children. base.OnHandleInput(context); if (!InputService.IsMouseOrTouchHandled && InputService.IsPressed(MouseButtons.Left, false)) { // The mouse is not handled and the left button is pressed. var headerHeight = Header != null ? Header.ActualHeight : 0; if (context.MousePosition.X > ActualX && context.MousePosition.X < ActualX + Padding.X && context.MousePosition.Y > ActualY && context.MousePosition.Y < ActualY + headerHeight) { // The area left of the label was clicked. This is the area where a Expand/Collapse // icon is drawn. --> Switch between expanded and collapsed state. IsExpanded = !IsExpanded; InputService.IsMouseOrTouchHandled = true; } else { // If the mouse was over the Header, this control should be selected. if (Header != null && Header.IsMouseOver && TreeView != null) TreeView.SelectItem(this); } } }