This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

GraphEditing.scala 7.31 KB
Newer Older
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
package de.bbisping.coupledsim.tool.view

import org.singlespaced.d3js.d3
import scala.scalajs.js
import scala.collection.mutable.HashMap
import org.scalajs.dom
import org.scalajs.dom.raw.EventTarget
import org.singlespaced.d3js.svg.Brush
import org.singlespaced.d3js.Ops.fromFunction1To2
import org.singlespaced.d3js.Ops.fromFunction1To3
import org.singlespaced.d3js.Ops.fromFunction2To3

/**
 * GraphEditing manages the interplay of HTML-Events, node selection, scrolling/zooming
 * and graph editing behaviors.
 */
trait GraphEditing extends ViewComponent {
  self: GraphView =>
    
  import GraphView._
      
  val graphEditor = d3.select("#es-graph-editor")
  
  val svg = d3.select("#es-graph")
  
  // these scales ensure that zoom behavior and box selection behavior by sharing them use the same view port
  val viewportX = d3.scale.linear().domain(js.Array(0, 1000)).range(js.Array(0, 1000))
  val viewportY = d3.scale.linear().domain(js.Array(0, 1000)).range(js.Array(0, 1000))
   
  val brush = d3.svg.brush().asInstanceOf[Brush[EventTarget]]
    .x(viewportX)
    .y(viewportY)
    .on("brushstart", onSelectionBrushStart _)
    .on("brush", onSelectionBrush _)
    .on("brushend", onSelectionBrushEnd _)
  
  val brushRect = svg.append("g")
    .classed("brush", true)
    
  val zoomWindow = d3.behavior.zoom[EventTarget]()
  zoomWindow
    .x(viewportX)
    .y(viewportY)
    .on("zoom", onZoom _)
    
  svg.call(zoomWindow)
    .on("click", onClickBackground _)
  
  val drag = d3.behavior.drag[GraphNode]()
    .origin((d: GraphNode, _: Double) => d.asInstanceOf[js.Any])
    .on("dragstart", onDragStart _)
    .on("drag", onDrag _)
    .on("dragend", onDragEnd _)
    
  val dragLink = d3.behavior.drag[LinkTrait]()
    .origin((d: LinkTrait, _: Double) => d.asInstanceOf[js.Any])
    .on("dragstart", onDragStart _)
    .on("drag", onDrag _)
    .on("dragend", onDragEnd _)
    
  val sceneRoot = svg.append("g")
    
  val behaviors = HashMap[String, GraphEditBehavior]()
  
  var selectionExtensionActive = false
  
  var editingBehavior: GraphEditBehavior = new Object() with GraphEditBehavior
  
  d3.select("body")
    .on("keydown", onKeyDown _)
    .on("keyup", onKeyUp _) 
  
  def registerEditingBehavior(name: String, behavior: GraphEditBehavior) = {
    behaviors.+= (name -> behavior)
  }
  
  def setEditingBehavior(behaviorName: String) = {
    if (editingBehavior != null) {
      editingBehavior.deactivate()
    }
    behaviors.foreach { case (n: String, b: GraphEditBehavior) =>
      d3.select("#"+n).classed("active", behaviorName == n)
    }
    editingBehavior = behaviors(behaviorName)
    editingBehavior.activate()
  }
  
  def selectNode(node: GraphNode) {
    nodes foreach { 
       n: GraphNode =>
        n.selected = 
          if (selectionExtensionActive)
            n.selected != (n == node)
          else
            n == node
    }
    onSelectionChange()
    editingBehavior.onSelectionChange()
  }
  
  def onSelectionChange()
  
  def getSelectedNodes() = {
    nodes.filter(_.selected).toList
  }
  
  def onDragStart(node: GraphNode, id: Double) {
    println("dragstart: "+node)
    if (!node.selected) {
      selectNode(node)
    }
    editingBehavior.onDragStart(node)
    
    d3.event.asInstanceOf[js.Dynamic].sourceEvent.asInstanceOf[org.scalajs.dom.DragEvent].stopPropagation()
  }
  
  def onDragStart(link: LinkTrait, id: Double) {
    editingBehavior.onDragStart(link)
    js.Object.getOwnPropertyDescriptor(d3.event.asInstanceOf[js.Object],"sourceEvent").value.asInstanceOf[org.scalajs.dom.DragEvent].stopPropagation()
  }
  
