Example #1
0
        private void Init(ITextDisplay display, IRotaryEncoder encoder, IButton buttonNext, IButton buttonPrevious, IButton buttonSelect, MenuPage menuTree)
        {
            if (encoder != null)
            {
                _encoder = encoder;
            }
            else if (buttonNext != null && buttonPrevious != null)
            {
                _buttonPrevious = buttonPrevious;
                _buttonNext     = buttonNext;
            }
            else
            {
                throw new ArgumentNullException("Must have either a Rotary Encoder or Next/Previous buttons");
            }

            _display      = display;
            _buttonSelect = buttonSelect;
            _rootMenuPage = menuTree;
            _menuLevel    = new Stack();

            // Save our custom characters
            _display.SaveCustomCharacter(TextCharacters.RightArrow.CharMap, TextCharacters.RightArrow.MemorySlot);
            _display.SaveCustomCharacter(TextCharacters.RightArrowSelected.CharMap, TextCharacters.RightArrow.MemorySlot);
            _display.SaveCustomCharacter(TextCharacters.BoxSelected.CharMap, TextCharacters.BoxSelected.MemorySlot);
        }
Example #2
0
        public bool Select()
        {
            if (currentInputItem != null)
            {
                currentInputItem.Select();
                return(true);
            }
            else
            {
                currentMenuPage.Select();
            }

            if (currentMenuPage.ScrollPosition == 0 && pageStack.Count == 0 && showBackOnRoot)
            {
                Disable();
                Exited(this, new EventArgs());
                return(true);
            }

            // if currently on a subpage and user selects back, pop back to parent page.
            if (currentMenuPage.ScrollPosition == 0 && pageStack.Count > 0)
            {
                MenuPage parent = pageStack.Pop() as MenuPage;
                currentMenuPage = parent;
                ShowCurrentPage();
                return(true);
            }

            int      pos      = currentMenuPage.ScrollPosition;
            MenuItem menuItem = currentMenuPage.MenuItems[pos];

            // go to the submenu if children are present
            if (menuItem.HasSubItems)
            {
                pageStack.Push(currentMenuPage);
                // currentMenuPage = child.SubMenu;
                currentMenuPage = CreateMenuPage(menuItem.SubItems, true);
                ShowCurrentPage();
                return(true);
            }
            // if there is a command, notify the subscribers
            else if (menuItem.Command != string.Empty)
            {
                Selected(this, new MenuSelectedEventArgs(menuItem.Command));
                return(true);
            }
            // if there is a type, then let the type handle the input
            else if (menuItem.Type != string.Empty)
            {
                pageStack.Push(currentMenuPage);

                isEditMode = true;

                // create the new input type
                var type = Type.GetType(INPUT_TYPES_NAMESPACE + menuItem.Type);

                if (type == null)
                {
                    throw new ArgumentException(menuItem.Type + " was not found");
                }

                var constructor = type.GetConstructor(new Type[] { });
                currentInputItem = constructor.Invoke(new object[] { }) as IMenuInputItem;

                // setup callback
                currentInputItem.ValueChanged += delegate(object sender, ValueChangedEventArgs e)
                {
                    // set the value and notify the eager listeners
                    menuItem.Value = e.Value;
                    ValueChanged(this, new ValueChangedEventArgs(e.ItemID, e.Value));

                    // reload the parent menu
                    var parent = pageStack.Pop() as MenuPage;
                    currentMenuPage  = parent;
                    currentInputItem = null;
                    ShowCurrentPage();
                    isEditMode = false;
                };

                // initialize input mode and get new value
                currentInputItem.Init(display);

                currentInputItem.GetInput(menuItem.Id, menuItem.Value);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #3
0
        public bool SelectCurrentItem()
        {
            if (_currentMenuPage.ScrollPosition == 0 && _menuLevel.Count == 0 && _showBackOnRoot)
            {
                this.Disable();
                Exited(this, new EventArgs());
                return(true);
            }

            // if currently on a subpage and user selects back, pop back to parent page.
            if (_currentMenuPage.ScrollPosition == 0 && _menuLevel.Count > 0)
            {
                MenuPage parent = _menuLevel.Pop() as MenuPage;
                _currentMenuPage = parent;
                RenderCurrentMenuPage();
                return(true);
            }

            int      pos   = _currentMenuPage.ScrollPosition;
            MenuItem child = ((MenuItem)_currentMenuPage.MenuItems[pos]);

            // go to the submenu if children are present
            if (child.SubMenu.MenuItems.Count > 0)
            {
                _menuLevel.Push(_currentMenuPage);
                _currentMenuPage = child.SubMenu;
                RenderCurrentMenuPage();
                return(true);
            }
            // if there is a command, notify the subscribers
            else if (child.Command != string.Empty)
            {
                Selected(this, new MenuSelectedEventArgs(child.Command));
                return(true);
            }
            // if there is a type, then let the type handle the input
            else if (child.Type != string.Empty)
            {
                // preserve current menu state and remove handlers
                if (_encoder != null)
                {
                    _encoder.Rotated -= HandlEncoderRotation;
                }
                else
                {
                    _buttonPrevious.Clicked -= HandleButtonPrevious;
                    _buttonNext.Clicked     -= HandleButtonNext;
                }
                _buttonSelect.Clicked -= HandleEncoderClick;

                _menuLevel.Push(_currentMenuPage);
                _isEditMode = true;

                // create the new input type
                var type = Type.GetType(INPUT_TYPES_NAMESPACE + child.Type);
                if (type == null)
                {
                    throw new ArgumentException(child.Type + " was not found");
                }

                var            constructor = type.GetConstructor(new Type[] { });
                IMenuInputItem inputItem   = constructor.Invoke(new object[] { }) as IMenuInputItem;

                // setup callback
                inputItem.ValueChanged += delegate(object sender, ValueChangedEventArgs e)
                {
                    // set the value and notify the eager listeners
                    child.Value = e.Value;
                    ValueChanged(this, new ValueChangedEventArgs(e.ItemID, e.Value));

                    // setup handlers again
                    if (_encoder != null)
                    {
                        _encoder.Rotated += HandlEncoderRotation;
                    }
                    else
                    {
                        _buttonPrevious.Clicked += HandleButtonPrevious;
                        _buttonNext.Clicked     += HandleButtonNext;
                    }
                    _buttonSelect.Clicked += HandleEncoderClick;

                    // reload the parent menu
                    MenuPage parent = _menuLevel.Pop() as MenuPage;
                    _currentMenuPage = parent;
                    RenderCurrentMenuPage();
                    _isEditMode = false;
                };

                // initialize input mode and get new value
                if (_encoder != null)
                {
                    inputItem.Init(_display, _encoder, _buttonSelect);
                }
                else
                {
                    inputItem.Init(_display, _buttonNext, _buttonPrevious, _buttonSelect);
                }

                inputItem.GetInput(child.ItemID, child.Value);
                return(true);
            }
            else
            {
                return(false);
            }
        }