Class MenuController

java.lang.Object
edu.ntnu.idi.idatt.controller.common.MenuController
All Implemented Interfaces:
ButtonClickObserver
Direct Known Subclasses:
LadderGameMenuController, LudoMenuController

public abstract class MenuController extends Object implements ButtonClickObserver
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 Details

    • logger

      protected final org.slf4j.Logger logger
    • DEFAULT_BOARD_INDEX

      protected static final int DEFAULT_BOARD_INDEX
      See Also:
    • boardFactory

      protected BoardFactory boardFactory
    • boardVariants

      protected final Map<Integer,Board> boardVariants
    • currentBoardIndex

      protected int currentBoardIndex
    • onStartGame

      protected BiConsumer<Board,List<Player>> onStartGame
      Consumer for handling the start game action, taking a Board and a List of Players.
    • onCreateBoard

      protected Runnable onCreateBoard
      Runnable action for navigating to the board creation screen.
    • onBackToGameSelection

      protected Runnable onBackToGameSelection
      Runnable action for navigating back to the game selection screen.
  • Constructor Details

    • MenuController

      protected MenuController(MenuView menuView)
      Constructs a MenuController with the specified menu view.
      Parameters:
      menuView - The MenuView associated with this controller.
  • Method Details

    • loadBoardsFromFactory

      protected abstract void loadBoardsFromFactory()
      Loads available board variants using the configured boardFactory. Subclasses must implement this to populate the boardVariants 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 the menuView.
    • getPlayers

      protected abstract List<Player> getPlayers()
      Retrieves the list of players configured in the menu. Subclasses must implement this to get player data, typically from the menuView.
      Returns:
      A list of Players.
    • setBoardFactory

      protected void setBoardFactory(BoardFactory boardFactory)
      Sets the BoardFactory to be used for creating and loading boards.
      Parameters:
      boardFactory - The board factory instance.
    • setOnStartGame

      public void setOnStartGame(BiConsumer<Board,List<Player>> onStartGame)
      Sets the action to be performed when the game is started.
      Parameters:
      onStartGame - A BiConsumer that accepts the selected Board and list of Players.
    • setOnBackToGameSelection

      public void setOnBackToGameSelection(Runnable onBackToGameSelection)
      Sets the action to be performed when navigating back to the game selection screen.
      Parameters:
      onBackToGameSelection - A Runnable action.
    • setOnCreateBoard

      public void setOnCreateBoard(Runnable onCreateBoard)
      Sets the action to be performed when navigating to the board creation screen.
      Parameters:
      onCreateBoard - A Runnable action.
    • onButtonClicked

      public void onButtonClicked(String buttonId)
      Handles button click events without parameters from the MenuView. Delegates to specific handlers based on the button ID.
      Specified by:
      onButtonClicked in interface ButtonClickObserver
      Parameters:
      buttonId - The ID of the button that was clicked.
    • onButtonClickedWithParams

      public void onButtonClickedWithParams(String buttonId, Map<String,Object> params)
      Handles button click events with parameters from the MenuView. Delegates to specific handlers based on the button ID.
      Specified by:
      onButtonClickedWithParams in interface ButtonClickObserver
      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. Updates currentBoardIndex and calls showBoardVariant(int).
    • handlePreviousBoard

      public void handlePreviousBoard()
      Handles the action to display the previous available board variant. Updates currentBoardIndex and calls showBoardVariant(int).
    • loadPlayersFromFile

      public void loadPlayersFromFile(String filePath)
      Loads players from a specified file path using PlayerFactory and updates the menuView. Shows appropriate alerts on success or failure.
      Parameters:
      filePath - The path to the file containing player data.
    • savePlayersToFile

      public void savePlayersToFile(String filePath)
      Saves the current list of players (obtained via getPlayers()) to a specified file path using a PlayerFileHandlerCsv. Shows appropriate alerts on success or failure.
      Parameters:
      filePath - The path to the file where players should be saved.