/// <summary> /// Attempt to focus a control in given container. Returns boolean declaring /// if a control was focused. /// </summary> private bool Focus(IStaticContainerControl container, Rectf mouse) { // Container not enabled or does not accept mouse input, // can't focus. if (!container.Enabled || !container.AcceptsMouseInput) { return(false); } // Not inside container, can't have focus to anything then. if (!Rectf.Intersects(container.CollisionBoundingBox, mouse)) { return(false); } if (container.UseRenderTarget) { mouse = new Rectf(mouse.Position, mouse.Bounds); } for (var i = container.ControlsCount - 1; i >= 0; i--) { var child = container[i]; if (!child.Enabled || !child.Visible || !child.VisibleFromParent || !child.AcceptsMouseInput || !Rectf.Intersects(child.CollisionBoundingBox, mouse)) { continue; } if (child is IStaticContainerControl inner) { // Keep track of the currently focused control. var last = Context.Mouse; // Attempt to focus to inner container. if (Focus(inner, mouse)) { return(true); } // If the currently focused control changed, focus has changed. // In other case, keep looking for the control that could contain the // focus. var current = Context.Mouse; if (!ReferenceEquals(current, last)) { return(true); } // If no focus was applied to any child control, apply focus to current container. Context.Mouse?.Defocus(); inner.Focus(); } else { // Do not refocus if focused control has not changed. if (ReferenceEquals(Context.Mouse, child)) { return(true); } Context.Mouse?.Defocus(); child.Focus(); } } return(false); }
public bool IsHovering(Rectf area) => Rectf.Intersects(area, new Rectf(CurrentLocalPosition, UiCanvas.ToLocalUnits(Vector2.One)));