Skip to content

Commit

Permalink
Implement kart grouping/searching/favorite (#5172)
Browse files Browse the repository at this point in the history
* Implement kart grouping/searching/favprite

* Add search bar for arenas

* Use methods instead of friend class
  • Loading branch information
CodingJellyfish authored Oct 16, 2024
1 parent 41d6aea commit 7a25743
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 231 deletions.
15 changes: 8 additions & 7 deletions data/gui/screens/arenas.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

<div x="2%" y="1%" width="96%" height="99%" layout="vertical-row" >
<div width="100%" height="8%" layout="horizontal-row" >
<spacer width="30%" height="100%" />
<header height="8%" width="40%" I18N="Section in arena tracks selection screen" text="Arenas"
<header width="30%" height="100%" I18N="Section in arena tracks selection screen" text="Arenas"
align="center" text_align="center" />
<spacer width="1%" height="100%" />
<checkbox id="favorite" align="center"/>
<spacer width="1%" height="100%" />
<bright width="30%" height="100%" I18N="In the track and grand prix selection screen" text="Edit favorite tracks"/>
<spacer width="20" height="1%" />
<textbox width="40%" id="search" align="center"/>
<spacer width="5%" height="100%" />
<div width="fit" height="100%" layout="horizontal-row" align="center">
<checkbox id="favorite" align="center"/>
<spacer width="5%" height="100%" />
<tiny-header width="fit" height="100%" I18N="Section in arena tracksselection screen" text="Edit favorite arenas"/>
</div>
</div>

<box proportion="1" width="100%" layout="vertical-row" padding="2">
Expand Down
17 changes: 15 additions & 2 deletions data/gui/screens/karts.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
align="center" text_align="center" />
<spacer height="1%" width="25"/>

<placeholder id="playerskarts" width="100%" height="70%" align="center" proportion="4">
<placeholder id="playerskarts" width="100%" height="65%" align="center" proportion="4">
<!-- Contents is added programatically -->
</placeholder>

<spacer height="1f"/>
<div width="100%" height="30%" layout="horizontal-row" >
<div width="100%" height="35%" layout="horizontal-row" >
<div id="kartlist" width="85%" height="100%" layout="vertical-row" >
<box proportion="2" width="100%" layout="vertical-row" padding="2">
<spacer height="5%"/>
<div width="95%" height="1f" layout="horizontal-row" align="center">
<label text="Kart Class" align="center" I18N="In karts selection screen, determine the kart group shown on the list"/>
<spacer width="1%" />
<spinner id="kart_class" proportion="2" align="center" min_value="0" wrap_around="true"/>
<spacer width="3%"/>
<textbox proportion="3" id="search" align="center"/>
<spacer width="3%"/>
<checkbox id="favorite"/>
<spacer width="1%"/>
<label width="fit" height="100%" I18N="In the track and grand prix selection screen" text="Edit favorite karts"/>
</div>
<spacer height="3%"/>
<ribbon_grid id="karts" proportion="1" square_items="true" width="100%" align="center"
child_width="90" child_height="90"/>
</box>
Expand Down
15 changes: 14 additions & 1 deletion data/gui/screens/online/network_karts.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@
</placeholder>

<spacer height="1f"/>
<div width="100%" height="30%" layout="horizontal-row" >
<div width="100%" height="35%" layout="horizontal-row" >
<div id="kartlist" width="85%" height="100%" layout="vertical-row" >
<box proportion="2" width="100%" layout="vertical-row" padding="2">
<spacer height="5%"/>
<div width="95%" height="1f" layout="horizontal-row" align="center">
<label text="Kart Class" align="center" I18N="In karts selection screen, determine the kart group shown on the list"/>
<spacer width="1%" />
<spinner id="kart_class" proportion="2" align="center" min_value="0" wrap_around="true"/>
<spacer width="3%"/>
<textbox proportion="3" id="search" align="center"/>
<spacer width="3%"/>
<checkbox id="favorite"/>
<spacer width="1%"/>
<label width="fit" height="100%" I18N="In the track and grand prix selection screen" text="Edit favorite karts"/>
</div>
<spacer height="3%"/>
<ribbon_grid id="karts" proportion="1" square_items="true" width="100%" align="center"
child_width="90" child_height="90" max_rows="2"/>
</box>
Expand Down
4 changes: 2 additions & 2 deletions data/gui/screens/race_setup.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<header height="8%" width="80%" text="Race Setup" align="center" text_align="center" />
<spacer height="1%" width="25"/>

<bright width="100%" text="Select a difficulty" align="center" text_align="left" />
<tiny-header width="100%" text="Select a difficulty" align="center" text_align="left" />
<spacer height="10" width="25"/>
<box width="100%" proportion="3" padding="10" layout="vertical-row">
<spacer height="15%" width="25"/>
Expand All @@ -21,7 +21,7 @@
</box>
<spacer height="2%" width="25"/>

<bright width="100%" text="Select a game mode" align="center" text_align="left" />
<tiny-header width="100%" text="Select a game mode" align="center" text_align="left" />

<spacer height="1%" width="25"/>
<box width="100%" proportion="4" padding="10" layout="vertical-row">
Expand Down
10 changes: 6 additions & 4 deletions data/gui/screens/tracks_and_gp.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
<header width="30%" I18N="In the track and grand prix selection screen" text="All Tracks"
align="center" text_align="center" />
<textbox width="40%" id="search"/>
<spacer width="1%" height="100%" />
<checkbox id="favorite"/>
<spacer width="1%" height="100%" />
<bright width="25%" height="100%" I18N="In the track and grand prix selection screen" text="Edit favorite tracks"/>
<spacer width="3%">
<div width="fit" height="100%" layout="horizontal-row" align="center">
<checkbox id="favorite"/>
<spacer width="5%"/>
<tiny-header width="fit" height="100%" I18N="In the track and grand prix selection screen" text="Edit favorite tracks"/>
</div>
</div>

<spacer width="100%" height="1%" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,114 +16,114 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "config/favorite_track_status.hpp"
#include "config/favorite_status.hpp"

#include "config/player_manager.hpp"
#include "io/utf_writer.hpp"
#include "io/xml_node.hpp"
#include "utils/string_utils.hpp"

const std::string FavoriteTrackStatus::DEFAULT_FAVORITE_GROUP_NAME = "Favorites";
const std::string FavoriteStatus::DEFAULT_FAVORITE_GROUP_NAME = "Favorites";

//------------------------------------------------------------------------------
FavoriteTrackStatus::FavoriteTrackStatus(const XMLNode* node)
FavoriteStatus::FavoriteStatus(const XMLNode* node, std::string parse_type)
{
m_parse_type = parse_type;

std::vector<XMLNode*> xml_favorite_tracks;
std::vector<XMLNode*> xml_favorite_groups;

if (node)
{
node->getNodes("track", xml_favorite_tracks);
node->getNodes(parse_type.c_str(), xml_favorite_tracks);
node->getNodes("group", xml_favorite_groups);
}
for (unsigned int i = 0; i < xml_favorite_tracks.size(); i++)
{
std::string temp_string;
xml_favorite_tracks[i]->get("ident", &temp_string);
m_favorite_tracks[DEFAULT_FAVORITE_GROUP_NAME].insert(temp_string);
m_favorite[DEFAULT_FAVORITE_GROUP_NAME].insert(temp_string);
}
for (unsigned int i = 0; i < xml_favorite_groups.size(); i++)
{
std::string temp_group_string;
std::vector<XMLNode*> temp_group;

xml_favorite_groups[i]->get("name", &temp_group_string);
xml_favorite_groups[i]->getNodes("track", temp_group);
xml_favorite_groups[i]->getNodes(parse_type.c_str(), temp_group);

for (unsigned int j = 0; j < temp_group.size(); j++)
{
std::string temp_string;
temp_group[j]->get("ident", &temp_string);
m_favorite_tracks[temp_group_string].insert(temp_string);
m_favorite[temp_group_string].insert(temp_string);
}
}
} // FavoriteTrackStatus
} // FavoriteStatus

