Skip to content

Commit

Permalink
Engine now just accepts a config object
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown authored and Unknown committed May 18, 2016
1 parent 92f85b9 commit a4f6b53
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/main/java/Engine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scalafx.scene.control.Alert.AlertType
/**
* Created by Chris on 8/4/2015. This class actually handles the trackball emulation
*/
class Engine(pollingRate: Option[Double], startupThreshold: Option[Double], giveupThreshold: Option[Double], drag: Option[Double]) extends Runnable{
class Engine(config: Config) extends Runnable{

//Setting up engine
val robot = new Robot
Expand Down Expand Up @@ -61,15 +61,15 @@ class Engine(pollingRate: Option[Double], startupThreshold: Option[Double], give
//Reset click tracking
clicked = false
//Poll for mouse movement and give time to detect clicks
Thread.sleep(pollingRate.getOrElse(10.00).toInt)
Thread.sleep(config.pollingRate.getOrElse(10.00).toInt)
val startPos = new Point2D(mouseX, mouseY)
Thread.sleep(pollingRate.getOrElse(10.00).toInt)
Thread.sleep(config.pollingRate.getOrElse(10.00).toInt)
val endPos = new Point2D(mouseX, mouseY)

//If this movement was significant
if(!startPos.almostEquals(endPos, giveupThreshold.getOrElse(1)) || clicked){
if(!startPos.almostEquals(endPos, config.giveupThreshold.getOrElse(1)) || clicked){
//If this movement was violent
if (!startPos.almostEquals(endPos, startupThreshold.getOrElse(5))) {
if (!startPos.almostEquals(endPos, config.startupThreshold.getOrElse(5))) {
//Use the last polled movement as the new trackball speed
lastVector = new Vector2D(startPos, endPos)
}else{
Expand All @@ -83,7 +83,7 @@ class Engine(pollingRate: Option[Double], startupThreshold: Option[Double], give
//Move the mouse according to the last trackball speed
robot.mouseMove((endPos.x + lastVector.x).toInt, (endPos.y + lastVector.y).toInt)
//Apply friction to trackball
lastVector = new Vector2D(lastVector.x * drag.getOrElse(.95), lastVector.y * drag.getOrElse(.95))
lastVector = new Vector2D(lastVector.x * config.drag.getOrElse(.95), lastVector.y * config.drag.getOrElse(.95))
//If the trackball is almost stopped, stop it
if(lastVector.almostEquals(noVector, 2)){
lastVector = noVector
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/GUI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Gui extends JFXApp {
onAction = handle {selected.value match{
case true =>
try{
engineThread = new Thread(new Engine(pollingField.value, startupField.value, giveupField.value, dragField.value))
engineThread = new Thread(new Engine(new Config(pollingField.value, startupField.value, giveupField.value, dragField.value)))
engineThread.start()
}catch{
case e: NumberFormatException =>
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.beust.jcommander.{JCommander, Parameter}
/**
* Created by Chris on 8/4/2015. Main execution environment
*/

case class Config(pollingRate: Option[Double], startupThreshold: Option[Double], giveupThreshold: Option[Double], drag: Option[Double])

object Main extends App {
//Strings
final val name = "Virtual Trackball"
Expand Down Expand Up @@ -80,12 +83,13 @@ object Main extends App {
}

//Start engine with provided parameters
val engine = new Engine(pollingRate, startupThreshold, giveupThreshold, drag)

val engine = new Engine(new Config(pollingRate, startupThreshold, giveupThreshold, drag))
engine.run()
}catch{
//If anything goes wrong at all, give up and run with defaults
case e: Exception =>
val engine = new Engine(None, None, None, None)
val engine = new Engine(new Config(None, None, None, None))
engine.run()
}
}
Expand Down

0 comments on commit a4f6b53

Please sign in to comment.