  def onDrag(node: GraphNode, id: Double) {
    editingBehavior.onDrag(node)
  }
  
  def onDrag(link: LinkTrait, id: Double) {
    editingBehavior.onDrag(link)
  }
  
  def onDragEnd(node: GraphNode, id: Double) {
    editingBehavior.onDragEnd(node)
  }
  
  def onDragEnd(link: LinkTrait, id: Double) {
    editingBehavior.onDragEnd(link)
  }
  
  def onHover(node: GraphNode) {
    editingBehavior.onHover(node)
  }
  
  def onHoverEnd(node: GraphNode) {
    editingBehavior.onHoverEnd(node)
  }
  
  def onHover(link: LinkTrait) {
    editingBehavior.onHover(link)
  }
  
  def onHoverEnd(link: LinkTrait) {
    editingBehavior.onHoverEnd(link)
  }
  
  def onClick(node: GraphNode) {
    editingBehavior.onClick(node)
    d3.event.asInstanceOf[dom.Event].stopPropagation()
  }
  
  def onClickBackground(node: EventTarget) {
    //Note: for reasons I don't understand, node is always undefined...
    editingBehavior.onClick(d3.mouse(sceneRoot.node()))
  }
  
  def onKeyDown(et: EventTarget) {
    if (d3.event.asInstanceOf[org.scalajs.dom.KeyboardEvent].shiftKey && !selectionExtensionActive) {
      setSelectionExtension(true)
    }
  }
  
  def onKeyUp(et: EventTarget) {
    if (selectionExtensionActive && !d3.event.asInstanceOf[org.scalajs.dom.KeyboardEvent].shiftKey) {
      setSelectionExtension(false)
    }
  }
  
  def onSelectionBrushStart(et: Any, id: Double) {
    nodes.foreach { n =>
      n.previouslySelected = n.selected
    }
  }
  
  def onSelectionBrush(et: Any, id: Double) {
    val ext = brush.extent().asInstanceOf[js.Array[js.Array[Double]]]
    nodes.foreach { node: GraphNode =>
      node.selected = ((node.x.get >= ext(0)(0) && node.x.get <= ext(1)(0) &&
                        node.y.get >= ext(0)(1) && node.y.get <= ext(1)(1))
                    ^  (node.previouslySelected && selectionExtensionActive))
      node.selected
    }
    onSelectionChange()
    editingBehavior.onSelectionChange()
  }
  
  def onSelectionBrushEnd(et: EventTarget, id: Double) {
    brush.clear()
    brushRect.call(brush) // something like this is done in http://bl.ocks.org/pkerpedjiev/0389e39fad95e1cf29ce
        // it ensures that the brush stays usable when the shift key is released. but it introduces the bug
        // that in such situations, subsequent zoom-scrolling also activates the selection brush.
  }
  
  def setSelectionExtension(active: Boolean) = {
    selectionExtensionActive = active
    if (selectionExtensionActive) {
      svg.call(zoomWindow)
        .on("mousedown.zoom", null)
        .on("touchstart.zoom", null)                                                                      
        .on("touchmove.zoom", null)                                                                       
        .on("touchend.zoom", null)
      brushRect.select(".background").style("cursor", "crosshair")
      brushRect.call(brush)
    } else {
      brushRect.call(brush)
       .on("mousedown.brush", null)
       .on("touchstart.brush", null)                                                                      
       .on("touchmove.brush", null)                                                                       
       .on("touchend.brush", null)
      brushRect.select(".background").style("cursor", "auto")
      svg.call(zoomWindow)
    }
  }
  
  def deselectAll() {
    nodes.foreach { node: GraphNode =>
      node.selected = false
    }
    onSelectionChange()
    editingBehavior.onSelectionChange()
  }
  
  def onZoom(node: EventTarget, id: Double) {
    
    // deselect all nodes if clicking on background without shift key
    if (!selectionExtensionActive) {
      deselectAll()
    }
    
    val transl = zoomWindow.translate()
    val scale = zoomWindow.scale()
    
    sceneRoot.attr("transform",
      "translate(" + transl._1 + ","
                   + transl._2 + ")"
        + "scale(" + scale + ")")
  }
}