//------------------------------------------------------------------------------
FavoriteTrackStatus::~FavoriteTrackStatus()
FavoriteStatus::~FavoriteStatus()
{

} // ~FavoriteTrackStatus
} // ~FavoriteStatus

//------------------------------------------------------------------------------
/** Adds a new favorite track to this player profile and to the group
* of favorite tracks of the Track Manager.
* To be used only if this player profile is the current player.
*/
bool FavoriteTrackStatus::isFavoriteTrack(std::string ident)
bool FavoriteStatus::isFavorite(std::string ident)
{
return m_favorite_tracks[DEFAULT_FAVORITE_GROUP_NAME].find(ident)
!= m_favorite_tracks[DEFAULT_FAVORITE_GROUP_NAME].end();
} // addFavoriteTrack
return m_favorite[DEFAULT_FAVORITE_GROUP_NAME].find(ident)
!= m_favorite[DEFAULT_FAVORITE_GROUP_NAME].end();
} // addFavorite

//------------------------------------------------------------------------------
/** Adds a new favorite track to this player profile and to the group
* of favorite tracks of the Track Manager.
*/
void FavoriteTrackStatus::addFavoriteTrack(std::string ident, std::string group)
void FavoriteStatus::addFavorite(std::string ident, std::string group)
{
m_favorite_tracks[group].insert(ident);
} // addFavoriteTrack
m_favorite[group].insert(ident);
} // addFavorite

