From a6a8b57ed5ef62982ae5f0a1620432bed31177ab Mon Sep 17 00:00:00 2001 From: Marco Gulino Date: Fri, 12 Jul 2019 19:14:19 +0100 Subject: [PATCH] Add `type` getter to MenuComponent and subclasses, in order to detect the right type for casting a menu item. Add getter and setter for increment in NumericMenuItem --- MenuSystem.cpp | 27 +++++++++++++++++++++++++++ MenuSystem.h | 13 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/MenuSystem.cpp b/MenuSystem.cpp index a2cb7d4..77a619c 100644 --- a/MenuSystem.cpp +++ b/MenuSystem.cpp @@ -200,6 +200,12 @@ void Menu::render(MenuComponentRenderer const& renderer) const { renderer.render_menu(*this); } +MenuComponent::Type Menu::type() const { + return MenuComponent::TypeMenu; +} + + + // ********************************************************* // BackMenuItem // ********************************************************* @@ -224,6 +230,10 @@ void BackMenuItem::render(MenuComponentRenderer const& renderer) const { renderer.render_back_menu_item(*this); } +MenuComponent::Type BackMenuItem::type() const { + return MenuComponent::TypeBackMenuItem; +} + // ********************************************************* // MenuItem // ********************************************************* @@ -253,6 +263,10 @@ bool MenuItem::prev(bool loop) { return false; } +MenuComponent::Type MenuItem::type() const { + return MenuComponent::TypeMenuItem; +} + // ********************************************************* // NumericMenuItem // ********************************************************* @@ -325,6 +339,15 @@ void NumericMenuItem::set_max_value(float value) { _max_value = value; } +float NumericMenuItem::get_increment() const { + return _increment; +} + +void NumericMenuItem::set_increment(float increment) { + _increment = increment; +} + + bool NumericMenuItem::next(bool loop) { _value += _increment; if (_value > _max_value) { @@ -347,6 +370,10 @@ bool NumericMenuItem::prev(bool loop) { return true; } +MenuComponent::Type NumericMenuItem::type() const { + return MenuComponent::TypeNumericMenuItem; +} + // ********************************************************* // MenuSystem // ********************************************************* diff --git a/MenuSystem.h b/MenuSystem.h index 06f8d27..a5f4320 100644 --- a/MenuSystem.h +++ b/MenuSystem.h @@ -101,6 +101,12 @@ class MenuComponent { //! selected. void set_select_function(SelectFnPtr select_fn); + enum Type { TypeMenu, TypeMenuItem, TypeNumericMenuItem, TypeBackMenuItem }; + //! \brief Returns the type of this item. + //! + //! Subclasses should reimplement this to return the correct type. + virtual Type type() const = 0; + protected: //! \brief Processes the next action //! @@ -196,6 +202,7 @@ class MenuItem : public MenuComponent { //! \copydoc MenuComponent::render virtual void render(MenuComponentRenderer const& renderer) const; + virtual Type type() const; protected: //! \copydoc MenuComponent::next @@ -226,6 +233,7 @@ class BackMenuItem : public MenuItem { virtual void render(MenuComponentRenderer const& renderer) const; + virtual Type type() const; protected: virtual Menu* select(); @@ -269,15 +277,19 @@ class NumericMenuItem : public MenuItem { float get_value() const; float get_min_value() const; float get_max_value() const; + float get_increment() const; void set_value(float value); void set_min_value(float value); void set_max_value(float value); + void set_increment(float increment); + String get_formatted_value() const; virtual void render(MenuComponentRenderer const& renderer) const; + virtual Type type() const; protected: virtual bool next(bool loop=false); virtual bool prev(bool loop=false); @@ -322,6 +334,7 @@ class Menu : public MenuComponent { //! \copydoc MenuComponent::render void render(MenuComponentRenderer const& renderer) const; + virtual Type type() const; protected: void set_parent(Menu* p_parent); Menu const* get_parent() const;