fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1#include "display/ConfigMenu.hpp" 2 3#include "core/Persistence.hpp" 4#include "core/config_utils.hpp" 5#include "core/mode_selection.hpp" 6#include "reboot.hpp" 7 8ConfigMenu ::ConfigMenu (Config & config ,CommunicationBackend ** backends ,size_t backends_count ) 9 :_config (config ), 10_backends (backends ), 11_backends_count (backends_count ) {} 12 13DisplayModeId ConfigMenu ::GetId () { 14return DISPLAY_MODE_CONFIG ; 15} 16 17void ConfigMenu ::HandleControls ( 18IntegratedDisplay * instance , 19const DisplayControls & controls , 20Button button 21) { 22if (_current_menu_page == nullptr ) { 23_current_menu_page = _top_level_page ; 24_current_menu_offset = 0 ; 25return ; 26 } 27 28if (button == controls .up ) { 29_highlighted_menu_item = max (0 ,_highlighted_menu_item - 1 ); 30 }else if (button == controls .down ) { 31_highlighted_menu_item = 32min (_current_menu_page -> items_count - 1 ,_highlighted_menu_item + 1 ); 33 }else if (button == controls .enter ) { 34// Bounds check. 35if (_highlighted_menu_item > _current_menu_page -> items_count ) { 36_highlighted_menu_item = 0 ; 37return ; 38 } 39 40const MenuPage ::MenuItem & selected_item = _current_menu_page -> items [_highlighted_menu_item ]; 41 42// If there's an action defined, perform it. 43if (selected_item .action != nullptr ) { 44selected_item .action (instance ,this ,_config ,selected_item .key ); 45 46// Go back up a level. 47if (_current_menu_page -> parent != nullptr ) { 48_current_menu_page = _current_menu_page -> parent ; 49_current_menu_offset = 0 ; 50 } 51_highlighted_menu_item = 0 ; 52return ; 53 } 54 55// If there's a child page defined, drill into it. 56if (selected_item .page != nullptr ) { 57_current_menu_page = selected_item .page ; 58_current_menu_offset = 0 ; 59_highlighted_menu_item = 0 ; 60return ; 61 } 62 }else if (button == controls .back ) { 63// If at top-level page, go back to input viewer. 64if (_current_menu_page -> parent == nullptr ) { 65// Restore gamemode. 66if (_backends [0 ]!= nullptr ) { 67_backends [0 ]-> SetGameMode (instance -> CurrentGameMode ()); 68 } 69ReturnToDashboard (instance ); 70return ; 71 } 72 73_current_menu_page = _current_menu_page -> parent ; 74_highlighted_menu_item = 0 ; 75 } 76} 77 78void ConfigMenu ::UpdateDisplay (IntegratedDisplay * instance ,Adafruit_GFX & display ) { 79// Unset gamemode to prevent menu button presses being sent to console. 80if (_backends [0 ]!= nullptr && _backends [0 ]-> CurrentGameMode ()!= nullptr ) { 81instance -> SetGameMode (_backends [0 ]-> CurrentGameMode ()); 82_backends [0 ]-> SetGameMode (nullptr ); 83 } 84 85uint8_t font_width = instance -> font_width ; 86uint8_t font_height = instance -> font_height ; 87 88if (_highlighted_menu_item - _current_menu_offset > max_visible_lines - 1 ) { 89_current_menu_offset ++ ; 90 }else if (_highlighted_menu_item < _current_menu_offset ) { 91_current_menu_offset -- ; 92 } 93uint8_t last_item_to_display = 94min (_current_menu_page -> items_count - _current_menu_offset ,max_visible_lines + 1 ); 95for (size_t i = 0 ;i < last_item_to_display ;i ++ ) { 96if (i + _current_menu_offset == _highlighted_menu_item ) { 97display .setCursor (0 ,i * (font_height + padding )); 98display .highlight_string ); 99 } 100display .setCursor (font_width + padding ,i * (font_height + padding )); 101display ._current_menu_page -> items [i + _current_menu_offset ].text ); 102 } 103} 104 105void ConfigMenu ::ReturnToDashboard (IntegratedDisplay * instance ) { 106instance -> SetDisplayMode (DISPLAY_MODE_VIEWER ); 107}