Skip to content

Commit

Permalink
Feat: Added base classes representing View and Controller in the MVC
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed Apr 24, 2026
1 parent d63b9ba commit 6d3ea55
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.ntnu.idi.idatt2003.g40.mappe.utils;

public enum ConfigValues {
VIEWPORT_WIDTH(600),
VIEWPORT_HEIGHT(600);

private int value;

ConfigValues(final int value) {
this.value = value;
}

public int getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view;

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;

/**
* Handles logic and event publishing for {@link ViewElement} objects.
*
* <p>Implements {@link EventPublisher}</p>
*
* @param <T1> The type of {@link ViewElement} this controller is attached to. We use generics
* because we want to call methods that may be specific for certain view element
* objects, and we want to reduce casting.
* @see ViewElement
* @see EventPublisher
* @see EventManager
*
*/
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.
*
*/
private final EventManager eventManager;

/**
* Constructor.
*
* @param viewElement the instance of type T1 this controller is attached to.
* @param eventManager the {@link EventManager} object this controller communicates with.
*
*/
protected ViewController(final T1 viewElement, final EventManager eventManager) {
this.viewElement = viewElement;
this.eventManager = eventManager;
initInteractions();
}

/**
* Getter method for the event manager.
*
* @return the {@link EventManager} object.
*
*/
protected EventManager getEventManager() {
return eventManager;
}

/**
* Getter method for the current view element.
*
* @return the {@link ViewElement} object.
*
*/
public T1 getViewElement() {
return viewElement;
}

/**
* Abstract method to initialize logic for the view element.
*
*/
protected abstract void initInteractions();

@Override
public final <T> void invoke(final EventData<T> data, final EventManager eventManager) {
eventManager.invokeEvent(data);
}
}
39 changes: 39 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view;

/**
* Object containing data about {@link ViewElement} objects.
*
* <p>Sent in {@link edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData}
* objects of type SCENE_CHANGE</p>
*
* <p>Handled by the {@link ViewManager} to update and change views</p>
*
*/
public class ViewData {

/**
* Name of the scene.
*
*/
private final String sceneName;

/**
* Constructor.
*
* @param sceneName the name of the scene.
*
*/
public ViewData(final String sceneName) {
this.sceneName = sceneName;
}

/**
* Getter method for the scene name.
*
* @return scene name.
*
*/
public String getSceneName() {
return sceneName;
}
}
155 changes: 155 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view;


import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import java.util.ArrayList;

/**
* Base class for view elements, such as the main menu, or list items.
*
* <p>Extends {@link Pane}</p>
*
* @param <T> the type of pane this view element represents.
*
*/
public abstract class ViewElement<T extends Pane> {

/**
* List of buttons in this view.
*
*/
private final ArrayList<Button> buttons = new ArrayList<>();
/**
* The {@link Pane} element the view element has as a root.
*
* <p>This allows different view elements to have different layouts.</p>
*
*/
private T rootPane;
/**
* The name of the view.
*
* <p>Used as identification by the {@link ViewManager}.</p>
*
*/
private String viewName;

/**
* Constructor with a name.
*
* @param rootPane an instance of type T (defined in the class).
* @param viewName The name of the view.
*
*/
protected ViewElement(final T rootPane, final String viewName) {
this.viewName = viewName;
this(rootPane, true);
}

/**
* Constructor with a name and initLoading.
*
* @param rootPane an instance of type T (defined in the class).
* @param viewName The name of the view.
* @param initLoading boolean value representing if the view should be initialized. Default is
* true.
*
*/
protected ViewElement(final T rootPane, final String viewName, boolean initLoading) {
this.viewName = viewName;
this(rootPane, initLoading);
}

/**
* Constructor without a specific name.
*
* @param rootPane an instance of type T (defined in the class).
*
*/
protected ViewElement(final T rootPane, boolean isInit) {
setRootPane(rootPane);
if (isInit) {
initLayout();
}
initStyling();
}

/**
* Getter method for view name.
*
* @return the name of this view.
*
*/
public String getViewName() {
return viewName;
}

/**
* Setter method for the view name.
*
* @param name the new name to set this view element to.
*
*/
protected void setViewName(final String name) {
viewName = name;
}

/**
* Getter method for the root pane.
*
* @return the root pane of this view element.
*
*/
public T getRootPane() {
return rootPane;
}

/**
* Setter method for the root pane.
*
* @param pane the pane to set the root pane to.
*
*/
protected void setRootPane(final T pane) {
rootPane = pane;
}

/**
* Getter method for buttons in this view.
*
* <p>Gotten by the associated {@link ViewController} object that hooks
* the buttons to functionality.</p>
*
* @return {@link ArrayList<Button>} object containing all buttons in this view element.
*
*/
public ArrayList<Button> getButtons() {
return buttons;
}

/**
* Method that initializes the layout for the view element.
*
*/
protected abstract void initLayout();

/**
* Method that initializes the styling for the view element.
*
*/
protected abstract void initStyling();

/**
* Method that defines how view elements set data.
*
* @param <T2> The type of data to set.
* @param data the data to set.
*
*/
public <T2 extends ViewData> void setData(final T2 data) {
setViewName(data.getSceneName());
}

public abstract void onUpdate();
}
Loading

0 comments on commit 6d3ea55

Please sign in to comment.