Blame view

src/main/kotlin/map/Marker.kt 4.61 KB
d06a68ec6   lsagona   add Leaflet Kotli...
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
  package fdit.leafletmap
  
  import fdit.gui.graphicalScenarioEditor.GraphicalScenarioEditorContext
  import fdit.gui.utils.tooltip.VesselTooltipUtils.formatVesselSnapshotTooltip
  import fdit.metamodel.vessel.Vessel
  
  /**
   * Creates a marker at the specified geographical position.
   *
   * @author Niklas Kellner
   *
   * @param position marker position
   * @param title marker title shown in tooltip (pass empty string when tooltip not needed)
   * @param zIndexOffset zIndexOffset (higher number means on top)
   *
   */
  class Marker private constructor(private var position: LatLong, private var zIndexOffset: Int) {
      private var marker = "aircraftIcon"
      private var markerSmall = "aircraftSmallIcon"
      private lateinit var map: LeafletMapView
      private var attached = false
      private var clickable = false
      private var name = ""
      private var tooltip = ""
      private var rotation = 0
      private lateinit var aircraft: Vessel
      private lateinit var context: GraphicalScenarioEditorContext
      private var relativeDate: Double = 0.0
  
  
      constructor(position: LatLong, aircraft: Vessel, relativeDate: Double, context: GraphicalScenarioEditorContext, aircraftIcon: String, zIndexOffset: Int) : this(position, zIndexOffset){
          this.aircraft = aircraft
          this.context = context
          this.relativeDate = relativeDate
          this.marker = aircraftIcon
      }
  
      /**
       * Adds the marker to a map, gets called from the mapAddMarker
       *
       * @param nextMarkerName the variable name of the marker
       * @param map the LeafetMapView
       */
      internal fun addToMap(nextMarkerName: String, map: LeafletMapView) {
          this.name = nextMarkerName
          this.map = map
          this.attached = true
          map.execScript("""
              |var currentZoom = myMap.getZoom();
              |var $name;
                  |if (currentZoom < ${map.zoomLimitSmallMarker}) {
                      |$name = L.marker([${position.latitude}, ${position.longitude}], {title: '', icon: $markerSmall, zIndexOffset: $zIndexOffset}).addTo(markersGroup);
                  |} else {
                      |$name = L.marker([${position.latitude}, ${position.longitude}], {title: '', icon: $marker, zIndexOffset: $zIndexOffset}).addTo(markersGroup);
                  |}
          """.trimMargin())
          setTooltip()
          if (clickable) {
              setClickable()
          }
      }
  
      fun setTooltip() {
          this.tooltip = formatVesselSnapshotTooltip(aircraft,
                  context.getGraphicalScenario().getRecording(),
                  relativeDate)
          this.tooltip = tooltip.replace("
  ", "<br>")
          this.tooltip = tooltip.replace("'", "&apos;")
          map.execScript("$name.bindTooltip('<div id=\"html_c92f9552ec164f36978869550cb44ffe\" style=\"width: 100.0%; height: 100.0%;\">${this.tooltip}</div>');")
      }
  
  
      /**
       * Changes the icon of the marker
       *
       * @param newIcon the name of the new icon
       */
      fun changeIcon(newIcon: String) {
          this.marker = newIcon
          if (attached) {
              map.execScript("$name.setIcon($marker);")
          }
      }
  
      /**
       * Changes the icon of the marker
       *
       * @param newIcon the new ColorMarker
       */
      fun changeIcon(newIcon: ColorMarker) {
          this.marker = newIcon.iconName
          if (attached) {
              map.execScript("$name.setIcon(${newIcon.iconName});")
          }
      }
  
      /**
       * Moves the existing marker specified by the variable name to the new geographical position.
       *
       * @param position new marker position
       */
      fun move(position: LatLong) {
          this.position = position
          if (attached) {
              map.execScript("$name.setLatLng([${this.position.latitude}, ${this.position.longitude}]);")
              setTooltip()
          }
      }
  
      fun move(position: LatLong, aircraft: Vessel, relativeDate: Double) {
          this.aircraft = aircraft
          this.relativeDate = relativeDate
          this.position = position
          if (attached) {
              map.execScript("$name.setLatLng([${this.position.latitude}, ${this.position.longitude}]);")
          }
      }
  
      fun setRotation(rotation: Int) {
          if (rotation > 360 || rotation < 0) {
              this.rotation = 0
          } else {
              this.rotation = rotation
          }
          if (attached) {
              map.execScript("$name.setRotationAngle(${this.rotation})")
          }
      }
  
  
      /**
       * Sets the marker clickable
       */
      private fun setClickable() {
          this.clickable = true
          if (attached) {
              map.execScript("$name.on('click', function(e){ document.java.markerClick($name.options.title)})")
          }
      }
  
      internal fun getName(): String = this.name
  }