Skip to content

Commit

Permalink
Feat: Updated View structure to use enums for easier usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 12, 2026
1 parent 7061f88 commit 8851393
Show file tree
Hide file tree
Showing 17 changed files with 364 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventPublisher;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType;

/**
* Handles logic and event publishing for {@link ViewElement} objects.
Expand All @@ -18,14 +19,15 @@
* @see EventManager
*
*/
public abstract class ViewController<T1 extends ViewElement<?>>
public abstract class ViewController<T1 extends ViewElement<?, ?>>
implements EventPublisher {

/**
* The {@link ViewElement} object this controller is associated with.
*
*/
private final T1 viewElement;

/**
* The {@link EventManager} object to send events to.
*
Expand Down Expand Up @@ -84,4 +86,15 @@ public final <T> void invoke(final EventData<T> data,
final EventManager eventManager) {
eventManager.invokeEvent(data);
}

/**
* Basic method for invoking an event to change to a specific view.
*
* @param viewName the view to set to.
* */
protected void changeScene(final ViewEnum viewName) {
ViewData viewData = new ViewData(viewName);
EventData<ViewData> eventData = new EventData<ViewData>(EventType.SCENE_CHANGE, viewData);
invoke(eventData, eventManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ViewData {
* Name of the scene.
*
*/
private final String sceneName;
private final ViewEnum sceneName;

/**
* Constructor.
Expand All @@ -26,8 +26,8 @@ public class ViewData {
*
* @throws IllegalArgumentException if sceneName is empty or null.
*/
public ViewData(final String sceneName) throws IllegalArgumentException {
if (Validator.NOT_EMPTY.isValid(sceneName)) {
public ViewData(final ViewEnum sceneName) throws IllegalArgumentException {
if (Validator.NOT_EMPTY.isValid(sceneName.toString())) {
this.sceneName = sceneName;
} else {
throw new IllegalArgumentException(Validator.NOT_EMPTY.getErrorMessage());
Expand All @@ -40,7 +40,7 @@ public ViewData(final String sceneName) throws IllegalArgumentException {
* @return scene name.
*
*/
public String getSceneName() {
public ViewEnum getSceneName() {
return sceneName;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view;

import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.Map;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

Expand All @@ -12,15 +13,16 @@
* <p>Extends {@link Pane}</p>
*
* @param <T> the type of pane this view element represents.
* @param <A> an enum representing all interactable actions found in the view.
*
*/
public abstract class ViewElement<T extends Pane> {
public abstract class ViewElement<T extends Pane, A extends Enum<A>> {

/**
* List of buttons in this view.
*
*/
private final ArrayList<Button> buttons = new ArrayList<>();
private final Map<A, Button> buttonMap;

/**
* The {@link Pane} element the view element has as a root.
Expand All @@ -36,18 +38,18 @@ public abstract class ViewElement<T extends Pane> {
* <p>Used as identification by the {@link ViewManager}.</p>
*
*/
private String viewName;
private ViewEnum viewName;

/**
* Constructor with a name.
*
* @param rootPane an instance of type T (defined in the class).
* @param viewName The name of the view.
* @param viewName The name of the view as an {@link ViewEnum}.
*
*/
protected ViewElement(final T rootPane, final String viewName) {
this(rootPane);
if (Validator.NOT_EMPTY.isValid(viewName)) {
protected ViewElement(final T rootPane, final ViewEnum viewName, final Class<A> actionEnum) {
this(rootPane, actionEnum);
if (Validator.NOT_EMPTY.isValid(viewName.name())) {
this.viewName = viewName;
} else {
throw new IllegalArgumentException(Validator.NOT_EMPTY.getErrorMessage());
Expand All @@ -60,9 +62,10 @@ protected ViewElement(final T rootPane, final String viewName) {
* @param rootPane the root of this view.
*
*/
protected ViewElement(final T rootPane) {
protected ViewElement(final T rootPane, final Class<A> actionEnum) {
if (rootPane != null) {
setRootPane(rootPane);
this.buttonMap = new EnumMap<>(actionEnum);
initLayout();
initStyling();
} else {
Expand All @@ -76,7 +79,7 @@ protected ViewElement(final T rootPane) {
* @return the name of this view.
*
*/
public String getViewName() {
public ViewEnum getViewName() {
return viewName;
}

Expand All @@ -86,7 +89,7 @@ public String getViewName() {
* @param name the new name to set this view element to.
*
*/
protected void setViewName(final String name) {
protected void setViewName(final ViewEnum name) {
viewName = name;
}

Expand Down Expand Up @@ -116,12 +119,12 @@ protected void setRootPane(final T pane) {
* <p>Gotten by the associated {@link ViewController} object that hooks
* the buttons to functionality.</p>
*
* @return {@link ArrayList<Button>} object containing all buttons
* @return {@link Map<Enum, Button>} object containing all buttons
* in this view element.
*
*/
public ArrayList<Button> getButtons() {
return buttons;
public Map<A, Button> getButtons() {
return buttonMap;
}

/**
Expand All @@ -136,6 +139,35 @@ public ArrayList<Button> getButtons() {
*/
protected abstract void initStyling();

/**
* Connects an action to a button.
*
* @param action the enum of type A to put the button to.
* @param button the button to assign the action to.
*
* */
protected void registerButton(final A action, final Button button) {
buttonMap.put(action, button);
}

/**
* Method for assigning logic to a button by referencing the action.
*
* @param action the action to assign logic to.
* @param logic the {@link Runnable} code defining the logic.
*
* @throws IllegalArgumentException if Action is not in map.
* */
public void setOnAction(final A action, final Runnable logic)
throws IllegalArgumentException {
Button btn = buttonMap.get(action);
if (btn == null) {
throw new IllegalArgumentException("Invalid button");
} else {
btn.setOnAction(e -> logic.run());
}
}

/**
* Method that defines how view elements set data.
*
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view;

/**
* Enum representing different views in the system.
* */
public enum ViewEnum {
/**
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuView}.
* */
MAIN_MENU,

/**
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsView}.
* */
SETTINGS,

/**
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.playgame.PlayGameView}.
* */
PLAY_GAME,

/**
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.ingame.InGameView}.
* */
IN_GAME;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class ViewManager implements EventSubscriber {
* A map with String keys and {@link ViewElement} objects as values.
*
*/
private final Map<String, ViewElement<?>> viewMap;
private final Map<ViewEnum, ViewElement<?, ?>> viewMap;

/**
* The main stage of the application.
Expand All @@ -38,7 +38,7 @@ public final class ViewManager implements EventSubscriber {
* Current view. Used for testing and debugging purposes.
*
*/
private ViewElement<?> currentView;
private ViewElement<?, ?> currentView;

/**
* Constructor.
Expand Down Expand Up @@ -68,7 +68,7 @@ public ViewManager(final Stage stage, final EventManager eventManager)
* @throws IllegalArgumentException if view is already in map, or if
* view does not have view name (widget).
*/
public void addView(final ViewElement<?> view)
public void addView(final ViewElement<?, ?> view)
throws IllegalArgumentException {
if (view.getViewName() == null) {
throw new IllegalArgumentException("Cannot add widget"
Expand All @@ -91,7 +91,7 @@ public void addView(final ViewElement<?> view)
*
* @throws IllegalArgumentException if view is not in view map.
*/
public void setScene(final ViewElement<?> viewElement)
public void setScene(final ViewElement<?, ?> viewElement)
throws IllegalArgumentException {
if (viewElement == null
|| !viewMap.containsKey(viewElement.getViewName())) {
Expand Down Expand Up @@ -129,7 +129,7 @@ public void setScene(final ViewData data) throws IllegalArgumentException {
if (data == null) {
throw new IllegalArgumentException("Data is null!");
} else {
ViewElement<?> viewElement = viewMap.get(data.getSceneName());
ViewElement<?, ?> viewElement = viewMap.get(data.getSceneName());
viewElement.setData(data);
setScene(viewElement);
currentView = viewElement;
Expand All @@ -144,7 +144,7 @@ public void setScene(final ViewData data) throws IllegalArgumentException {
* @return the view element currently active.
*
*/
public ViewElement<?> getCurrentView() {
public ViewElement<?, ?> getCurrentView() {
return currentView;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu;

public enum MainMenuActions {
START_GAME,
SETTINGS,
EXIT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewData;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewEnum;
import javafx.application.Platform;

/**
* Controller for the {@link MainMenuView}.
*
* <p>
* Extends {@link ViewController}
* </p>
* <p>Extends {@link ViewController} </p>
*/
public class MainMenuController extends ViewController<MainMenuView> {

Expand All @@ -31,24 +30,20 @@ public MainMenuController(final MainMenuView view,
/**
* {@inheritDoc}
*
* <p>
* Sets play game, settings, and exit button functionality.
* </p>
* <p>Sets play game, settings, and exit button functionality.</p>
*/
@Override
protected void initInteractions() {
getViewElement().getToGameButton().setOnAction(e -> {
ViewData viewData = new ViewData("PlayGameView");
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
invoke(eventData, getEventManager());

getViewElement().setOnAction(MainMenuActions.START_GAME, () -> {
changeScene(ViewEnum.PLAY_GAME);
});

getViewElement().getSettingsButton().setOnAction(e -> {
ViewData viewData = new ViewData("SettingsView");
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
getViewElement().setOnAction(MainMenuActions.SETTINGS, () -> {
changeScene(ViewEnum.SETTINGS);
});

getViewElement().getExitButton().setOnAction(e -> Platform.exit());
getViewElement().setOnAction(MainMenuActions.EXIT, Platform::exit);

}
}
Loading

0 comments on commit 8851393

Please sign in to comment.