Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/main/java/edu/ntnu/idi/idatt/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package edu.ntnu.idi.idatt;

import edu.ntnu.idi.idatt.view.SceneFactory;
import edu.ntnu.idi.idatt.view.SceneManager;
import edu.ntnu.idi.idatt.view.entry.StartController;
import edu.ntnu.idi.idatt.view.entry.StartModel;
import edu.ntnu.idi.idatt.view.entry.StartView;
import javafx.application.Application;
import javafx.stage.Stage;

Expand All @@ -24,14 +22,7 @@ public void start(Stage stage) {
stage.setHeight(700);
stage.setTitle("Stock Game");

StartModel model = new StartModel();
StartView view = new StartView();
StartController controller = new StartController(model);

view.setModel(model);
view.setController(controller);

SceneManager.init(stage, view.getInstance());
SceneManager.init(stage, SceneFactory.createStartView());
stage.show();
}

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/edu/ntnu/idi/idatt/common/Observer.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/view/SceneFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.ntnu.idi.idatt.view;

import edu.ntnu.idi.idatt.view.entry.StartController;
import edu.ntnu.idi.idatt.view.entry.StartModel;
import edu.ntnu.idi.idatt.view.entry.StartView;
import javafx.scene.Parent;

public class SceneFactory {

public static Parent createStartView() {

StartModel model = new StartModel();
StartView view = new StartView();
StartController controller = new StartController(model);

view.setModel(model);
view.setController(controller);

return view.getInstance();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class AbstractViewUI extends AbstractView<StackPane> {
private VBox navigation;
private HBox header;
private HBox toolbar;
private VBox menu;
private SimpleBooleanProperty isMenuVisible = new SimpleBooleanProperty(false);

public AbstractViewUI() {
Expand All @@ -35,8 +36,7 @@ public AbstractViewUI() {

this.setInstance(new StackPane());

Parent menu = createMenu();
StackPane.setAlignment(menu, Pos.CENTER_RIGHT);
menu.getChildren().add(createMenu());
menu.setVisible(false);

Region disableMenu = new Region();
Expand All @@ -59,16 +59,20 @@ public void createUIComponents() {
navigation = new VBox();
navigation.setMaxHeight(Double.MAX_VALUE);
navigation.getStyleClass().add("dark");
navigation.setPrefWidth(150);
navigation.setMaxWidth(150);

header = new HBox();
header.getStyleClass().add("light");
header.setPrefHeight(80);
header.setMaxHeight(80);

toolbar = new HBox();
toolbar.getStyleClass().add("light");
HBox.setHgrow(toolbar, Priority.ALWAYS);
toolbar.setPrefHeight(80);
toolbar.setMaxHeight(80);

menu = new VBox();
StackPane.setAlignment(menu, Pos.CENTER_RIGHT);
menu.getStyleClass().add("dark");
menu.setMaxWidth(300);
}

public abstract Parent createContent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package edu.ntnu.idi.idatt.view.components.ui;

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

import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

public class UIElementCompositor {

private final Pane parent;
private final List<Parent> elements;

public UIElementCompositor(Builder builder) {
this.parent = builder.parent;
this.elements = builder.elements;
}

public Parent makeUI() {
parent.getChildren().addAll(elements);
return parent;
}

public static class Builder {
private Pane parent;
private ArrayList<Parent> elements = new ArrayList<>();

public Builder parent(Pane parent) {
this.parent = parent;
return this;
}

public Builder growWithAlignment(Pos position) {
if (parent instanceof HBox) {
((HBox) parent).setAlignment(position);
parent.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(parent, Priority.ALWAYS);
}
if (parent instanceof VBox) {
((VBox) parent).setAlignment(position);
parent.setMaxHeight(Double.MAX_VALUE);
VBox.setVgrow(parent, Priority.ALWAYS);
}
return this;
}

public Builder setPrefSize(double width, double height) {
parent.setPrefSize(width, height);
return this;
}

public Builder addContent(Parent parent) {
elements.add(parent);
return this;
}

public Builder addAllContent(Parent... parents) {
elements.addAll(List.of(parents));
return this;
}

public Builder filler() {
Region filler = new Region();
if (parent instanceof HBox) {
HBox.setHgrow(filler, Priority.ALWAYS);
}
if (parent instanceof VBox) {
VBox.setVgrow(filler, Priority.ALWAYS);
}
elements.add(filler);
return this;
}

public UIElementCompositor build() {
return new UIElementCompositor(this);
}

}

}
137 changes: 137 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/view/components/ui/UIFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package edu.ntnu.idi.idatt.view.components.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import edu.ntnu.idi.idatt.session.UserSession;
import edu.ntnu.idi.idatt.view.components.elements.IconComponent;
import edu.ntnu.idi.idatt.view.components.elements.SearchBarComponent;
import edu.ntnu.idi.idatt.view.components.primitives.ActionEventHandler;
import edu.ntnu.idi.idatt.view.util.CssUtils;
import edu.ntnu.idi.idatt.view.util.ResourceUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class UIFactory {

public static Parent createHeader(String placeholder, Consumer<String> onSearchQuery) {
SearchBarComponent bar = new SearchBarComponent(placeholder);

HBox.setMargin(bar, new Insets(20));

UIElementCompositor header = new UIElementCompositor.Builder()
.parent(new HBox())
.growWithAlignment(Pos.CENTER)
.filler()
.addContent(bar)
.filler()
.build();

bar.onSearchQuery(() -> onSearchQuery.accept(bar.getQuery())); // Remake?

return header.makeUI();
}

public static Parent createNavigation(String title, List<String> buttonLables, ActionEventHandler... handlers) {
if (buttonLables.size() != handlers.length) {
System.out.println("Failed to build navigation!");
}

Label titleLabel = new Label(title);
CssUtils.apply(titleLabel, CssUtils.BIG_TEXT_32);

ArrayList<Button> buttons = new ArrayList<>();
for (String buttonLabel : buttonLables) {
buttons.add(new Button(buttonLabel));
}
buttons.forEach(b -> {
CssUtils.apply(b, CssUtils.MED_TEXT_16);
b.setOnAction((e) -> handlers[buttons.indexOf(b)].handle());
});

UIElementCompositor.Builder navigationBuilder = new UIElementCompositor.Builder()
.parent(new VBox())
.addContent(titleLabel);

buttons.forEach(b -> navigationBuilder.addContent(b));

UIElementCompositor navigation = navigationBuilder.build();

return navigation.makeUI();

}

public static Parent createMenu(String title, List<String> buttonLables, ActionEventHandler... handlers) {
if (buttonLables.size() != handlers.length) {
System.out.println("Failed to build menu!");
}

IconComponent menuIcon = new IconComponent(ResourceUtils.MENU_ICON, title, 60);
CssUtils.apply(menuIcon, CssUtils.BIG_TEXT_32);

ArrayList<Button> buttons = new ArrayList<>();
for (String buttonLabel : buttonLables) {
buttons.add(new Button(buttonLabel));
}
buttons.forEach(b -> {
CssUtils.apply(b, CssUtils.MED_TEXT_16);
b.setOnAction((e) -> handlers[buttons.indexOf(b)].handle());
});

UIElementCompositor.Builder menuBuilder = new UIElementCompositor.Builder()
.parent(new VBox())
.growWithAlignment(Pos.TOP_CENTER)
.setPrefSize(300, Double.MAX_VALUE)
.addContent(menuIcon);

buttons.forEach(b -> menuBuilder.addContent(b));

UIElementCompositor menu = menuBuilder.build();

return menu.makeUI();

}

public static Parent createToolbar(ActionEventHandler menuHandle, ActionEventHandler quitHandle) {
Label balance = new Label(" Money: " + UserSession.getInstance().getPlayer().getMoney().toString() + " USD");
Label playerStatus = new Label("Status:" + UserSession.getInstance().getPlayer().getStatus());
Label playerNetWorth = new Label(
"Net Worth: " + UserSession.getInstance().getPlayer().getNetWorth().toString() + " USD");

CssUtils.apply(balance, CssUtils.MED_TEXT_16);
CssUtils.apply(playerStatus, CssUtils.MED_TEXT_16);
CssUtils.apply(playerNetWorth, CssUtils.MED_TEXT_16);

VBox infoWrapper = new VBox();
infoWrapper.setAlignment(Pos.CENTER);
infoWrapper.getChildren().addAll(playerStatus, playerNetWorth);

IconComponent userIcon = new IconComponent(ResourceUtils.MENU_ICON, null, 44);
IconComponent quitIcon = new IconComponent(ResourceUtils.QUIT_ICON, null, 44);

HBox iconWrapper = new HBox();
iconWrapper.setAlignment(Pos.CENTER);
iconWrapper.getChildren().addAll(userIcon, quitIcon);

UIElementCompositor toolbar = new UIElementCompositor.Builder()
.parent(new HBox())
.growWithAlignment(Pos.CENTER)
.addContent(balance)
.filler()
.addAllContent(infoWrapper, iconWrapper)
.build();

userIcon.onIconClick(() -> menuHandle.handle());
quitIcon.onIconClick(() -> quitHandle.handle());

return toolbar.makeUI();

}

}
Loading
Loading