Skip to content

Commit

Permalink
New: Buggy undo support!
Browse files Browse the repository at this point in the history
  • Loading branch information
trethaller committed Aug 11, 2013
1 parent 7ae5eea commit b6d8d6f
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 4 deletions.
61 changes: 61 additions & 0 deletions js/app.coffee
Expand Up @@ -2,6 +2,50 @@
class Document
constructor: (@width,@height)->
@layer = new Layer(@width,@height)
@backup = new Layer(@width,@height)
@history = []
@histIndex = -1

afterEdit: (rect)->
if @histIndex > 0
@history.splice 0, @histIndex

@history.splice 0, 0, {
data: @backup.getCopy(rect)
rect: rect
}
@backup.getBuffer().set(@layer.getBuffer())
histSize = 10
if @history.length > histSize
@history.splice(histSize)

@histIndex = -1
console.log "History len: #{this.history.length}"

undo: ->
if @histIndex >= @history.length - 1
return

if @histIndex is -1
@afterEdit(@history[0].rect)
@histIndex = 1
else
@histIndex++

console.log "History idx: #{this.histIndex}"
@restore( @history[ @histIndex ] )

redo: ->
if @histIndex <= 0
return
@histIndex--
console.log "History idx: #{this.histIndex}"
@restore( @history[ @histIndex ] )


restore: (histItem)->
@layer.setData(histItem.data, histItem.rect)


Renderers = [GammaRenderer, NormalRenderer, GradientRenderer]
Tools = [RoundBrush, Picker]
Expand Down Expand Up @@ -110,6 +154,7 @@ class DocumentView
e.preventDefault()
if e.which is 1
@drawing = true
@actionDirtyRect = null
coords = getCanvasCoords(e)
editor.getToolObject().beginDraw(coords)
@onDraw(coords)
Expand All @@ -124,6 +169,8 @@ class DocumentView
if e.which is 1
editor.getToolObject().endDraw(getCanvasCoords(e))
@drawing = false
if @actionDirtyRect?
doc.afterEdit(@actionDirtyRect)

if e.which is 2
@panning = false
Expand Down Expand Up @@ -181,6 +228,12 @@ class DocumentView
.map((r)->r.intersect(layerRect))
.filter((r)->not r.isEmpty())

dirtyRects.forEach (r)=>
if not @actionDirtyRect?
@actionDirtyRect = r.clone()
else
@actionDirtyRect.extend(r)

if false # Log dirty rects
totalArea = dirtyRects
.map((r)-> r.width * r.height)
Expand Down Expand Up @@ -401,6 +454,14 @@ $(window).keyup (e)->
if e.key is 'Control'
editor.set('altkeyDown', false)

if e.ctrlKey
switch e.keyCode
when 90
editor.get('doc').undo()
editor.refresh()
when 89
editor.get('doc').redo()
editor.refresh()

$(document).ready ()->

Expand Down
74 changes: 72 additions & 2 deletions js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions js/core.coffee
Expand Up @@ -64,6 +64,9 @@ class Rect
ret.extend(rect.bottomRight())
return ret

clone: ->
return new Rect(@x, @y, @width, @height)

offset: (vec)->
return new Rect(@x+vec.x, @y+vec.y, @width, @height)

Expand Down Expand Up @@ -110,10 +113,10 @@ class Layer
constructor: (@width, @height) ->
@data = new FloatBuffer(@width, @height)

getRect: () ->
getRect: ->
return new Rect(0,0,@width,@height)

getBuffer: () ->
getBuffer: ->
return @data.fbuffer

getAt: (pos)->
Expand All @@ -124,6 +127,28 @@ class Layer
p = pos.round()
fb = @data.fbuffer

getCopy: (rect)->
srcData = @data.buffer
dstData = new ArrayBuffer(rect.width * rect.height * 4)
`
for(var iy=0; iy<rect.height; ++iy) {
var src = new Uint32Array(srcData, 4 * ((iy + rect.y) * this.width + rect.x), rect.width);
var dst = new Uint32Array(dstData, 4 * iy * rect.width, rect.width);
dst.set(src);
}`
return dstData

setData: (buffer, rect)->
dstData = @data.buffer
`
for(var iy=0; iy<rect.height; ++iy) {
var src = new Uint32Array(buffer, 4 * iy * rect.width, rect.width);
var dstOff = 4 * ((iy + rect.y) * this.width + rect.x);
var dst = new Uint32Array(dstData, dstOff, rect.width);
dst.set(src);
}`
return

Bezier =
quadratic: (pts, t)->
lerp = (a, b, t) ->
Expand Down
29 changes: 29 additions & 0 deletions js/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6d8d6f

Please sign in to comment.