Skip to content

Commit

Permalink
setup tutorial center screen
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 13, 2024
1 parent 9826646 commit 7bdb5ad
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/github/creme332/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public Controller() {
canvas = new Canvas(app.getCanvasModel(), toolbar);
new CanvasController(app, canvas);

frame = new Frame(canvas, menuBar);
TutorialCenter tutorialCenter = new TutorialCenter();
new TutorialController(tutorialCenter);

frame = new Frame(canvas, menuBar, tutorialCenter);
frameController = new FrameController(app, frame);

new SideMenuController(app, frame.getSideMenuPanel());
Expand Down
39 changes: 31 additions & 8 deletions src/main/java/com/github/creme332/controller/FrameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Timer;
import java.util.TimerTask;

import com.github.creme332.model.AppState;
import com.github.creme332.model.Screen;
import com.github.creme332.view.Frame;

public class FrameController {
public class FrameController implements PropertyChangeListener {
private Frame frame;
private AppState app;

public FrameController(AppState app, Frame frame) {
this.frame = frame;
this.app = app;

app.addPropertyChangeListener(this);

frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Expand All @@ -37,23 +42,41 @@ public void componentResized(ComponentEvent e) {

public void playStartAnimation() {
Timer timer = new Timer();
TimerTask showMainScreen;
TimerTask showNextScreen;
final long animationDuration = 800; // ms

frame.setMenuBarVisibility(false);
frame.showSplashScreen();
frame.showScreen(Screen.SPLASH_SCREEN);

// show main screen when timer has elapsed
showMainScreen = new TimerTask() {
// show next screen when timer has elapsed
showNextScreen = new TimerTask() {
@Override
public void run() {
frame.setMenuBarVisibility(true);
frame.showMainScreen();
if (app.getCurrentScreen().equals(Screen.MAIN_SCREEN)) {
frame.showScreen(Screen.MAIN_SCREEN);
}

if (app.getCurrentScreen().equals(Screen.TUTORIAL_SCREEN)) {
frame.showScreen(Screen.TUTORIAL_SCREEN);
}

timer.cancel();
timer.purge();
}
};
timer.schedule(showMainScreen, animationDuration);
timer.schedule(showNextScreen, animationDuration);
}

@Override
public void propertyChange(PropertyChangeEvent e) {
String property = e.getPropertyName();
if ("screen".equals(property)) {
if (Screen.MAIN_SCREEN.equals(e.getNewValue()))
frame.showScreen(Screen.MAIN_SCREEN);

if (Screen.TUTORIAL_SCREEN.equals(e.getNewValue()))
frame.showScreen(Screen.TUTORIAL_SCREEN);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.creme332.controller;

import com.github.creme332.view.TutorialCenter;

public class TutorialController {
TutorialCenter view;

public TutorialController(TutorialCenter view) {
this.view = view;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/github/creme332/model/AppState.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class AppState {

Map<Mode, Integer> modeToMenuMapper;

Screen currentScreen = Screen.MAIN_SCREEN;

public AppState() {
try {
menuModels = createMenuModels();
Expand Down Expand Up @@ -104,6 +106,15 @@ private MenuModel[] createMenuModels() throws InvalidIconSizeException, InvalidP
};
}

public void switchScreen(Screen newScreen) {
support.firePropertyChange("screen", currentScreen, newScreen);
currentScreen = newScreen;
}

public Screen getCurrentScreen() {
return currentScreen;
}

private Map<Mode, Integer> initModeToMenuMapper() {
Map<Mode, Integer> mapper = new EnumMap<>(Mode.class);
for (int i = 0; i < menuModels.length; i++) {
Expand All @@ -129,6 +140,7 @@ public CanvasModel getCanvasModel() {
public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener("sidebarVisibility", listener);
support.addPropertyChangeListener("mode", listener);
support.addPropertyChangeListener("screen", listener);
}

public boolean getSideBarVisibility() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/github/creme332/model/Screen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.creme332.model;

public enum Screen {
SPLASH_SCREEN("SPLASH_SCREEN"),
MAIN_SCREEN("MAIN_SCREEN"),
TUTORIAL_SCREEN("TUTORIAL_SCREEN");

private String screen;

Screen(String str) {
screen = str;
}

@Override
public String toString() {
return screen;
}
}
51 changes: 34 additions & 17 deletions src/main/java/com/github/creme332/view/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import javax.swing.JPanel;

import static com.github.creme332.utils.IconLoader.loadIcon;

import com.github.creme332.model.Screen;
import com.github.creme332.utils.exception.InvalidPathException;

/**
Expand All @@ -22,12 +24,12 @@ public class Frame extends JFrame {
private CardLayout cl = new CardLayout();

/**
* A container for mainScreen and splashScreen
* A container for all screens.
*/
private JPanel screenContainer = new JPanel(cl);

/**
* Screen which is displayed on startup
* Loading screen which is displayed on startup
*/
private SplashScreen splashScreen = new SplashScreen();

Expand All @@ -36,10 +38,17 @@ public class Frame extends JFrame {
*/
private JPanel mainScreen = new JPanel(new BorderLayout());

/**
* A sidemenu for main screen.
*/
SideMenuPanel sideMenu = new SideMenuPanel();

/**
* A menubar for main screen.
*/
MenuBar menubar = null;

public Frame(Canvas canvas, MenuBar menubar) throws InvalidPathException {
public Frame(Canvas canvas, MenuBar menubar, TutorialCenter tutorialCenter) throws InvalidPathException {
// set frame title
this.setTitle("polydraw");

Expand All @@ -65,8 +74,10 @@ public Frame(Canvas canvas, MenuBar menubar) throws InvalidPathException {
setJMenuBar(menubar);

// setup screen container
screenContainer.add(splashScreen, "splashScreen");
screenContainer.add(mainScreen, "mainScreen");
screenContainer.add(splashScreen, Screen.SPLASH_SCREEN.toString());
screenContainer.add(mainScreen, Screen.MAIN_SCREEN.toString());
screenContainer.add(tutorialCenter, Screen.TUTORIAL_SCREEN.toString());

this.add(screenContainer);

// add canvas to mainScreen
Expand All @@ -87,18 +98,24 @@ public void setSideBarVisibility(boolean visible) {
sideMenu.setVisible(visible);
}

/**
* Displays frame which is initially hidden.
*
* Call this function once all components have been added to the frame
* to ensure proper rendering.
*/
public void showMainScreen() {
cl.show(screenContainer, "mainScreen");
}

public void showSplashScreen() {
cl.show(screenContainer, "splashScreen");
public void showScreen(Screen screen) {
switch (screen) {
case SPLASH_SCREEN:
cl.show(screenContainer, Screen.SPLASH_SCREEN.toString());
break;
case MAIN_SCREEN:
menubar.setVisible(true);
cl.show(screenContainer, Screen.MAIN_SCREEN.toString());
break;
case TUTORIAL_SCREEN:
// hide menubar and sidemenu belonging to main screen
menubar.setVisible(false);
sideMenu.setVisible(false);
cl.show(screenContainer, Screen.TUTORIAL_SCREEN.toString());
break;
default:
break;
}
}

public SideMenuPanel getSideMenuPanel() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/creme332/view/TutorialCenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.creme332.view;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TutorialCenter extends JPanel {
JLabel test = new JLabel("welcome to tutorial center");

public TutorialCenter() {
add(test);
}
}

0 comments on commit 7bdb5ad

Please sign in to comment.