More cleanup and code simplification for addons_screen.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@7188 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2010-12-30 05:17:10 +00:00
parent 9dbd09f812
commit 9097ece07c
5 changed files with 29 additions and 60 deletions

View File

@ -19,13 +19,11 @@
#include "states_screens/addons_screen.hpp" #include "states_screens/addons_screen.hpp"
#include <pthread.h>
#include <sstream> #include <sstream>
#include "irrlicht.h" #include "irrlicht.h"
#include "addons/addons_manager.hpp" #include "addons/addons_manager.hpp"
#include "addons/network_http.hpp"
#include "guiengine/widget.hpp" #include "guiengine/widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp" #include "guiengine/widgets/ribbon_widget.hpp"
#include "guiengine/CGUISpriteBank.h" #include "guiengine/CGUISpriteBank.h"
@ -34,7 +32,6 @@
#include "states_screens/dialogs/addons_loading.hpp" #include "states_screens/dialogs/addons_loading.hpp"
#include "states_screens/state_manager.hpp" #include "states_screens/state_manager.hpp"
DEFINE_SCREEN_SINGLETON( AddonsScreen ); DEFINE_SCREEN_SINGLETON( AddonsScreen );
// ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------
@ -56,10 +53,11 @@ void AddonsScreen::loadedFromFile()
+ "/package-update.png" ); + "/package-update.png" );
m_icon_bank = new irr::gui::STKModifiedSpriteBank( GUIEngine::getGUIEnv()); m_icon_bank = new irr::gui::STKModifiedSpriteBank( GUIEngine::getGUIEnv());
m_icon_bank->addTextureAsSprite(icon1); m_icon_installed = m_icon_bank->addTextureAsSprite(icon1);
m_icon_bank->addTextureAsSprite(icon2); m_icon_not_installed = m_icon_bank->addTextureAsSprite(icon2);
m_icon_bank->addTextureAsSprite(icon3); m_icon_needs_update = m_icon_bank->addTextureAsSprite(icon3);
} } // loadedFromFile
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void AddonsScreen::init() void AddonsScreen::init()
@ -68,7 +66,6 @@ void AddonsScreen::init()
getWidget<GUIEngine::RibbonWidget>("category")->setDeactivated(); getWidget<GUIEngine::RibbonWidget>("category")->setDeactivated();
m_type = "kart"; m_type = "kart";
pthread_mutex_init(&m_mutex, NULL);
std::cout << "[Addons] Using directory <" + file_manager->getAddonsDir() std::cout << "[Addons] Using directory <" + file_manager->getAddonsDir()
<< ">\n"; << ">\n";
GUIEngine::ListWidget* w_list = GUIEngine::ListWidget* w_list =
@ -76,17 +73,15 @@ void AddonsScreen::init()
w_list->setIcons(m_icon_bank); w_list->setIcons(m_icon_bank);
//w_list->clear(); //w_list->clear();
std::cout << "icon bank" << std::endl; std::cout << "icon bank" << std::endl;
m_can_load_list = false;
getWidget<GUIEngine::LabelWidget>("update_status") getWidget<GUIEngine::LabelWidget>("update_status")
->setText(_("Updating the list...")); ->setText(_("Updating the list..."));
pthread_t thread; loadList("kart");
pthread_create(&thread, NULL, &AddonsScreen::downloadList, this); } // init
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void AddonsScreen::loadList(const std::string &type) void AddonsScreen::loadList(const std::string &type)
{ {
std::cout << "load list" << std::endl;
GUIEngine::ListWidget* w_list = GUIEngine::ListWidget* w_list =
getWidget<GUIEngine::ListWidget>("list_addons"); getWidget<GUIEngine::ListWidget>("list_addons");
w_list->clear(); w_list->clear();
@ -98,32 +93,35 @@ void AddonsScreen::loadList(const std::string &type)
if(addon.isInstalled() && addon.needsUpdate()) if(addon.isInstalled() && addon.needsUpdate())
{ {
w_list->addItem(addon.getId(), addon.getName().c_str(), w_list->addItem(addon.getId(), addon.getName().c_str(),
2 /* icon installed */); m_icon_needs_update);
} }
else if(addon.isInstalled()) else if(addon.isInstalled())
{ {
w_list->addItem(addon.getId(), addon.getName().c_str(), w_list->addItem(addon.getId(), addon.getName().c_str(),
0 /* icon installed */); m_icon_installed);
} }
else else
{ {
w_list->addItem(addon.getId(), addon.getName().c_str(), w_list->addItem(addon.getId(), addon.getName().c_str(),
1 /* icon unsinstalled*/); m_icon_not_installed);
} }
} }
m_can_load_list = false; m_can_load_list = false;
getWidget<GUIEngine::RibbonWidget>("category")->setActivated(); getWidget<GUIEngine::RibbonWidget>("category")->setActivated();
getWidget<GUIEngine::LabelWidget>("update_status")->setText(""); getWidget<GUIEngine::LabelWidget>("update_status")->setText("");
if(m_type == "kart") if(type == "kart")
getWidget<GUIEngine::RibbonWidget>("category")->select("tab_kart", getWidget<GUIEngine::RibbonWidget>("category")->select("tab_kart",
PLAYER_ID_GAME_MASTER); PLAYER_ID_GAME_MASTER);
else if(m_type == "track") else if(type == "track")
getWidget<GUIEngine::RibbonWidget>("category")->select("tab_track", getWidget<GUIEngine::RibbonWidget>("category")->select("tab_track",
PLAYER_ID_GAME_MASTER); PLAYER_ID_GAME_MASTER);
} else
// ---------------------------------------------------------------------------- getWidget<GUIEngine::RibbonWidget>("category")->select("tab_update",
PLAYER_ID_GAME_MASTER);
} // loadList
// ----------------------------------------------------------------------------
void AddonsScreen::eventCallback(GUIEngine::Widget* widget, void AddonsScreen::eventCallback(GUIEngine::Widget* widget,
const std::string& name, const int playerID) const std::string& name, const int playerID)
{ {
@ -138,7 +136,7 @@ void AddonsScreen::eventCallback(GUIEngine::Widget* widget,
getWidget<GUIEngine::ListWidget>("list_addons"); getWidget<GUIEngine::ListWidget>("list_addons");
std::string addons = list->getSelectionInternalName(); std::string addons = list->getSelectionInternalName();
m_load = new AddonsLoading(0.8f, 0.8f, addons); new AddonsLoading(0.8f, 0.8f, addons);
} }
if (name == "category") if (name == "category")
{ {
@ -158,30 +156,8 @@ void AddonsScreen::eventCallback(GUIEngine::Widget* widget,
loadList("kart"); loadList("kart");
} }
} }
} } // eventCallback
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void AddonsScreen::onUpdate(float delta, irr::video::IVideoDriver* driver)
{
pthread_mutex_lock(&m_mutex);
if(m_can_load_list)
{
loadList(m_type);
}
pthread_mutex_unlock(&m_mutex);
}
// ----------------------------------------------------------------------------
void *AddonsScreen::downloadList( void *pthis)
{
AddonsScreen *pt = (AddonsScreen*)pthis;
pthread_mutex_lock(&(pt->m_mutex));
pt->m_can_load_list = true;
pthread_mutex_unlock(&(pt->m_mutex));
return NULL;
}
#endif #endif

