/// <summary>Triggers the FocusChanged event</summary> /// <param name="focusedControl">Control that has gotten the input focus</param> private void onFocusChanged(Control focusedControl) { if (FocusChanged != null) { FocusChanged(this, new ControlEventArgs(focusedControl)); } }
public void HideToolTip() { _tooltipTimeout = DateTime.Now; if (_toolTipActive) { OnToolTipHide(); _toolTipActive = false; _toolTipHoveredControl = null; } }
/// <summary>Handle user text input by a physical or virtual keyboard</summary> /// <param name="character">Character that has been entered</param> public void InjectCharacter(char character) { // Send the text to the currently focused control in the GUI Control focusedControl = this.focusedControl.Target; var writable = focusedControl as IWritable; if (writable != null) { writable.OnCharacterEntered(character); } }
/// <summary>Determines whether a control can obtain the input focus</summary> /// <param name="control">Control that will be checked for focusability</param> /// <returns>True if the specified control can obtain the input focus</returns> private static bool canControlGetFocus(Control control) { var focusableControl = control as IFocusable; if (focusableControl != null) { return(focusableControl.CanGetFocus); } else { return(false); } }
/// <summary> /// Returns the currently hovered control /// </summary> /// <param name="c"></param> /// <returns></returns> public Control GetTopControl(Control c = null) { if (c == null) { c = desktopControl; } if (c.MouseOverControl != null && c.MouseOverControl != c) { return(GetTopControl(c.MouseOverControl)); } return(c); }
/// <summary> /// Checks to show tooltip /// </summary> public void Update() { if (!_toolTipActive && ((DateTime.Now - _tooltipTimeout).TotalMilliseconds >= TooltipTimeout)) { var topControl = GetTopControl(); if (topControl.ToolTipEnabled) { OnToolTipShow(new ToolTipEventArgs { Control = topControl, CursorPos = _prevMousePos }); _toolTipHoveredControl = topControl; _toolTipActive = true; } } }
/// <summary>Called when a mouse button has been released again</summary> /// <param name="button">Index of the button that has been released</param> public void InjectMouseRelease(MouseButtons button) { heldMouseButtons &= ~button; // If a control signed responsible for the earlier mouse press, it will now // receive the release notification. if (activatedControl != null) { activatedControl.ProcessMouseRelease(button); } // Reset the activated control if the user has released all buttons on all // input devices. if (!anyKeysOrButtonsPressed) { activatedControl = null; } }
/// <summary>Called when a mouse button has been pressed down</summary> /// <param name="button">Index of the button that has been pressed</param> public void InjectMousePress(MouseButtons button) { HideToolTip(); heldMouseButtons |= button; // If a control is activated, it will receive any input notifications if (activatedControl != null) { activatedControl.ProcessMousePress(button); return; } // No control was activated, so the desktop control becomes activated and // is responsible for routing the input to the control under the mouse. activatedControl = desktopControl; desktopControl.ProcessMousePress(button); }
/// <summary>Called when a key on the keyboard has been released again</summary> /// <param name="keyCode">Code of the key that was released</param> public void InjectKeyRelease(Keys keyCode) { if (!heldKeys.Get((int)keyCode)) { return; } --heldKeyCount; heldKeys.Set((int)keyCode, false); // If a control signed responsible for the earlier key press, it will now // receive the release notification. if (activatedControl != null) { activatedControl.ProcessKeyRelease(keyCode); } // Reset the activated control if the user has released all buttons on all // input devices. if (!anyKeysOrButtonsPressed) { activatedControl = null; } }
public void MessageBox(string message, string title = "", string[] buttonsText = null, System.Action <string> action = null) { var screenWidth = _d3DEngine.ViewPort.Width; var screenHeight = _d3DEngine.ViewPort.Height; var windowWidth = 300; var windowHeight = 100; if (buttonsText == null) { buttonsText = new[] { "Ok" } } ; var mouseCapture = _inputManager.MouseManager.MouseCapture; _inputManager.MouseManager.MouseCapture = false; var mbWindow = new WindowControl { Title = title, Bounds = new UniRectangle((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2, windowWidth, windowHeight) }; mbWindow.Children.Add(new LabelControl { Text = message, Autosizing = true, Bounds = new UniRectangle(15, 25, windowWidth - 40, 40) }); var buttonsPlace = new Control { Bounds = new UniRectangle(0, 0, 0, 20), LayoutFlags = ControlLayoutFlags.WholeRowCenter, LeftTopMargin = new SharpDX.Vector2() }; foreach (var text in buttonsText) { var button = new ButtonControl { Text = text, Bounds = new UniRectangle((windowWidth - 50) / 2, windowHeight - 30, 20 + 5 * text.Length, 20) }; buttonsPlace.Bounds.Size.X += button.Bounds.Size.X + 5; var text1 = text; button.Pressed += delegate { _screen.Desktop.Children.Remove(DialogHelper.DialogBg); mbWindow.Close(); DialogClosed = true; if (action != null) { action(text1); } _inputManager.MouseManager.MouseCapture = mouseCapture; }; buttonsPlace.Children.Add(button); } buttonsPlace.UpdateLayout(); mbWindow.Children.Add(buttonsPlace); mbWindow.UpdateLayout(); _screen.Desktop.Children.Add(mbWindow); // block all underlying controls if (!_screen.Desktop.Children.Contains(DialogHelper.DialogBg)) { _screen.Desktop.Children.Add(DialogHelper.DialogBg); } DialogHelper.DialogBg.BringToFront(); mbWindow.BringToFront(); }
public DialogControl() { // generate inputs controls // supported types: string, int, float, bool, array var type = typeof(T); var fieldInfos = type.GetFields(); int LabelWidth = 80; int EditWidth = 80; foreach (var fieldInfo in fieldInfos) { LabelWidth = Math.Max(LabelWidth, fieldInfo.Name.Length * 6); } foreach (var fieldInfo in fieldInfos) { if (!fieldInfo.IsPublic) { continue; } var label = new LabelControl { Bounds = new UniRectangle(0, 0, LabelWidth, 20), Text = fieldInfo.Name }; Children.Add(label); if (fieldInfo.FieldType == typeof(int)) { var edit = new InputControl { Name = fieldInfo.Name, Bounds = new UniRectangle(0, 0, EditWidth, 20), IsNumeric = true }; Children.Add(edit); Bounds.Size.Y += 25; } if (fieldInfo.FieldType == typeof(float)) { var edit = new InputControl { Name = fieldInfo.Name, Bounds = new UniRectangle(0, 0, EditWidth, 20), IsNumeric = true }; Children.Add(edit); Bounds.Size.Y += 25; } if (fieldInfo.FieldType == typeof(string)) { var edit = new InputControl { Name = fieldInfo.Name, Bounds = new UniRectangle(0, 0, EditWidth, 20) }; Children.Add(edit); Bounds.Size.Y += 25; } if (fieldInfo.FieldType == typeof(bool)) { var edit = new OptionControl { Name = fieldInfo.Name, Bounds = new UniRectangle(0, 0, EditWidth, 20) }; Children.Add(edit); Bounds.Size.Y += 25; } if (fieldInfo.FieldType == typeof(DialogSelection)) { var edit = new ListControl { Name = fieldInfo.Name, Bounds = new UniRectangle(0, 0, EditWidth, 80), SelectionMode = ListSelectionMode.Single }; Children.Add(edit); Bounds.Size.Y += 85; } if (fieldInfo.FieldType == typeof(ByteColor)) { var edit = new ColorButtonControl { Name = fieldInfo.Name, Bounds = new UniRectangle(0, 0, EditWidth, 20) }; edit.Pressed += delegate { var colorDialog = new ColorDialog(); colorDialog.Color = System.Drawing.Color.FromArgb(edit.Color.A, edit.Color.R, edit.Color.G, edit.Color.B); if (colorDialog.ShowDialog() == DialogResult.OK) { edit.Color = new ByteColor(colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B, colorDialog.Color.A); } }; Children.Add(edit); Bounds.Size.Y += 25; } } _buttonOk = new ButtonControl { Text = "Ok", Bounds = new UniRectangle(0, 0, 50, 20) }; _buttonOk.Pressed += delegate { ApplyDialog(); }; _buttonCancel = new ButtonControl { Text = "Cancel", Bounds = new UniRectangle(0, 0, 50, 20) }; _buttonCancel.Pressed += delegate { HideDialog(); }; var buttonsGroup = new Control { Bounds = new UniRectangle(0, 0, 105, 20), LayoutFlags = ControlLayoutFlags.WholeRowCenter, LeftTopMargin = new Vector2() }; buttonsGroup.Children.Add(_buttonOk); buttonsGroup.Children.Add(_buttonCancel); buttonsGroup.UpdateLayout(); Children.Add(buttonsGroup); Bounds.Size.Y += 60; Bounds.Size.X = 120 + LabelWidth; UpdateLayout(); }
/// <summary>Called when a key on the keyboard has been pressed down</summary> /// <param name="keyCode">Code of the key that was pressed</param> public void InjectKeyPress(Keys keyCode) { bool repetition = heldKeys.Get((int)keyCode); if (activatedControl is IKeyPressLookUp) { return; } // If a control is activated, it will receive any input notifications if (activatedControl != null) { activatedControl.ProcessKeyPress(keyCode, repetition); if (!repetition) { ++heldKeyCount; heldKeys.Set((int)keyCode, true); } return; } // No control is activated, try the focused control before searching // the entire tree for a responder. Control focusedControl = this.focusedControl.Target; if (focusedControl is IKeyPressLookUp) { return; } if (focusedControl != null) { if (focusedControl.ProcessKeyPress(keyCode, false)) { activatedControl = focusedControl; if (!repetition) { ++heldKeyCount; heldKeys.Set((int)keyCode, true); } return; } } // Focused control didn't process the notification, now let the desktop // control traverse the entire control tree is earch for a handler. if (desktopControl.ProcessKeyPress(keyCode, false)) { activatedControl = desktopControl; if (!repetition) { ++heldKeyCount; heldKeys.Set((int)keyCode, true); } } else { switch (keyCode) { case Keys.Up: { InjectCommand(Command.Up); break; } case Keys.Down: { InjectCommand(Command.Down); break; } case Keys.Left: { InjectCommand(Command.Left); break; } case Keys.Right: { InjectCommand(Command.Right); break; } case Keys.Enter: { InjectCommand(Command.Accept); break; } case Keys.Escape: { InjectCommand(Command.Cancel); break; } } } }
/// <summary>Injects a command into the processor</summary> /// <param name="command">Input command that will be injected</param> public void InjectCommand(Command command) { switch (command) { // Accept or cancel the current control case Command.Accept: case Command.Cancel: { Control focusedControl = FocusedControl; if (focusedControl == null) { return; // Also catches when focusedControl is not part of the tree } // exTODO: Should this be propagated down the control tree? focusedControl.ProcessCommand(command); break; } // Change focus to another control case Command.SelectPrevious: case Command.SelectNext: { // exTODO: Implement focus switching break; } // Control specific. Changes focus if unhandled. case Command.Up: case Command.Down: case Command.Left: case Command.Right: { Control focusedControl = FocusedControl; if (focusedControl == null) { return; // Also catches when focusedControl is not part of the tree } // First send the command to the focused control. If the control handles // the command, there's nothing for us to do. Otherwise, use the directional // commands for focus switching. if (focusedControl.ProcessCommand(command)) { return; } // These will be determined in the following code block float nearestDistance = float.NaN; Control nearestControl = null; { // Determine the center of the focused control RectangleF parentBounds = focusedControl.Parent.GetAbsoluteBounds(); RectangleF focusedBounds = focusedControl.Bounds.ToOffset( parentBounds.Width, parentBounds.Height ); // Search all siblings of the focused control for the nearest control in the // direction the command asks to move into Collection <Control> siblings = focusedControl.Parent.Children; for (int index = 0; index < siblings.Count; ++index) { Control sibling = siblings[index]; // Only consider this sibling if it's focusable if (!ReferenceEquals(sibling, focusedControl) && canControlGetFocus(sibling)) { RectangleF siblingBounds = sibling.Bounds.ToOffset( parentBounds.Width, parentBounds.Height ); // Calculate the distance the control has in the direction focus is being // changed to. If the control doesn't lie in that direction, NaN will // be returned float distance = getDirectionalDistance( ref focusedBounds, ref siblingBounds, command ); if (float.IsNaN(nearestDistance) || (distance < nearestDistance)) { nearestControl = sibling; nearestDistance = distance; } } } } // beauty scope // Search completed, if we found a candidate, change focus to it if (nearestDistance != float.NaN) { FocusedControl = nearestControl; } break; } } }