mirror of
https://github.com/rastapasta/mapscii.git
synced 2024-11-21 23:53:08 +01:00
🌐 adding global config file, centralizing options
This commit is contained in:
parent
2d7cff71eb
commit
6a1eb8afe8
@ -13,27 +13,9 @@ LabelBuffer = require './LabelBuffer'
|
|||||||
Styler = require './Styler'
|
Styler = require './Styler'
|
||||||
Tile = require './Tile'
|
Tile = require './Tile'
|
||||||
utils = require './utils'
|
utils = require './utils'
|
||||||
|
config = require './config'
|
||||||
|
|
||||||
module.exports = class Renderer
|
module.exports = class Renderer
|
||||||
config:
|
|
||||||
language: 'en'
|
|
||||||
|
|
||||||
labelMargin: 5
|
|
||||||
|
|
||||||
tileSize: 4096
|
|
||||||
projectSize: 256
|
|
||||||
maxZoom: 14
|
|
||||||
|
|
||||||
layers:
|
|
||||||
housenum_label:
|
|
||||||
margin: 4
|
|
||||||
poi_label:
|
|
||||||
margin: 5
|
|
||||||
cluster: true
|
|
||||||
|
|
||||||
place_label: cluster: true
|
|
||||||
state_label: cluster: true
|
|
||||||
|
|
||||||
terminal:
|
terminal:
|
||||||
CLEAR: "\x1B[2J"
|
CLEAR: "\x1B[2J"
|
||||||
MOVE: "\x1B[?6h"
|
MOVE: "\x1B[?6h"
|
||||||
@ -84,12 +66,12 @@ module.exports = class Renderer
|
|||||||
frame
|
frame
|
||||||
|
|
||||||
_visibleTiles: (center, zoom) ->
|
_visibleTiles: (center, zoom) ->
|
||||||
z = Math.min @config.maxZoom, Math.max 0, Math.floor zoom
|
z = Math.min config.tileRange, Math.max 0, Math.floor zoom
|
||||||
center = utils.ll2tile center.lon, center.lat, z
|
center = utils.ll2tile center.lon, center.lat, z
|
||||||
|
|
||||||
tiles = []
|
tiles = []
|
||||||
scale = @_scaleAtZoom zoom
|
scale = @_scaleAtZoom zoom
|
||||||
tileSize = @config.tileSize / scale
|
tileSize = config.tileSize / scale
|
||||||
|
|
||||||
for y in [Math.floor(center.y)-1..Math.floor(center.y)+1]
|
for y in [Math.floor(center.y)-1..Math.floor(center.y)+1]
|
||||||
for x in [Math.floor(center.x)-1..Math.floor(center.x)+1]
|
for x in [Math.floor(center.x)-1..Math.floor(center.x)+1]
|
||||||
@ -167,8 +149,8 @@ module.exports = class Renderer
|
|||||||
@labelBuffer.featuresAt x, y
|
@labelBuffer.featuresAt x, y
|
||||||
|
|
||||||
_scaleAtZoom: (zoom) ->
|
_scaleAtZoom: (zoom) ->
|
||||||
baseZoom = Math.min @config.maxZoom, Math.floor Math.max 0, zoom
|
baseZoom = Math.min config.tileRange, Math.floor Math.max 0, zoom
|
||||||
@config.tileSize / @config.projectSize / Math.pow(2, zoom-baseZoom)
|
config.tileSize / config.projectSize / Math.pow(2, zoom-baseZoom)
|
||||||
|
|
||||||
_drawFeature: (tile, feature) ->
|
_drawFeature: (tile, feature) ->
|
||||||
if feature.style.minzoom and tile.zoom < feature.style.minzoom
|
if feature.style.minzoom and tile.zoom < feature.style.minzoom
|
||||||
@ -192,7 +174,7 @@ module.exports = class Renderer
|
|||||||
true
|
true
|
||||||
|
|
||||||
when "symbol"
|
when "symbol"
|
||||||
text = feature.properties["name_"+@config.language] or
|
text = feature.properties["name_"+config.language] or
|
||||||
feature.properties["name_en"] or
|
feature.properties["name_en"] or
|
||||||
feature.properties["name"] or
|
feature.properties["name"] or
|
||||||
feature.properties.house_num or
|
feature.properties.house_num or
|
||||||
@ -203,14 +185,14 @@ module.exports = class Renderer
|
|||||||
placed = false
|
placed = false
|
||||||
for point in @_scaleAndReduce tile, feature, feature.points
|
for point in @_scaleAndReduce tile, feature, feature.points
|
||||||
x = point.x - text.length
|
x = point.x - text.length
|
||||||
margin = @config.layers[feature.layer]?.margin or @config.labelMargin
|
margin = config.layers[feature.layer]?.margin or config.labelMargin
|
||||||
|
|
||||||
if @labelBuffer.writeIfPossible text, x, point.y, feature, margin
|
if @labelBuffer.writeIfPossible text, x, point.y, feature, margin
|
||||||
@canvas.text text, x, point.y, feature.color
|
@canvas.text text, x, point.y, feature.color
|
||||||
placed = true
|
placed = true
|
||||||
break
|
break
|
||||||
|
|
||||||
else if @config.layers[feature.layer]?.cluster and
|
else if config.layers[feature.layer]?.cluster and
|
||||||
@labelBuffer.writeIfPossible "◉", point.x, point.y, feature, 3
|
@labelBuffer.writeIfPossible "◉", point.x, point.y, feature, 3
|
||||||
@canvas.text "◉", point.x, point.y, feature.color
|
@canvas.text "◉", point.x, point.y, feature.color
|
||||||
placed = true
|
placed = true
|
||||||
@ -244,7 +226,7 @@ module.exports = class Renderer
|
|||||||
else
|
else
|
||||||
if outside
|
if outside
|
||||||
outside = null
|
outside = null
|
||||||
scaled.push [lastX, lastY]
|
scaled.push x: lastX, y: lastY
|
||||||
|
|
||||||
scaled.push x: x, y: y
|
scaled.push x: x, y: y
|
||||||
|
|
||||||
|
@ -12,24 +12,9 @@ Promise = require 'bluebird'
|
|||||||
Renderer = require './Renderer'
|
Renderer = require './Renderer'
|
||||||
TileSource = require './TileSource'
|
TileSource = require './TileSource'
|
||||||
utils = require './utils'
|
utils = require './utils'
|
||||||
|
config = require './config'
|
||||||
|
|
||||||
module.exports = class Termap
|
module.exports = class Termap
|
||||||
config:
|
|
||||||
input: process.stdin
|
|
||||||
output: process.stdout
|
|
||||||
|
|
||||||
source: "http://nachbar.io/data/osm2vectortiles/"
|
|
||||||
#source: __dirname+"/../mbtiles/regensburg.mbtiles"
|
|
||||||
styleFile: __dirname+"/../styles/dark.json"
|
|
||||||
|
|
||||||
initialZoom: null
|
|
||||||
maxZoom: 18
|
|
||||||
zoomStep: 0.2
|
|
||||||
headless: false
|
|
||||||
|
|
||||||
# size:
|
|
||||||
# width: 40*2
|
|
||||||
# height: 10*4
|
|
||||||
|
|
||||||
width: null
|
width: null
|
||||||
height: null
|
height: null
|
||||||
@ -53,13 +38,13 @@ module.exports = class Termap
|
|||||||
minZoom: null
|
minZoom: null
|
||||||
|
|
||||||
constructor: (options) ->
|
constructor: (options) ->
|
||||||
@config[key] = val for key, val of options
|
config[key] = val for key, val of options
|
||||||
|
|
||||||
init: ->
|
init: ->
|
||||||
Promise
|
Promise
|
||||||
.resolve()
|
.resolve()
|
||||||
.then =>
|
.then =>
|
||||||
unless @config.headless
|
unless config.headless
|
||||||
@_initKeyboard()
|
@_initKeyboard()
|
||||||
@_initMouse()
|
@_initMouse()
|
||||||
|
|
||||||
@ -73,17 +58,17 @@ module.exports = class Termap
|
|||||||
|
|
||||||
_initTileSource: ->
|
_initTileSource: ->
|
||||||
@tileSource = new TileSource()
|
@tileSource = new TileSource()
|
||||||
@tileSource.init @config.source
|
@tileSource.init config.source
|
||||||
|
|
||||||
_initKeyboard: ->
|
_initKeyboard: ->
|
||||||
keypress @config.input
|
keypress config.input
|
||||||
@config.input.setRawMode true
|
config.input.setRawMode true
|
||||||
@config.input.resume()
|
config.input.resume()
|
||||||
|
|
||||||
@config.input.on 'keypress', (ch, key) => @_onKey key
|
config.input.on 'keypress', (ch, key) => @_onKey key
|
||||||
|
|
||||||
_initMouse: ->
|
_initMouse: ->
|
||||||
@mouse = TermMouse input: @config.input, output: @config.output
|
@mouse = TermMouse input: config.input, output: config.output
|
||||||
@mouse.start()
|
@mouse.start()
|
||||||
|
|
||||||
@mouse.on 'click', (event) => @_onClick event
|
@mouse.on 'click', (event) => @_onClick event
|
||||||
@ -91,23 +76,23 @@ module.exports = class Termap
|
|||||||
@mouse.on 'move', (event) => @_onMouseMove event
|
@mouse.on 'move', (event) => @_onMouseMove event
|
||||||
|
|
||||||
_initRenderer: ->
|
_initRenderer: ->
|
||||||
@renderer = new Renderer @config.output, @tileSource
|
@renderer = new Renderer config.output, @tileSource
|
||||||
@renderer.loadStyleFile @config.styleFile
|
@renderer.loadStyleFile config.styleFile
|
||||||
|
|
||||||
@config.output.on 'resize', =>
|
config.output.on 'resize', =>
|
||||||
@_resizeRenderer()
|
@_resizeRenderer()
|
||||||
@_draw()
|
@_draw()
|
||||||
|
|
||||||
@_resizeRenderer()
|
@_resizeRenderer()
|
||||||
@zoom = if @config.initialZoom isnt null then @config.initialZoom else @minZoom
|
@zoom = if config.initialZoom isnt null then config.initialZoom else @minZoom
|
||||||
|
|
||||||
_resizeRenderer: (cb) ->
|
_resizeRenderer: (cb) ->
|
||||||
if @config.size
|
if config.size
|
||||||
@width = @config.size.width
|
@width = config.size.width
|
||||||
@height = @config.size.height
|
@height = config.size.height
|
||||||
else
|
else
|
||||||
@width = @config.output.columns >> 1 << 2
|
@width = config.output.columns >> 1 << 2
|
||||||
@height = @config.output.rows * 4 - 4
|
@height = config.output.rows * 4 - 4
|
||||||
|
|
||||||
@minZoom = 4-Math.log(4096/@width)/Math.LN2
|
@minZoom = 4-Math.log(4096/@width)/Math.LN2
|
||||||
|
|
||||||
@ -124,12 +109,12 @@ module.exports = class Termap
|
|||||||
|
|
||||||
_onMouseScroll: (event) ->
|
_onMouseScroll: (event) ->
|
||||||
# TODO: handle .x/y for directed zoom
|
# TODO: handle .x/y for directed zoom
|
||||||
@zoomBy @config.zoomStep * if event.button is "up" then -1 else 1
|
@zoomBy config.zoomStep * if event.button is "up" then -1 else 1
|
||||||
@_draw()
|
@_draw()
|
||||||
|
|
||||||
_onMouseMove: (event) ->
|
_onMouseMove: (event) ->
|
||||||
# only continue if x/y are valid
|
# only continue if x/y are valid
|
||||||
return unless event.x <= @config.output.columns and event.y <= @config.output.rows
|
return unless event.x <= config.output.columns and event.y <= config.output.rows
|
||||||
|
|
||||||
# start dragging
|
# start dragging
|
||||||
if event.button is "left"
|
if event.button is "left"
|
||||||
@ -157,8 +142,8 @@ module.exports = class Termap
|
|||||||
when "w" then @zoomy = 1
|
when "w" then @zoomy = 1
|
||||||
when "s" then @zoomy = -1
|
when "s" then @zoomy = -1
|
||||||
|
|
||||||
when "a" then @zoomBy @config.zoomStep
|
when "a" then @zoomBy config.zoomStep
|
||||||
when "z" then @zoomBy -@config.zoomStep
|
when "z" then @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)
|
||||||
@ -184,8 +169,8 @@ module.exports = class Termap
|
|||||||
@notify "renderer is busy"
|
@notify "renderer is busy"
|
||||||
.then =>
|
.then =>
|
||||||
if @zoomy
|
if @zoomy
|
||||||
if (@zoomy > 0 and @zoom < @config.maxZoom) or (@zoomy < 0 and @zoom > @minZoom)
|
if (@zoomy > 0 and @zoom < config.maxZoom) or (@zoomy < 0 and @zoom > @minZoom)
|
||||||
@zoom += @zoomy * @config.zoomStep
|
@zoom += @zoomy * config.zoomStep
|
||||||
else
|
else
|
||||||
@zoomy *= -1
|
@zoomy *= -1
|
||||||
setImmediate => @_draw()
|
setImmediate => @_draw()
|
||||||
@ -196,17 +181,17 @@ module.exports = class Termap
|
|||||||
|
|
||||||
"center: #{utils.digits @center.lat, 3}, #{utils.digits @center.lon, 3} "+
|
"center: #{utils.digits @center.lat, 3}, #{utils.digits @center.lon, 3} "+
|
||||||
"zoom: #{utils.digits @zoom, 2} "+
|
"zoom: #{utils.digits @zoom, 2} "+
|
||||||
"mouse: #{@mousePosition.x} #{@mousePosition.y} "+
|
"mouse: #{@mousePosition.x*2-@width/2} #{@mousePosition.y} "
|
||||||
|
|
||||||
notify: (text) ->
|
notify: (text) ->
|
||||||
@_write "\r\x1B[K"+text unless @config.headless
|
@_write "\r\x1B[K"+text unless config.headless
|
||||||
|
|
||||||
_write: (output) ->
|
_write: (output) ->
|
||||||
@config.output.write output
|
config.output.write output
|
||||||
|
|
||||||
zoomBy: (step) ->
|
zoomBy: (step) ->
|
||||||
return @zoom = @minZoom if @zoom+step < @minZoom
|
return @zoom = @minZoom if @zoom+step < @minZoom
|
||||||
return @zoom = @config.maxZoom if @zoom+step > @config.maxZoom
|
return @zoom = config.maxZoom if @zoom+step > config.maxZoom
|
||||||
|
|
||||||
@zoom += step
|
@zoom += step
|
||||||
|
|
||||||
|
27
src/config.coffee
Normal file
27
src/config.coffee
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
module.exports =
|
||||||
|
language: "en"
|
||||||
|
|
||||||
|
source: "http://nachbar.io/data/osm2vectortiles/"
|
||||||
|
#source: __dirname+"/../mbtiles/regensburg.mbtiles"
|
||||||
|
styleFile: __dirname+"/../styles/dark.json"
|
||||||
|
|
||||||
|
initialZoom: false
|
||||||
|
maxZoom: 18
|
||||||
|
zoomStep: 0.2
|
||||||
|
|
||||||
|
tileSize: 4096
|
||||||
|
tileRange: 14
|
||||||
|
projectSize: 256
|
||||||
|
|
||||||
|
labelMargin: 5
|
||||||
|
|
||||||
|
layers:
|
||||||
|
housenum_label: margin: 4
|
||||||
|
poi_label: cluster: true, margin: 5
|
||||||
|
place_label: cluster: true
|
||||||
|
state_label: cluster: true
|
||||||
|
|
||||||
|
input: process.stdin
|
||||||
|
output: process.stdout
|
||||||
|
|
||||||
|
headless: false
|
Loading…
Reference in New Issue
Block a user