|
3 | 3 | import model.Model; |
4 | 4 | import view.View; |
5 | 5 |
|
| 6 | +import javax.swing.*; |
| 7 | +import java.awt.event.ActionEvent; |
| 8 | +import java.awt.event.ActionListener; |
| 9 | + |
6 | 10 | public class Controller { |
7 | 11 |
|
8 | 12 | private final Model model; |
9 | 13 | private final View view; |
| 14 | + private final ActionListener autoIterateFromPlayer = new ActionListener() { |
| 15 | + @Override |
| 16 | + public void actionPerformed(ActionEvent e) { |
| 17 | + model.step(); |
| 18 | + view.updateView(model.updateView()); |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + private final int delayInMilliseconds = 100; |
| 23 | + private final Timer timer = new Timer(delayInMilliseconds, autoIterateFromPlayer); |
10 | 24 |
|
11 | 25 | public Controller(Model model, View view) { |
12 | 26 | this.model = model; |
13 | 27 | this.view = view; |
14 | 28 |
|
15 | | - this.view.getMenuPanel().getGenerateMazeButton().addActionListener(new GenerateAction(this.model, this.view)); |
| 29 | + this.view.getMenuPanel().getGenerateMazeButton().addActionListener(new GenerateAction(this.model, this.view, this)); |
16 | 30 | this.view.getMenuPanel().getMazeGeneratorComboBox().addActionListener(new GenerateSelectAction(this.model, this.view)); |
17 | 31 |
|
18 | | - this.view.getMenuPanel().getSolveMazeButton().addActionListener(new SolveAction(this.model, this.view)); |
| 32 | + this.view.getMenuPanel().getSolveMazeButton().addActionListener(new SolveAction(this.model, this.view, this)); |
19 | 33 | this.view.getMenuPanel().getMazeSolverComboBox().addActionListener(new SolverSelectAction(this.model, this.view)); |
20 | 34 |
|
21 | | - this.view.getMenuPanel().getFinishButton().addActionListener(new FinishAction(this.model, this.view)); |
22 | | - this.view.getMenuPanel().getStepButton().addActionListener(new StepAction(this.model, this.view)); |
| 35 | + this.view.getMenuPanel().getFinishButton().addActionListener(new FinishAction(this.model, this.view, this)); |
| 36 | + this.view.getMenuPanel().getStepButton().addActionListener(new StepAction(this.model, this.view, this)); |
| 37 | + |
| 38 | + this.view.getMenuPanel().getPlayPauseButton().addActionListener(new PlayPauseAction(this.model, this.view, this)); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public Timer getTimer() { |
| 43 | + return timer; |
| 44 | + } |
| 45 | + |
| 46 | + public void playPause() { |
| 47 | + if (this.timer.isRunning()) { |
| 48 | + this.timer.stop(); |
| 49 | + } else { |
| 50 | + this.timer.restart(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void stopPlaying() { |
| 55 | + this.timer.stop(); |
23 | 56 | } |
24 | 57 |
|
25 | 58 | } |
0 commit comments