private static GuiControl FindControlAtPoint(GuiControl control, Point point)
        {
            var layoutControl = control as GuiLayoutControl;

            if (layoutControl != null)
            {
                var children = layoutControl.Children;

                for(var i = children.Count - 1; i >= 0; i--)
                {
                    var child = children[i];

                    if (!child.Contains(point))
                        continue;

                    var c = FindControlAtPoint(child, point);

                    if (c != null)
                        return c;
                }
            }

            if (control.Contains(point))
                return control;

            return null;
        }
        private void OnMouseMoved(object sender, MouseEventArgs args)
        {
            if (_focusedControl != null)
                _focusedControl.OnMouseLeave(this, args);

            _focusedControl = FindControlAtPoint(Layout, args.Position);

            if (_focusedControl != null)
                _focusedControl.OnMouseEnter(this, args);

            Layout.OnMouseMoved(this, args);
        }
        protected int GetHorizontalAlignment(GuiControl control, Rectangle rectangle)
        {
            switch (control.HorizontalAlignment)
            {
                case GuiHorizontalAlignment.Stretch:
                case GuiHorizontalAlignment.Left:
                    return rectangle.Left;
                case GuiHorizontalAlignment.Right:
                    return rectangle.Right - control.Width;
                case GuiHorizontalAlignment.Center:
                    return rectangle.Left + rectangle.Width / 2 - control.Width / 2;
            }

            throw new NotSupportedException(string.Format("{0} is not supported", control.HorizontalAlignment));
        }
        public GuiManager(ViewportAdapter viewportAdapter, GraphicsDevice graphicsDevice)
        {
            _viewportAdapter = viewportAdapter;
            _inputManager = new InputListenerManager(viewportAdapter);
            _spriteBatch = new SpriteBatch(graphicsDevice);

            Controls = new List<GuiControl>();

            var mouseListener = _inputManager.AddListener<MouseListener>();
            mouseListener.MouseMoved += (sender, args) =>
            {
                if (_focusedControl != null)
                    _focusedControl.OnMouseLeave(this, args);

                _focusedControl = FindFocusedControl(args.Position);

                if (_focusedControl != null)
                    _focusedControl.OnMouseEnter(this, args);

                DelegateMouseEvent(args, c => c.OnMouseMoved(this, args));
            };
            mouseListener.MouseDown += (sender, args) => DelegateMouseEvent(args, c => c.OnMouseDown(this, args));
            mouseListener.MouseUp += (sender, args) => DelegateMouseEvent(args, c => c.OnMouseUp(this, args));
        }
        protected int GetVerticalAlignment(GuiControl control, Rectangle rectangle)
        {
            switch (control.VerticalAlignment)
            {
                case GuiVerticalAlignment.Stretch:
                case GuiVerticalAlignment.Top:
                    return rectangle.Top;
                case GuiVerticalAlignment.Bottom:
                    return rectangle.Bottom - control.Height;
                case GuiVerticalAlignment.Center:
                    return rectangle.Top + rectangle.Height / 2 - control.Height / 2;
            }

            throw new NotSupportedException(string.Format("{0} is not supported", control.VerticalAlignment));
        }