//------------------------------------------------------------------------------
/** Removes a favorite track from this player profile and from the group
* of favorite tracks of the Track Manager.
*/
void FavoriteTrackStatus::removeFavoriteTrack(std::string ident, std::string group)
void FavoriteStatus::removeFavorite(std::string ident, std::string group)
{
if (m_favorite_tracks[group].find(ident) != m_favorite_tracks[group].end())
if (m_favorite[group].find(ident) != m_favorite[group].end())
{
m_favorite_tracks[group].erase(ident);
m_favorite[group].erase(ident);
}
} // removeFavoriteTrack
} // removeFavorite

//------------------------------------------------------------------------------
/** Writes the data for this player to the specified UTFWriter.
* \param out The utf writer to write the data to.
*/
void FavoriteTrackStatus::save(UTFWriter &out)
void FavoriteStatus::save(UTFWriter &out)
{
out << " <favorites>\n";
for (auto it_group = m_favorite_tracks.begin(); it_group != m_favorite_tracks.end(); it_group++)
for (auto it_group = m_favorite.begin(); it_group != m_favorite.end(); it_group++)
{
std::string group_name = it_group->first;

if (group_name == DEFAULT_FAVORITE_GROUP_NAME)
{
for (auto it_track = it_group->second.begin(); it_track != it_group->second.end(); it_track++)
{
out << " <track ident=\"" << *it_track << "\"/>\n";
out << " <" << m_parse_type.c_str() << " ident=\"" << *it_track << "\"/>\n";
}
}
else
{
out << " <group name=\"" << group_name << "\">\n";
for (auto it_track = it_group->second.begin(); it_track != it_group->second.end(); it_track++)
{
out << " <track ident=\"" << *it_track << "\"/>\n";
out << " <" << m_parse_type.c_str() << " ident=\"" << *it_track << "\"/>\n";
}
out << " </group>\n";
}
}
out << " </favorites>\n";
} // save
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#ifndef HEADER_FAVORITE_TRACK_STATUS_HPP
#define HEADER_FAVORITE_TRACK_STATUS_HPP
#ifndef HEADER_FAVORITE_STATUS_HPP
#define HEADER_FAVORITE_STATUS_HPP

#include "utils/leak_check.hpp"

Expand All @@ -29,45 +29,45 @@

using namespace irr;

class KartPropertiesManager;
class TrackManager;
class UTFWriter;
class XMLNode;

