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); } }
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); }
public Menu(ITextDisplay display, RotaryEncoderWithButton encoder, MenuPage menuTree) { Init(display, encoder, menuTree); }