#include "supertux/menu/addon_menu.hpp"
#include <config.h>
+#include <algorithm>
#include "addon/addon.hpp"
+#include "addon/addon_manager.hpp"
#include "util/gettext.hpp"
-AddonMenu::AddonMenu(const std::vector<Addon*>& addons)
+namespace {
+
+bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
+{
+ return a1->title < a2->title;
+}
+
+} // namespace
+
+AddonMenu::AddonMenu()
+{
+ refresh();
+}
+
+void
+AddonMenu::refresh()
{
+ clear();
+
+ AddonManager& adm = AddonManager::get_instance();
+
+ // refresh list of addons
+ m_addons = adm.get_addons();
+
+ // sort list
+ std::sort(m_addons.begin(), m_addons.end(), generate_addons_menu_sorter);
+
+
add_label(_("Add-ons"));
add_hl();
//add_hl();
- for (unsigned int i = 0; i < addons.size(); i++)
+ for (unsigned int i = 0; i < m_addons.size(); i++)
{
- const Addon& addon = *addons[i];
+ const Addon& addon = *m_addons[i];
std::string text = "";
if (!addon.kind.empty())
add_back(_("Back"));
}
+void
+AddonMenu::check_menu()
+{
+ int index = check();
+
+ if (index == -1)
+ {
+ // do nothing
+ }
+ else if (index == 0) // check if "Check Online" was chosen
+ {
+ try
+ {
+ AddonManager::get_instance().check_online();
+ refresh();
+ set_active_item(index);
+ }
+ catch (std::exception& e)
+ {
+ log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
+ }
+ }
+ else
+ {
+ // if one of the Addons listed was chosen, take appropriate action
+ if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + m_addons.size())
+ {
+ Addon& addon = *m_addons[index - ADDON_LIST_START_ID];
+ if (!addon.installed)
+ {
+ try
+ {
+ AddonManager::get_instance().install(&addon);
+ }
+ catch (std::exception& e)
+ {
+ log_warning << "Installing Add-on failed: " << e.what() << std::endl;
+ }
+ set_toggled(index, addon.loaded);
+ }
+ else if (!addon.loaded)
+ {
+ try
+ {
+ AddonManager::get_instance().enable(&addon);
+ }
+ catch (std::exception& e)
+ {
+ log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
+ }
+ set_toggled(index, addon.loaded);
+ }
+ else
+ {
+ try
+ {
+ AddonManager::get_instance().disable(&addon);
+ }
+ catch (std::exception& e)
+ {
+ log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
+ }
+ set_toggled(index, addon.loaded);
+ }
+ }
+ }
+}
+
/* EOF */
class AddonMenu : public Menu
{
private:
+ std::vector<Addon*> m_addons;
+
public:
- AddonMenu(const std::vector<Addon*>& addons);
+ AddonMenu();
+
+ void refresh();
+ void check_menu();
private:
AddonMenu(const AddonMenu&);
#include <algorithm>
#include <physfs.h>
-#include "addon/addon_manager.hpp"
#include "audio/sound_manager.hpp"
#include "gui/menu.hpp"
#include "gui/menu_manager.hpp"
main_menu(),
contrib_menu(),
contrib_world_menu(),
+ addons_menu(),
main_world(),
contrib_worlds(),
- addons_menu(),
- addons(),
current_world(),
frame(),
controller(),
}
}
-namespace {
-bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
-{
- return a1->title < a2->title;
-}
-}
-
-void
-TitleScreen::generate_addons_menu()
-{
- AddonManager& adm = AddonManager::get_instance();
-
- // refresh list of addons
- addons = adm.get_addons();
-
- // sort list
- std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
-
- // (re)generate menu
- addons_menu.reset(new AddonMenu(addons));
-}
-
-void
-TitleScreen::check_addons_menu()
-{
- int index = addons_menu->check();
- if (index == -1) return;
-
- // check if "Check Online" was chosen
- if (index == 0) {
- try {
- AddonManager::get_instance().check_online();
- generate_addons_menu();
- MenuManager::set_current(addons_menu.get());
- addons_menu->set_active_item(index);
- }
- catch (std::runtime_error e) {
- log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
- }
- return;
- }
-
- // if one of the Addons listed was chosen, take appropriate action
- if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
- Addon& addon = *addons[index - ADDON_LIST_START_ID];
- if (!addon.installed) {
- try {
- AddonManager::get_instance().install(&addon);
- }
- catch (std::runtime_error e) {
- log_warning << "Installing Add-on failed: " << e.what() << std::endl;
- }
- addons_menu->set_toggled(index, addon.loaded);
- } else if (!addon.loaded) {
- try {
- AddonManager::get_instance().enable(&addon);
- }
- catch (std::runtime_error e) {
- log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
- }
- addons_menu->set_toggled(index, addon.loaded);
- } else {
- try {
- AddonManager::get_instance().disable(&addon);
- }
- catch (std::runtime_error e) {
- log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
- }
- addons_menu->set_toggled(index, addon.loaded);
- }
- }
-}
-
void
TitleScreen::make_tux_jump()
{
case MNID_ADDONS:
// Add-ons Menu
- generate_addons_menu();
+ addons_menu.reset(new AddonMenu());
MenuManager::push_current(addons_menu.get());
break;
} else if(menu == contrib_menu.get()) {
check_levels_contrib_menu();
} else if(menu == addons_menu.get()) {
- check_addons_menu();
+ addons_menu->check_menu();
} else if (menu == contrib_world_menu.get()) {
contrib_world_menu->check_menu();
}
#include "addon/addon.hpp"
#include "supertux/game_session.hpp"
+class AddonMenu;
class CodeController;
class ContribMenu;
class ContribWorldMenu;
void make_tux_jump();
void generate_main_menu();
void check_levels_contrib_menu();
- void generate_addons_menu();
- void check_addons_menu();
private:
std::auto_ptr<MainMenu> main_menu;
std::auto_ptr<ContribMenu> contrib_menu;
std::auto_ptr<ContribWorldMenu> contrib_world_menu;
+ std::auto_ptr<AddonMenu> addons_menu;
std::auto_ptr<World> main_world;
std::vector<World*> contrib_worlds;
- std::auto_ptr<Menu> addons_menu;
- std::vector<Addon*> addons; /**< shown list of Add-ons */
World* current_world;
std::auto_ptr<Surface> frame;