Commit b6d53889 authored by Turner Hall's avatar Turner Hall

CLI Argument parsing and graphics demo mode implemented.

parent 35fb9945
package my.scalafx
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.event.ActionEvent
import scalafx.scene.control._
import scalafx.scene.layout.BorderPane
import scalafx.scene.layout.FlowPane
import scalafx.scene.effect.DropShadow
import scalafx.scene.layout.HBox
import scalafx.scene.paint.Color._
import scalafx.scene.paint._
import scalafx.scene.text.Text
import scalafx.scene.canvas._
import scalafx.animation._
import scalafx.Includes._
import scalafx.stage.FileChooser
import scalafx.stage.FileChooser.ExtensionFilter
import scala.xml._
import my.scalafx.Grid._
import my.scalafx.GridRenderer._
import my.scalafx.EntityType._
import my.scalafx.Face._
import my.scalafx.Simulation._
class Graphics(data: Array[String]) extends JFXApp {
// 40x40 grid with squares of size 22 (20px empty inner space each)
var grid = new Grid(40, 22)
// change this value to modify sim speed
var refreshInterval = 0.5
var playState = true
stage = new JFXApp.PrimaryStage {
title.value = "buddy clean"
scene = new Scene(grid.getCanvasSz, grid.getCanvasSz+30) {
var canvas = new Canvas(grid.getCanvasSz, grid.getCanvasSz)
var gc = canvas.graphicsContext2D
// create the gridRenderer
var gr = new GridRenderer(gc)
var sim = new Simulation(Some(gr),grid)
// setup simulation
// sim.setup() // sim.squareSetup() sim.convexSetup() sim.interiorSetup() sim.classicSetup()
// sim.convexSetup()
sim.spreadSetup
// GUI stuff
val tickLabel = new Label("Tick: 0 ")
val lastMsgLabel = new Label(" Message: None")
root = new BorderPane {
val pane = new FlowPane
center = canvas
val playToggleButton = new Button("Play/Pause")
val spacerLabel = new Label(" ")
val speedLabel = new Label("Speed: "+refreshInterval+" ")
val speedButton = new Button("+Speed")
val slowButton = new Button("-Speed")
val playLabel = new Label("State: Running"+" ")
val saveButton = new Button("Save")
speedButton.onAction = handle => {
refreshInterval-=0.2
speedLabel.text = "Speed: "+refreshInterval+" "
}
playToggleButton.onAction = handle => {
playState = if (playState) false else true
val playStateText:String = if (playState) "Running" else "Paused"
playLabel.text = "State: "+playStateText+" "
}
saveButton.onAction = handle => {
playState = false
playLabel.text = "State: Paused"
XML.save("tick-"+sim.getTick()+".xml",sim.toXML(),"UTF-8")
lastMsgLabel.text = " Message: Saved Successfully "
}
pane.children = List(playToggleButton,speedButton,slowButton,spacerLabel,playLabel,tickLabel,
speedLabel,saveButton,lastMsgLabel)
top = pane
}
/**
Setting up the simulation:
Unless you want to skip to a particular test case, run sim.setup()
Once simulation ends (reaches specified number of ticks) next test
case simulation will run automatically, repeating until all tests
have been run.
**/
// the grid gets updated below
var lastTime = 0L
var refreshdelay = 0.0
val timer = AnimationTimer(t => {
if (lastTime > 0 && playState) { // do stuff
val delta = (t - lastTime) / 1e9
refreshdelay += delta
if (refreshdelay >= refreshInterval) {
sim.update()
tickLabel.text = "Tick: "+sim.getTick()+" "
lastMsgLabel.text = " Message: "+sim.getMsg()+" "
refreshdelay = 0.0
}
}
lastTime = t
})
timer.start
}
}
}
package my.scalafx
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.event.ActionEvent
import scalafx.scene.control._
import scalafx.scene.layout.BorderPane
import scalafx.scene.layout.FlowPane
import scalafx.scene.effect.DropShadow
import scalafx.scene.layout.HBox
import scalafx.scene.paint.Color._
import scalafx.scene.paint._
import scalafx.scene.text.Text
import scalafx.scene.canvas._
import scalafx.animation._
import scalafx.Includes._
import scalafx.stage.FileChooser
import scalafx.stage.FileChooser.ExtensionFilter
import scala.xml._
import my.scalafx.Grid._
import my.scalafx.GridRenderer._
import my.scalafx.EntityType._
import my.scalafx.Face._
import my.scalafx.Simulation._
object Main extends JFXApp {
// 40x40 grid with squares of size 22 (20px empty inner space each)
var grid = new Grid(40, 22)
// change this value to modify sim speed
var refreshInterval = 0.5
var playState = true
stage = new JFXApp.PrimaryStage {
title.value = "buddy clean"
scene = new Scene(grid.getCanvasSz, grid.getCanvasSz+30) {
var canvas = new Canvas(grid.getCanvasSz, grid.getCanvasSz)
var gc = canvas.graphicsContext2D
object Main {
def main(args: Array[String]):Unit = {
if(args.length == 0) printUsage
// string tuples, flag and argument each
val cmds:List[(String,String)] = runOpts(args)
// check graphicsMode and demoMode flags
var graphicsMode = false
var demoMode = false
val (nArgs,yArgs) = cmds.partition(_._2=="")
nArgs.foreach(x => x._1 match {
case "-g" => graphicsMode = true
case "-d" => demoMode = true
case "-dg" => {graphicsMode = true; demoMode = true}
case "-gd" => {graphicsMode = true; demoMode = true}
case _ => printUsage
})
if (demoMode && graphicsMode) new Graphics(args).main(Array())
if (yArgs.map(_._1).contains("load")) {
// load simulation from file
// take the argument of the -f flag
val simfile = yArgs.filter(_._1 == "load").head._2
// TODO: load simulation from file, graphically or headless
???
} else if(yArgs.map(_._1).contains("tests")) {
// load file listing simulation files to load as tests
val testsListingfile = yArgs.filter(_._1 == "tests").head._2
// TODO: setup environment with several simulation files to run headless
???
} else printUsage
}
// create the gridRenderer
var gr = new GridRenderer(gc)
var sim = new Simulation(Some(gr),grid)
// setup simulation
// sim.setup() // sim.squareSetup() sim.convexSetup() sim.interiorSetup() sim.classicSetup()
// sim.convexSetup()
sim.spreadSetup
// GUI stuff
val tickLabel = new Label("Tick: 0 ")
val lastMsgLabel = new Label(" Message: None")
root = new BorderPane {
val pane = new FlowPane
center = canvas
val playToggleButton = new Button("Play/Pause")
val spacerLabel = new Label(" ")
val speedLabel = new Label("Speed: "+refreshInterval+" ")
val speedButton = new Button("+Speed")
val slowButton = new Button("-Speed")
val playLabel = new Label("State: Running"+" ")
val saveButton = new Button("Save")
speedButton.onAction = handle => {
refreshInterval-=0.2
speedLabel.text = "Speed: "+refreshInterval+" "
}
playToggleButton.onAction = handle => {
playState = if (playState) false else true
val playStateText:String = if (playState) "Running" else "Paused"
playLabel.text = "State: "+playStateText+" "
// return a list of actions to take
private def runOpts(args: Array[String]):List[(String,String)] = {
// this works because no flag has multiple arguments
val (flgs, argz) = args.toSeq.partition(a => a.head=='-')
val argbuffer = scala.collection.mutable.ArrayBuffer(argz).map(_.toString)
val ret:List[(String,String)] = flgs.map(flag =>
flag match {
case "-f" => {
val arg = argbuffer.head
argbuffer.remove(0)
("load",arg)
}
saveButton.onAction = handle => {
playState = false
playLabel.text = "State: Paused"
XML.save("tick-"+sim.getTick()+".xml",sim.toXML(),"UTF-8")
lastMsgLabel.text = " Message: Saved Successfully "
case "-t" => {
val arg = argbuffer.head
argbuffer.remove(0)
("tests",arg)
}
pane.children = List(playToggleButton,speedButton,slowButton,spacerLabel,playLabel,tickLabel,
speedLabel,saveButton,lastMsgLabel)
top = pane
}
case x => (x,"")
}).toList
ret
}
/**
Setting up the simulation:
Unless you want to skip to a particular test case, run sim.setup()
Once simulation ends (reaches specified number of ticks) next test
case simulation will run automatically, repeating until all tests
have been run.
**/
// the grid gets updated below
var lastTime = 0L
var refreshdelay = 0.0
val timer = AnimationTimer(t => {
if (lastTime > 0 && playState) { // do stuff
val delta = (t - lastTime) / 1e9
refreshdelay += delta
if (refreshdelay >= refreshInterval) {
sim.update()
tickLabel.text = "Tick: "+sim.getTick()+" "
lastMsgLabel.text = " Message: "+sim.getMsg()+" "
refreshdelay = 0.0
}
}
lastTime = t
})
timer.start
}
private def printUsage():Unit = {
val tabIndent = " "
println("Usage: java -jar buddyclean.jar [OPTION]")
println("Run the buddyclean simulation of the DCC problem. \n")
println("""-f [SIMFILE.xml] """+tabIndent+"loads simulation file")
println("""-t [TESTLIST] """+tabIndent+"loads simulation files specified in test list file")
println("""-g """+tabIndent+"run with GUI")
println("""-d """+tabIndent+"run in graphical demo mode")
println("""-h """+tabIndent+"show this help dialog")
println("""Example Usage: """+tabIndent+"java -jar buddyclean.jar -gd")
System.exit(0)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment