-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.coffee
317 lines (263 loc) · 17 KB
/
View.coffee
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
class View
subviews: []
constructor: (@element)->
element = "DIV" if not element?
if typeof element is "string"
@element = document.createElement element
else
@element = element
@element.isView = yes
document.body.appendChild @element
@subviews = []
@render()
update: () ->
@render()
render: () ->
# content = document.createTextNode "Hello"
# @element.appendChild content
addSubview: (aView) ->
if aView in @subviews
return aView
@subviews.push aView
@element.appendChild aView.element
class SliderView extends View
constructor: (func) ->
super('input')
@element.type = 'range'
@element.min = 0.5
@element.max = 5
@element.step = 0.001
@element.onchange = func
@element.value = 1.0
value: (value) ->
@element.value = 1.0 * value
@element.onchange()
class SearchView extends View
# contructor: (func) ->
# super('input')
# @element.onKeyUp = (event) ->
# func(event) if event.keyCode == 13
class SearchView extends View
constructor: (func) ->
super('input')
@element.type = 'search'
@element.onkeyup = (event)->
func event if event.keyCode == 13
label = document.createElement("label")
#@element.parent.appendChild()
class CheckBoxView extends View
constructor: (title, func) ->
super('input')
@element.type = 'checkbox'
@element.checked = no
@element.onchange = func
label = document.createElement("label")
#@element.parent.appendChild()
class PlanetView extends View
planetNames: []
planetCoordinates: []
canvas: null
scale: 1.0
maxDistance: 84.5
dimension: [4000, 4000]
offset: [0, 0]
drawsLines: no
lineColor: 'rgba(0,255,255,0.25)'
planetColor: 'white'
textColor: 'cyan'
constructor:(slider)->
@fetchPlanetNames()
@fetchPlanetCoordinates()
super()
@slider = slider
@element.appendChild (@canvas = document.createElement "canvas")
@canvas.width = document.body.clientWidth
window.addEventListener("resize", (event) =>
@canvas.width = document.body.clientWidth - 100
@update()
)
@canvas.height = 800
@canvas.style.cursor = "move"
mouseIsDown = no
lastEvent = null
@canvas.addEventListener "mousedown", (event)=>
mouseIsDown = yes
lastEvent = event
@canvas.addEventListener "mouseup", (event)=>
mouseIsDown = no
lastEvent = null
@canvas.addEventListener "mousewheel", (event)=>
if event.wheelDelta > 0
newValue = @scale + 0.1
else
newValue = @scale - 0.1
@slider.value newValue
@canvas.addEventListener "mousemove", (event)=>
if mouseIsDown
@mouseDragged event, lastEvent
lastEvent = event
@image = new Image();
@image.onload = () =>
@update()
@image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAWgWlDQ1BJQ0MgUHJvZmlsZQAAWIWVmAVUVd236NfeJzlw6O7u7pDukk6ROnRzCBFQAQkJRSkpRQGlFBUlRKQEkRAkFFRUEBVQVAQJCbmHzyvfff/x7njjrTHW3r8xxxxzxZx7zzUXAMzxHmFhQTA1AMEhkURrQx1uRydnbsw0gAEK0AFWQONBiAjTtrQ0A/9rW38BoP33uMS+rf9d7//aaLy8IwgAQJYk9vSKIAST+B6pJxDCiJEAIORJcv5jkWH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LtjwkBAKriiri0Kp1II3j+Dn2jOd1Lwt3w+6svnMW5xOABhDSQqsUgOobH3F49zatzk3ckk8YxTx++IjJHz8AUKg2iGfXAAhAC2miVYZdP+ade3dxt8rkpRoPPnyP9l4L4ZaZ5yb/9v/i268+/R9ECGlqrTJKN+5w62ifVrtGs73LLBYstM37d454+/YhnU6Heq3Kryd/88WXX2OefA+lrc0Cv/4WO60WRWPG3sExN3ZbeH6RRHqcLxJCr867RwcYJZ/D/Rb9ZwO++fwBrgMGWN16+00W16cslilPno/YbtSYzAKGwyuW84CLixE/Xi6whcEn9/ap17f4Laow/v0h0q93SNIlcThFq5yiA54lUUlEsArptHe5f7PCZ60Cbb0kms0QWcLHH9wHwCjWmt0sC8njBNd1iVOF71qskxSVw/HRLbI8o+Q6TOYB/VcTnr54yU5jizi1MQqlnW6cBMgsQRomjm0zGC1YRRFVV3Jy+gzXNng+uKDTrFNyCnz351OavsNuxUaicrRWRGFAEKzI0oSKLakWJHG4ZHx5gY4DomBMr9dj8mrAXhaQBHPGowkmKsO0HBJA5QnBYo6V5Diug6UUVb/A8PKCdZSSRiGDs5QoDDGTfYYvzzGEcLqW6xGvxkgM8jSiVquglCLLcrJccXb2D6aZ4dmScLUiDKYUnCq9n37GyJJl1/UbZKZArUOkMIjXEUIAOqdgGlTKDtFqxWw6JZiNqG23yFPJef8XTNCEs2v8rQbzLEYvAwq2jwRGwyvcoo/WCtM0idcRWR6jlM/pk95rF4TUaIVbauKUKyzCMfl8hFAby2zLQmmF1jl2sUFt+4DlfMriuo+Q5ubKCAlaUXAbeNU6uVDESYRK1+hcYVoenlfGMj2C6RXhYoAQJlpnrwEAQoDeRMdvYrseCAMhBFII0jhmOX2x6RMGWucA/AcZBXDXL4MCggAAAABJRU5ErkJggg=="
mouseDragged: (event, lastEvent) ->
x = (event.clientX - lastEvent.clientX) * (1 / @scale)
y = (event.clientY - lastEvent.clientY) * (1 / @scale)
@offset = [@offset[0] + x, @offset[1] + y]
@constrainOffset()
@update()
constrainOffset: () ->
@offset[0] = -2000 if @offset[0] < -2000
@offset[1] = -2000 if @offset[1] < -2000
@offset[0] = 2000 if @offset[0] > 2000
@offset[1] = 2000 if @offset[1] > 2000
@offset[1] = 0 if window.isNaN @offset[1]
@offset[0] = 0 if window.isNaN @offset[0]
center: (coordinate) ->
@offset[0] = (2000 - coordinate[0] )
@offset[1] = (coordinate[1] - 2000 )
@constrainOffset()
@update()
searchPlanet: (planetName) ->
return if not planetName?
coord
for index in [0..@planetNames.length-1]
if @planetNames[index].toLowerCase().indexOf(planetName.toLowerCase()) isnt -1
coord = @planetCoordinates[index]
break
@center coord if coord?
setScale: (newScale) ->
@scale = parseFloat newScale
@update()
appendText: (text)->
element = document.createElement "div"
@element.appendChild element
text = document.createTextNode text
element.appendChild text
render: () ->
return no if @planetNames.length isnt 500
return no if @planetCoordinates.length isnt 500
context = @canvas.getContext '2d'
context.fillStyle = 'rgba(0,0,0,1)'
context.fillRect 0, 0, @canvas.width, @canvas.height
@drawLines(context) if @drawsLines
@drawPlanets(context)
drawPlanets: (context) ->
for i in [0..@planetNames.length-1]
@drawPlanet context, i
generateLines: () ->
distance = (coord1, coord2) ->
square = (x) -> x * x
d = Math.sqrt( square(coord2[0] - coord1[0]) + square(coord2[1] - coord1[1]) )
return d
lines = []
upperBound = @planetCoordinates.length-1
for index1 in [0..upperBound]
for index2 in [index1..upperBound]
coord1 = @planetCoordinates[index1]
coord2 = @planetCoordinates[index2]
lines.push [coord1, coord2] if distance(coord1, coord2) < @maxDistance
@lines = lines
@update()
drawLines: (context) ->
if not @lines?
@generateLines()
for line in @lines
@drawLine(context, line)
scaleCoordinates: (coord) ->
x = 4000 - Math.round((coord[0] - @offset[0]) * @scale) - @canvas.width / 2
y = 4000 - Math.round((coord[1] - @offset[1]) * @scale) - @canvas.height / 2
x = (coord[0] - (@dimension[0] / 2) + @offset[0]) * @scale + @canvas.width / 2
y = (4000 - coord[1] - (@dimension[1] / 2) + @offset[1]) * @scale + @canvas.height / 2
return [x, y]
drawPlanet: (context, nr ) ->
coord = @planetCoordinates[nr]
name = @planetNames[nr]
scaledCoord = @scaleCoordinates(coord)
x = scaledCoord[0]
y = scaledCoord[1]
return if not (@canvas.width > x > 0 )
return if not (@canvas.height > y > 0)
if @scale < 1.5
context.fillStyle = @planetColor
@drawCircle context, x, y
else
@drawPlanetImage context, x, y
context.fillStyle = @textColor
nameAdjustment = context.measureText(name).width/2
context.fillText(name, x - nameAdjustment, y+12 + (1*@scale))
drawPlanetImage: (context, x, y) ->
@image
width = 1 + 4 * @scale
height = 1 + 4 * @scale
context.drawImage @image, x - (width/2), y - (height/2), width, height
drawLine: (context, line) ->
coord1 = @scaleCoordinates(line[0])
coord2 = @scaleCoordinates(line[1])
return unless (@canvas.width + @maxDistance > coord1[0] > (0 - @maxDistance)) or
(@canvas.width + @maxDistance > coord2[0] > (0 - @maxDistance)) or
(@canvas.height + @maxDistance > coord1[1] > (0 - @maxDistance)) or
(@canvas.height + @maxDistance > coord2[1] > (0 - @maxDistance))
context.lineWidth = 1 # if @scale < 1 then 1 else 1 * @scale
context.strokeStyle = @lineColor
context.beginPath()
context.moveTo coord1[0], coord1[1]
context.lineTo coord2[0], coord2[1]
context.stroke()
drawCircle: (context, left, right) ->
circleSize = 2
context.beginPath()
context.arc(left-(circleSize/2), right-(circleSize/2), circleSize, 0, Math.PI * 2, true)
context.closePath()
context.fill()
fetchPlanetNames: ()->
self = this
xhr = new XMLHttpRequest()
xhr.open 'GET', 'PLANET.NM', true
xhr.overrideMimeType = 'text/plain; charset=x-user-defined'
xhr.responseType = 'text'
xhr.onload = (event) ->
self.didFetchPlanetNames(planetName.trimRight() for planetName in @response.match(/.{1,20}/g))
xhr.onerror = (event) ->
console.log event
xhr.send()
didFetchPlanetNames: (names)->
@planetNames = names
@update()
fetchPlanetCoordinates: () ->
self = this
xhr = new XMLHttpRequest()
xhr.open 'GET', 'XYPLAN.DAT', true
xhr.overrideMimeType = 'text/plain; charset=x-user-defined'
xhr.responseType = 'arraybuffer'
xhr.onload = (event) ->
self.didFetchPlanetCoordinates(new Uint8Array(@response))
xhr.onerror = (event) ->
console.log event
xhr.send()
didFetchPlanetCoordinates: (coordinates) ->
@planetCoordinates = []
i = 0
for n in [1..500]
x1 = coordinates[i]
x2 = coordinates[i+1]*256
x = x1+x2
y1 = coordinates[i+2]
y2 = coordinates[i+3]*256
y = y1+y2
@planetCoordinates.push([x,y])
i = i + 6
@update()
shouldDrawLines: (shouldDraw) ->
@drawsLines = shouldDraw
@update()
window.ready = () ->
planetView = null
slider = new SliderView(()->
planetView.setScale slider.element.value
)
checker = new CheckBoxView('show warp-routes', ()->
planetView.shouldDrawLines (checker.element.checked)
)
searcher = new SearchView((even) -> planetView.searchPlanet(searcher.element.value))
planetView = new PlanetView(slider)