Skip to content
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

Add generic list selection menu and use it in an editor menu #1895

Merged
merged 5 commits into from
Dec 8, 2021
Merged
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
5 changes: 4 additions & 1 deletion src/gui/menu_badguy_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "gui/dialog.hpp"
#include "gui/menu_item.hpp"
#include "gui/menu_manager.hpp"
#include "gui/menu_list.hpp"

std::vector<std::string> BadguySelectMenu::all_badguys;

Expand Down Expand Up @@ -92,7 +93,7 @@ BadguySelectMenu::refresh_menu()

add_label(_("List of enemies"));
add_hl();
add_string_select(-2, _("Enemy"), &selected, all_badguys);
add_entry(-2, _("Select enemy"));
add_entry(-3, _("Add"));
add_hl();

Expand Down Expand Up @@ -138,6 +139,8 @@ BadguySelectMenu::menu_action(MenuItem& item)
});
dialog->add_cancel_button(_("No"));
MenuManager::instance().set_dialog(std::move(dialog));
} else if (item.get_id() == -2) {
MenuManager::instance().push_menu(std::make_unique<ListMenu>(all_badguys, &selected));
} else if (item.get_id() == -3) {
add_badguy();
}
Expand Down
39 changes: 39 additions & 0 deletions src/gui/menu_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SuperTux -- List menu
// Copyright (C) 2021 Rami <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "gui/menu_item.hpp"
#include "gui/menu_list.hpp"
#include "gui/menu_manager.hpp"

ListMenu::ListMenu(const std::vector<std::string>& items, int* selected) :
m_selected(selected)
{
for(size_t i = 0; i < items.size(); i++) {
add_entry(static_cast<int>(i), items[i]);
}
add_hl();
add_back("OK");
}

void
ListMenu::menu_action(MenuItem& item) {
if(m_selected) {
*m_selected = item.get_id();
}
MenuManager::instance().pop_menu();
}

/* EOF */
40 changes: 40 additions & 0 deletions src/gui/menu_list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SuperTux -- List menu
// Copyright (C) 2021 Rami <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_GUI_MENU_LIST_HPP
#define HEADER_SUPERTUX_GUI_MENU_LIST_HPP

#include "gui/menu.hpp"

class ListMenu final : public Menu
{
public:
ListMenu(const std::vector<std::string>& items, int* selected);

void menu_action(MenuItem& item) override;

private:
int* m_selected;

private:
// non-copyable footer
ListMenu(const ListMenu&) = delete;
ListMenu& operator=(const ListMenu&) = delete;
};

#endif

/* EOF */