Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new FocusManager
        /// </summary>
        /// <param name="rootControl">The control which represents</param>
        /// <param name="startControl"></param>
        /// <exception cref="ArgumentNullException">Is thrown if <paramref name="startControl"/> or <paramref name="rootControl"/> is null</exception>
        /// <exception cref="ArgumentException">Is thrown if <paramref name="startControl"/> has no container</exception>
        /// <exception cref="ArgumentException">Is thrown if <paramref name="startControl"/> is no IInputHandler</exception>
        /// <exception cref="ArgumentException">Is thrown if <paramref name="rootControl"/> has an container</exception>
        /// <exception cref="ArgumentException">Is thrown if <paramref name="startControl"/> is not within <paramref name="rootControl"/></exception>
        public FocusManager(ContainerControl rootControl, Control startControl)
        {
            if (rootControl == null)
                throw new ArgumentNullException("rootControl");
            if (rootControl.Container != null)
                throw new ArgumentException("rootControl has an container!", "rootControl");

            if (startControl == null)
                throw new ArgumentNullException("startControl");
            if (startControl.Container == null)
                throw new ArgumentException("startControl doesn't have an container!", "startControl");
            if (!(startControl is IInputHandler))
                throw new ArgumentException("startControl isn't an IInputHandler!", "startControl");

            _rootControl = rootControl;

            CalculateList();
            _focusedIndex = -1;
            for (int i = 0; i < _controls.Length; i++)
            {
                if (_controls[i] == startControl)
                {
                    _focusedIndex = i;
                    _controls[i].IsFocused = true;
                }
                else
                {
                    _controls[i].IsFocused = false;
                }
            }

            if(_focusedIndex == -1)
                throw new ArgumentException("startControl is not within rootControl", "startControl");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the color of <paramref name="c"/> and all its sub-controls to <paramref name="fColor"/> and <paramref name="bColor"/>
        /// </summary>
        /// <param name="c">The control to set the color of</param>
        /// <param name="fColor">The foreground color to set</param>
        /// <param name="bColor">The background color to set</param>
        private void SetControlColors(Control c, ConsoleColor fColor, ConsoleColor bColor)
        {
            if (!c._foreColorSet) c._foreColor = fColor;
            if (!c._backColorSet) c._backColor = bColor;

            if (c is ContainerControl)
            {
                foreach (var cc in c as ContainerControl)
                {
                    SetControlColors(cc, fColor, bColor);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Searches all controlls which are nearby <paramref name="c"/>.
        /// Controls in the same row are searched if <paramref name="checkTop"/> is <code>true</code>.
        /// Controls in the same collumn are searched if <paramref name="checkLeft"/> is <code>true</code>.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="checkTop"></param>
        /// <param name="checkLeft"></param>
        /// <returns>A list of controls nearby <paramref name="c"/></returns>
        private IEnumerable<int> GetNearbyControls(Control c, bool checkTop = false, bool checkLeft = false)
        {
            if(c.Drawer == null)
                yield break;

            for (int i = 0; i < _controls.Length; i++)
            {
                if(_controls[i].Drawer == null)
                    continue;
                if (c.Drawer.Boundary.Top == _controls[i].Drawer.Boundary.Top && checkTop ||
                    c.Drawer.Boundary.Left == _controls[i].Drawer.Boundary.Left && checkLeft)
                    yield return i;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Subscribes <code>RedrawRequested</code> to <code>Control.RedrawRequested</code> on <paramref name="control"/> and all its child-controls.
        /// Also subscribes <code>OnControlAdded</code> to <code>ContainerControl.ControlAdded</code> if <paramref name="control"/> is an <code>ContainerControl</code>
        /// </summary>
        /// <param name="control">The control to whose events should be subscribed</param>
        /// <seealso cref="RemoveControlEvents"/>
        private void WireControlEvents(Control control)
        {
            control.RedrawRequested += RedrawRequested;

            if (control is ContainerControl)
            {
                var container = control as ContainerControl;
                container.ControlAdded += OnControlAdded;

                foreach (var c in container)
                    WireControlEvents(c);
            }
        }