aboutsummaryrefslogtreecommitdiff
path: root/vfd-dashboard/src/main/scala/vfd/dashboard/ui/components/SvgInstrument.scala
blob: 8ddba1acadb6ee4d446ec89946c6790273bc236c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package vfd.dashboard.ui.components

import scala.scalajs.js.Any.fromFunction1

import org.scalajs.dom

import scalatags.JsDom.all.ExtendedString
import scalatags.JsDom.all.`object`
import scalatags.JsDom.all.stringAttr
import scalatags.JsDom.all.stringFrag
import scalatags.JsDom.all.stringStyle
import scalatags.JsDom.all.`type`
import scalatags.JsDom.all.width
import vfd.dashboard.Environment

trait SvgInstrument[A] {
  
  /** SVG object element that contains the rendered instrument */
  def element: dom.HTMLObjectElement
  
  /** Actual svg document */
  protected def content: dom.Document = element.contentDocument
  
  /** Moveable parts of the instrument */
  protected def moveable: Seq[dom.HTMLElement]
  
  /** Updates the instrument to show a new value */
  def update(value: A): Unit
  
  protected def load(event: dom.Event): Unit = {
    for (part <- moveable) {
      part.style.transition = "transform 250ms ease-out"
    }
  }
  
  element.addEventListener("load", (e: dom.Event) => load(e))
}

object SvgInstrument {

  def svg(name: String)(implicit app: Environment): dom.HTMLObjectElement = {
    val path = app.asset("images/instruments/" + name + ".svg")
    `object`(`type` := "image/svg+xml", "data".attr := path, width := "100%")(
      "Error loading instrument " + name).render
  }

  def translate(elem: dom.HTMLElement, x: Int, y: Int): Unit = {
    elem.style.transform = "translate(" + x + "px, " + y + "px)";
  }

  def rotate(elem: dom.HTMLElement, deg: Int): Unit = {
    elem.style.transform = "rotateZ(" + deg + "deg)";
  }

}