diff --git a/MenuSystem.cpp b/MenuSystem.cpp index a2cb7d4..2794b5a 100644 --- a/MenuSystem.cpp +++ b/MenuSystem.cpp @@ -253,6 +253,54 @@ bool MenuItem::prev(bool loop) { return false; } + +// ********************************************************* +// TextMenuItem +// ********************************************************* + +TextMenuItem::TextMenuItem(const char* name, SelectFnPtr select_fn) +: MenuItem(name, select_fn),current_value_id(0) { +} + +Menu* TextMenuItem::select() { + _has_focus = !_has_focus; + + // Only run _select_fn when the user is done editing the value + if (!_has_focus && _select_fn != nullptr) + _select_fn(this); + return nullptr; +} + +void TextMenuItem::reset() { + // Do nothing. +} + +void TextMenuItem::render(MenuComponentRenderer const& renderer) const { + renderer.render_text_menu_item(*this); +} + +bool TextMenuItem::next(bool loop) { + current_value_id++; + if (current_value_id > count-1) { + if (loop) + current_value_id = 0; + else + current_value_id = count-1; + } + return true; +} + +bool TextMenuItem::prev(bool loop) { + current_value_id--; + if (current_value_id < 0) { + if (loop) + current_value_id = count-1; + else + current_value_id = 0; + } + return true; +} + // ********************************************************* // NumericMenuItem // ********************************************************* diff --git a/MenuSystem.h b/MenuSystem.h index 06f8d27..2da7bf2 100644 --- a/MenuSystem.h +++ b/MenuSystem.h @@ -217,6 +217,46 @@ class MenuItem : public MenuComponent { virtual Menu* select(); }; +class TextMenuItem : public MenuItem { +public: + //! \brief Construct a TextMenuItem + //! \param[in] name The name of the menu component that is displayed in + //! clients. + //! \param[in] select_fn The function to call when the MenuItem is + //! selected. + TextMenuItem(const char* name, SelectFnPtr select_fn); + void set_values(char** values, int count){this->_values = values;this->count = count;} + void set_value(String value){this->_value = value;} + char* get_value(){return _values[current_value_id];} + + //! \copydoc MenuComponent::render + virtual void render(MenuComponentRenderer const& renderer) const; + +protected: + //! \copydoc MenuComponent::next + //! + //! This method does nothing in MenyItem. + virtual bool next(bool loop=false); + + //! \copydoc MenuComponent::prev + //! + //! This method does nothing in MenuItem. + virtual bool prev(bool loop=false); + + //! \copydoc MenuComponent::reset + //! + //! This method does nothing in MenuItem. + virtual void reset(); + + //! \copydoc MenuComponent:select + virtual Menu* select(); +private: + String _value; + int current_value_id; + char** _values; + int count; +}; + //! \brief A MenuItem that calls MenuSystem::back() when selected. //! \see MenuItem @@ -292,7 +332,6 @@ class NumericMenuItem : public MenuItem { FormatValueFnPtr _format_value_fn; }; - //! \brief A MenuComponent that can contain other MenuComponents. //! //! Menu represents the branch in the composite design pattern (see: @@ -382,6 +421,7 @@ class MenuComponentRenderer { virtual void render(Menu const& menu) const = 0; virtual void render_menu_item(MenuItem const& menu_item) const = 0; + virtual void render_text_menu_item(TextMenuItem const& menu_item) const = 0; virtual void render_back_menu_item(BackMenuItem const& menu_item) const = 0; virtual void render_numeric_menu_item(NumericMenuItem const& menu_item) const = 0; virtual void render_menu(Menu const& menu) const = 0;