Permalink
Browse files

Fix undo, fully working now

  • Loading branch information...
1 parent d446527 commit 51d9fa0c22a6de911f7d6024e17dc9e3adc5d137 @trethaller committed Aug 29, 2013
Showing with 92 additions and 67 deletions.
  1. +0 −1 out/js/teztura-renderers.js
  2. +48 −38 out/js/teztura.js
  3. +5 −11 src/app.coffee
  4. +28 −10 src/document-view.coffee
  5. +9 −4 src/document.coffee
  6. +0 −1 src/renderers/gradient-renderer.js
  7. +2 −2 src/tools.coffee
@@ -53,7 +53,6 @@ GradientRenderer = (function() {
var width = layer.width;
var height = layer.height;
var imgData = view.imageData.data;
- var destBuffer = new Uint32Array(view.imageData.data.buffer);
var fb = layer.getBuffer();
var gamma = properties.gamma;
var lut = properties.gradient.lut;
View
@@ -1,4 +1,4 @@
-var BlendModes, Commands, Document, DocumentView, Editor, Flatten, Picker, PropertyPanel, PropertyView, Renderers, RoundBrush, StepBrush, Tools, createCommandsButtons, createPalette, createRenderersButtons, createToolsButtons, editor, getPenPressure, loadGradient, refresh, status, toolsProperties, _ref,
+var BlendModes, Commands, Document, DocumentView, Editor, Flatten, Picker, PropertyPanel, PropertyView, Renderers, RoundBrush, StepBrush, Tools, createCommandsButtons, createPalette, createRenderersButtons, createToolsButtons, editor, loadGradient, refresh, status, toolsProperties, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -159,16 +159,6 @@ Editor = (function(_super) {
})(Backbone.Model);
-getPenPressure = function() {
- var penAPI, plugin;
- plugin = document.getElementById('wtPlugin');
- penAPI = plugin.penAPI;
- if (penAPI && penAPI.pointerType > 0) {
- return penAPI.pressure;
- }
- return 1.0;
-};
-
status = function(txt) {
return $('#status-bar').text(txt);
};
@@ -259,13 +249,7 @@ loadGradient = function(name, url) {
$(window).keydown(function(e) {
if (e.key === 'Control') {
- return editor.set('altkeyDown', true);
- }
-});
-
-$(window).keyup(function(e) {
- if (e.key === 'Control') {
- editor.set('altkeyDown', false);
+ editor.set('altkeyDown', true);
}
if (e.ctrlKey) {
switch (e.keyCode) {
@@ -279,6 +263,12 @@ $(window).keyup(function(e) {
}
});
+$(window).keyup(function(e) {
+ if (e.key === 'Control') {
+ return editor.set('altkeyDown', false);
+ }
+});
+
$(document).ready(function() {
loadGradient('g1', 'img/gradient-1.png');
Renderers = [GammaRenderer, NormalRenderer, GradientRenderer];
@@ -316,7 +306,7 @@ DocumentView = (function() {
DocumentView.prototype.scale = 1.0;
function DocumentView($container, doc) {
- var $backCanvas, $canvas, getCanvasCoords, getCoords, local,
+ var $backCanvas, $canvas, getCanvasCoords, getPenCoords, getPressure, local, penAPI, plugin,
_this = this;
this.doc = doc;
$container.empty();
@@ -338,17 +328,33 @@ DocumentView = (function() {
this.context = $canvas[0].getContext('2d');
this.imageData = this.context.getImageData(0, 0, doc.width, doc.height);
this.context.mozImageSmoothingEnabled = false;
- getCoords = function(e) {
- var x, y;
- x = e.pageX - $backCanvas.position().left;
- y = e.pageY - $backCanvas.position().top;
- return new Vec2(x, y);
- };
+ plugin = document.getElementById('wtPlugin');
+ penAPI = plugin != null ? plugin.penAPI : null;
getCanvasCoords = function(e) {
var v;
- v = getCoords(e);
+ v = getPenCoords(e);
return _this.screenToCanvas(v);
};
+ getPenCoords = function(e) {
+ var v;
+ v = new Vec2(e.pageX, e.pageY);
+ /*
+ penAPI = plugin.penAPI
+ if penAPI? and penAPI.pointerType > 0
+ v.x += penAPI.sysX - penAPI.posX
+ v.y += penAPI.sysY - penAPI.posY
+ */
+
+ v.x -= $backCanvas.position().left;
+ v.y -= $backCanvas.position().top;
+ return v;
+ };
+ getPressure = function() {
+ if ((penAPI != null) && penAPI.pointerType > 0) {
+ return penAPI.pressure;
+ }
+ return 1.0;
+ };
local = {};
$backCanvas.mousedown(function(e) {
var coords;
@@ -358,7 +364,8 @@ DocumentView = (function() {
_this.actionDirtyRect = null;
coords = getCanvasCoords(e);
editor.getToolObject().beginDraw(coords);
- _this.onDraw(coords);
+ doc.beginEdit();
+ _this.onDraw(coords, getPressure());
}
if (e.which === 2) {
_this.panning = true;
@@ -383,7 +390,7 @@ DocumentView = (function() {
var curPos, o;
e.preventDefault();
if (_this.drawing) {
- _this.onDraw(getCanvasCoords(e));
+ _this.onDraw(getCanvasCoords(e), getPressure());
}
if (_this.panning) {
curPos = getCoords(e);
@@ -419,10 +426,9 @@ DocumentView = (function() {
}
};
- DocumentView.prototype.onDraw = function(pos) {
- var dirtyRects, layer, layerRect, pressure, r, tool, totalArea, xoff, yoff, _i, _j, _len, _len1, _ref1, _ref2,
+ DocumentView.prototype.onDraw = function(pos, pressure) {
+ var dirtyRects, layer, layerRect, r, tool, totalArea, xoff, yoff, _i, _j, _len, _len1, _ref1, _ref2,
_this = this;
- pressure = getPenPressure();
dirtyRects = [];
layer = this.doc.layer;
tool = editor.getToolObject();
@@ -478,22 +484,26 @@ Document = (function() {
this.layer = new Layer(this.width, this.height);
this.backup = new Layer(this.width, this.height);
this.history = [];
- this.histIndex = 0;
+ this.histIndex = 1;
}
- Document.prototype.afterEdit = function(rect) {
- var histSize;
+ Document.prototype.beginEdit = function() {
if (this.histIndex > 0) {
this.history.splice(0, this.histIndex);
+ this.histIndex = 0;
+ return this.backup.getBuffer().set(this.layer.getBuffer());
}
- this.histIndex = 0;
+ };
+
+ Document.prototype.afterEdit = function(rect) {
+ var histSize;
this.history.splice(0, 0, {
data: this.backup.getCopy(rect),
rect: rect
});
this.backup.getBuffer().set(this.layer.getBuffer());
histSize = 10;
- if (this.history.length > histSize) {
+ if (this.history.length >= histSize) {
return this.history.splice(histSize);
}
};
@@ -746,7 +756,7 @@ RoundBrush = (function() {
}, {
id: 'size',
name: "Size",
- defaultValue: 16.0,
+ defaultValue: 8.0,
range: [1.0, 256.0],
type: 'int'
}, {
@@ -757,7 +767,7 @@ RoundBrush = (function() {
}, {
id: 'intensity',
name: "Intensity",
- defaultValue: 1.0,
+ defaultValue: 0.6,
range: [0.0, 1.0],
power: 2.0
}
View
@@ -124,13 +124,6 @@ class Editor extends Backbone.Model
# ---
-getPenPressure = () ->
- plugin = document.getElementById('wtPlugin')
- penAPI = plugin.penAPI
- if penAPI and penAPI.pointerType > 0
- return penAPI.pressure
- return 1.0
-
status = (txt)->
$('#status-bar').text(txt)
@@ -199,10 +192,6 @@ $(window).keydown (e)->
if e.key is 'Control'
editor.set('altkeyDown', true)
-$(window).keyup (e)->
- if e.key is 'Control'
- editor.set('altkeyDown', false)
-
if e.ctrlKey
switch e.keyCode
when 90
@@ -212,9 +201,14 @@ $(window).keyup (e)->
editor.get('doc').redo()
editor.refresh()
+$(window).keyup (e)->
+ if e.key is 'Control'
+ editor.set('altkeyDown', false)
+
$(document).ready ()->
loadGradient('g1', 'img/gradient-1.png')
+ #loadGradient('g2', 'img/gradient-2.png')
Renderers = [GammaRenderer, NormalRenderer, GradientRenderer]
Tools = [RoundBrush, Picker]
@@ -24,26 +24,43 @@ class DocumentView
@context.mozImageSmoothingEnabled = false
-
- getCoords = (e)=>
- x = e.pageX-$backCanvas.position().left
- y = e.pageY-$backCanvas.position().top
- return new Vec2(x,y)
+ plugin = document.getElementById('wtPlugin')
+ penAPI = if plugin? then plugin.penAPI else null
getCanvasCoords = (e)=>
- v = getCoords(e)
+ v = getPenCoords(e)
return @screenToCanvas(v)
+ getPenCoords = (e)=>
+ v = new Vec2(e.pageX, e.pageY)
+
+ ###
+ penAPI = plugin.penAPI
+ if penAPI? and penAPI.pointerType > 0
+ v.x += penAPI.sysX - penAPI.posX
+ v.y += penAPI.sysY - penAPI.posY
+ ###
+ v.x -= $backCanvas.position().left
+ v.y -= $backCanvas.position().top
+ return v
+
+ getPressure = ()=>
+ if penAPI? and penAPI.pointerType > 0
+ return penAPI.pressure
+ return 1.0
+
local = {}
$backCanvas.mousedown (e)=>
e.preventDefault()
+
if e.which is 1
@drawing = true
@actionDirtyRect = null
coords = getCanvasCoords(e)
editor.getToolObject().beginDraw(coords)
- @onDraw(coords)
+ doc.beginEdit()
+ @onDraw(coords, getPressure())
if e.which is 2
@panning = true
@@ -64,13 +81,14 @@ class DocumentView
$container.mousemove (e)=>
e.preventDefault()
if @drawing
- @onDraw(getCanvasCoords(e))
+ @onDraw(getCanvasCoords(e), getPressure())
if @panning
curPos = getCoords(e)
o = local.offsetStart.add(curPos.sub(local.panningStart))
@offset = o
@rePaint()
+
screenToCanvas: (pt)->
return pt.sub(@offset).scale(1.0/@scale)
@@ -92,8 +110,7 @@ class DocumentView
else
ctx.drawImage(@canvas, 0, 0)
- onDraw: (pos)->
- pressure = getPenPressure()
+ onDraw: (pos, pressure)->
dirtyRects = []
layer = @doc.layer
@@ -131,3 +148,4 @@ class DocumentView
editor.get('renderer').renderLayer(layer, @, dirtyRects)
@rePaint()
#), 0
+
View
@@ -4,14 +4,19 @@ class Document
@layer = new Layer(@width,@height)
@backup = new Layer(@width,@height)
@history = []
- @histIndex = 0
+ @histIndex = 1
- afterEdit: (rect)->
+ beginEdit: ->
if @histIndex > 0
# Discard obsolete history branch
@history.splice 0, @histIndex
- @histIndex = 0
+ @histIndex = 0
+
+ # Backup layer, which becomes the head
+ @backup.getBuffer().set(@layer.getBuffer())
+ afterEdit: (rect)->
+
# Insert item at the top
@history.splice 0, 0, {
data: @backup.getCopy(rect)
@@ -23,7 +28,7 @@ class Document
# Limit history
histSize = 10
- if @history.length > histSize
+ if @history.length >= histSize
@history.splice(histSize)
undo: ->
@@ -10,7 +10,6 @@ GradientRenderer = (function() {
var width = layer.width;
var height = layer.height;
var imgData = view.imageData.data;
- var destBuffer = new Uint32Array(view.imageData.data.buffer);
var fb = layer.getBuffer();
var gamma = properties.gamma;
var lut = properties.gradient.lut;
View
@@ -106,7 +106,7 @@ RoundBrush = (()->
{
id: 'size'
name: "Size"
- defaultValue: 16.0
+ defaultValue: 8.0
range: [1.0, 256.0]
type: 'int'
},
@@ -119,7 +119,7 @@ RoundBrush = (()->
{
id:'intensity'
name: "Intensity"
- defaultValue: 1.0
+ defaultValue: 0.6
range: [0.0, 1.0]
power: 2.0
}

0 comments on commit 51d9fa0

Please sign in to comment.