mirror of
				https://github.com/klmp200/sarl-fireworks.git
				synced 2025-10-31 00:53:12 +00:00 
			
		
		
		
	Converting controller from java into sarl
This commit is contained in:
		| @@ -1,176 +0,0 @@ | |||||||
| package io.sarl.demos.fireworks.gui; |  | ||||||
|  |  | ||||||
| import java.util.UUID; |  | ||||||
|  |  | ||||||
| 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 javafx.animation.PauseTransition; |  | ||||||
| import javafx.application.Platform; |  | ||||||
| import javafx.beans.value.ChangeListener; |  | ||||||
| import javafx.beans.value.ObservableValue; |  | ||||||
| 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; |  | ||||||
|  |  | ||||||
| public class FXMLViewerController implements EventListener { |  | ||||||
| 	 |  | ||||||
| 	private OpenEventSpace space; |  | ||||||
| 	private final UUID id = UUID.randomUUID(); |  | ||||||
| 	 |  | ||||||
| 	private boolean launched = false; |  | ||||||
| 	private boolean areaCreated = false; |  | ||||||
| 	 |  | ||||||
| 	@FXML private Canvas draw_zone; |  | ||||||
| 	 |  | ||||||
| 	@FXML private Label gravity_display; |  | ||||||
| 	@FXML private Label rocket_quantity_display; |  | ||||||
| 	@FXML private Label fire_quantity_display; |  | ||||||
| 	 |  | ||||||
| 	@FXML private ScrollBar gravity_input; |  | ||||||
| 	@FXML private ScrollBar rocket_quantity_input; |  | ||||||
| 	@FXML private ScrollBar fire_quantity_input; |  | ||||||
| 	 |  | ||||||
| 	@FXML private Button setup_button; |  | ||||||
| 	@FXML private Button launch_button; |  | ||||||
| 	@FXML private Button stop_button; |  | ||||||
| 	 |  | ||||||
| 	public void cleanExit(){ |  | ||||||
| 		if (this.space != null) |  | ||||||
| 			this.space.emit(new Exit()); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	public void setGUISpace(OpenEventSpace space) { |  | ||||||
| 		this.space= space; |  | ||||||
| 		this.space.register(this); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	public double getGravity() { |  | ||||||
| 		return gravity_input.getValue(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	public int getRocketQuantity() { |  | ||||||
| 		return (int) rocket_quantity_input.getValue(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	public int getFireQuantity() { |  | ||||||
| 		return (int) fire_quantity_input.getValue(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	public void listenAndDraw(Positions grid){ |  | ||||||
| 		GraphicsContext gc = draw_zone.getGraphicsContext2D(); |  | ||||||
| 		PauseTransition wait = 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), 5, 5); |  | ||||||
| 					}); |  | ||||||
| 				}); |  | ||||||
| 	    	} catch (Exception exception) {} |  | ||||||
| 	        wait.playFromStart(); |  | ||||||
| 	    }); |  | ||||||
| 	    wait.play(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	@FXML public void exitApplication(ActionEvent event) { |  | ||||||
| 		space.emit(new Exit()); |  | ||||||
| 		Platform.exit(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	@FXML protected void actionSetup(){ |  | ||||||
| 		SetupSettings event = 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; |  | ||||||
| 		} |  | ||||||
| 		this.space.emit(event); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	@FXML protected void actionLaunch(){ |  | ||||||
| 		launch_button.setDisable(true); |  | ||||||
| 		stop_button.setDisable(false); |  | ||||||
| 		setup_button.setDisable(true); |  | ||||||
| 		if (!areaCreated){ |  | ||||||
| 			this.space.emit(new CreateArea()); |  | ||||||
| 			this.areaCreated = true; |  | ||||||
| 		} else { |  | ||||||
| 			this.space.emit(new Freeze(false)); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	@FXML protected void actionStop(){ |  | ||||||
| 		stop_button.setDisable(true); |  | ||||||
| 		launch_button.setDisable(false); |  | ||||||
| 		setup_button.setDisable(false); |  | ||||||
| 		this.space.emit(new Freeze(true)); |  | ||||||
| 	}  |  | ||||||
| 	@FXML protected void actionGravityDisplay(){ |  | ||||||
| 		gravity_input.valueProperty().addListener(new ChangeListener<Number>() { |  | ||||||
| 			public void changed(ObservableValue<? extends Number> ov,  |  | ||||||
| 				Number old_val, Number new_val){ |  | ||||||
| 					gravity_display.setText(String.format("%.1f", gravity_input.getValue())); |  | ||||||
| 			} |  | ||||||
| 		}); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	@FXML protected void actionRocketQuantityDisplay(){ |  | ||||||
| 		rocket_quantity_input.valueProperty().addListener(new ChangeListener<Number>() { |  | ||||||
| 			public void changed(ObservableValue<? extends Number> ov,  |  | ||||||
| 				Number old_val, Number new_val){ |  | ||||||
| 					rocket_quantity_display.setText(String.format("%.0f", rocket_quantity_input.getValue())); |  | ||||||
| 			} |  | ||||||
| 		}); |  | ||||||
| 		 |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	@FXML protected void actionFireQuantityDisplay(){ |  | ||||||
| 		fire_quantity_input.valueProperty().addListener(new ChangeListener<Number>() { |  | ||||||
| 			public void changed(ObservableValue<? extends Number> ov,  |  | ||||||
| 				Number old_val, Number new_val){ |  | ||||||
| 					fire_quantity_display.setText(String.format("%.0f", fire_quantity_input.getValue())); |  | ||||||
| 			} |  | ||||||
| 		}); |  | ||||||
| 		 |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	@Override |  | ||||||
| 	public UUID getID() { |  | ||||||
| 		// TODO Auto-generated method stub |  | ||||||
| 		 |  | ||||||
| 		return this.id; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	@Override |  | ||||||
| 	public void receiveEvent(Event event) { |  | ||||||
| 		/* |  | ||||||
| 		if (event instanceof TestEvent){ |  | ||||||
| 			System.out.println("Guy recieved an event " + ((TestEvent) event).message); |  | ||||||
| 			this.space.emit(new TestEventHack()); |  | ||||||
| 		} |  | ||||||
| 		*/ |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @@ -0,0 +1,192 @@ | |||||||
|  | package io.sarl.demos.fireworks.gui; | ||||||
|  |  | ||||||
|  | import java.util.UUID; | ||||||
|  |  | ||||||
|  | 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 javafx.animation.PauseTransition; | ||||||
|  | import javafx.application.Platform; | ||||||
|  | import javafx.beans.value.ChangeListener; | ||||||
|  | import javafx.beans.value.ObservableValue; | ||||||
|  | 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 var ^space : OpenEventSpace; | ||||||
|  | 	private val id : UUID = UUID.randomUUID(); | ||||||
|  |  | ||||||
|  | 	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; | ||||||
|  |  | ||||||
|  | 	public def cleanExit() { | ||||||
|  | 		if (this.^space !== null) | ||||||
|  | 			this.^space.emit(new Exit()); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public def setGUISpace(ispace : OpenEventSpace) { | ||||||
|  | 		this.^space = ispace; | ||||||
|  | 		this.^space.register(this); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public def getGravity() : double { | ||||||
|  | 		return gravity_input.getValue(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public def getRocketQuantity() : int { | ||||||
|  | 		return rocket_quantity_input.getValue() as int; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public def getFireQuantity() : int { | ||||||
|  | 		return fire_quantity_input.getValue() as int; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	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), 5, 5); | ||||||
|  | 						}); | ||||||
|  | 					}); | ||||||
|  | 			} catch (exception : Exception) { | ||||||
|  | 			} | ||||||
|  | 			wait.playFromStart(); | ||||||
|  | 		]; | ||||||
|  | 		wait.play(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	public def exitApplication(ievent : ActionEvent) { | ||||||
|  | 		^space.emit(new Exit()); | ||||||
|  | 		Platform.exit(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML protected def actionSetup() { | ||||||
|  | 		var ^event : 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; | ||||||
|  | 		} | ||||||
|  | 		this.^space.emit(^event); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	protected def actionLaunch() { | ||||||
|  | 		launch_button.setDisable(true); | ||||||
|  | 		stop_button.setDisable(false); | ||||||
|  | 		setup_button.setDisable(true); | ||||||
|  | 		if (!areaCreated) { | ||||||
|  | 			this.^space.emit(new CreateArea()); | ||||||
|  | 			this.areaCreated = true; | ||||||
|  | 		} else { | ||||||
|  | 			this.^space.emit(new Freeze(false)); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	protected def actionStop() { | ||||||
|  | 		stop_button.setDisable(true); | ||||||
|  | 		launch_button.setDisable(false); | ||||||
|  | 		setup_button.setDisable(false); | ||||||
|  | 		this.^space.emit(new Freeze(true)); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	protected def actionGravityDisplay() { | ||||||
|  | 		gravity_input.valueProperty().addListener(new ChangeListener<Number>() { | ||||||
|  | 			public def changed(ov : ObservableValue<? extends Number>, old_val : Number, new_val : Number) { | ||||||
|  | 				gravity_display.setText(String.format("%.1f", gravity_input.getValue())); | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	protected def actionRocketQuantityDisplay() { | ||||||
|  | 		rocket_quantity_input.valueProperty().addListener(new ChangeListener<Number>() { | ||||||
|  | 			public def changed(ov : ObservableValue<? extends Number>, old_val : Number, new_val : Number) { | ||||||
|  | 				rocket_quantity_display.setText(String.format("%.0f", rocket_quantity_input.getValue())); | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	protected def actionFireQuantityDisplay() { | ||||||
|  | 		fire_quantity_input.valueProperty().addListener(new ChangeListener<Number>() { | ||||||
|  | 			public def changed(ov : ObservableValue<? extends Number>, old_val : Number, new_val : Number) { | ||||||
|  | 				fire_quantity_display.setText(String.format("%.0f", fire_quantity_input.getValue())); | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public def getID() : UUID { | ||||||
|  | 		// TODO Auto-generated method stub | ||||||
|  | 		return this.id; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public def receiveEvent(^event : Event) { | ||||||
|  | 		/*  | ||||||
|  | 		 * if (event instanceof TestEvent){ | ||||||
|  | 		 * System.out.println("Guy recieved an event " + ((TestEvent) event).message); | ||||||
|  | 		 * this.space.emit(new TestEventHack()); | ||||||
|  | 		 * } | ||||||
|  | 		 */ | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user