From ca80a1c5db2af607aaeec1c40a6785ab56eb7481 Mon Sep 17 00:00:00 2001
From: Alayan <25536748+Alayan-stk-2@users.noreply.github.com>
Date: Thu, 8 Aug 2024 17:25:26 +0200
Subject: [PATCH] Add an UI to edit favorite tracks
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.
---
data/gui/screens/tracks_and_gp.stkgui | 6 +++-
src/config/player_profile.cpp | 40 +++++++++++++++++++--
src/config/player_profile.hpp | 2 ++
src/states_screens/tracks_and_gp_screen.cpp | 21 +++++++++--
4 files changed, 63 insertions(+), 6 deletions(-)
diff --git a/data/gui/screens/tracks_and_gp.stkgui b/data/gui/screens/tracks_and_gp.stkgui
index d01cdac2614..8629c777b0c 100644
--- a/data/gui/screens/tracks_and_gp.stkgui
+++ b/data/gui/screens/tracks_and_gp.stkgui
@@ -24,7 +24,11 @@
-
+
+
+
+
+
diff --git a/src/config/player_profile.cpp b/src/config/player_profile.cpp
index f84382bbb8b..d0f4a2240c8 100644
--- a/src/config/player_profile.cpp
+++ b/src/config/player_profile.cpp
@@ -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
@@ -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
@@ -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
@@ -230,8 +266,6 @@ const std::string PlayerProfile::getIconFilename() const
*/
void PlayerProfile::save(UTFWriter &out)
{
- //m_favorite_tracks.push_back("cornfield_crossing");
-
out << " setTrack(track);
- TrackInfoScreen::getInstance()->push();
+ // In favorite edit mode, switch the status of the selected track
+ if (getWidget("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"
@@ -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
// -----------------------------------------------------------------------------
@@ -142,6 +155,10 @@ void TracksAndGPScreen::beforeAddingWidget()
RibbonWidget* tabs = getWidget("trackgroups");
tabs->clearAllChildren();
+ CheckBoxWidget* favorite_cb = getWidget("favorite");
+ assert( favorite_cb != NULL );
+ favorite_cb->setState(false);
+
const std::vector& groups = track_manager->getAllTrackGroups();
const int group_amount = (int)groups.size();