Skip to content

Commit

Permalink
Merge pull request #88 from Team-40-IDATT2003/85-update-pubsub-model
Browse files Browse the repository at this point in the history
85 update pubsub model
  • Loading branch information
etsorens authored May 11, 2026
2 parents ff3c59f + 355d75f commit ee1f818
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 50 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
<version>6.0.1</version>
<scope>test</scope>
</dependency>

<!-- JavaFX testing -->
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit5</artifactId>
<version>4.0.18</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
import edu.ntnu.idi.idatt2003.g40.mappe.view.ingame.InGameView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuView;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
* Main class.
*
* <p>Extends {@link Application}</p>
*
* <p>Initializes the application through the javafx framework.</p>
* */
public class Main extends Application {
static void main() {
FileParser parser1 = new FileParser("src/main/resources/dummydata.txt");
Expand All @@ -32,11 +38,15 @@ static void main() {
}
}

/**
* {@inheritDoc}
* */
@Override
public void start(final Stage stage) throws Exception {
Scene scene = new Scene(new Pane());
scene.getStylesheets()
.add(Objects.requireNonNull(getClass().getResource("/styles.css")).toExternalForm());
Font.loadFont(getClass().getResourceAsStream("/Fonts/Aptos.ttf"), 16);
stage.setScene(scene);
stage.setWidth(ConfigValues.VIEWPORT_WIDTH.getValue());
stage.setHeight(ConfigValues.VIEWPORT_HEIGHT.getValue());
Expand All @@ -48,7 +58,6 @@ public void start(final Stage stage) throws Exception {

InGameView inGameView = new InGameView();


viewManager.addView(mainMenuView);
viewManager.addView(inGameView);
viewManager.setScene(mainMenuView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import edu.ntnu.idi.idatt2003.g40.mappe.model.Purchase;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Sale;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Transaction;

import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.ntnu.idi.idatt2003.g40.mappe.service.event;

/**
* Interface representing an event channel.
*
* <p>Used to separate event types into groups.</p>
*
* <p>Decreases coupling and enables testing of event types.</p>
* */
public interface EventChannel {

/**
* Getter method for enum name.
*
* @return String name of enum.
* */
String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Data object sent through the event system by the {@link EventManager}.
*
* @param <T> the type of data this object represents.
* @param eventType the event type this object represents.
* @param channel the event type this object represents.
* @param data the data this object contains.
*
*/
public record EventData<T>(EventType eventType, T data) {
public record EventData<T>(EventChannel channel, T data) {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package edu.ntnu.idi.idatt2003.g40.mappe.service.event;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Event broker in the pub/sub model.
Expand All @@ -26,19 +23,19 @@ public final class EventManager {
* <p>Used to identify which subscribers to fire the event towards.</p>
*
*/
private final Map<EventType, List<EventSubscriber>> subscriberMap =
new EnumMap<>(EventType.class);
private final Map<EventChannel, List<EventSubscriber>> subscriberMap =
new HashMap<>();

/**
* Method for adding a subscriber to the subscriber map given an event type.
*
* @param subscriber the {@link EventSubscriber} object to add.
* @param eventType the {@link EventType} this subscriber should subscribe to.
* @param subscriber the {@link EventSubscriber} object to add.
* @param eventChannel the {@link EventChannel} type this subscriber should subscribe to.
*
*
*/
public void addSubscriber(final EventSubscriber subscriber, final EventType eventType) {
subscriberMap.computeIfAbsent(eventType, k -> new ArrayList<>()).add(subscriber);
public void addSubscriber(final EventSubscriber subscriber, final EventChannel eventChannel) {
subscriberMap.computeIfAbsent(eventChannel, k -> new ArrayList<>()).add(subscriber);
}

/**
Expand All @@ -47,11 +44,19 @@ public void addSubscriber(final EventSubscriber subscriber, final EventType even
* @param <T> the type of event data to send.
* @param data the data to send.
*
*
* @throws IllegalArgumentException if no subscriber is found of the
* event type.
*/
public <T> void invokeEvent(final EventData<T> data) {
for (EventSubscriber e : subscriberMap.get(data.eventType())) {
e.handleEvent(data);
public <T> void invokeEvent(final EventData<T> data)
throws IllegalArgumentException {
if (data == null || !subscriberMap.containsKey(data.channel())) {
throw new IllegalArgumentException(
"No subscriber listening to this event!"
);
} else {
for (EventSubscriber e : subscriberMap.get(data.channel())) {
e.handleEvent(data);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
* <li>Other</li>
* </ul>
*
*
*/
public enum EventType {
public enum EventType implements EventChannel {

/**
* Event type representing events that causes the current scene to change.
Expand All @@ -23,16 +22,13 @@ public enum EventType {
* @see edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager
*
*/
SCENE_CHANGE,
SCENE_CHANGE;

/**
* Event type representing events that causes the scene to change to the previous one.
*
* <p>Primarily handled by the active
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager} object.</p>
*
* @see edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager
*
*/
SCENE_BACK,
* {@inheritDoc}
* */
@Override
public String getName() {
return this.name();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package edu.ntnu.idi.idatt2003.g40.mappe.utils;

/**
* Enum containing configuration values for the application.
* */
public enum ConfigValues {
/**
* Standard viewport width.
* */
VIEWPORT_WIDTH(600),

/**
* Standard viewport height.
* */
VIEWPORT_HEIGHT(600);

private int value;
/**
* Integer parameter of a config value.
* */
private final int value;

/**
* Constructor.
*
* @param value the value to assign this configuration.
* */
ConfigValues(final int value) {
this.value = value;
}

/**
* Getter method for value.
*
* @return value of the configuration.
* */
public int getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
*
* <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.
* @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
Expand All @@ -35,13 +36,21 @@ public abstract class ViewController<T1 extends ViewElement<?>>
* Constructor.
*
* @param viewElement the instance of type T1 this controller is attached to.
* @param eventManager the {@link EventManager} object this controller communicates with.
* @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();
protected ViewController(final T1 viewElement,
final EventManager eventManager)
throws IllegalArgumentException {
if (viewElement == null || eventManager == null) {
throw new IllegalArgumentException(
"Improper ViewController instantiating!");
} else {
this.viewElement = viewElement;
this.eventManager = eventManager;
initInteractions();
}
}

/**
Expand Down Expand Up @@ -71,7 +80,8 @@ public T1 getViewElement() {
protected abstract void initInteractions();

@Override
public final <T> void invoke(final EventData<T> data, final EventManager eventManager) {
public final <T> void invoke(final EventData<T> data,
final EventManager eventManager) {
eventManager.invokeEvent(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void initInteractions() {
ViewData viewData = new ViewData("InGameView");
EventData<ViewData> eventData =
new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
invoke(eventData, getEventManager());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
* */
public class MainMenuView extends ViewElement<StackPane> {

/**
* Game title.
* */
private Text titleText;

/**
* "Play Game" Button.
* */
Expand Down Expand Up @@ -87,8 +92,10 @@ protected void initLayout() {
settingsButton = new Button("Settings");
exitButton = new Button("Exit");
buttonContainer = new VBox();
titleText = new Text("Millions");

buttonContainer.getChildren().addAll(
new Text("This is the main menu!"),
titleText,
toGameButton,
settingsButton,
exitButton
Expand All @@ -103,9 +110,8 @@ protected void initLayout() {
@Override
protected void initStyling() {
getRootPane().getStyleClass().add("main-menu-bg");

titleText.getStyleClass().add("title-text");
buttonContainer.getStyleClass().add("button-container");

toGameButton.getStyleClass().add("menu-button");
settingsButton.getStyleClass().add("menu-button");
exitButton.getStyleClass().add("menu-button");
Expand Down
Binary file added src/main/resources/Fonts/Aptos.ttf
Binary file not shown.
9 changes: 8 additions & 1 deletion src/main/resources/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.root {
-fx-font-family: "Aptos";
}

/* Container for the buttons */
.button-container {
-fx-spacing: 20;
Expand All @@ -10,7 +14,6 @@
-fx-background-radius: 15;
-fx-min-width: 350;
-fx-min-height: 60;
-fx-font-family: "System";
-fx-font-weight: bold;
-fx-font-style: italic;
-fx-font-size: 24px;
Expand All @@ -30,4 +33,8 @@
-fx-background-image: url("millionsbackground.png"); /* Replace with your image path */
-fx-background-size: cover;
-fx-background-position: center;
}

.title-text {
-fx-font-size: 100px;
}
Loading

0 comments on commit ee1f818

Please sign in to comment.