diff options
author | Nao Pross <naopross@thearcway.org> | 2018-11-19 12:05:03 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-11-19 12:05:03 +0100 |
commit | e6fb6cc1ba478d7f89df9f968306e639df2f8df1 (patch) | |
tree | 9f43592664adb3cc123180f779c302de83f87952 /src/GameWindow.java | |
parent | Update GameWindow and remove parent frame dependency on Scene (diff) | |
download | Subconscious-java-e6fb6cc1ba478d7f89df9f968306e639df2f8df1.tar.gz Subconscious-java-e6fb6cc1ba478d7f89df9f968306e639df2f8df1.zip |
Set up CardLayout for GameWindow and Scene pause/resume
Diffstat (limited to 'src/GameWindow.java')
-rw-r--r-- | src/GameWindow.java | 96 |
1 files changed, 69 insertions, 27 deletions
diff --git a/src/GameWindow.java b/src/GameWindow.java index d1afad4..a908a0a 100644 --- a/src/GameWindow.java +++ b/src/GameWindow.java @@ -2,6 +2,8 @@ import java.awt.Dimension; import java.awt.GridLayout; +import java.awt.CardLayout; +import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; @@ -10,10 +12,17 @@ import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; + public class GameWindow extends JFrame implements ActionListener { public static final Dimension WINDOW_SIZE = new Dimension(600, 400); - private JPanel menu; + public final String MENU_CARD = "menu"; + public final String SCENE_CARD = "scene"; + + private JPanel root; + + private Scene scene = null; + private Thread sceneThread = null; // TODO: remove map editor, start directly on Battle mode public GameWindow() { @@ -24,28 +33,78 @@ public class GameWindow extends JFrame implements ActionListener { this.setPreferredSize(WINDOW_SIZE); this.setLocationRelativeTo(null); + this.initComponents(); + + this.add(this.root, BorderLayout.CENTER); + this.pack(); + this.setVisible(true); + } + + private void initComponents() { + // set up a cardlayout + this.root = new JPanel(new CardLayout()); + + // build Menu card JPanel menu = new JPanel(); menu.setLayout(new GridLayout(3, 1)); + JButton editor = new JButton("Editor"); editor.setActionCommand("btn-editor"); + editor.addActionListener(this); + JButton battle = new JButton("Battle"); battle.setActionCommand("btn-battle"); + battle.addActionListener(this); + JButton exit = new JButton("Exit"); exit.setActionCommand("btn-exit"); - - editor.addActionListener(this); - battle.addActionListener(this); exit.addActionListener(this); menu.add(editor); menu.add(battle); menu.add(exit); - this.menu = menu; + // add cards + this.root.add(menu, MENU_CARD); + } - this.add(this.menu); - this.pack(); - this.setVisible(true); + public void showScene(Scene scene) { + // the current scene is already open + if (scene == this.scene) { + ((CardLayout)this.root.getLayout()).show(this.root, SCENE_CARD); + scene.updateCanvasSize(); + scene.resume(); + + return; + } + + // if there is an old scene + if (this.scene != null) { + // close old scene + this.scene.stop(); + try { + this.sceneThread.join(); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } + + // build new thread + this.scene = scene; + this.sceneThread = new Thread(this.scene); + + // add to layout + this.root.add(scene, SCENE_CARD); + ((CardLayout)this.root.getLayout()).show(this.root, SCENE_CARD); + + this.sceneThread.start(); + scene.updateCanvasSize(); + scene.resume(); + } + + public void showMenu() { + this.scene.pause(); + ((CardLayout)this.root.getLayout()).show(this, MENU_CARD); } @Override @@ -65,26 +124,9 @@ public class GameWindow extends JFrame implements ActionListener { scene = new BattleScene(); } - if (scene == null) { - return; + if (scene != null) { + showScene(scene); } - - this.getContentPane().removeAll(); - this.getContentPane().invalidate(); - this.getContentPane().add(scene); - this.getContentPane().revalidate(); - - scene.updateCanvasSize(); - - Thread sceneThread = new Thread(scene); - sceneThread.start(); } } - - public void backToMenu() { - this.getContentPane().removeAll(); - this.getContentPane().invalidate(); - this.getContentPane().add(this.menu); - this.getContentPane().revalidate(); - } } |