Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FirefighterStarter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COUETOUX Basile
FirefighterStarter
Merge requests
!2
Simple
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Simple
alaboure/firefighter-template:simple
into
main
Overview
1
Commits
17
Pipelines
1
Changes
16
Merged
COUETOUX Basile
requested to merge
alaboure/firefighter-template:simple
into
main
8 months ago
Overview
1
Commits
17
Pipelines
1
Changes
16
0
0
Merge request reports
Viewing commit
8f3a4ef2
Prev
Next
Show latest version
16 files
+
638
−
217
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
16
8f3a4ef2
new template for project
· 8f3a4ef2
LABOUREL Arnaud
authored
1 year ago
src/main/java/controller/Controller.java
0 → 100644
+
126
−
0
View file @ 8f3a4ef2
Edit in single-file editor
Open in Web IDE
package
controller
;
import
javafx.animation.Animation
;
import
javafx.animation.KeyFrame
;
import
javafx.animation.Timeline
;
import
javafx.event.ActionEvent
;
import
javafx.event.EventHandler
;
import
javafx.fxml.FXML
;
import
javafx.scene.control.Button
;
import
javafx.scene.control.ToggleButton
;
import
javafx.scene.control.ToggleGroup
;
import
javafx.util.Duration
;
import
javafx.util.Pair
;
import
model.Board
;
import
model.ModelElement
;
import
model.FirefighterBoard
;
import
model.Position
;
import
view.FirefighterGrid
;
import
view.ViewElement
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
java
.
util
.
Objects
.
requireNonNull
;
public
class
Controller
{
public
static
final
int
PERIOD_IN_MILLISECONDS
=
50
;
@FXML
private
Button
restartButton
;
@FXML
private
ToggleButton
pauseToggleButton
;
@FXML
private
ToggleButton
playToggleButton
;
@FXML
private
FirefighterGrid
grid
;
private
Timeline
timeline
;
private
Board
<
List
<
ModelElement
>>
board
;
@FXML
private
void
initialize
()
{
initializePlayAndPauseToggleButtons
();
initializeTimeline
();
}
private
void
initializePlayAndPauseToggleButtons
()
{
ToggleGroup
toggleGroup
=
new
PersistentToggleGroup
();
toggleGroup
.
getToggles
().
addAll
(
playToggleButton
,
pauseToggleButton
);
pauseToggleButton
.
setSelected
(
true
);
}
private
void
setModel
(
FirefighterBoard
firefighterBoard
)
{
this
.
board
=
requireNonNull
(
firefighterBoard
,
"model is null"
);
}
private
void
updateBoard
(){
List
<
Position
>
updatedPositions
=
board
.
updateToNextGeneration
();
List
<
Pair
<
Position
,
ViewElement
>>
updatedSquares
=
new
ArrayList
<>();
for
(
Position
updatedPosition
:
updatedPositions
){
List
<
ModelElement
>
squareState
=
board
.
getState
(
updatedPosition
);
ViewElement
viewElement
=
getViewElement
(
squareState
);
updatedSquares
.
add
(
new
Pair
<>(
updatedPosition
,
viewElement
));
}
grid
.
repaint
(
updatedSquares
);
}
private
void
repaintBoard
(){
int
columnCount
=
board
.
columnCount
();
int
rowCount
=
board
.
rowCount
();
ViewElement
[][]
viewElements
=
new
ViewElement
[
rowCount
][
columnCount
];
for
(
int
column
=
0
;
column
<
columnCount
;
column
++)
for
(
int
row
=
0
;
row
<
rowCount
;
row
++)
viewElements
[
row
][
column
]
=
getViewElement
(
board
.
getState
(
new
Position
(
row
,
column
)));
grid
.
repaint
(
viewElements
);
}
private
ViewElement
getViewElement
(
List
<
ModelElement
>
squareState
)
{
if
(
squareState
.
contains
(
ModelElement
.
FIREFIGHTER
)){
return
ViewElement
.
FIREFIGHTER
;
}
if
(
squareState
.
contains
(
ModelElement
.
FIRE
)){
return
ViewElement
.
FIRE
;
}
return
ViewElement
.
EMPTY
;
}
private
void
initializeTimeline
()
{
Duration
duration
=
new
Duration
(
Controller
.
PERIOD_IN_MILLISECONDS
);
EventHandler
<
ActionEvent
>
eventHandler
=
event
->
updateBoard
();
KeyFrame
keyFrame
=
new
KeyFrame
(
duration
,
eventHandler
);
timeline
=
new
Timeline
(
keyFrame
);
timeline
.
setCycleCount
(
Animation
.
INDEFINITE
);
}
public
void
play
()
{
timeline
.
play
();
}
public
void
pause
()
{
timeline
.
pause
();
}
public
void
pauseToggleButtonAction
(
ActionEvent
actionEvent
)
{
this
.
pause
();
}
public
void
playToggleButtonAction
(
ActionEvent
actionEvent
)
{
this
.
play
();
}
public
void
restartButtonAction
(
ActionEvent
actionEvent
)
{
this
.
pause
();
board
.
reset
();
pauseToggleButton
.
setSelected
(
true
);
repaintBoard
();
}
public
void
initialize
(
int
squareWidth
,
int
squareHeight
,
int
columnCount
,
int
rowCount
,
int
initialFireCount
,
int
initialFirefighterCount
)
{
grid
.
initialize
(
squareWidth
,
squareHeight
,
columnCount
,
rowCount
);
this
.
setModel
(
new
FirefighterBoard
(
columnCount
,
rowCount
,
initialFireCount
,
initialFirefighterCount
));
repaintBoard
();
}
}
\ No newline at end of file
Loading