mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-05-30 23:06:15 +02:00
🎨 using term-mouse, improving zoom<>view relation
This commit is contained in:
parent
bf5cc5a21d
commit
3b03122807
@ -7,6 +7,10 @@ zlib = require 'zlib'
|
|||||||
mouse = require('term-mouse')()
|
mouse = require('term-mouse')()
|
||||||
|
|
||||||
keypress process.stdin
|
keypress process.stdin
|
||||||
|
process.stdin.setRawMode(true)
|
||||||
|
process.stdin.resume()
|
||||||
|
|
||||||
|
mouse.start()
|
||||||
|
|
||||||
width = null
|
width = null
|
||||||
height = null
|
height = null
|
||||||
@ -41,16 +45,17 @@ zlib.gunzip data, (err, buffer) ->
|
|||||||
|
|
||||||
draw()
|
draw()
|
||||||
|
|
||||||
zoom = 0
|
|
||||||
view = [-400, -80]
|
view = [-400, -80]
|
||||||
size = 4
|
scale = 4
|
||||||
|
|
||||||
flush = ->
|
flush = ->
|
||||||
process.stdout.write canvas._canvas.frame()
|
process.stdout.write canvas._canvas.frame()
|
||||||
|
|
||||||
|
lastDraw = null
|
||||||
drawing = false
|
drawing = false
|
||||||
draw = ->
|
draw = ->
|
||||||
return if drawing
|
return if drawing
|
||||||
|
lastDraw = Date.now()
|
||||||
drawing = true
|
drawing = true
|
||||||
canvas.clearRect(0, 0, width, height)
|
canvas.clearRect(0, 0, width, height)
|
||||||
|
|
||||||
@ -65,7 +70,7 @@ draw = ->
|
|||||||
for line in feature
|
for line in feature
|
||||||
found = false
|
found = false
|
||||||
points = for point in line
|
points = for point in line
|
||||||
p = [point.x/size, point.y/size]
|
p = [point.x/scale, point.y/scale]
|
||||||
if not found and p[0]+view[0]>=0 and p[0]+view[0]<width and p[1]+view[1]>=0 and p[1]+view[1]<height
|
if not found and p[0]+view[0]>=0 and p[0]+view[0]<width and p[1]+view[1]>=0 and p[1]+view[1]<height
|
||||||
found = true
|
found = true
|
||||||
p
|
p
|
||||||
@ -83,41 +88,41 @@ draw = ->
|
|||||||
drawing = false
|
drawing = false
|
||||||
|
|
||||||
getStatus = ->
|
getStatus = ->
|
||||||
"view: [#{view.toString()}] scale: [#{size}] columns: [#{process.stdout.columns}]"
|
"TerMap up and running!"
|
||||||
|
|
||||||
notify = (text) ->
|
notify = (text) ->
|
||||||
return if drawing
|
return if drawing
|
||||||
process.stdout.write "\r\x1B[K#{getStatus()} #{text}"
|
process.stdout.write "\r\x1B[K#{getStatus()} #{text}"
|
||||||
|
|
||||||
moving = null
|
# moving = null
|
||||||
process.stdin.on 'mousepress', (info) ->
|
# process.stdin.on 'mousepress', (info) ->
|
||||||
# TODO: file bug @keypress, fails after x>95 / sequence: '\u001b[M#B'
|
# # TODO: file bug @keypress, fails after x>95 / sequence: '\u001b[M#B'
|
||||||
if info.x > 2048
|
# if info.x > 2048
|
||||||
info.x = 100
|
# info.x = 100
|
||||||
|
#
|
||||||
|
# if info.button is "left"
|
||||||
|
# moving = info
|
||||||
|
#
|
||||||
|
# else if moving and info.release
|
||||||
|
#
|
||||||
|
# draw()
|
||||||
|
|
||||||
switch info.scroll
|
zoomBy = (step) ->
|
||||||
when -1
|
return unless scale+step > 0
|
||||||
size -= .2
|
|
||||||
when 1
|
|
||||||
size += .2
|
|
||||||
|
|
||||||
if info.button is 0
|
before = scale
|
||||||
moving = info
|
scale += step
|
||||||
|
|
||||||
else if moving and info.release
|
view[0] = view[0]*before/scale + if step > 0 then 8 else -8
|
||||||
view[0] -= (moving.x-info.x)*2
|
view[1] = view[1]*before/scale + if step > 0 then 8 else -8
|
||||||
view[1] -= (moving.y-info.y)*4
|
|
||||||
moving = null
|
|
||||||
|
|
||||||
draw()
|
|
||||||
|
|
||||||
process.stdin.on 'keypress', (ch, key) ->
|
process.stdin.on 'keypress', (ch, key) ->
|
||||||
result = switch key?.name
|
result = switch key?.name
|
||||||
when "q"
|
when "q"
|
||||||
process.exit 0
|
process.exit 0
|
||||||
|
|
||||||
when "a" then size += 1
|
when "a" then zoomBy(.5)
|
||||||
when "z" then size -= 1
|
when "z" then zoomBy(-.5)
|
||||||
when "left" then view[0] += 5
|
when "left" then view[0] += 5
|
||||||
when "right" then view[0] -= 5
|
when "right" then view[0] -= 5
|
||||||
when "up" then view[1]+= 5
|
when "up" then view[1]+= 5
|
||||||
@ -135,5 +140,25 @@ process.stdout.on 'resize', ->
|
|||||||
init()
|
init()
|
||||||
draw()
|
draw()
|
||||||
|
|
||||||
process.stdin.setRawMode(true)
|
moving = null
|
||||||
process.stdin.resume()
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user