/** Class for managing player profiles (name, usage frequency,
* etc.). All PlayerProfiles are managed by the PlayerManager.
* A PlayerProfile keeps track of the story mode progress using an instance
* of StoryModeStatus, and achievements with AchievementsStatus. All data
* is saved in the players.xml file.
* This class also defines the interface for handling online data. All of
* the online handling is done in the derived class OnlinePlayerProfile,
* where the interface is fully implemented.
/** Class for storing the current favorites/custom groups of karts and tracks.
* Put it into KartPropertiesManager or TrackManager by setFavoriteStatus()
* to add them into the original groups.
* \ingroup config
*/
class FavoriteTrackStatus
class FavoriteStatus
{
private:
LEAK_CHECK()

std::string m_parse_type;

/** unordered_map<Group Name, set<Track Name> > .*/
std::unordered_map<std::string, std::set<std::string> > m_favorite_tracks;
std::unordered_map<std::string, std::set<std::string> > m_favorite;

public:
friend class TrackManager;

static const std::string DEFAULT_FAVORITE_GROUP_NAME;

FavoriteTrackStatus(const XMLNode *node);
/** Parse all <(parse_type)/> in <favorite> in xml node */
FavoriteStatus(const XMLNode *node, std::string parse_type);

virtual ~FavoriteTrackStatus();
virtual ~FavoriteStatus();

void save(UTFWriter &out);

bool isFavoriteTrack(std::string ident);
bool isFavorite(std::string ident);

void addFavorite(std::string ident, std::string group = DEFAULT_FAVORITE_GROUP_NAME);

void addFavoriteTrack(std::string ident, std::string group = DEFAULT_FAVORITE_GROUP_NAME);
void removeFavorite(std::string ident, std::string group = DEFAULT_FAVORITE_GROUP_NAME);

void removeFavoriteTrack(std::string ident, std::string group = DEFAULT_FAVORITE_GROUP_NAME);
}; // class PlayerProfile
const std::unordered_map<std::string, std::set<std::string> >& getAllFavorites() const
{ return m_favorite; }
}; // class FavoriteStatus

#endif

Expand Down
14 changes: 12 additions & 2 deletions src/config/player_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ PlayerProfile::PlayerProfile(const XMLNode* node)
m_story_mode_status = NULL;
m_achievements_status = NULL;
m_favorite_track_status = NULL;
m_favorite_kart_status = NULL;
m_default_kart_color = 0.0f;
m_icon_filename = "";

Expand Down Expand Up @@ -132,7 +133,10 @@ void PlayerProfile::loadRemainingData(const XMLNode *node)
// Some favorites may correspond to uninstalled addons, so we do not sanitize the strings
assert(m_favorite_track_status == NULL);
const XMLNode *xml_favorites = node->getNode("favorites");
m_favorite_track_status = new FavoriteTrackStatus(xml_favorites);
m_favorite_track_status = new FavoriteStatus(xml_favorites, "track");

assert(m_favorite_kart_status == NULL);
m_favorite_kart_status = new FavoriteStatus(xml_favorites, "kart");

// Fix up any potentially missing icons.
addIcon();
Expand All @@ -147,7 +151,8 @@ void PlayerProfile::initRemainingData()
m_story_mode_status = unlock_manager->createStoryModeStatus();
m_achievements_status =
AchievementsManager::get()->createAchievementsStatus();
m_favorite_track_status = new FavoriteTrackStatus(NULL);
m_favorite_track_status = new FavoriteStatus(NULL, "track");
m_favorite_kart_status = new FavoriteStatus(NULL, "kart");
addIcon();
} // initRemainingData

Expand Down Expand Up @@ -241,8 +246,13 @@ void PlayerProfile::save(UTFWriter &out)
if (m_achievements_status)
m_achievements_status->save(out);


out << " <favorites>\n";
if (m_favorite_track_status)
m_favorite_track_status->save(out);
if (m_favorite_kart_status)
m_favorite_kart_status->save(out);
out << " </favorites>\n";
}
out << " </player>\n";
} // save
Expand Down
Loading

0 comments on commit 7a25743

Please sign in to comment.