Added support for sorting addons by column; renamed addons.stkgui

to addons_screen.stkgui to indicate better where it is used.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@8646 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2011-05-19 22:43:27 +00:00
parent 8d760b11cd
commit be258f1b75
4 changed files with 86 additions and 19 deletions

View File

@@ -27,6 +27,8 @@
#include "io/xml_node.hpp"
#include "utils/string_utils.hpp"
Addon::SortOrder Addon::m_sort_order=Addon::SO_DEFAULT;
Addon::Addon(const XMLNode &xml)
{
m_name = "";

View File

@@ -43,6 +43,14 @@ public:
AS_LATEST = 0X0100,
AS_BAD_DIM = 0x0200
};
/** Set the sort order used in the comparison function. */
enum SortOrder { SO_DEFAULT, // featured first, then alphabetically
SO_NAME, // Sorted alphabetically by name
SO_DATE // Sorted by date, newest first
};
private:
/** The name to be displayed. */
std::string m_name;
/** Internal id for this addon, which is the name in lower case.
@@ -77,9 +85,17 @@ public:
/** Type, must be 'kart' or 'track'. */
std::string m_type;
Addon() {};
/** Initialises the object from an XML node. */
Addon(const XMLNode &xml);
/** The sort order to be used in the comparison. */
static SortOrder m_sort_order;
public:
Addon() {};
/** Initialises the object from an XML node. */
Addon(const XMLNode &xml);
// ------------------------------------------------------------------------
/** Sets the sort order used in the comparison function. It is static, so
* that each instance can access the sort order. */
static void setSortOrder(SortOrder so) { m_sort_order = so; }
// ------------------------------------------------------------------------
void writeXML(std::ofstream *out_stram);
// ------------------------------------------------------------------------
@@ -191,6 +207,31 @@ public:
{
return file_manager->getAddonsFile(getTypeDirectory()+getId());
} // getDataDir
// ------------------------------------------------------------------------
/** Compares two addons according to the sort order currently defined.
* \param a The addon to compare this addon to.
*/
bool operator<(const Addon &a) const
{
switch(m_sort_order)
{
case SO_DEFAULT:
if(testStatus(AS_FEATURED) &&
!a.testStatus(AS_FEATURED)) return true;
if(!testStatus(AS_FEATURED) &&
a.testStatus(AS_FEATURED)) return false;
// Otherwise fall through to name comparison!
case SO_NAME:
// m_id is the lower case name
return m_id < a.m_id;
break;
case SO_DATE:
return m_date < a.m_date;
break;
} // switch
// Fix compiler warning.
return true;
} // operator<
}; // Addon

View File

@@ -30,12 +30,13 @@
#include "io/file_manager.hpp"
#include "states_screens/dialogs/addons_loading.hpp"
#include "states_screens/state_manager.hpp"
#include "utils/ptr_vector.hpp"
DEFINE_SCREEN_SINGLETON( AddonsScreen );
// ------------------------------------------------------------------------------------------------------
AddonsScreen::AddonsScreen() : Screen("addons.stkgui")
AddonsScreen::AddonsScreen() : Screen("addons_screen.stkgui")
{
}
@@ -43,7 +44,6 @@ AddonsScreen::AddonsScreen() : Screen("addons.stkgui")
void AddonsScreen::loadedFromFile()
{
video::ITexture* icon1 = irr_driver->getTexture( file_manager->getGUIDir()
+ "/package.png" );
video::ITexture* icon2 = irr_driver->getTexture( file_manager->getGUIDir()
@@ -79,6 +79,9 @@ void AddonsScreen::init()
w_list->setIcons(m_icon_bank);
m_type = "kart";
// Set the default sort order
Addon::setSortOrder(Addon::SO_DEFAULT);
loadList();
} // init
@@ -89,9 +92,8 @@ void AddonsScreen::init()
*/
void AddonsScreen::loadList()
{
GUIEngine::ListWidget* w_list =
getWidget<GUIEngine::ListWidget>("list_addons");
w_list->clear();
// First create a list of sorted entries
PtrVector<const Addon, REF> sorted_list;
for(unsigned int i=0; i<addons_manager->getNumAddons(); i++)
{
const Addon &addon = addons_manager->getAddon(i);
@@ -100,23 +102,39 @@ void AddonsScreen::loadList()
if(!UserConfigParams::m_artist_debug_mode &&
!addon.testStatus(Addon::AS_APPROVED) )
continue;
sorted_list.push_back(&addon);
}
sorted_list.insertionSort(/*start=*/0);
GUIEngine::ListWidget* w_list =
getWidget<GUIEngine::ListWidget>("list_addons");
w_list->clear();
for(int i=0; i<sorted_list.size(); i++)
{
const Addon *addon = &(sorted_list[i]);
// Ignore addons of a different type
if(addon->getType()!=m_type) continue;
if(!UserConfigParams::m_artist_debug_mode &&
!addon->testStatus(Addon::AS_APPROVED) )
continue;
// Get the right icon to display
int icon;
if(addon.isInstalled())
icon = addon.needsUpdate() ? m_icon_needs_update
: m_icon_installed;
if(addon->isInstalled())
icon = addon->needsUpdate() ? m_icon_needs_update
: m_icon_installed;
else
icon = m_icon_not_installed;
core::stringw s;
if(addon.getDesigner().size()==0)
s = (addon.getName()+"\t"+addon.getDateAsString()).c_str();
if(addon->getDesigner().size()==0)
s = (addon->getName()+"\t"+addon->getDateAsString()).c_str();
else
s = _("%s by %s\t%d", addon.getName().c_str(),
addon.getDesigner().c_str(),
addon.getDateAsString().c_str());
w_list->addItem(addon.getId(), s.c_str(), icon);
s = _("%s by %s\t%d", addon->getName().c_str(),
addon->getDesigner().c_str(),
addon->getDateAsString().c_str());
w_list->addItem(addon->getId(), s.c_str(), icon);
}
getWidget<GUIEngine::RibbonWidget>("category")->setActivated();
@@ -132,9 +150,15 @@ void AddonsScreen::loadList()
} // loadList
// ----------------------------------------------------------------------------
void AddonsScreen::onColumnClicked(int columnId)
void AddonsScreen::onColumnClicked(int column_id)
{
printf("Clicked on column %d.\n", columnId);
switch(column_id)
{
case 0: Addon::setSortOrder(Addon::SO_NAME); break;
case 1: Addon::setSortOrder(Addon::SO_DATE); break;
default: assert(0);
} // switch
loadList();
} // onColumnClicked
// ----------------------------------------------------------------------------
void AddonsScreen::eventCallback(GUIEngine::Widget* widget,