View File

@ -20,7 +20,6 @@
#ifndef HEADER_ADDONS_SCREEN_HPP #ifndef HEADER_ADDONS_SCREEN_HPP
#define HEADER_ADDONS_SCREEN_HPP #define HEADER_ADDONS_SCREEN_HPP
#include <pthread.h>
#include "irrlicht.h" #include "irrlicht.h"
#include "addons/addons_manager.hpp" #include "addons/addons_manager.hpp"
@ -46,7 +45,12 @@ class AddonsScreen : public GUIEngine::Screen,
AddonsManager *m_addons; AddonsManager *m_addons;
AddonsLoading *m_load; AddonsLoading *m_load;
void loadInformations(); void loadInformations();
/** For the addons list, a package when it is installed. */ /** Icon for installed addon, which can be updated. */
int m_icon_needs_update;
/** Icon for installed addons, no update available. */
int m_icon_installed;
/** Icon for is not installed yet. */
int m_icon_not_installed;
irr::gui::STKModifiedSpriteBank irr::gui::STKModifiedSpriteBank
*m_icon_bank; *m_icon_bank;
GUIEngine::LabelWidget GUIEngine::LabelWidget
@ -55,7 +59,6 @@ class AddonsScreen : public GUIEngine::Screen,
public: public:
bool m_can_load_list; bool m_can_load_list;
pthread_mutex_t m_mutex;
std::string m_type; std::string m_type;
/** Load the addons into the main list.*/ /** Load the addons into the main list.*/
@ -67,15 +70,10 @@ public:
/** \brief implement callback from parent class GUIEngine::Screen */ /** \brief implement callback from parent class GUIEngine::Screen */
virtual void eventCallback(GUIEngine::Widget* widget, const std::string& name, const int playerID); virtual void eventCallback(GUIEngine::Widget* widget, const std::string& name, const int playerID);
/** \brief implement callback from parent class GUIEngine::Screen */
virtual void init(); virtual void init();
friend void *startInstall(void *); friend void *startInstall(void *);
static void *downloadList(void *);
/** This function is used to handle the thread (load the new list, etc...). */
virtual void onUpdate(float delta, irr::video::IVideoDriver*);
}; };
#endif #endif

View File

@ -61,7 +61,7 @@ void AddonsUpdateScreen::eventCallback(GUIEngine::Widget* widget,
GUIEngine::ListWidget* list = getWidget<GUIEngine::ListWidget>("list_addons"); GUIEngine::ListWidget* list = getWidget<GUIEngine::ListWidget>("list_addons");
std::string addons = list->getSelectionInternalName(); std::string addons = list->getSelectionInternalName();
m_load = new AddonsLoading(0.8f, 0.8f, addons); new AddonsLoading(0.8f, 0.8f, addons);
} }
else if (name == "category") else if (name == "category")
{ {

View File

@ -34,10 +34,8 @@ class AddonsUpdateScreen : public GUIEngine::Screen,
public GUIEngine::ScreenSingleton<AddonsUpdateScreen> public GUIEngine::ScreenSingleton<AddonsUpdateScreen>
{ {
friend class GUIEngine::ScreenSingleton<AddonsUpdateScreen>; friend class GUIEngine::ScreenSingleton<AddonsUpdateScreen>;
AddonsUpdateScreen();
Addons *m_addons; AddonsUpdateScreen();
/*uneeded*/
AddonsLoading *m_load;
void loadInformations(); void loadInformations();
public: public:

View File

@ -184,11 +184,8 @@ void AddonsLoading::onUpdate(float delta)
// ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------
void AddonsLoading::close() void AddonsLoading::close()
{ {
AddonsScreen* curr_screen = AddonsScreen::getInstance(); AddonsScreen* curr_screen = AddonsScreen::getInstance();
pthread_mutex_lock(&(((AddonsScreen*)curr_screen)->m_mutex));
((AddonsScreen*)curr_screen)->m_can_load_list = true; ((AddonsScreen*)curr_screen)->m_can_load_list = true;
pthread_mutex_unlock(&(((AddonsScreen*)curr_screen)->m_mutex));
dismiss(); dismiss();
} }
// ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------