Class MenuController
java.lang.Object
edu.ntnu.idi.idatt.controller.common.MenuController
- All Implemented Interfaces:
ButtonClickObserver
- Direct Known Subclasses:
LadderGameMenuController
,LudoMenuController
MenuController.
Abstract controller for handling the logic of menu views.
It manages interactions between a MenuView
and the underlying game setup components,
such as BoardFactory
and PlayerFactory
. It handles board selection, player
configuration, and navigation to other parts of the application (e.g., starting a game, creating
a board).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BoardFactory
protected int
protected static final int
protected final org.slf4j.Logger
protected final MenuView
protected Runnable
Runnable action for navigating back to the game selection screen.protected Runnable
Runnable action for navigating to the board creation screen.protected BiConsumer
<Board, List<Player>> Consumer for handling the start game action, taking a Board and a List of Players. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MenuController
(MenuView menuView) Constructs a MenuController with the specified menu view. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the list of players configured in the menu.void
Handles the action to display the next available board variant.void
Handles the action to display the previous available board variant.protected abstract void
Initializes the menu view.protected abstract void
Loads available board variants using the configuredboardFactory
.void
loadPlayersFromFile
(String filePath) Loads players from a specified file path usingPlayerFactory
and updates themenuView
.void
onButtonClicked
(String buttonId) Handles button click events without parameters from theMenuView
.void
onButtonClickedWithParams
(String buttonId, Map<String, Object> params) Handles button click events with parameters from theMenuView
.void
savePlayersToFile
(String filePath) Saves the current list of players (obtained viagetPlayers()
) to a specified file path using aPlayerFileHandlerCsv
.protected void
setBoardFactory
(BoardFactory boardFactory) Sets theBoardFactory
to be used for creating and loading boards.void
setOnBackToGameSelection
(Runnable onBackToGameSelection) Sets the action to be performed when navigating back to the game selection screen.void
setOnCreateBoard
(Runnable onCreateBoard) Sets the action to be performed when navigating to the board creation screen.void
setOnStartGame
(BiConsumer<Board, List<Player>> onStartGame) Sets the action to be performed when the game is started.
-
Field Details
-
logger
protected final org.slf4j.Logger logger -
DEFAULT_BOARD_INDEX
protected static final int DEFAULT_BOARD_INDEX- See Also:
-
boardFactory
-
boardVariants
-
currentBoardIndex
protected int currentBoardIndex -
onStartGame
Consumer for handling the start game action, taking a Board and a List of Players. -
onCreateBoard
Runnable action for navigating to the board creation screen. -
onBackToGameSelection
Runnable action for navigating back to the game selection screen.
-
-
Constructor Details
-
MenuController
Constructs a MenuController with the specified menu view.- Parameters:
menuView
- TheMenuView
associated with this controller.
-
-
Method Details
-
loadBoardsFromFactory
protected abstract void loadBoardsFromFactory()Loads available board variants using the configuredboardFactory
. Subclasses must implement this to populate theboardVariants
map. -
initializeMenuView
protected abstract void initializeMenuView()Initializes the menu view. Subclasses should implement this to set up UI elements, display initial data (like boards), and configure event handlers within themenuView
. -
getPlayers
Retrieves the list of players configured in the menu. Subclasses must implement this to get player data, typically from themenuView
.- Returns:
- A list of
Player
s.
-
setBoardFactory
Sets theBoardFactory
to be used for creating and loading boards.- Parameters:
boardFactory
- The board factory instance.
-
setOnStartGame
Sets the action to be performed when the game is started.- Parameters:
onStartGame
- ABiConsumer
that accepts the selectedBoard
and list ofPlayer
s.
-
setOnBackToGameSelection
Sets the action to be performed when navigating back to the game selection screen.- Parameters:
onBackToGameSelection
- ARunnable
action.
-
setOnCreateBoard
Sets the action to be performed when navigating to the board creation screen.- Parameters:
onCreateBoard
- ARunnable
action.
-
onButtonClicked
Handles button click events without parameters from theMenuView
. Delegates to specific handlers based on the button ID.- Specified by:
onButtonClicked
in interfaceButtonClickObserver
- Parameters:
buttonId
- The ID of the button that was clicked.
-
onButtonClickedWithParams
Handles button click events with parameters from theMenuView
. Delegates to specific handlers based on the button ID.- Specified by:
onButtonClickedWithParams
in interfaceButtonClickObserver
- Parameters:
buttonId
- The ID of the button that was clicked.params
- A map of parameters associated with the button click.
-
handleNextBoard
public void handleNextBoard()Handles the action to display the next available board variant. UpdatescurrentBoardIndex
and callsshowBoardVariant(int)
. -
handlePreviousBoard
public void handlePreviousBoard()Handles the action to display the previous available board variant. UpdatescurrentBoardIndex
and callsshowBoardVariant(int)
. -
loadPlayersFromFile
Loads players from a specified file path usingPlayerFactory
and updates themenuView
. Shows appropriate alerts on success or failure.- Parameters:
filePath
- The path to the file containing player data.
-
savePlayersToFile
Saves the current list of players (obtained viagetPlayers()
) to a specified file path using aPlayerFileHandlerCsv
. Shows appropriate alerts on success or failure.- Parameters:
filePath
- The path to the file where players should be saved.
-