Skip to content

Commit 414a8e9

Browse files
Make sure only game master controls kart filter (#5368)
1 parent 6c287e0 commit 414a8e9

File tree

2 files changed

+110
-57
lines changed

2 files changed

+110
-57
lines changed

src/states_screens/kart_selection.cpp

+101-57
Original file line numberDiff line numberDiff line change
@@ -1144,8 +1144,6 @@ void KartSelectionScreen::eventCallback(Widget* widget,
11441144
{
11451145
RibbonWidget* tabs = getWidget<RibbonWidget>("kartgroups");
11461146
assert(tabs != NULL);
1147-
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");
1148-
assert(w != NULL);
11491147

11501148
setKartsFromCurrentGroup();
11511149

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

11551153
UserConfigParams::m_last_used_kart_group = selected_kart_group;
11561154

1157-
RandomGenerator random;
1158-
1159-
const int num_players = m_kart_widgets.size();
1160-
for (int n=0; n<num_players; n++)
1161-
{
1162-
// The game master is the one that can change the groups, leave
1163-
// his focus on the tabs for others, remove focus from kart that
1164-
// might no more exist in this tab.
1165-
if (n != PLAYER_ID_GAME_MASTER)
1166-
GUIEngine::focusNothingForPlayer(n);
1167-
1168-
if (!m_kart_widgets[n].isReady())
1169-
{
1170-
// try to preserve the same kart for each player (except for
1171-
// game master, since it's the one that can change the
1172-
// groups, so focus for this player must remain on the tabs)
1173-
const std::string& selected_kart =
1174-
m_kart_widgets[n].getKartInternalName();
1175-
if (!w->setSelection( selected_kart, n,
1176-
n != PLAYER_ID_GAME_MASTER))
1177-
{
1178-
// if we get here, it means one player "lost" his kart in
1179-
// the tab switch
1180-
if (UserConfigParams::logGUI())
1181-
Log::info("KartSelectionScreen", "Player %u"
1182-
" lost their selection when switching tabs!!!",n);
1183-
1184-
// Select a random kart in this case
1185-
const int count = (int) w->getItems().size();
1186-
if (count > 0)
1187-
{
1188-
// FIXME: two players may be given the same kart by
1189-
// the use of random
1190-
const int random_id = random.get( count );
1191-
1192-
// select kart for players > 0 (player 0 is the one
1193-
// that can change the groups, so focus for player 0
1194-
// must remain on the tabs)
1195-
const bool success =
1196-
w->setSelection( random_id, n,
1197-
n != PLAYER_ID_GAME_MASTER );
1198-
if (!success)
1199-
Log::warn("KartSelectionScreen",
1200-
"setting kart of player %u failed");
1201-
}
1202-
else
1203-
{
1204-
Log::warn("KartSelectionScreen", " 0 items "
1205-
"in the ribbon");
1206-
}
1207-
}
1208-
}
1209-
} // end for
1155+
handleKartListFocus();
12101156
}
12111157
else if (name == "karts")
12121158
{
@@ -1215,7 +1161,7 @@ void KartSelectionScreen::eventCallback(Widget* widget,
12151161
const std::string selection = w->getSelectionIDString(player_id);
12161162

12171163
if (getWidget<CheckBoxWidget>("favorite")->getState() &&
1218-
player_id == PLAYER_ID_GAME_MASTER &&
1164+
player_id == PLAYER_ID_GAME_MASTER && !m_game_master_confirmed &&
12191165
selection != RANDOM_KART_ID && !selection.empty())
12201166
{
12211167
const KartProperties *kp = kart_properties_manager->getKart(selection);
@@ -1229,13 +1175,17 @@ void KartSelectionScreen::eventCallback(Widget* widget,
12291175
PlayerManager::getCurrentPlayer()->addFavoriteKart(kp->getIdent());
12301176
}
12311177
setKartsFromCurrentGroup();
1178+
1179+
handleKartListFocus();
12321180
}
12331181
else if (m_kart_widgets.size() > unsigned(player_id) && !useContinueButton())
12341182
playerConfirm(player_id);
12351183
}
1236-
else if (name == "kart_class")
1184+
else if (name == "kart_class" && !m_game_master_confirmed)
12371185
{
12381186
setKartsFromCurrentGroup();
1187+
1188+
handleKartListFocus();
12391189
}
12401190
else if (name == "continue")
12411191
{
@@ -1289,6 +1239,38 @@ bool KartSelectionScreen::onEscapePressed()
12891239

12901240
// ----------------------------------------------------------------------------
12911241

1242+
void KartSelectionScreen::onFocusChanged(GUIEngine::Widget* previous,
1243+
GUIEngine::Widget* focus, int playerID)
1244+
{
1245+
if (playerID == PLAYER_ID_GAME_MASTER || !previous || !focus)
1246+
{
1247+
return;
1248+
}
1249+
GUIEngine::SpinnerWidget* kart_class = getWidget<GUIEngine::SpinnerWidget>("kart_class");
1250+
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");
1251+
1252+
if (GUIEngine::isFocusedForPlayer(kart_class, playerID))
1253+
{
1254+
for (int i = 0; i < m_kart_widgets.size(); i++)
1255+
{
1256+
if (m_kart_widgets[i].getPlayerID() == playerID)
1257+
{
1258+
if (previous->getType() == WTYPE_RIBBON)
1259+
{
1260+
m_kart_widgets[i].getPlayerNameSpinner()->setFocusForPlayer(playerID);
1261+
}
1262+
else
1263+
{
1264+
w->setSelection(playerID, playerID, true);
1265+
}
1266+
break;
1267+
}
1268+
}
1269+
}
1270+
}
1271+
1272+
// ----------------------------------------------------------------------------
1273+
12921274
#if 0
12931275
#pragma mark -
12941276
#pragma mark KartSelectionScreen (private)
@@ -1429,6 +1411,68 @@ void KartSelectionScreen::allPlayersDone()
14291411

14301412
// ----------------------------------------------------------------------------
14311413

1414+
void KartSelectionScreen::handleKartListFocus()
1415+
{
1416+
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");
1417+
assert(w != NULL);
1418+
1419+
RandomGenerator random;
1420+
1421+
const int num_players = m_kart_widgets.size();
1422+
for (int n=0; n<num_players; n++)
1423+
{
1424+
// The game master is the one that can change the groups, leave
1425+
// his focus on the tabs for others, remove focus from kart that
1426+
// might no more exist in this tab.
1427+
if (n != PLAYER_ID_GAME_MASTER)
1428+
GUIEngine::focusNothingForPlayer(n);
1429+
1430+
if (!m_kart_widgets[n].isReady())
1431+
{
1432+
// try to preserve the same kart for each player (except for
1433+
// game master, since it's the one that can change the
1434+
// groups, so focus for this player must remain on the tabs)
1435+
const std::string& selected_kart =
1436+
m_kart_widgets[n].getKartInternalName();
1437+
if (!w->setSelection( selected_kart, n,
1438+
n != PLAYER_ID_GAME_MASTER))
1439+
{
1440+
// if we get here, it means one player "lost" his kart in
1441+
// the tab switch
1442+
if (UserConfigParams::logGUI())
1443+
Log::info("KartSelectionScreen", "Player %u"
1444+
" lost their selection when switching tabs!!!",n);
1445+
1446+
// Select a random kart in this case
1447+
const int count = (int) w->getItems().size();
1448+
if (count > 0)
1449+
{
1450+
// FIXME: two players may be given the same kart by
1451+
// the use of random
1452+
const int random_id = random.get( count );
1453+
1454+
// select kart for players > 0 (player 0 is the one
1455+
// that can change the groups, so focus for player 0
1456+
// must remain on the tabs)
1457+
const bool success =
1458+
w->setSelection( random_id, n,
1459+
n != PLAYER_ID_GAME_MASTER );
1460+
if (!success)
1461+
Log::warn("KartSelectionScreen",
1462+
"setting kart of player %u failed");
1463+
}
1464+
else
1465+
{
1466+
Log::warn("KartSelectionScreen", " 0 items "
1467+
"in the ribbon");
1468+
}
1469+
}
1470+
}
1471+
} // end for
1472+
}
1473+
1474+
// ----------------------------------------------------------------------------
1475+
14321476
bool KartSelectionScreen::validateIdentChoices()
14331477
{
14341478
bool ok = true;

src/states_screens/kart_selection.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class KartSelectionScreen : public GUIEngine::Screen,
9292
/** Called when all players selected their kart */
9393
virtual void allPlayersDone();
9494

95+
/** When kart list has been changed, make sure all players have valid
96+
* focus */
97+
void handleKartListFocus();
98+
9599
/** Called when number/order of karts changed, so that all will keep
96100
* an up-to-date ID */
97101
void renumberKarts();
@@ -182,8 +186,13 @@ class KartSelectionScreen : public GUIEngine::Screen,
182186
setKartsFromCurrentGroup();
183187
// After setKartsFromCurrentGroup the m_search_box may be unfocused
184188
m_search_box->focused(PLAYER_ID_GAME_MASTER);
189+
190+
handleKartListFocus();
185191
}
186192

193+
virtual void onFocusChanged(GUIEngine::Widget* previous,
194+
GUIEngine::Widget* focus, int playerID) OVERRIDE;
195+
187196
/** \brief implement optional callback from parent
188197
* class GUIEngine::Screen */
189198
virtual void unloaded() OVERRIDE;

0 commit comments

Comments
 (0)