🎨 refactoring

This commit is contained in:
Michael Straßburger 2016-09-18 17:46:14 +02:00
parent d3ddc23de4
commit e52ae425f6

View File

@ -4,18 +4,10 @@ Protobuf = require 'pbf'
keypress = require 'keypress' keypress = require 'keypress'
fs = require 'fs' fs = require 'fs'
zlib = require 'zlib' zlib = require 'zlib'
mouse = require('term-mouse')() TermMouse = require('term-mouse')
keypress process.stdin class Termap
process.stdin.setRawMode(true) config:
process.stdin.resume()
mouse.start()
width = null
height = null
config =
drawOrder: ["admin", "water", "landuse", "building", "road", "poi_label"] drawOrder: ["admin", "water", "landuse", "building", "road", "poi_label"]
icons: icons:
@ -29,10 +21,10 @@ config =
cafe: "" cafe: ""
laundry: "👚" laundry: "👚"
bus: "🚌" bus: "🚌"
restaurant: "🍕" restaurant: "🍛"
lodging: "🏨" lodging: "🛏"
'fire-station': "🚒" 'fire-station': "🚒"
shop: "🏬" shop: "🛍"
pharmacy: "💊" pharmacy: "💊"
beer: "🍺" beer: "🍺"
cinema: "🎦" cinema: "🎦"
@ -51,158 +43,186 @@ config =
building: building:
color: 8 color: 8
canvas = null mouse: null
width: null
height: null
canvas: null
init = -> isDrawing: false
width = Math.floor((process.stdout.columns-1)/2)*2*2 lastDrawAt: 0
height = Math.ceil(process.stdout.rows/4)*4*4
canvas = new Canvas width, height
init()
mousePosition: [0, 0]
mouseDragging: false
view: [-400, -80]
scale: 4
constructor: ->
@_initControls()
@_initCanvas()
@_onResize =>
@_initCanvas()
@_draw()
_initControls: ->
keypress process.stdin
process.stdin.setRawMode true
process.stdin.resume()
process.stdin.on 'keypress', (ch, key) => @_onKey key
@mouse = TermMouse()
@mouse.start()
@mouse.on 'click', (event) => @_onClick event
@mouse.on 'scroll', (event) => @_onMouseScroll event
@mouse.on 'move', (event) => @_onMouseMove event
_initCanvas: ->
@width = Math.floor((process.stdout.columns-1)/2)*2*2
@height = Math.ceil(process.stdout.rows/4)*4*4
@canvas = new Canvas @width, @height
_onResize: (cb) ->
process.stdout.on 'resize', cb
_onClick: (event) ->
if @mouseDragging and event.button is "left"
@view[0] -= (@mouseDragging.x-@mousePosition.x)*2
@view[1] -= (@mouseDragging.y-@mousePosition.y)*4
@_draw()
@mouseDragging = false
_onMouseScroll: (event) ->
# TODO: handle .x/y for directed zoom
@zoomBy .5 * if event.button is "up" then 1 else -1
@_draw()
_onMouseMove: (event) ->
# only continue if x/y are valid
return unless event.x <= process.stdout.columns and event.y <= process.stdout.rows
# start dragging
if not @mouseDragging and event.button is "left"
@mouseDragging = x: event.x, y: event.y
# update internal mouse tracker
@mousePosition = x: event.x, y: event.y
_onKey: (key) ->
# check if the pressed key is configured
draw = switch key?.name
when "q"
process.exit 0
when "a" then @zoomBy(.5)
when "z" then @zoomBy(-.5)
when "left" then @view[0] += 5
when "right" then @view[0] -= 5
when "up" then @view[1]+= 5
when "down" then @view[1]-= 5
else
false
if draw
@_draw()
else
# display debug info for unhandled keys
@notify JSON.stringify key
_parseTile: (buffer) ->
# extract, decode and parse a given tile buffer
new VectorTile new Protobuf zlib.gunzipSync data
_getFeatures: (tile) ->
features = {} features = {}
data = fs.readFileSync __dirname+"/tiles/regensburg.pbf.gz"
zlib.gunzip data, (err, buffer) ->
throw new Error err if err
tile = new VectorTile new Protobuf buffer
# Load all layers and preparse the included geometries
for name,layer of tile.layers for name,layer of tile.layers
if config.layers[name] continue unless @config.layers[name]
features[name] = []
for i in [0...layer.length] features[name] = for i in [0...layer.length]
feature = layer.feature i feature = layer.feature i
features[name].push
type: [undefined, "point", "line", "polygon"][feature.type] type: [undefined, "point", "line", "polygon"][feature.type]
id: feature.id id: feature.id
properties: feature.properties properties: feature.properties
points: feature.loadGeometry() points: feature.loadGeometry()
draw() features
view = [-400, -80] _draw: ->
scale = 4 return if @isDrawing
@isDrawing = true
@lastDrawAt = Date.now()
flush = -> @canvas.clearRect 0, 0, @width, @height
process.stdout.write canvas._canvas.frame() #@_write @canvas._canvas.frame()
lastDraw = null @canvas.save()
drawing = false
draw = ->
return if drawing
lastDraw = Date.now()
drawing = true
canvas.clearRect(0, 0, width, height)
canvas.save() @canvas.translate @view[0], @view[1]
canvas.translate view[0], view[1] for layer in @config.drawOrder
for layer in config.drawOrder continue unless @features?[layer]
continue unless features[layer]
canvas.strokeStyle = canvas.fillStyle = config.layers[layer].color @canvas.strokeStyle = @canvas.fillStyle = @config.layers[layer].color
for feature in features[layer] for feature in @features[layer]
for points in feature.points for points in feature.points
visible = false visible = false
points = for point in points points = for point in points
p = [point.x/scale, point.y/scale] p = [point.x/@scale, point.y/@scale]
if not visible and p[0]+view[0]>=4 and p[0]+view[0]<width-4 and p[1]+view[1]>=0 and p[1]+view[1]<height if not visible and
p[0]+@view[0]>=4 and
p[0]+@view[0]<@width-4 and
p[1]+@view[1]>=0 and
p[1]+@view[1]<@height
visible = true visible = true
p p
continue unless visible continue unless visible
switch feature.type switch feature.type
when "polygon", "line" when "polygon", "line"
canvas.beginPath() @canvas.beginPath()
canvas.moveTo points.shift()... @canvas.moveTo points.shift()...
canvas.lineTo point... for point in points @canvas.lineTo point... for point in points
canvas.stroke() @canvas.stroke()
when "point" when "point"
canvas.fillText (config.icons[feature.properties.maki] or "X"), point... for point in points @canvas.fillText (@config.icons[feature.properties.maki] or "X"), point... for point in points
canvas.restore() @canvas.restore()
flush() @_write @canvas._canvas.frame()
process.stdout.write getStatus() @_write @_getFooter()
drawing = false @isDrawing = false
getStatus = -> _write: (text) ->
process.stdout.write text
_getFooter: ->
"TerMap up and running!" "TerMap up and running!"
notify = (text) -> notify: ->
return if drawing return if @isDrawing
process.stdout.write "\r\x1B[K#{getStatus()} #{text}" @_write "\r\x1B[K#{@_getFooter()} #{text}"
# moving = null zoomBy: (step) ->
# process.stdin.on 'mousepress', (info) -> return unless @scale+step > 0
# # TODO: file bug @keypress, fails after x>95 / sequence: '\u001b[M#B'
# if info.x > 2048
# info.x = 100
#
# if info.button is "left"
# moving = info
#
# else if moving and info.release
#
# draw()
zoomBy = (step) -> before = @scale
return unless scale+step > 0 @scale += step
before = scale @view[0] = @view[0]*before/@scale + if step > 0 then 8 else -8
scale += step @view[1] = @view[1]*before/@scale + if step > 0 then 8 else -8
view[0] = view[0]*before/scale + if step > 0 then 8 else -8 termap = new Termap()
view[1] = view[1]*before/scale + if step > 0 then 8 else -8
process.stdin.on 'keypress', (ch, key) -> # TODO: abstracing this class, create loader class
result = switch key?.name data = fs.readFileSync __dirname+"/tiles/regensburg.pbf.gz"
when "q" termap.features = termap._getFeatures termap._parseTile data
process.exit 0 termap._draw()
when "a" then zoomBy(.5)
when "z" then zoomBy(-.5)
when "left" then view[0] += 5
when "right" then view[0] -= 5
when "up" then view[1]+= 5
when "down" then view[1]-= 5
else
false
if result
draw()
else
notify JSON.stringify key
process.stdout.on 'resize', ->
init()
draw()
moving = null
mousePosition = null
mouse.on 'click', (event) ->
if moving and event.button is "left"
view[0] -= (moving.x-mousePosition.x)*2
view[1] -= (moving.y-mousePosition.y)*4
draw()
moving = null
mouse.on 'scroll', (event) ->
# TODO: handle .x/y for directed zoom
zoomBy .5 * if event.button is "up" then 1 else -1
draw()
mouse.on 'move', (event) ->
return unless event.x <= process.stdout.columns and event.y <= process.stdout.rows
if not moving and event.button is "left"
moving = x: event.x, y: event.y
mousePosition = x: event.x, y: event.y