PaintUndo.js module

This commit is contained in:
Tim Mickel 2016-01-13 18:06:52 -05:00
parent 4361198487
commit eae6c944f0

View file

@ -1,16 +1,22 @@
import ScratchJr from '../editor/ScratchJr';
import Path from './Path';
import Paint from './Paint';
import Camera from './Camera';
import SVGTools from './SVGTools';
import {newHTML, gn, isTablet} from '../utils/lib';
import ScratchAudio from '../utils/ScratchAudio';
//////////////////////////////////
// Undo / Redo Functions
//////////////////////////////////
var PaintUndo = function () {};
PaintUndo.buffer = [];
PaintUndo.index = 0;
export let buffer = [];
export let index = 0;
////////////////////////////////////////
// Undo Controls Setup
///////////////////////////////////////
PaintUndo.setup = function (p) {
export default class PaintUndo {
////////////////////////////////////////
// Undo Controls Setup
///////////////////////////////////////
static setup (p) {
var div = newHTML('div', 'paintundo', p);
div.setAttribute('id', 'paintundocontrols');
var lib = [['undo', PaintUndo.undo], ['redo', PaintUndo.redo]];
@ -21,9 +27,9 @@ PaintUndo.setup = function (p) {
dx += 20;
}
PaintUndo.updateActiveUndo();
};
}
PaintUndo.newToggleClicky = function (p, prefix, key, fcn) {
static newToggleClicky (p, prefix, key, fcn) {
var button = newHTML('div', 'undocircle', p);
newHTML('div', key + ' off', button);
button.setAttribute('type', 'toggleclicky');
@ -40,41 +46,41 @@ PaintUndo.newToggleClicky = function (p, prefix, key, fcn) {
}
}
return button;
};
}
PaintUndo.runUndo = function () {
static runUndo () {
Path.quitEditMode();
Paint.root.removeChild(gn('layer1'));
Paint.root.appendChild(SVGTools.toObject(PaintUndo.buffer[PaintUndo.index]));
Paint.root.appendChild(SVGTools.toObject(buffer[index]));
Paint.root.appendChild(gn('draglayer'));
Paint.root.appendChild(gn('paintgrid'));
Paint.setZoomTo(Paint.currentZoom);
};
// you record before introducing a change
PaintUndo.record = function (dontStartStories) {
if ((PaintUndo.index + 1) <= PaintUndo.buffer.length) {
PaintUndo.buffer.splice(PaintUndo.index + 1, PaintUndo.buffer.length);
}
PaintUndo.buffer.push(PaintUndo.getCanvas());
PaintUndo.index++;
// you record before introducing a change
static record (dontStartStories) {
if ((index + 1) <= buffer.length) {
buffer.splice(index + 1, buffer.length);
}
buffer.push(PaintUndo.getCanvas());
index++;
if (gn('id_pundo')) {
PaintUndo.updateActiveUndo();
}
if (!dontStartStories) {
ScratchJr.storyStart('PaintUndo.record'); // Record a change for sample projects in story-starter mode
}
};
}
PaintUndo.getCanvas = function () {
static getCanvas () {
return SVGTools.svg2string(gn('layer1'));
};
}
//////////////////////////////////
// Control buttons callbacks
//////////////////////////////////
//////////////////////////////////
// Control buttons callbacks
//////////////////////////////////
PaintUndo.undo = function (e) {
static undo (e) {
if (e.touches && (e.touches.length > 1)) {
return;
}
@ -83,21 +89,21 @@ PaintUndo.undo = function (e) {
if (Camera.active) {
Camera.doAction('undo');
}
while (PaintUndo.index >= PaintUndo.buffer.length) {
PaintUndo.index--;
while (index >= buffer.length) {
index--;
}
PaintUndo.index--;
var snd = (PaintUndo.index < 0) ? 'boing.wav' : 'tap.wav';
index--;
var snd = (index < 0) ? 'boing.wav' : 'tap.wav';
ScratchAudio.sndFX(snd);
if (PaintUndo.index < 0) {
PaintUndo.index = 0;
if (index < 0) {
index = 0;
} else {
PaintUndo.runUndo();
}
PaintUndo.updateActiveUndo();
};
}
PaintUndo.redo = function (e) {
static redo (e) {
if (e.touches && (e.touches.length > 1)) {
return;
}
@ -106,44 +112,45 @@ PaintUndo.redo = function (e) {
if (Camera.active) {
Camera.doAction('undo');
}
PaintUndo.index++;
var snd = (PaintUndo.index > PaintUndo.buffer.length - 1) ? 'boing.wav' : 'tap.wav';
index++;
var snd = (index > buffer.length - 1) ? 'boing.wav' : 'tap.wav';
ScratchAudio.sndFX(snd);
if (PaintUndo.index > PaintUndo.buffer.length - 1) {
PaintUndo.index = PaintUndo.buffer.length - 1;
if (index > buffer.length - 1) {
index = buffer.length - 1;
} else {
PaintUndo.runUndo();
}
PaintUndo.updateActiveUndo();
};
}
PaintUndo.updateActiveUndo = function () {
static updateActiveUndo () {
if (gn('id_pundo')) {
if (PaintUndo.buffer.length == 1) {
if (buffer.length == 1) {
PaintUndo.tunOffButton(gn('id_pundo'));
} else {
if (PaintUndo.index < 1) {
if (index < 1) {
PaintUndo.tunOffButton(gn('id_pundo'));
} else {
PaintUndo.tunOnButton(gn('id_pundo'));
}
}
if (PaintUndo.index >= PaintUndo.buffer.length - 1) {
if (index >= buffer.length - 1) {
PaintUndo.tunOffButton(gn('id_predo'));
} else {
PaintUndo.tunOnButton(gn('id_predo'));
}
}
};
}
PaintUndo.tunOnButton = function (p) {
static tunOnButton (p) {
var kid = p.childNodes[0];
var kclass = kid.getAttribute('class').split(' ')[0];
kid.setAttribute('class', kclass + ' on');
};
}
PaintUndo.tunOffButton = function (p) {
static tunOffButton (p) {
var kid = p.childNodes[0];
var kclass = kid.getAttribute('class').split(' ')[0];
kid.setAttribute('class', kclass + ' off');
};
}
}