Cursor can move around screen

Still not handling corner cases like moving off the edge of screen,
line, etc.
This commit is contained in:
Danny Greg & Nathan Sobo
2012-01-17 18:13:50 -08:00
parent 5198a88cce
commit cd127c009e
5 changed files with 71 additions and 9 deletions

View File

@@ -7,8 +7,12 @@ class Cursor extends Template
viewProperties:
setPosition: (@_position) ->
@css(@parentView.toPixelPosition(@_position))
@updateAbsolutePosition()
getPosition: ->
@_position
updateAbsolutePosition: ->
position = @parentView.toPixelPosition(@_position)
@css(position)

View File

@@ -7,8 +7,8 @@ _ = require 'underscore'
module.exports =
class Editor extends Template
content: ->
@div class: 'editor', =>
@style @div outlet: 'lines'
@div class: 'editor', tabindex: -1, =>
@div outlet: 'lines'
@subview 'cursor', Cursor.build()
viewProperties:
@@ -17,13 +17,43 @@ class Editor extends Template
initialize: () ->
requireStylesheet 'editor.css'
@setBuffer(new Buffer)
@one 'attach', => @calculateDimensions()
atom.bindKeys '*',
right: 'move-right'
left: 'move-left'
down: 'move-down'
up: 'move-up'
@on 'move-right', => @moveRight()
@on 'move-left', => @moveLeft()
@on 'move-down', => @moveDown()
@on 'move-up', => @moveUp()
@one 'attach', =>
@calculateDimensions()
moveRight: ->
{ row, col } = @getPosition()
@setPosition({row, col: col + 1})
moveDown: ->
{ row, col } = @getPosition()
@setPosition({row: row + 1, col})
moveLeft: ->
{ row, col } = @getPosition()
@setPosition({row, col: col - 1})
moveUp: ->
{ row, col } = @getPosition()
@setPosition({row: row - 1, col})
setBuffer: (@buffer) ->
@lines.empty()
for line in @buffer.getLines()
@lines.append "<pre>#{line}</pre>"
_.defer => @setPosition(row: 3, col: 4)
@setPosition(row: 0, col: 0)
setPosition: (position) ->
@cursor.setPosition(position)
@@ -46,4 +76,5 @@ class Editor extends Template
@cachedCharWidth = fragment.width()
@cachedLineHeight = fragment.outerHeight()
fragment.remove()
@cursor.updateAbsolutePosition()