summaryrefslogtreecommitdiffstats
path: root/src/subconscious/graphics/GameWindow.java
blob: 4d2f91c7d2bb0f3901cd31252904d81170f1262a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package subconscious.graphics;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.CardLayout;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
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);

	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() {
		super("Subconscious");

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(WINDOW_SIZE);
		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");
		exit.addActionListener(this);
		
		menu.add(editor);
		menu.add(battle);
		menu.add(exit);

		// add cards
		this.root.add(menu, MENU_CARD);
	}

	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
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().startsWith("btn")) {
			if (e.getActionCommand().equals("btn-exit")) {
				this.setVisible(false);
				this.dispose();
				return;
			}

			Scene scene = null;

			if (e.getActionCommand().equals("btn-editor")) {
				scene = new MapEditorScene();
			} else if (e.getActionCommand().equals("btn-battle")) {
				scene = new BattleScene();
			}

			if (scene != null) {
				showScene(scene);
			}
		}
	}
}