From: Ingo Ruhnke Date: Thu, 19 Nov 2009 15:24:10 +0000 (+0000) Subject: Move some code from TitleScreen to AddonMenu X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=446fbdb577923af3d38f9d32d4a89bb2ea6da566;p=supertux.git Move some code from TitleScreen to AddonMenu SVN-Revision: 6048 --- diff --git a/src/supertux/menu/addon_menu.cpp b/src/supertux/menu/addon_menu.cpp index c21e1db05..d9f293b50 100644 --- a/src/supertux/menu/addon_menu.cpp +++ b/src/supertux/menu/addon_menu.cpp @@ -17,12 +17,40 @@ #include "supertux/menu/addon_menu.hpp" #include +#include #include "addon/addon.hpp" +#include "addon/addon_manager.hpp" #include "util/gettext.hpp" -AddonMenu::AddonMenu(const std::vector& 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(); @@ -35,9 +63,9 @@ AddonMenu::AddonMenu(const std::vector& addons) //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()) @@ -57,4 +85,72 @@ AddonMenu::AddonMenu(const std::vector& addons) 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 */ diff --git a/src/supertux/menu/addon_menu.hpp b/src/supertux/menu/addon_menu.hpp index 9be3468c7..29c3d7919 100644 --- a/src/supertux/menu/addon_menu.hpp +++ b/src/supertux/menu/addon_menu.hpp @@ -28,8 +28,13 @@ class Addon; class AddonMenu : public Menu { private: + std::vector m_addons; + public: - AddonMenu(const std::vector& addons); + AddonMenu(); + + void refresh(); + void check_menu(); private: AddonMenu(const AddonMenu&); diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index b1db0f7b0..5ac4e9dd5 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -22,7 +22,6 @@ #include #include -#include "addon/addon_manager.hpp" #include "audio/sound_manager.hpp" #include "gui/menu.hpp" #include "gui/menu_manager.hpp" @@ -53,10 +52,9 @@ TitleScreen::TitleScreen() : main_menu(), contrib_menu(), contrib_world_menu(), + addons_menu(), main_world(), contrib_worlds(), - addons_menu(), - addons(), current_world(), frame(), controller(), @@ -114,79 +112,6 @@ TitleScreen::check_levels_contrib_menu() } } -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() { @@ -299,7 +224,7 @@ TitleScreen::update(float elapsed_time) case MNID_ADDONS: // Add-ons Menu - generate_addons_menu(); + addons_menu.reset(new AddonMenu()); MenuManager::push_current(addons_menu.get()); break; @@ -317,7 +242,7 @@ TitleScreen::update(float elapsed_time) } 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(); } diff --git a/src/supertux/title_screen.hpp b/src/supertux/title_screen.hpp index f955d7aab..492fd114e 100644 --- a/src/supertux/title_screen.hpp +++ b/src/supertux/title_screen.hpp @@ -21,6 +21,7 @@ #include "addon/addon.hpp" #include "supertux/game_session.hpp" +class AddonMenu; class CodeController; class ContribMenu; class ContribWorldMenu; @@ -52,18 +53,15 @@ private: 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 main_menu; std::auto_ptr contrib_menu; std::auto_ptr contrib_world_menu; + std::auto_ptr addons_menu; std::auto_ptr main_world; std::vector contrib_worlds; - std::auto_ptr addons_menu; - std::vector addons; /**< shown list of Add-ons */ World* current_world; std::auto_ptr frame;