Skip to content

Patch 1 #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions MenuSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One initialization list item per line. See other classes for example.

}

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
// *********************************************************
Expand Down
42 changes: 41 additions & 1 deletion MenuSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,46 @@ class MenuItem : public MenuComponent {
virtual Menu* select();
};

class TextMenuItem : public MenuItem {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefit does a TextMenuItem have over using multiple MenuItem's and cycling between them?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not a fan of the name since a MenuItem is really a menu item with some text. This is more like a combo box or select box. So if it is useful, I'd prefer a different name.

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All private member variable should be prefixed with an underscore.

};


//! \brief A MenuItem that calls MenuSystem::back() when selected.
//! \see MenuItem
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down