diff options
author | Jakob Odersky <jodersky@gmail.com> | 2015-03-27 12:30:52 +0100 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2015-03-27 12:40:41 +0100 |
commit | c2b0a3de4dca16cb9143acffdcc7a9ad1a730b50 (patch) | |
tree | fa89d47f97779c16d3a54b62518b8c83fc907d3c /vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments | |
parent | f7f157cea1afd3386e46938a110c5a2ed72933fb (diff) | |
download | mavigator-c2b0a3de4dca16cb9143acffdcc7a9ad1a730b50.tar.gz mavigator-c2b0a3de4dca16cb9143acffdcc7a9ad1a730b50.tar.bz2 mavigator-c2b0a3de4dca16cb9143acffdcc7a9ad1a730b50.zip |
Clean up internal structure of dashboard ui.
* move instruments to separate files
* instruments are now updated reactively by means of scalarx Vars
* upgrade scalarx version
* remove panels: layout and message routing is now handled centrally
Diffstat (limited to 'vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments')
9 files changed, 243 insertions, 0 deletions
diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Altimeter.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Altimeter.scala new file mode 100644 index 0000000..9ff3bb5 --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Altimeter.scala @@ -0,0 +1,19 @@ +package vfd.dashboard.ui.instruments + +import org.scalajs.dom.html +import vfd.dashboard.Environment + +class Altimeter(implicit env: Environment) extends SvgInstrument[Double] { + import SvgInstrument._ + + val initial = 0.0 + + lazy val element = svgObject("altimeter") + lazy val hand = part("hand") + lazy val moveable = Seq(hand) + + // 36deg === 1m + protected def update(altitude: Double) = { + rotate(hand, (altitude * 36).toInt) + } +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Balance.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Balance.scala new file mode 100644 index 0000000..ef5a3cc --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Balance.scala @@ -0,0 +1,27 @@ +package vfd.dashboard.ui.instruments + +import org.scalajs.dom.html +import vfd.dashboard.Environment + +class Balance(implicit env: Environment) extends SvgInstrument[(Double, Double, Double, Double)] { + import SvgInstrument._ + + val initial = (0.0, 0.0, 0.0, 0.0) + + lazy val element = svgObject("balance") + lazy val position = part("position") + lazy val moveable = Seq(position) + + protected def update(value: (Double, Double, Double, Double)) = { + val m0 = value._1 + val m1 = value._2 + val m2 = value._3 + val m3 = value._4 + val s = m0 + m1 + m2 + m3 + val i = (m0 - m2) / s + val j = (m1 - m3) / s + val x = 0.5 * (i - j) + val y = 0.5 * (-i - j) + translate(position, (x * 50).toInt, (y * 50).toInt) + } +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Bar.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Bar.scala new file mode 100644 index 0000000..7cda301 --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Bar.scala @@ -0,0 +1,19 @@ +package vfd.dashboard.ui.instruments + +import org.scalajs.dom.html +import vfd.dashboard.Environment + +class Bar(implicit env: Environment) extends SvgInstrument[Double] { + import SvgInstrument._ + + val initial = 0.0 + + lazy val element = svgObject("bar") + lazy val level = part("level") + lazy val moveable = Seq(level) + + protected def update(value: Double) = { + translate(level, 0, (97 * (1 - value / 100)).toInt) + } + +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Compass.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Compass.scala new file mode 100644 index 0000000..e2d79e4 --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Compass.scala @@ -0,0 +1,18 @@ +package vfd.dashboard.ui.instruments + +import org.scalajs.dom.html +import vfd.dashboard.Environment + +class Compass(implicit env: Environment) extends SvgInstrument[Double] { + import SvgInstrument._ + + val initial = 0.0 + + lazy val element = svgObject("compass") + lazy val plate = part("heading") + lazy val moveable = Seq(plate) + + protected def update(heading: Double) = { + rotate(plate, heading.toInt) + } +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Generic.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Generic.scala new file mode 100644 index 0000000..c46bdcc --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Generic.scala @@ -0,0 +1,40 @@ +package vfd.dashboard.ui.instruments + +import org.scalajs.dom +import org.scalajs.dom.html +import vfd.dashboard.Environment + +class Generic( + min: Double, + med: Double, + max: Double, + unit: String)(implicit env: Environment) + extends SvgInstrument[Double] { + + import SvgInstrument._ + + val initial = 0.0 + + lazy val element = svgObject("generic") + lazy val handElement = part("hand") + lazy val unitElement = element.contentDocument.getElementById("unit") + lazy val valueElement = element.contentDocument.getElementById("value") + lazy val minElement = element.contentDocument.getElementById("min") + lazy val medElement = element.contentDocument.getElementById("med") + lazy val maxElement = element.contentDocument.getElementById("max") + lazy val moveable = Seq(handElement) + + override protected def load(e: dom.Event) = { + unitElement.textContent = unit + minElement.textContent = min.toString + medElement.textContent = med.toString + maxElement.textContent = max.toString + update(min) + super.load(e) + } + + protected def update(value: Double) = { + rotate(handElement, (value * 270 / (max - min)).toInt) + valueElement.textContent = value.toString + } +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Horizon.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Horizon.scala new file mode 100644 index 0000000..2484561 --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Horizon.scala @@ -0,0 +1,20 @@ +package vfd.dashboard.ui.instruments + +import org.scalajs.dom.html +import vfd.dashboard.Environment + +class Horizon(implicit env: Environment) extends SvgInstrument[(Double, Double)] { + import SvgInstrument._ + + val initial = (0.0, 0.0) + + lazy val element = svgObject("horizon") + lazy val pitch = part("pitch") + lazy val roll = part("roll") + lazy val moveable = Seq(pitch, roll) + + protected def update(pitchRoll: (Double, Double)) = { + translate(pitch, 0, pitchRoll._1.toInt) + rotate(roll, pitchRoll._2.toInt) + } +} diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Instrument.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Instrument.scala new file mode 100644 index 0000000..5b0a13d --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Instrument.scala @@ -0,0 +1,28 @@ +package vfd.dashboard.ui.instruments + +import rx._ +import org.scalajs.dom.html + +/** Common trait to all flight instruments. */ +trait Instrument[A] { + + /** Initial value. */ + val initial: A + + /** Current value that is displayed in the instrument. */ + val value: Var[A] = Var(initial) + + /** HTML element that contains the rendered instrument */ + val element: html.Element + + /** Performs the actual UI update of this instrument. */ + protected def update(newValue: A): Unit + + /** Call when instrument has finished setting up its UI. */ + protected def ready() = { + Obs(value, skipInitial = true) { + update(value()) + } + } + +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Led.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Led.scala new file mode 100644 index 0000000..4173f37 --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/Led.scala @@ -0,0 +1,18 @@ +package vfd.dashboard.ui.instruments + +import scalatags.JsDom.all._ +import vfd.dashboard.Environment + +class Led(implicit env: Environment) extends SvgInstrument[String] { + + val initial = "none" + + lazy val element = `object`(`type` := "image/svg+xml", "data".attr := env.asset("images/leds/led.svg"), width := 100.pct)( + "Error loading led.").render + protected def moveable = Seq() + + protected def update(color: String) = { + part("light").setAttribute("fill", color) + } + +}
\ No newline at end of file diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/SvgInstrument.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/SvgInstrument.scala new file mode 100644 index 0000000..c639cec --- /dev/null +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/ui/instruments/SvgInstrument.scala @@ -0,0 +1,54 @@ +package vfd.dashboard.ui.instruments + + +import org.scalajs.dom +import org.scalajs.dom.html + +import scalatags.JsDom.all._ + +import vfd.dashboard.Environment + +/** An instrument backed by an SVG image. */ +trait SvgInstrument[A] extends Instrument[A] { + + /** SVG object element that contains the rendered instrument */ + val element: html.Object + + /** Retrieves an element of the underlying SVG document by ID. */ + protected def part(id: String) = element.contentDocument.getElementById(id).asInstanceOf[html.Element] + + /** Movable parts of the instrument */ + protected def moveable: Seq[html.Element] + + /** Called when element has been loaded. */ + protected def load(event: dom.Event): Unit = { + for (part <- moveable) { + part.style.transition = "transform 250ms ease-out" + } + ready() + } + + element.addEventListener("load", (e: dom.Event) => load(e)) +} + +/** Contains helpers for SVG instruments. */ +object SvgInstrument { + + /** Retrieves an SVG object element by its instrument's name. */ + def svgObject(name: String)(implicit app: Environment): html.Object = { + val path = app.asset("images/instruments/" + name + ".svg") + `object`(`type` := "image/svg+xml", "data".attr := path, width := 100.pct)( + "Error loading instrument " + name).render + } + + /** Applies translation styling to an element. */ + def translate(elem: html.Element, x: Int, y: Int): Unit = { + elem.style.transform = "translate(" + x + "px, " + y + "px)"; + } + + /** Applies rotation styling to an element. */ + def rotate(elem: html.Element, deg: Int): Unit = { + elem.style.transform = "rotateZ(" + deg + "deg)"; + } + +}
\ No newline at end of file |