Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure only game master controls kart filter #5368

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 101 additions & 57 deletions src/states_screens/kart_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,6 @@ void KartSelectionScreen::eventCallback(Widget* widget,
{
RibbonWidget* tabs = getWidget<RibbonWidget>("kartgroups");
assert(tabs != NULL);
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");
assert(w != NULL);

setKartsFromCurrentGroup();

Expand All @@ -1154,59 +1152,7 @@ void KartSelectionScreen::eventCallback(Widget* widget,

UserConfigParams::m_last_used_kart_group = selected_kart_group;

RandomGenerator random;

const int num_players = m_kart_widgets.size();
for (int n=0; n<num_players; n++)
{
// The game master is the one that can change the groups, leave
// his focus on the tabs for others, remove focus from kart that
// might no more exist in this tab.
if (n != PLAYER_ID_GAME_MASTER)
GUIEngine::focusNothingForPlayer(n);

if (!m_kart_widgets[n].isReady())
{
// try to preserve the same kart for each player (except for
// game master, since it's the one that can change the
// groups, so focus for this player must remain on the tabs)
const std::string& selected_kart =
m_kart_widgets[n].getKartInternalName();
if (!w->setSelection( selected_kart, n,
n != PLAYER_ID_GAME_MASTER))
{
// if we get here, it means one player "lost" his kart in
// the tab switch
if (UserConfigParams::logGUI())
Log::info("KartSelectionScreen", "Player %u"
" lost their selection when switching tabs!!!",n);

// Select a random kart in this case
const int count = (int) w->getItems().size();
if (count > 0)
{
// FIXME: two players may be given the same kart by
// the use of random
const int random_id = random.get( count );

// select kart for players > 0 (player 0 is the one
// that can change the groups, so focus for player 0
// must remain on the tabs)
const bool success =
w->setSelection( random_id, n,
n != PLAYER_ID_GAME_MASTER );
if (!success)
Log::warn("KartSelectionScreen",
"setting kart of player %u failed");
}
else
{
Log::warn("KartSelectionScreen", " 0 items "
"in the ribbon");
}
}
}
} // end for
handleKartListFocus();
}
else if (name == "karts")
{
Expand All @@ -1215,7 +1161,7 @@ void KartSelectionScreen::eventCallback(Widget* widget,
const std::string selection = w->getSelectionIDString(player_id);

if (getWidget<CheckBoxWidget>("favorite")->getState() &&
player_id == PLAYER_ID_GAME_MASTER &&
player_id == PLAYER_ID_GAME_MASTER && !m_game_master_confirmed &&
selection != RANDOM_KART_ID && !selection.empty())
{
const KartProperties *kp = kart_properties_manager->getKart(selection);
Expand All @@ -1229,13 +1175,17 @@ void KartSelectionScreen::eventCallback(Widget* widget,
PlayerManager::getCurrentPlayer()->addFavoriteKart(kp->getIdent());
}
setKartsFromCurrentGroup();

handleKartListFocus();
}
else if (m_kart_widgets.size() > unsigned(player_id) && !useContinueButton())
playerConfirm(player_id);
}
else if (name == "kart_class")
else if (name == "kart_class" && !m_game_master_confirmed)
{
setKartsFromCurrentGroup();

handleKartListFocus();
}
else if (name == "continue")
{
Expand Down Expand Up @@ -1289,6 +1239,38 @@ bool KartSelectionScreen::onEscapePressed()

// ----------------------------------------------------------------------------

void KartSelectionScreen::onFocusChanged(GUIEngine::Widget* previous,
GUIEngine::Widget* focus, int playerID)
{
if (playerID == PLAYER_ID_GAME_MASTER || !previous || !focus)
{
return;
}
GUIEngine::SpinnerWidget* kart_class = getWidget<GUIEngine::SpinnerWidget>("kart_class");
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");

if (GUIEngine::isFocusedForPlayer(kart_class, playerID))
{
for (int i = 0; i < m_kart_widgets.size(); i++)
{
if (m_kart_widgets[i].getPlayerID() == playerID)
{
if (previous->getType() == WTYPE_RIBBON)
{
m_kart_widgets[i].getPlayerNameSpinner()->setFocusForPlayer(playerID);
}
else
{
w->setSelection(playerID, playerID, true);
}
break;
}
}
}
}

// ----------------------------------------------------------------------------

#if 0
#pragma mark -
#pragma mark KartSelectionScreen (private)
Expand Down Expand Up @@ -1429,6 +1411,68 @@ void KartSelectionScreen::allPlayersDone()

// ----------------------------------------------------------------------------

void KartSelectionScreen::handleKartListFocus()
{
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");
assert(w != NULL);

RandomGenerator random;

const int num_players = m_kart_widgets.size();
for (int n=0; n<num_players; n++)
{
// The game master is the one that can change the groups, leave
// his focus on the tabs for others, remove focus from kart that
// might no more exist in this tab.
if (n != PLAYER_ID_GAME_MASTER)
GUIEngine::focusNothingForPlayer(n);

if (!m_kart_widgets[n].isReady())
{
// try to preserve the same kart for each player (except for
// game master, since it's the one that can change the
// groups, so focus for this player must remain on the tabs)
const std::string& selected_kart =
m_kart_widgets[n].getKartInternalName();
if (!w->setSelection( selected_kart, n,
n != PLAYER_ID_GAME_MASTER))
{
// if we get here, it means one player "lost" his kart in
// the tab switch
if (UserConfigParams::logGUI())
Log::info("KartSelectionScreen", "Player %u"
" lost their selection when switching tabs!!!",n);

// Select a random kart in this case
const int count = (int) w->getItems().size();
if (count > 0)
{
// FIXME: two players may be given the same kart by
// the use of random
const int random_id = random.get( count );

// select kart for players > 0 (player 0 is the one
// that can change the groups, so focus for player 0
// must remain on the tabs)
const bool success =
w->setSelection( random_id, n,
n != PLAYER_ID_GAME_MASTER );
if (!success)
Log::warn("KartSelectionScreen",
"setting kart of player %u failed");
}
else
{
Log::warn("KartSelectionScreen", " 0 items "
"in the ribbon");
}
}
}
} // end for
}

// ----------------------------------------------------------------------------

bool KartSelectionScreen::validateIdentChoices()
{
bool ok = true;
Expand Down
9 changes: 9 additions & 0 deletions src/states_screens/kart_selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class KartSelectionScreen : public GUIEngine::Screen,
/** Called when all players selected their kart */
virtual void allPlayersDone();

/** When kart list has been changed, make sure all players have valid
* focus */
void handleKartListFocus();

/** Called when number/order of karts changed, so that all will keep
* an up-to-date ID */
void renumberKarts();
Expand Down Expand Up @@ -182,8 +186,13 @@ class KartSelectionScreen : public GUIEngine::Screen,
setKartsFromCurrentGroup();
// After setKartsFromCurrentGroup the m_search_box may be unfocused
m_search_box->focused(PLAYER_ID_GAME_MASTER);

handleKartListFocus();
}

virtual void onFocusChanged(GUIEngine::Widget* previous,
GUIEngine::Widget* focus, int playerID) OVERRIDE;

/** \brief implement optional callback from parent
* class GUIEngine::Screen */
virtual void unloaded() OVERRIDE;
Expand Down
Loading