↔️ adding config for used input/output stream

This commit is contained in:
Michael Straßburger 2016-10-01 21:05:16 +02:00
parent d0ada3f13d
commit afe7a53009
4 changed files with 28 additions and 21 deletions

View File

@ -69,7 +69,7 @@ No web browser around? No worries - discover the planet in your console!
* [ ] calculate tile areas * [ ] calculate tile areas
* [ ] center based on mercator * [ ] center based on mercator
* [x] handle console resize * [x] handle console resize
* [x] handle console resize * [x] handle console resize
* [ ] mouse control * [ ] mouse control
* [x] accurate mouse drag&drop with instant update * [x] accurate mouse drag&drop with instant update
* [x] hover POIs/labels * [x] hover POIs/labels

View File

@ -31,8 +31,8 @@ module.exports = class Canvas
reset: -> reset: ->
@matrix = mat2d.create() @matrix = mat2d.create()
print: -> frame: ->
process.stdout.write @buffer.frame() @buffer.frame()
translate: (x, y) -> translate: (x, y) ->
mat2d.translate @matrix, @matrix, vec2.fromValues(x, y) mat2d.translate @matrix, @matrix, vec2.fromValues(x, y)

View File

@ -23,6 +23,7 @@ module.exports = class Renderer
drawOrder: [ drawOrder: [
"admin" "admin"
"building" "building"
"road" "road"
"water" "water"
"road:structure=bridge" "road:structure=bridge"
@ -65,7 +66,7 @@ module.exports = class Renderer
labelBuffer: null labelBuffer: null
constructor: -> constructor: (@output) ->
@labelBuffer = new LabelBuffer() @labelBuffer = new LabelBuffer()
loadStyleFile: (file) -> loadStyleFile: (file) ->
@ -83,7 +84,6 @@ module.exports = class Renderer
@labelBuffer.clear() @labelBuffer.clear()
# TODO: better way for background color instead of setting filling FG?
if color = @styler.styleById['background']?.paint['background-color'] if color = @styler.styleById['background']?.paint['background-color']
@canvas.setBackground x256 utils.hex2rgb color @canvas.setBackground x256 utils.hex2rgb color
@ -93,15 +93,14 @@ module.exports = class Renderer
@canvas.translate @view[0], @view[1] @canvas.translate @view[0], @view[1]
@_drawLayers() @_drawLayers()
process.stdout.cursorTo 0, 0 @output.write "\x1B[?6h"
@canvas.print() @output.write @canvas.frame()
@isDrawing = false @isDrawing = false
featuresAt: (x, y) -> featuresAt: (x, y) ->
@labelBuffer.featuresAt x, y @labelBuffer.featuresAt x, y
_write: (output) -> _write: (output) ->
process.stdout.write output process.stdout.write output

View File

@ -15,6 +15,9 @@ utils = require './utils'
module.exports = class Termap module.exports = class Termap
config: config:
input: process.stdin
output: process.stdout
styleFile: __dirname+"/../styles/bright.json" styleFile: __dirname+"/../styles/bright.json"
zoomStep: 0.4 zoomStep: 0.4
@ -34,18 +37,23 @@ module.exports = class Termap
zoom: 0 zoom: 0
view: [0, 0] view: [0, 0]
constructor: -> constructor: (options) ->
@_initControls() @config[key] = val for key, val of options
@_initKeyboard()
@_initMouse()
@_initRenderer() @_initRenderer()
_initControls: -> _initKeyboard: ->
keypress process.stdin keypress @config.input
process.stdin.setRawMode true @config.input.setRawMode true
process.stdin.resume() @config.input.resume()
process.stdin.on 'keypress', (ch, key) => @_onKey key @config.input.on 'keypress', (ch, key) => @_onKey key
@mouse = TermMouse() _initMouse: ->
@mouse = TermMouse input: @config.input, output: @config.output
@mouse.start() @mouse.start()
@mouse.on 'click', (event) => @_onClick event @mouse.on 'click', (event) => @_onClick event
@ -53,10 +61,10 @@ module.exports = class Termap
@mouse.on 'move', (event) => @_onMouseMove event @mouse.on 'move', (event) => @_onMouseMove event
_initRenderer: -> _initRenderer: ->
@renderer = new Renderer() @renderer = new Renderer @config.output
@renderer.loadStyleFile @config.styleFile @renderer.loadStyleFile @config.styleFile
process.stdout.on 'resize', => @config.output.on 'resize', =>
@_resizeRenderer() @_resizeRenderer()
@_draw() @_draw()
@ -64,8 +72,8 @@ module.exports = class Termap
@zoom = Math.log(4096/@width)/Math.LN2 @zoom = Math.log(4096/@width)/Math.LN2
_resizeRenderer: (cb) -> _resizeRenderer: (cb) ->
@width = process.stdout.columns >> 1 << 2 @width = @config.output.columns >> 1 << 2
@height = process.stdout.rows * 4 - 4 @height = @config.output.rows * 4 - 4
@renderer.setSize @width, @height @renderer.setSize @width, @height
@ -84,7 +92,7 @@ module.exports = class Termap
_onMouseMove: (event) -> _onMouseMove: (event) ->
# only continue if x/y are valid # only continue if x/y are valid
return unless event.x <= process.stdout.columns and event.y <= process.stdout.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"