public void TestButtonPressOnFocusAffectingControl() { Control parent = new Control(); Controls.Desktop.WindowControl child1 = new Controls.Desktop.WindowControl(); Controls.Desktop.WindowControl child2 = new Controls.Desktop.WindowControl(); GamePadTestControl child3 = new GamePadTestControl(true); parent.Children.Add(child1); parent.Children.Add(child2); parent.Children.Add(child3); GamePadTestControl child1child1 = new GamePadTestControl(false); GamePadTestControl child1child2 = new GamePadTestControl(false); GamePadTestControl child2child1 = new GamePadTestControl(false); child1.Children.Add(child1child1); child1.Children.Add(child1child2); child2.Children.Add(child2child1); parent.ProcessButtonPress(Buttons.A); Assert.AreEqual(1, child1child1.HeldButtonCount); Assert.AreEqual(1, child1child2.HeldButtonCount); // because child 1 returned false Assert.AreEqual(0, child2child1.HeldButtonCount); // because its parent affects focus Assert.AreEqual(1, child3.HeldButtonCount); // because it doesn't affect focus }
public void TestButtonPressWithActivatedControl() { Control parent = new Control(); parent.Bounds = new UniRectangle(10.0f, 10.0f, 80.0f, 80.0f); GamePadTestControl child1 = new GamePadTestControl(true); GamePadTestControl child2 = new GamePadTestControl(true); child2.Bounds = new UniRectangle(10.0f, 10.0f, 25.0f, 60.0f); parent.Children.Add(child1); parent.Children.Add(child2); parent.ProcessMouseMove(100.0f, 100.0f, 20.0f, 30.0f); parent.ProcessMousePress(MouseButtons.Left); Assert.AreEqual(0, child1.HeldButtonCount); Assert.AreEqual(0, child2.HeldButtonCount); parent.ProcessButtonPress(Buttons.A); Assert.AreEqual(0, child1.HeldButtonCount); Assert.AreEqual(1, child2.HeldButtonCount); // Because child 1 was activated parent.ProcessButtonRelease(Buttons.A); Assert.AreEqual(0, child1.HeldButtonCount); Assert.AreEqual(0, child2.HeldButtonCount); }
/// <summary>Called when a button on the game pad has been pressed</summary> /// <param name="button">Button that has been pressed</param> /// <returns> /// True if the button press was processed by the control and future game pad /// input belongs to the control until all buttons are released again /// </returns> internal bool ProcessButtonPress(Buttons button) { // If there's an activated control (one being held down by the mouse or having // accepted a previous button press), this control will get the button press // delivered, whether it wants to or not. if (this.activatedControl != null) { ++this.heldButtonCount; // If one of our children is the activated control, pass on the message if (this.activatedControl != this) { this.activatedControl.ProcessButtonPress(button); } else // We're the activated control { OnButtonPressed(button); } // We're already activated, so this button press is accepted in any case return(true); } // A button has been pressed but no control is activated currently. This means we // have to look for a control which feels responsible for the button press, // starting with ourselves. // Does the user code in our derived class feel responsible for this button? // If so, we're the new activated control and the button has been handled. if (OnButtonPressed(button)) { this.activatedControl = this; ++this.heldButtonCount; return(true); } // Nope, we have to ask our children to find a control that feels responsible. bool encounteredOrderingControl = false; for (int index = 0; index < this.children.Count; ++index) { Control child = this.children[index]; // We only process one child that has the affectsOrdering field set. This // ensures that key presses will not be delivered to windows sitting behind // another window. Other siblings that are not windows are asked still, so // a bunch of buttons on the desktop would be asked in addition to a window. if (child.affectsOrdering) { if (encounteredOrderingControl) { continue; } else { encounteredOrderingControl = true; } } // Does this child feel responsible for the button press? if (child.ProcessButtonPress(button)) { this.activatedControl = child; ++this.heldButtonCount; return(true); } } // Neither we nor any of our children felt responsible for the button. Give up. return(false); }