🎨 welcome message, callbacks, latlng normalization

This commit is contained in:
Michael Straßburger 2017-05-10 14:26:04 +02:00
parent ca6a4e27ae
commit 3888293f0e

View File

@ -55,6 +55,7 @@ module.exports = class Mapscii
.then => .then =>
@_draw() @_draw()
.then => @notify("Welcome to MapSCII! Use your cursors to navigate, a/z to zoom, q to quit.")
_initTileSource: -> _initTileSource: ->
@tileSource = new TileSource() @tileSource = new TileSource()
@ -62,7 +63,7 @@ module.exports = class Mapscii
_initKeyboard: -> _initKeyboard: ->
keypress config.input keypress config.input
config.input.setRawMode true config.input.setRawMode true if config.input.setRawMode
config.input.resume() config.input.resume()
config.input.on 'keypress', (ch, key) => @_onKey key config.input.on 'keypress', (ch, key) => @_onKey key
@ -108,9 +109,11 @@ module.exports = class Mapscii
z = utils.baseZoom @zoom z = utils.baseZoom @zoom
center = utils.ll2tile @center.lon, @center.lat, z center = utils.ll2tile @center.lon, @center.lat, z
@mousePosition = utils.tile2ll center.x+(dx/size), center.y+(dy/size), z
@mousePosition = utils.normalize utils.tile2ll center.x+(dx/size), center.y+(dy/size), z
_onClick: (event) -> _onClick: (event) ->
return if event.x < 0 or event.x > @width/2 or event.y < 0 or event.y > @height/4
@_updateMousePosition event @_updateMousePosition event
if @mouseDragging and event.button is "left" if @mouseDragging and event.button is "left"
@ -127,6 +130,9 @@ module.exports = class Mapscii
@_draw() @_draw()
_onMouseMove: (event) -> _onMouseMove: (event) ->
return if event.x < 0 or event.x > @width/2 or event.y < 0 or event.y > @height/4
return if config.mouseCallback and not config.mouseCallback event
# start dragging # start dragging
if event.button is "left" if event.button is "left"
if @mouseDragging if @mouseDragging
@ -153,16 +159,20 @@ module.exports = class Mapscii
@notify @_getFooter() @notify @_getFooter()
_onKey: (key) -> _onKey: (key) ->
if config.keyCallback and not config.keyCallback key
return
# check if the pressed key is configured # check if the pressed key is configured
draw = switch key?.name draw = switch key?.name
when "q" when "q"
process.exit 0 if config.quitCallback
config.quitCallback()
when "w" then @zoomy = 1 else
when "s" then @zoomy = -1 process.exit 0
when "a" then @zoomBy config.zoomStep when "a" then @zoomBy config.zoomStep
when "z" then @zoomBy -config.zoomStep when "z", "y"
@zoomBy -config.zoomStep
when "left" then @moveBy 0, -8/Math.pow(2, @zoom) when "left" then @moveBy 0, -8/Math.pow(2, @zoom)
when "right" then @moveBy 0, 8/Math.pow(2, @zoom) when "right" then @moveBy 0, 8/Math.pow(2, @zoom)
@ -174,9 +184,6 @@ module.exports = class Mapscii
if draw isnt null if draw isnt null
@_draw() @_draw()
else
# display debug info for unhandled keys
@notify JSON.stringify key
_draw: -> _draw: ->
@renderer @renderer
@ -186,13 +193,6 @@ module.exports = class Mapscii
@notify @_getFooter() @notify @_getFooter()
.catch => .catch =>
@notify "renderer is busy" @notify "renderer is busy"
.then =>
if @zoomy
if (@zoomy > 0 and @zoom < config.maxZoom) or (@zoomy < 0 and @zoom > @minZoom)
@zoom += @zoomy * config.zoomStep
else
@zoomy *= -1
setImmediate => @_draw()
_getFooter: -> _getFooter: ->
# tile = utils.ll2tile @center.lon, @center.lat, @zoom # tile = utils.ll2tile @center.lon, @center.lat, @zoom
@ -203,6 +203,7 @@ module.exports = class Mapscii
"mouse: #{utils.digits @mousePosition.lat, 3}, #{utils.digits @mousePosition.lon, 3} " "mouse: #{utils.digits @mousePosition.lat, 3}, #{utils.digits @mousePosition.lon, 3} "
notify: (text) -> notify: (text) ->
config.onUpdate() if config.onUpdate
@_write "\r\x1B[K"+text unless config.headless @_write "\r\x1B[K"+text unless config.headless
_write: (output) -> _write: (output) ->
@ -218,11 +219,4 @@ module.exports = class Mapscii
@setCenter @center.lat+lat, @center.lon+lon @setCenter @center.lat+lat, @center.lon+lon
setCenter: (lat, lon) -> setCenter: (lat, lon) ->
lon += 360 if lon < -180 @center = utils.normalize lon: lon, lat: lat
lon -= 360 if lon > 180
lat = 85.0511 if lat > 85.0511
lat = -85.0511 if lat < -85.0511
@center.lat = lat
@center.lon = lon