220 lines
5.0 KiB
Plaintext
220 lines
5.0 KiB
Plaintext
package io.sarl.demos.fireworks.gui;
|
|
|
|
import io.sarl.demos.fireworks.Firework
|
|
import io.sarl.demos.fireworks.Positions
|
|
import io.sarl.demos.fireworks.events.CreateArea
|
|
import io.sarl.demos.fireworks.events.Exit
|
|
import io.sarl.demos.fireworks.events.Freeze
|
|
import io.sarl.demos.fireworks.events.SetupSettings
|
|
import io.sarl.lang.core.Event
|
|
import io.sarl.lang.core.EventListener
|
|
import io.sarl.util.OpenEventSpace
|
|
import java.util.UUID
|
|
import javafx.animation.PauseTransition
|
|
import javafx.application.Platform
|
|
import javafx.^event.ActionEvent
|
|
import javafx.fxml.FXML
|
|
import javafx.scene.canvas.Canvas
|
|
import javafx.scene.canvas.GraphicsContext
|
|
import javafx.scene.control.Button
|
|
import javafx.scene.control.Label
|
|
import javafx.scene.control.ScrollBar
|
|
import javafx.util.Duration
|
|
|
|
class FXMLViewerController implements EventListener {
|
|
|
|
private val id : UUID = UUID.randomUUID();
|
|
|
|
private var ispace : OpenEventSpace;
|
|
private var launched : boolean = false;
|
|
private var areaCreated : boolean = false;
|
|
|
|
@FXML
|
|
private var draw_zone : Canvas;
|
|
@FXML
|
|
private var gravity_display : Label;
|
|
@FXML
|
|
private var rocket_quantity_display : Label;
|
|
@FXML
|
|
private var fire_quantity_display : Label;
|
|
@FXML
|
|
private var gravity_input : ScrollBar;
|
|
@FXML
|
|
private var rocket_quantity_input : ScrollBar;
|
|
@FXML
|
|
private var fire_quantity_input : ScrollBar;
|
|
@FXML
|
|
private var setup_button : Button;
|
|
@FXML
|
|
private var launch_button : Button;
|
|
@FXML
|
|
private var stop_button : Button;
|
|
|
|
/*
|
|
* Emit a kill signal wen the app is exited
|
|
*/
|
|
public def cleanExit() {
|
|
if (this.ispace !== null)
|
|
this.ispace.emit(new Exit());
|
|
}
|
|
|
|
/*
|
|
* Method invoked by the sarl agent to register the object on a space
|
|
*/
|
|
public def setGUISpace(ispace : OpenEventSpace) {
|
|
this.ispace = ispace;
|
|
this.ispace.register(this);
|
|
}
|
|
|
|
/*
|
|
* Get gravity
|
|
*/
|
|
public def getGravity() : double {
|
|
return gravity_input.getValue();
|
|
}
|
|
|
|
/*
|
|
* Get Rocket Quantity
|
|
*/
|
|
public def getRocketQuantity() : int {
|
|
return rocket_quantity_input.getValue() as int;
|
|
}
|
|
|
|
/*
|
|
* Get Fire Quantity
|
|
*/
|
|
public def getFireQuantity() : int {
|
|
return fire_quantity_input.getValue() as int;
|
|
}
|
|
|
|
/*
|
|
* Draw every positions on main canvas
|
|
*/
|
|
public def listenAndDraw(grid : Positions) {
|
|
var gc : GraphicsContext = draw_zone.getGraphicsContext2D();
|
|
var wait : PauseTransition = new PauseTransition(Duration.seconds(0.03));
|
|
|
|
wait.setOnFinished [ e |
|
|
try {
|
|
gc.clearRect(0, 0, draw_zone.getWidth(), draw_zone.getHeight());
|
|
grid.getRockets().values().stream().filter(rocket|!rocket.getHidden()).forEach(rocket |
|
|
{
|
|
gc.setFill(rocket.getColor());
|
|
gc.fillOval(rocket.getPosition().get(0), rocket.getPosition().get(1), 10, 10);
|
|
});
|
|
|
|
grid.getFire().values().forEach(fire |
|
|
{
|
|
gc.setFill(fire.getColor());
|
|
fire.getPositions().forEach(pos |
|
|
{
|
|
gc.fillOval(pos.get(0), pos.get(1), 3, 3);
|
|
});
|
|
});
|
|
} catch (exception : Exception) {
|
|
}
|
|
wait.playFromStart();
|
|
];
|
|
wait.play();
|
|
}
|
|
|
|
/*
|
|
* Catch exit event
|
|
*/
|
|
@FXML
|
|
public def exitApplication(ievent : ActionEvent) {
|
|
ispace.emit(new Exit());
|
|
Platform.exit();
|
|
}
|
|
|
|
/*
|
|
* Create main sarl agent and do setup
|
|
*/
|
|
@FXML protected def actionSetup() {
|
|
var ievent : SetupSettings = new SetupSettings(this.getRocketQuantity(), this.getFireQuantity(),
|
|
this.getGravity(), this.draw_zone.getWidth());
|
|
if (!launched) {
|
|
launch_button.setDisable(false);
|
|
Firework.main(this);
|
|
launched = true;
|
|
areaCreated = false;
|
|
gravity_input.setDisable(true)
|
|
fire_quantity_input.setDisable(true)
|
|
rocket_quantity_input.setDisable(true)
|
|
setup_button.setDisable(true)
|
|
}
|
|
this.ispace.emit(ievent);
|
|
}
|
|
|
|
/*
|
|
* Launch fireworks
|
|
*/
|
|
@FXML
|
|
protected def actionLaunch() {
|
|
launch_button.setDisable(true);
|
|
stop_button.setDisable(false);
|
|
setup_button.setDisable(true);
|
|
if (!areaCreated) {
|
|
this.ispace.emit(new CreateArea());
|
|
this.areaCreated = true;
|
|
} else {
|
|
this.ispace.emit(new Freeze(false));
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Stop fireworks
|
|
*/
|
|
@FXML
|
|
protected def actionStop() {
|
|
stop_button.setDisable(true);
|
|
launch_button.setDisable(false);
|
|
this.ispace.emit(new Freeze(true));
|
|
}
|
|
|
|
/*
|
|
* Add a listener on gravity
|
|
*/
|
|
@FXML
|
|
protected def actionGravityDisplay() {
|
|
gravity_input.valueProperty().addListener [
|
|
gravity_display.setText(String.format("%.1f", gravity_input.getValue()));
|
|
]
|
|
}
|
|
|
|
/*
|
|
* Add a listener on rocket quantity
|
|
*/
|
|
@FXML
|
|
protected def actionRocketQuantityDisplay() {
|
|
rocket_quantity_input.valueProperty().addListener [
|
|
rocket_quantity_display.setText(String.format("%.0f", rocket_quantity_input.getValue()));
|
|
];
|
|
}
|
|
|
|
/*
|
|
* Add a listener on fire quantity
|
|
*/
|
|
@FXML
|
|
protected def actionFireQuantityDisplay() {
|
|
fire_quantity_input.valueProperty().addListener [
|
|
fire_quantity_display.setText(String.format("%.0f", fire_quantity_input.getValue()));
|
|
]
|
|
}
|
|
|
|
/*
|
|
* Get ID of the object on the space
|
|
*/
|
|
@Override
|
|
public def getID() : UUID {
|
|
return this.id;
|
|
}
|
|
|
|
/*
|
|
* Needed for implementing EventListener
|
|
*/
|
|
@Override
|
|
public def receiveEvent(ievent : Event) {
|
|
}
|
|
}
|