Skip to content

Commit

Permalink
Add an UI to edit favorite tracks
Browse files Browse the repository at this point in the history
The UI works simply by ticking a checkbox in the track and GP screen and then selecting a track icon to switch its favorite status. The checkbox is always unticked by default when entering the screen. Changes are saved in the current player profile.

The display of tracks inside groups is not automatically refreshed.
  • Loading branch information
Alayan-stk-2 committed Aug 8, 2024
1 parent bf62de8 commit ca80a1c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
6 changes: 5 additions & 1 deletion data/gui/screens/tracks_and_gp.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<div width="100%" height="fit" layout="horizontal-row" >
<header width="30%" I18N="In the track and grand prix selection screen" text="All Tracks"
align="center" text_align="center" />
<textbox width="60%" id="search"/>
<textbox width="35%" id="search"/>
<spacer width="1%" height="100%" />
<checkbox id="favorite"/>
<spacer width="1%" height="100%" />
<label width="30%" height="100%" I18N="In the track and grand prix selection screen" text="Edit favorite tracks"/>
</div>

<spacer width="100%" height="1%" />
Expand Down
40 changes: 37 additions & 3 deletions src/config/player_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void PlayerProfile::loadRemainingData(const XMLNode *node)

// Fix up any potentially missing icons.
addIcon();
} // initRemainingData
} // loadRemainingData

//------------------------------------------------------------------------------
/** Initialises the story- and achievement data structure in case of the first
Expand All @@ -158,6 +158,10 @@ void PlayerProfile::initRemainingData()
addIcon();
} // initRemainingData

//------------------------------------------------------------------------------
/** Update the group of favorite tracks handled by the Track Manager.
* To be used only if this player profile is the current player.
*/
void PlayerProfile::setFavoriteTracks()
{
// Update the group data from the Track Manager
Expand All @@ -168,6 +172,38 @@ void PlayerProfile::setFavoriteTracks()
}
} // setFavoriteTracks

//------------------------------------------------------------------------------
/** 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.
*/
void PlayerProfile::addFavoriteTrack(std::string ident)
{
m_favorite_tracks.push_back(ident);
printf("Ident %s added to favorite tracks.\n", ident.c_str());
track_manager->addFavoriteTrack(ident);
} // addFavoriteTrack

//------------------------------------------------------------------------------
/** Removes a favorite track from this player profile and from the group
* of favorite tracks of the Track Manager.
* To be used only if this player profile is the current player.
*/
void PlayerProfile::removeFavoriteTrack(std::string ident)
{
auto it = std::find(m_favorite_tracks.begin(), m_favorite_tracks.end(), ident);
if (it != m_favorite_tracks.end()) // the track to remove has been found
{
m_favorite_tracks.erase(it);
printf("Ident %s removed from favorite tracks.\n", ident.c_str());
setFavoriteTracks();
}
else
{
printf("Favorite track to remove not found.\n");
}
} // removeFavoriteTrack

//------------------------------------------------------------------------------
/** Creates an icon for a player if non exist so far. It takes the unique
* player id modulo the number of karts to pick an icon from the karts. It
Expand Down Expand Up @@ -230,8 +266,6 @@ const std::string PlayerProfile::getIconFilename() const
*/
void PlayerProfile::save(UTFWriter &out)
{
//m_favorite_tracks.push_back("cornfield_crossing");

out << " <player name=\"" << StringUtils::xmlEncode(m_local_name)
<< "\" guest=\"" << m_is_guest_account
<< "\" use-frequency=\"" << m_use_frequency << "\"\n";
Expand Down
2 changes: 2 additions & 0 deletions src/config/player_profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class PlayerProfile : public NoCopy
void loadRemainingData(const XMLNode *node);
void initRemainingData();
void setFavoriteTracks();
void addFavoriteTrack(std::string ident);
void removeFavoriteTrack(std::string ident);
void incrementUseFrequency();
int getUseFrequency() const { return m_use_frequency; }
bool operator<(const PlayerProfile &other);
Expand Down
21 changes: 19 additions & 2 deletions src/states_screens/tracks_and_gp_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "config/user_config.hpp"
#include "graphics/stk_tex_manager.hpp"
#include "guiengine/widget.hpp"
#include "guiengine/widgets/check_box_widget.hpp"
#include "guiengine/widgets/dynamic_ribbon_widget.hpp"
#include "guiengine/widgets/icon_button_widget.hpp"
#include "io/file_manager.hpp"
Expand Down Expand Up @@ -86,8 +87,19 @@ void TracksAndGPScreen::eventCallback(Widget* widget, const std::string& name,

if (track)
{
TrackInfoScreen::getInstance()->setTrack(track);
TrackInfoScreen::getInstance()->push();
// In favorite edit mode, switch the status of the selected track
if (getWidget<CheckBoxWidget>("favorite")->getState())
{
if(track->isInGroup("Favorites"))
PlayerManager::getCurrentPlayer()->removeFavoriteTrack(track->getIdent());
else
PlayerManager::getCurrentPlayer()->addFavoriteTrack(track->getIdent());
}
else // Normal mode
{
TrackInfoScreen::getInstance()->setTrack(track);
TrackInfoScreen::getInstance()->push();
}
} // if clicked_track

} // name=="tracks"
Expand Down Expand Up @@ -132,6 +144,7 @@ void TracksAndGPScreen::eventCallback(Widget* widget, const std::string& name,
{
StateManager::get()->escapePressed();
}
// The favorite track checkbox does not need any specific additional handling
} // eventCallback

// -----------------------------------------------------------------------------
Expand All @@ -142,6 +155,10 @@ void TracksAndGPScreen::beforeAddingWidget()
RibbonWidget* tabs = getWidget<RibbonWidget>("trackgroups");
tabs->clearAllChildren();

CheckBoxWidget* favorite_cb = getWidget<CheckBoxWidget>("favorite");
assert( favorite_cb != NULL );
favorite_cb->setState(false);

const std::vector<std::string>& groups = track_manager->getAllTrackGroups();
const int group_amount = (int)groups.size();

Expand Down

0 comments on commit ca80a1c

Please sign in to comment.