-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-editor.html
501 lines (472 loc) · 12.6 KB
/
map-editor.html
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'>
<html>
<head>
<script type='text/javascript'>
//only tested with chrome
const QUANTUM_SIZE=4
const FILE=typeof file!='undefined'?file:window.location.search.substring(1)
gCanvas=null
gContext=null
gEntities=[]
gSelected=new Set()
gDragging=false
gDragXi=0
gDragYi=0
gMoveXp=0
gMoveYp=0
window.onload=init
function transceive(message, callback){
gSocket.onmessage=function(event){ callback(JSON.parse(event.data).result) }
gSocket.send(JSON.stringify(message))
}
function e(name){ return document.getElementById(name) }
function v(name){ return e(name).value }
function drawSquare(x, y, color, size){
gContext.fillStyle=color
gContext.fillRect(x-size/2, y-size/2, size, size)
}
function drawText(x, y, text, selected=false){
gContext.fillStyle=selected?'magenta':'black'
var u=gContext.measureText('_').width
var s=gContext.measureText(' ').width
gContext.fillRect(x-s, y-u, gContext.measureText(text).width+s*2, u*2)
gContext.fillStyle='white'
gContext.font='12px sans-serif'
gContext.textBaseline='middle'
gContext.fillText(text, x, y)
}
function drawLine(xi, yi, xf, yf, color){
gContext.strokeStyle=color
gContext.beginPath()
gContext.moveTo(xi, yi)
gContext.lineTo(xf, yf)
gContext.stroke()
}
function Point(x, y, name=v('name')){
this.type='point'
this.x=x
this.y=y
this.name=name
this.draw=function(){
drawSquare(this.x, this.y, 'black', QUANTUM_SIZE*2)
drawSquare(this.x, this.y, gSelected.has(this)?'magenta':'white', QUANTUM_SIZE)
}
this.drawName=function(){
drawText(this.x+QUANTUM_SIZE*2, this.y, this.name, gSelected.has(this))
}
this.getPointNear=function(x, y){
dx=this.x-x
dy=this.y-y
return Math.pow(dx, 2)+Math.pow(dy, 2)<Math.pow(QUANTUM_SIZE*4, 2)?this:null
}
this.getPointsIn=function(xi, yi, xf, yf){
function between(x, a, b){ return a<=x&&x<=b||b<=x&&x<=a }
return between(this.x, xi, xf)&&between(this.y, yi, yf)?[this]:[]
}
this.snap=function(){
this.x=Math.floor(this.x/20.0+0.5)*20
this.y=Math.floor(this.y/20.0+0.5)*20
}
}
function Line(x, y, name=v('name'), color=v('color')){
this.type='line'
this.points=[new Point(x, y, '')]
this.name=name
this.color=color
this.draw=function(){
for(var i=0; i<this.points.length-1; ++i) drawLine(
this.points[i+0].x, this.points[i+0].y,
this.points[i+1].x, this.points[i+1].y,
this.color
)
for(var i=0; i<this.points.length; ++i)
if(gSelected.has(this)||gSelected.has(this.points[i]))
this.points[i].draw()
}
this.drawName=function(){
if(this.name==''&&this.points.length>1) return;
var m=this.points[Math.floor(this.points.length/2)]
drawText(m.x, m.y, this.name, gSelected.has(this))
}
this.add=function(x, y){
this.points.push(new Point(x, y, ''))
}
this.getPointNear=function(x, y){
for(var i=0; i<this.points.length; ++i)
if(this.points[i].getPointNear(x, y)) return this.points[i]
return null
}
this.getPointsIn=function(xi, yi, xf, yf){
var result=[]
for(var i=0; i<this.points.length; ++i)
if(this.points[i].getPointsIn(xi, yi, xf, yf).length)
result.push(this.points[i])
return result
}
this.snap=function(){
this.points.forEach(function(value, key, set){ value.snap() })
}
}
function Plane(x, y, name=v('name'), color=v('color')){
this.type='plane'
this.points=[new Point(x, y, '')]
this.name=name
this.color=color
this.draw=function(){
gContext.fillStyle=this.color
gContext.beginPath()
gContext.moveTo(this.points[0].x, this.points[0].y)
for(var i=1; i<this.points.length; ++i)
gContext.lineTo(this.points[i].x, this.points[i].y)
gContext.fill()
for(var i=0; i<this.points.length; ++i)
if(gSelected.has(this)||gSelected.has(this.points[i]))
this.points[i].draw()
}
this.drawName=function(){
if(this.name==''&&this.points.length>2) return;
var x=0, y=0
for(var i=0; i<this.points.length; ++i){
x+=this.points[i].x
y+=this.points[i].y
}
x/=this.points.length
y/=this.points.length
drawText(x, y, this.name, gSelected.has(this))
}
this.add=function(x, y){
this.points.push(new Point(x, y, ''))
}
this.getPointNear=function(x, y){
for(var i=0; i<this.points.length; ++i)
if(this.points[i].getPointNear(x, y)) return this.points[i]
return null
}
this.getPointsIn=function(xi, yi, xf, yf){
var result=[]
for(var i=0; i<this.points.length; ++i)
if(this.points[i].getPointsIn(xi, yi, xf, yf).length)
result.push(this.points[i])
return result
}
this.snap=function(){
this.points.forEach(function(value, key, set){ value.snap() })
}
}
function snap(){
if(gSelected.size) x=gSelected
else x=gEntities
x.forEach(function(value, key, set){ value.snap() })
draw()
}
function deselect(){
gSelected.clear()
draw()
}
function rename(){
gSelected.forEach(function(value, key, set){ value.name=v('name') })
draw()
}
function recolor(){
gSelected.forEach(function(value, key, set){ value.color=v('color') })
draw()
}
function remove(){
gSelected.forEach(function(value, key, set){
var i=gEntities.indexOf(value)
if(i>=0) gEntities.splice(i, 1)
})
draw()
}
function setBackground(color=v('color')){
gCanvas.style.backgroundColor=color
e('color').value=color
}
function resize(width=v('width'), height=v('height')){
gCanvas.width=width
gCanvas.height=height
e('width').value=width
e('height').value=height
draw()
}
function load(){
transceive({'command': 'load', 'path': FILE},
function(result){
if(!result.success) return;
gEntities=[]
var j=JSON.parse(result.contents)
for(var i=0; i<j.entities.length; ++i){
var e=j.entities[i]
switch(e.type){
case 'point':
gEntities.push(new Point(e.x, e.y, e.name))
break
case 'line':
var p=e.points[0]
var a=new Line(p.x, p.y, e.name, e.color)
gEntities.push(a)
for(var k=1; k<e.points.length; ++k)
a.points.push(new Point(e.points[k].x, e.points[k].y, ''))
break
case 'plane':
var p=e.points[0]
var a=new Plane(p.x, p.y, e.name, e.color)
gEntities.push(a)
for(var k=1; k<e.points.length; ++k)
a.points.push(new Point(e.points[k].x, e.points[k].y, ''))
break
}
}
setBackground(j.background)
resize(j.width, j.height)
draw()
}
)
}
function save(){
j=JSON.stringify({
'entities': gEntities,
'background': gCanvas.style.backgroundColor,
'width': gCanvas.width,
'height': gCanvas.height
})
indent=0
in_string=false
in_escape=false
newline=false
k=''
for(var i=0; i<j.length; ++i){
c=j.charAt(i)
if(!in_string){
if(c=='"') in_string=true
else if(c.match(/[{\[\(]/)) ++indent
else if(c.match(/[}\]\)]/)) --indent
else if(c==',') newline=true
}
else if(!in_escape){
if(c=='\\') in_escape=true
else if(c=='"') in_string=false
}
else in_escape=false
k+=c
if(newline){
k+='\n'
newline=false
k+='\t'.repeat(indent)
}
}
transceive(
{
'command': 'save',
'path': FILE,
'contents': k
},
function(result){
if(!result.success) alert(result.error)
}
)
}
function draw(){
gContext.clearRect(0, 0, gCanvas.width, gCanvas.height)
for(var i=0; i<gEntities.length; ++i) gEntities[i].draw()
var size=QUANTUM_SIZE*5
if(e('grid').checked) for(var i=size; i<gCanvas.width; i+=size){
color=i%(size*5)==0?'white':'grey'
drawLine(0, i, gCanvas.width, i, color)
drawLine(i, 0, i, gCanvas.height, color)
}
for(var i=0; i<gEntities.length; ++i) gEntities[i].drawName()
}
function handleMouseDown(event){
//var x=event.x+document.body.scrollLeft-gCanvas.offsetLeft
//var y=event.y+document.body.scrollTop-gCanvas.offsetTop
var x=event.pageX-gCanvas.offsetLeft
var y=event.pageY-gCanvas.offsetTop
console.log('mouse down ('+x+', '+y+')')
gDragging=true
gDragXi=x
gDragYi=y
gMoveXp=x
gMoveYp=y
addToSelection=function(potentials){
if(!potentials.length) return
gSelected.add(potentials[Math.floor(Math.random()*potentials.length)])
}
if(e('select').checked){
var potentials=[]
for(var i=0; i<gEntities.length; ++i){
var p=gEntities[i].getPointNear(x, y)
if(p) potentials.push(gEntities[i])
}
addToSelection(potentials)
}
if(e('select_point').checked){
var potentials=[]
for(var i=0; i<gEntities.length; ++i){
var p=gEntities[i].getPointNear(x, y)
if(p) potentials.push(p)
}
addToSelection(potentials)
}
if(e('point').checked){
var a=new Point(x, y)
gEntities.push(a)
gSelected.clear()
gSelected.add(a)
}
if(e('line').checked){
var selected=null
gSelected.forEach(function(value, key, set){
if(value instanceof Line) selected=value
})
if(selected) selected.add(x, y)
else{
var a=new Line(x, y)
gEntities.push(a)
gSelected.clear()
gSelected.add(a)
}
}
if(e('plane').checked){
var selected=null
gSelected.forEach(function(value, key, set){
if(value instanceof Plane) selected=value
})
if(selected) selected.add(x, y)
else{
var a=new Plane(x, y)
gEntities.push(a)
gSelected.clear()
gSelected.add(a)
}
}
draw()
}
function handleMouseUp(event){
var x=event.x+document.body.scrollLeft-gCanvas.offsetLeft
var y=event.y+document.body.scrollTop-gCanvas.offsetTop
if(gDragging)
for(var i=0; i<gEntities.length; ++i){
p=gEntities[i].getPointsIn(gDragXi, gDragYi, x, y)
if(e('select').checked&&p.length)
gSelected.add(gEntities[i])
if(e('select_point').checked) for(var j=0; j<p.length; ++j)
gSelected.add(p[j])
}
gDragging=false
draw()
}
function handleMouseMove(event){
var x=event.x+document.body.scrollLeft-gCanvas.offsetLeft
var y=event.y+document.body.scrollTop-gCanvas.offsetTop
if(gDragging){
if((e('select').checked||e('select_point').checked)){
draw()
gContext.strokeStyle='magenta'
gContext.strokeRect(gDragXi, gDragYi, x-gDragXi, y-gDragYi)
}
if(e('move').checked){
var a=new Set()
gSelected.forEach(function(value, key, set){
if(value instanceof Line||value instanceof Plane)
for(var i=0; i<value.points.length; ++i)
a.add(value.points[i])
else a.add(value)
})
a.forEach(function(value, key, set){
value.x+=x-gMoveXp
value.y+=y-gMoveYp
})
draw()
}
}
gMoveXp=x
gMoveYp=y
}
function handleKeyDown(event){
function getKeyCode(c){ return c.charCodeAt(0) }
switch(String.fromCharCode(event.keyCode)){
case 'Y': snap(); break
case 'A': e('select' ).checked=true; break
case 'S': e('select_point').checked=true; break
case 'D': e('move' ).checked=true; break
case 'F': e('point' ).checked=true; break
case 'G': e('line' ).checked=true; break
case 'H': e('plane' ).checked=true; break
case 'Z': deselect(); break
case 'X': rename(); break
case 'C': recolor(); break
case 'V': remove(); break
case 'B': setBackground(); break
case 'N': save(); break
case 'M': load(); break
}
}
function init(){
var loaded=false
gSocket=new WebSocket('ws://localhost:9160')
gSocket.onopen=function(){ load(); loaded=true }
gCanvas=e('canvas')
gCanvas.addEventListener('mousedown', handleMouseDown)
gCanvas.addEventListener('mouseup' , handleMouseUp)
gCanvas.addEventListener('mousemove', handleMouseMove)
gCanvas.addEventListener('keydown' , handleKeyDown)
gContext=gCanvas.getContext('2d')
if(!loaded&&gSocket.readyState==1) load()
}
</script>
</head>
<body>
<canvas
id='canvas'
width='500'
height='500'
tabindex='0'
style='border:1px solid'
></canvas>
<br>
name<input type='text' id='name'/>
color<input type='text' value='rgb(0, 0, 64)' id='color'/>
grid<input type='checkbox' onClick='draw()' id='grid'/>
<br>
<input type='button' value='snap' onClick='snap()'>
<br>
<input type='radio' name='tool' id='select' checked>select
<input type='radio' name='tool' id='select_point'>select point
<input type='radio' name='tool' id='move'>move
<input type='radio' name='tool' id='point'>point
<input type='radio' name='tool' id='line'>line
<input type='radio' name='tool' id='plane'>plane
<br>
<input type='button' value='deselect' onClick='deselect()'>
<input type='button' value='rename' onClick='rename()'>
<input type='button' value='recolor' onClick='recolor()'>
<input type='button' value='remove' onClick='remove()'>
<input type='button' value='set background' onClick='setBackground()'>
<input type='button' value='save' onClick='save()'>
<input type='button' value='load' onClick='load()'>
<br>
width<input type='text' value='500' id='width'/>
height<input type='text' value='500' id='height'/>
<input type='button' value='resize' onClick='resize()'>
<br>
<br>
Keyboard shortcuts:<br>
y: snap<br>
a: select<br>
s: select-point<br>
d: move<br>
f: point<br>
g: line<br>
h: plane<br>
z: deselect<br>
x: rename<br>
c: recolor<br>
v: remove<br>
b: set background<br>
n: save<br>
m: load<br>
</body>
</html>