From 9cd88d59e45b530e483490804503e6b47030fd4d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 8 Aug 2014 18:56:41 -0400 Subject: undo stack --- test/09-test-undo.js | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 test/09-test-undo.js (limited to 'test/09-test-undo.js') diff --git a/test/09-test-undo.js b/test/09-test-undo.js new file mode 100644 index 0000000..84b5d09 --- /dev/null +++ b/test/09-test-undo.js @@ -0,0 +1,163 @@ +var assert = require("assert") +var UndoStack = require("../public/assets/javascripts/rectangles/util/undo.js") + +describe('undo', function(){ + + var state = "zero" + + describe('#register()', function(){ + + UndoStack.register({ + type: "demo", + undo: function(action){ + state = action.prev + }, + redo: function(action){ + state = action.next + }, + }) + + it('registers undoable actions', function(){ + assert( UndoStack.types.hasOwnProperty("demo") ) + }) + }) + + describe('#push()', function(){ + + it('starts empty', function(){ + assert.equal(0, UndoStack.stack.length) + assert.equal(-1, UndoStack.pointer) + }) + + it('pushes some actions', function(){ + + UndoStack.push({ + type: "demo", + prev: state, + next: "one" + }) + state = "one" + + UndoStack.push({ + type: "demo", + prev: state, + next: "two" + }) + state = "two" + + UndoStack.push({ + type: "demo", + prev: state, + next: "three" + }) + state = "three" + + assert.equal(3, UndoStack.stack.length) + assert.equal(2, UndoStack.pointer) + }) + }) + + describe('#undo()', function(){ + + it('retrieves old state', function(){ + assert.equal("three", state) + UndoStack.undo() + assert.equal("two", state) + assert.equal(1, UndoStack.pointer) + }) + + it('can only undo so far', function(){ + var canUndo + + canUndo = UndoStack.undo() + assert.equal("one", state) + assert.equal(0, UndoStack.pointer) + assert.equal(true, canUndo) + + canUndo = UndoStack.undo() + assert.equal("zero", state) + assert.equal(-1, UndoStack.pointer) + assert.equal(false, canUndo) + + canUndo = UndoStack.undo() + assert.equal("zero", state) + assert.equal(-1, UndoStack.pointer) + assert.equal(false, canUndo) + }) + + }) + + describe('#redo()', function(){ + + it('reassigns new state', function(){ + UndoStack.redo() + assert.equal("one", state) + }) + + it('can only redo so far', function(){ + var canRedo + + canRedo = UndoStack.redo() + assert.equal("two", state) + assert.equal(true, canRedo) + + canRedo = UndoStack.redo() + assert.equal("three", state) + assert.equal(false, canRedo) + + canRedo = UndoStack.redo() + assert.equal("three", state) + assert.equal(false, canRedo) + }) + + it('clobbers old state if we undo then do something else', function(){ + UndoStack.undo() + assert.equal("two", state) + assert.equal(1, UndoStack.pointer) + + UndoStack.undo() + assert.equal("one", state) + assert.equal(0, UndoStack.pointer) + assert.equal(3, UndoStack.stack.length) + + UndoStack.push({ + type: "demo", + prev: state, + next: "four" + }) + state = "four" + + assert.equal(1, UndoStack.pointer) + assert.equal(2, UndoStack.stack.length) + + UndoStack.undo() + assert.equal("one", state) + assert.equal(0, UndoStack.pointer) + assert.equal(2, UndoStack.stack.length) + + UndoStack.redo() + assert.equal("four", state) + assert.equal(1, UndoStack.pointer) + assert.equal(2, UndoStack.stack.length) + }) + }) + +}) + +// 1) push action +// 2) push action +// 3) push action +// undo 3 +// undo 2 +// undo 1 +// undo * can't undo anymore +// redo 1 +// redo 2 +// redo 3 +// redo * can't redo anymore +// undo 3 +// 4) push action (clobbers action 3) +// undo 4 +// undo 2 +// redo 2 +// redo 4 -- cgit v1.2.3-70-g09d2 From 38125881369cb87c35eb6a7b7e24f7c0130a32bb Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Tue, 12 Aug 2014 09:15:30 -0400 Subject: register undo types --- .../javascripts/rectangles/engine/scenery/undo.js | 60 ++++++++++++++++++++++ public/assets/javascripts/rectangles/util/undo.js | 17 ++++-- test/09-test-undo.js | 8 +-- 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 public/assets/javascripts/rectangles/engine/scenery/undo.js (limited to 'test/09-test-undo.js') diff --git a/public/assets/javascripts/rectangles/engine/scenery/undo.js b/public/assets/javascripts/rectangles/engine/scenery/undo.js new file mode 100644 index 0000000..fb221f5 --- /dev/null +++ b/public/assets/javascripts/rectangles/engine/scenery/undo.js @@ -0,0 +1,60 @@ +(function(){ + UndoStack.register([ + { + type: "create-scenery", + undo: function(state){ + }, + redo: function(state){ + }, + }, + { + type: "update-scenery", + undo: function(state){ + }, + redo: function(state){ + }, + }, + { + type: "destroy-scenery", + undo: function(state){ + }, + redo: function(state){ + }, + }, + + // + + { + type: "create-rectangle", + undo: function(state){ + }, + redo: function(state){ + }, + }, + { + type: "update-rectangle", + undo: function(state){ + }, + redo: function(state){ + }, + }, + { + type: "destroy-rectangle", + undo: function(state){ + }, + redo: function(state){ + }, + }, + + // + + { + type: "update-wallpaper", + undo: function(state){ + }, + redo: function(state){ + }, + }, + + ]) +})() \ No newline at end of file diff --git a/public/assets/javascripts/rectangles/util/undo.js b/public/assets/javascripts/rectangles/util/undo.js index 3700817..5d8593c 100644 --- a/public/assets/javascripts/rectangles/util/undo.js +++ b/public/assets/javascripts/rectangles/util/undo.js @@ -17,7 +17,7 @@ UndoStack.prototype.undo = function(){ if (this.pointer == -1) return false var action = this.stack[this.pointer] - this.types[ action.type ].undo(action) + this.types[ action.type ].undo(action.prev) this.pointer-- return this.pointer > -1 } @@ -25,13 +25,20 @@ if (this.pointer == this.stack.length-1) return false this.pointer++ var action = this.stack[this.pointer] - this.types[ action.type ].redo(action) + this.types[ action.type ].redo(action.next) return this.pointer < this.stack.length-1 } UndoStack.prototype.register = function(actionType){ - this.types[ actionType.type ] = actionType - } - + if (actionType.length) { + actionType.forEach(this.registerOne.bind(this)) + } + else { + this.registerOne(actionType) + } + } + UndoStack.prototype.registerOne = function(actionType){ + this.types[ actionType.type ] = actionType + } if ('window' in this) { window.UndoStack = new UndoStack } diff --git a/test/09-test-undo.js b/test/09-test-undo.js index 84b5d09..17998a9 100644 --- a/test/09-test-undo.js +++ b/test/09-test-undo.js @@ -9,11 +9,11 @@ describe('undo', function(){ UndoStack.register({ type: "demo", - undo: function(action){ - state = action.prev + undo: function(myState){ + state = myState }, - redo: function(action){ - state = action.next + redo: function(myState){ + state = myState }, }) -- cgit v1.2.3-70-g09d2 From a279338f4dff62d87021ca28252d68b0c586d3a7 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 13 Aug 2014 02:12:40 -0400 Subject: add measurement stuff to media editor --- .../javascripts/mx/extensions/mx.movements.js | 4 +- .../rectangles/engine/scenery/types/_object.js | 4 ++ .../javascripts/rectangles/util/measurement.js | 82 +++++++++++++++++++++ .../assets/javascripts/ui/builder/BuilderInfo.js | 83 ---------------------- public/assets/javascripts/ui/editor/MediaEditor.js | 34 +++++++-- public/assets/stylesheets/app.css | 7 ++ test/09-test-undo.js | 1 + views/controls/editor/media-editor.ejs | 20 ++++-- views/partials/scripts.ejs | 1 + 9 files changed, 138 insertions(+), 98 deletions(-) create mode 100644 public/assets/javascripts/rectangles/util/measurement.js (limited to 'test/09-test-undo.js') diff --git a/public/assets/javascripts/mx/extensions/mx.movements.js b/public/assets/javascripts/mx/extensions/mx.movements.js index 191088f..3b7d3e2 100644 --- a/public/assets/javascripts/mx/extensions/mx.movements.js +++ b/public/assets/javascripts/mx/extensions/mx.movements.js @@ -149,7 +149,8 @@ MX.Movements = function (cam) { case 32: // space moveUp = moveDown = false break - + +/* case 48: // 0 cam.rotationX = 0 cam.rotationY = 0 @@ -157,6 +158,7 @@ MX.Movements = function (cam) { cam.y = viewHeight cam.z = 0 break +*/ } }) diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 7202ce0..66e0faf 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -66,6 +66,10 @@ Scenery.types.base = Fiber.extend(function(base){ this.center = this.wall.center() }, + set_scale: function(scale){ + this.scale = this.mx.scale = this.mx.ops.scale = scale || 1.0 + }, + recenter: function(){ this.mx.move({ x: this.center.a, diff --git a/public/assets/javascripts/rectangles/util/measurement.js b/public/assets/javascripts/rectangles/util/measurement.js new file mode 100644 index 0000000..d6a0b35 --- /dev/null +++ b/public/assets/javascripts/rectangles/util/measurement.js @@ -0,0 +1,82 @@ +$.fn.resetUnitVal = function(){ + this.each(function(){ + var n = $(this).data("px") + $(this).unitVal(n) + }); +} + +$.fn.unitVal = function(n){ + var s + if (typeof n === "undefined") { + s = $(this).val() + n = stringToMeasurement( s ) + if (! n || isNaN(n)) { + n = $(this).data("px") + } + } + s = measurementToString( n ) + $(this).val( s ).data("px", n) + return n +} + +function measurementToString( n ) { + var s, ft, inch + switch (app.units) { + case 'm': + s = round(n/36 * 0.3048 * 100) / 100 + " m" + break + case 'ft': + ft = floor(n / 36) + inch = abs(round((n % 36) / 3)) + s = ft + "'" + if (inch > 0) { + s += " " + inch + '"' + } + break + case 'px': + default: + s = round(n) + " px" + break + } + return s +} +function stringToMeasurement( s ) { + var ft, inch, ft_in, type + if (! s.match(/[0-9]/)) { + return NaN + } + if (s.indexOf("'") !== -1 || s.indexOf('"') !== -1 || s.indexOf('ft') !== -1) { + ft_in = s.match(/[0-9.]+/g) + if (ft_in.length >= 2) { + ft = parseFloat( ft_in[0] ) + inch = parseFloat( ft_in[1] ) + } + else if (ft_in.length == 1) { + if (s.indexOf('"') !== -1) { + ft = 0 + inch = parseFloat( ft_in[0] ) + } + else { + ft = parseFloat( ft_in[0] ) + inch = 0 + } + } + else { + ft = inch = 0 + } + n = ft * 36 + inch * 3 + } + else if (s.indexOf("m") !== -1) { + n = parseFloat(s.match(/[0-9.]+/)) * 36 / 0.3048 + } + else if (s.indexOf("px") !== -1) { + n = parseFloat(s.match(/[0-9.]+/)) + } + else { + n = abs( stringToMeasurement( s + app.units ) ) + } + if (s.indexOf('-') !== -1) { + n *= -1 + } + return n +} diff --git a/public/assets/javascripts/ui/builder/BuilderInfo.js b/public/assets/javascripts/ui/builder/BuilderInfo.js index 56f1338..2fffdba 100644 --- a/public/assets/javascripts/ui/builder/BuilderInfo.js +++ b/public/assets/javascripts/ui/builder/BuilderInfo.js @@ -96,86 +96,3 @@ var BuilderInfo = View.extend({ }, }) - -$.fn.resetUnitVal = function(){ - this.each(function(){ - var n = $(this).data("px") - $(this).unitVal(n) - }); -} - -$.fn.unitVal = function(n){ - var s - if (typeof n === "undefined") { - s = $(this).val() - n = stringToMeasurement( s ) - if (! n || isNaN(n)) { - n = $(this).data("px") - } - } - s = measurementToString( n ) - $(this).val( s ).data("px", n) - return n -} - -function measurementToString( n ) { - var s, ft, inch - switch (app.units) { - case 'm': - s = round(n/36 * 0.3048 * 100) / 100 + " m" - break - case 'ft': - ft = floor(n / 36) - inch = abs(round((n % 36) / 3)) - s = ft + "'" - if (inch > 0) { - s += " " + inch + '"' - } - break - case 'px': - default: - s = round(n) + " px" - break - } - return s -} -function stringToMeasurement( s ) { - var ft, inch, ft_in, type - if (! s.match(/[0-9]/)) { - return NaN - } - if (s.indexOf("'") !== -1 || s.indexOf('"') !== -1 || s.indexOf('ft') !== -1) { - ft_in = s.match(/[0-9.]+/g) - if (ft_in.length >= 2) { - ft = parseFloat( ft_in[0] ) - inch = parseFloat( ft_in[1] ) - } - else if (ft_in.length == 1) { - if (s.indexOf('"') !== -1) { - ft = 0 - inch = parseFloat( ft_in[0] ) - } - else { - ft = parseFloat( ft_in[0] ) - inch = 0 - } - } - else { - ft = inch = 0 - } - n = ft * 36 + inch * 3 - } - else if (s.indexOf("m") !== -1) { - n = parseFloat(s.match(/[0-9.]+/)) * 36 / 0.3048 - } - else if (s.indexOf("px") !== -1) { - n = parseFloat(s.match(/[0-9.]+/)) - } - else { - n = abs( stringToMeasurement( s + app.units ) ) - } - if (s.indexOf('-') !== -1) { - n *= -1 - } - return n -} \ No newline at end of file diff --git a/public/assets/javascripts/ui/editor/MediaEditor.js b/public/assets/javascripts/ui/editor/MediaEditor.js index cd8fb63..b9eb8fc 100644 --- a/public/assets/javascripts/ui/editor/MediaEditor.js +++ b/public/assets/javascripts/ui/editor/MediaEditor.js @@ -11,6 +11,9 @@ var MediaEditor = FormView.extend({ "change [name=autoplay]": "setAutoplay", "change [name=loop]": "setLoop", "change [name=mute]": "setMute", + "change [name=width]": 'changeWidth', + "change [name=height]": 'changeHeight', + "change [name=units]": 'changeUnits', "click [data-role=destroy-media]": "destroy", }, @@ -22,8 +25,8 @@ var MediaEditor = FormView.extend({ this.$description = this.$("[name=description]") // image fields - this.$widthDimension = this.$("[name=width]") - this.$heightDimension = this.$("[name=height]") + this.$width = this.$("[name=width]") + this.$height = this.$("[name=height]") this.$units = this.$("[name=units]") // video fields @@ -55,16 +58,14 @@ var MediaEditor = FormView.extend({ this.$name.val(media.title) this.$description.val(media.description) + this.setDimensions() + this.$units.val( "ft" ) switch (media.type) { case "image": this.$(".image").show() this.$(".video").hide() - - this.$widthDimension.val( Number(media.widthDimension) || "" ) - this.$heightDimension.val( Number(media.heightDimension) || "" ) - this.$units.val( media.units || "cm" ) - + break case "youtube": @@ -113,6 +114,25 @@ var MediaEditor = FormView.extend({ this.scenery.mute(checked) }, + setDimensions: function(){ + this.$width.unitVal( Number(this.scenery.dimensions.a * this.scenery.scale) || "" ) + this.$height.unitVal( Number(this.scenery.dimensions.b * this.scenery.scale) || "" ) + }, + changeWidth: function(e){ + e.stopPropagation() + this.scenery.set_scale( this.$width.unitVal() / this.scenery.dimensions.a ) + this.setDimensions() + }, + changeHeight: function(e){ + e.stopPropagation() + this.scenery.set_scale( this.$height.unitVal() / this.scenery.dimensions.b ) + this.setDimensions() + }, + changeUnits: function(){ + app.units = this.$units.val() + this.$('.units').resetUnitVal() + }, + bind: function(scenery){ this.scenery = scenery this.scenery.mx.bound = true diff --git a/public/assets/stylesheets/app.css b/public/assets/stylesheets/app.css index 1863add..8508cf7 100755 --- a/public/assets/stylesheets/app.css +++ b/public/assets/stylesheets/app.css @@ -1295,6 +1295,13 @@ input[type="range"]::-webkit-slider-thumb { top: 0px; } +#mediaEditor .setting.number label { + width: 40px; +} +#mediaEditor .setting.number [type=text] { + width: 140px; +} + .playButton,.muteButton { color: white; background: black; diff --git a/test/09-test-undo.js b/test/09-test-undo.js index 17998a9..f774c04 100644 --- a/test/09-test-undo.js +++ b/test/09-test-undo.js @@ -1,5 +1,6 @@ var assert = require("assert") var UndoStack = require("../public/assets/javascripts/rectangles/util/undo.js") +UndoStack.debug = false describe('undo', function(){ diff --git a/views/controls/editor/media-editor.ejs b/views/controls/editor/media-editor.ejs index 5db1fb2..7f8f299 100644 --- a/views/controls/editor/media-editor.ejs +++ b/views/controls/editor/media-editor.ejs @@ -34,13 +34,19 @@ -
- Dimensions
- - - +
+
+ + +
+
+
diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index b63d1bf..087c0d7 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -23,6 +23,7 @@ + -- cgit v1.2.3-70-g09d2 From 90cb5b343f3d56372f9b43faf215ed80dd879fe1 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 13 Aug 2014 16:12:02 -0400 Subject: undo for scenery stuff --- .../javascripts/rectangles/engine/map/ui_editor.js | 16 +++---- .../rectangles/engine/scenery/_scenery.js | 9 +++- .../javascripts/rectangles/engine/scenery/move.js | 20 +++++++++ .../rectangles/engine/scenery/resize.js | 13 +++++- .../javascripts/rectangles/engine/scenery/undo.js | 20 +++++++++ .../assets/javascripts/rectangles/models/wall.js | 8 +++- public/assets/javascripts/rectangles/util/undo.js | 52 ---------------------- .../javascripts/rectangles/util/undostack.js | 52 ++++++++++++++++++++++ public/assets/javascripts/ui/editor/MediaEditor.js | 8 ++-- public/assets/javascripts/ui/lib/Parser.js | 2 - test/09-test-undo.js | 16 +++---- views/partials/scripts.ejs | 2 +- 12 files changed, 139 insertions(+), 79 deletions(-) delete mode 100644 public/assets/javascripts/rectangles/util/undo.js create mode 100644 public/assets/javascripts/rectangles/util/undostack.js (limited to 'test/09-test-undo.js') diff --git a/public/assets/javascripts/rectangles/engine/map/ui_editor.js b/public/assets/javascripts/rectangles/engine/map/ui_editor.js index c5c996c..9a557b9 100644 --- a/public/assets/javascripts/rectangles/engine/map/ui_editor.js +++ b/public/assets/javascripts/rectangles/engine/map/ui_editor.js @@ -58,8 +58,8 @@ Map.UI.Editor = function(map){ UndoStack.push({ type: "destroy-room", - prev: room.copy(), - next: { id: room.id }, + undo: room.copy(), + redo: { id: room.id }, }) Rooms.remove(room) @@ -164,8 +164,8 @@ Map.UI.Editor = function(map){ UndoStack.push({ type: "create-room", - prev: { id: room.id }, - next: room.copy() + undo: { id: room.id }, + redo: room.copy() }) app.tube("builder-pick-room", room) @@ -185,8 +185,8 @@ Map.UI.Editor = function(map){ UndoStack.push({ type: "update-room", - prev: oldState, - next: base.dragging.copy() + undo: oldState, + redo: base.dragging.copy() }) } @@ -211,8 +211,8 @@ Map.UI.Editor = function(map){ wheelTimeout = setTimeout(function(){ UndoStack.push({ type: "update-room", - prev: wheelState, - next: intersects[0].copy() + undo: wheelState, + redo: intersects[0].copy() }) Rooms.clipper.update() wheelState = null diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index fe5f037..c96885c 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -30,10 +30,15 @@ var Scenery = new function(){ } base.addNextToWall = function(wall){ - base.add(wall, base.nextMedia) + var media = base.add(wall, base.nextMedia) base.nextMedia = null + return media } - + + base.find = function(id){ + return base.list[id] || null + } + base.remove = function(id){ var media = base.list[id] delete base.list[id] diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index 94a4e52..ef9bc32 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -3,6 +3,7 @@ Scenery.move = function(base){ var x, y, z, bounds var dragging = false + var oldState this.bind = function(){ Scenery.mouse.bind_el(base.mx.el) @@ -23,6 +24,12 @@ Scenery.move = function(base){ function down (e, cursor){ if (e.target != base.mx.el) return; if (editor.permissions.destroy) { + UndoStack.push({ + type: 'destroy-scenery', + undo: base.serialize(), + redo: { id: base.id }, + }) + Scenery.remove(base.id) return } @@ -39,6 +46,7 @@ Scenery.move = function(base){ y = base.mx.y z = base.mx.z bounds = base.bounds + oldState = base.serialize() document.body.classList.add("dragging") } @@ -63,8 +71,20 @@ Scenery.move = function(base){ } function up (e, cursor){ + if (! dragging || ! oldState) return + dragging = false document.body.classList.remove("dragging") + + console.log("pushing", oldState, base.serialize()) + + UndoStack.push({ + type: 'update-scenery', + undo: oldState, + redo: base.serialize(), + }) + + oldState = null } function switch_wall (e, new_wall, cursor){ diff --git a/public/assets/javascripts/rectangles/engine/scenery/resize.js b/public/assets/javascripts/rectangles/engine/scenery/resize.js index df058bb..c5c754a 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/resize.js +++ b/public/assets/javascripts/rectangles/engine/scenery/resize.js @@ -7,6 +7,7 @@ Scenery.resize = new function(){ var x, y, z, bounds var dragging = false var dimensions, position, scale + var oldState var dots = [], dot, selected_dot @@ -54,7 +55,7 @@ Scenery.resize = new function(){ } // move all the dots to the object's current position - base.move_dots = function(){ + base.move_dots = function(){ x = obj.mx.x + sin(rotationY) * dot_distance_from_picture y = obj.mx.y z = obj.mx.z - cos(rotationY) * dot_distance_from_picture @@ -88,7 +89,7 @@ Scenery.resize = new function(){ // pick a new object to focus on and show the dots base.show = function(new_object) { - if (obj === new_object) return + // if (obj === new_object) return obj = new_object base.add_dots() @@ -151,6 +152,7 @@ Scenery.resize = new function(){ dimensions = obj.dimensions position = new vec3(obj.mx.x, obj.mx.y, obj.mx.z) scale = obj.mx.scale + oldState = obj.serialize() document.body.classList.add("dragging") } @@ -191,6 +193,13 @@ Scenery.resize = new function(){ if (! editor.permissions.resize) { return } obj.scale = obj.mx.ops.scale = obj.mx.scale obj.set_wall() + + UndoStack.push({ + type: 'update-scenery', + undo: oldState, + redo: obj.serialize(), + }) + document.body.classList.remove("dragging") } diff --git a/public/assets/javascripts/rectangles/engine/scenery/undo.js b/public/assets/javascripts/rectangles/engine/scenery/undo.js index 4bdb2c4..7798550 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/undo.js +++ b/public/assets/javascripts/rectangles/engine/scenery/undo.js @@ -3,22 +3,42 @@ { type: "create-scenery", undo: function(state){ + Scenery.remove(state.id) }, redo: function(state){ + Scenery.deserialize([ state ]) }, }, { type: "update-scenery", undo: function(state){ + var scenery = Scenery.find(state.id) + scenery.deserialize(state) + scenery.set_wall(Rooms.walls[ state.wall_id ]) + + if (editor.permissions.resize) { + Scenery.resize.show(scenery) + } }, redo: function(state){ + var scenery = Scenery.find(state.id) + scenery.deserialize(state) + scenery.set_wall(Rooms.walls[ state.wall_id ]) + + if (editor.permissions.resize) { + Scenery.resize.show(scenery) + Scenery.resize.rotate_dots() + Scenery.resize.move_dots() + } }, }, { type: "destroy-scenery", undo: function(state){ + Scenery.deserialize([ state ]) }, redo: function(state){ + Scenery.remove(state.id) }, }, diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 91e7c18..6e2c728 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -43,7 +43,13 @@ window.Wall = (function(){ // base.randomize_colors() // console.log(sidesToString(base.side)) if (Scenery.nextMedia) { - Scenery.addNextToWall(base) + var scenery = Scenery.addNextToWall(base) + + UndoStack.push({ + type: 'create-scenery', + undo: { id: scenery.id }, + redo: scenery.serialize(), + }) } else if (Scenery.nextWallpaper) { base.wallpaper() diff --git a/public/assets/javascripts/rectangles/util/undo.js b/public/assets/javascripts/rectangles/util/undo.js deleted file mode 100644 index dfc74dc..0000000 --- a/public/assets/javascripts/rectangles/util/undo.js +++ /dev/null @@ -1,52 +0,0 @@ -(function(){ - - var UndoStack = function(){ - this.debug = true - this.stack = [] - this.types = {} - this.pointer = -1 - } - UndoStack.prototype.push = function(action){ - this.pointer++ - this.stack[this.pointer] = action - this.purge() - } - UndoStack.prototype.purge = function(){ - if (this.stack.length-1 == this.pointer) return - this.stack.length = this.pointer+1 - } - UndoStack.prototype.undo = function(){ - if (this.pointer == -1) return false - var action = this.stack[this.pointer] - this.debug && console.log("undo", action.type) - this.types[ action.type ].undo(action.prev) - this.pointer-- - return this.pointer > -1 - } - UndoStack.prototype.redo = function(){ - if (this.pointer == this.stack.length-1) return false - this.pointer++ - var action = this.stack[this.pointer] - this.debug && console.log("redo", action.type) - this.types[ action.type ].redo(action.next) - return this.pointer < this.stack.length-1 - } - UndoStack.prototype.register = function(actionType){ - if (actionType.length) { - actionType.forEach(this.registerOne.bind(this)) - } - else { - this.registerOne(actionType) - } - } - UndoStack.prototype.registerOne = function(actionType){ - this.types[ actionType.type ] = actionType - } - if ('window' in this) { - window.UndoStack = new UndoStack - } - else { - module.exports = new UndoStack - } - -})() diff --git a/public/assets/javascripts/rectangles/util/undostack.js b/public/assets/javascripts/rectangles/util/undostack.js new file mode 100644 index 0000000..b93c79e --- /dev/null +++ b/public/assets/javascripts/rectangles/util/undostack.js @@ -0,0 +1,52 @@ +(function(){ + + var UndoStack = function(){ + this.debug = true + this.stack = [] + this.types = {} + this.pointer = -1 + } + UndoStack.prototype.push = function(action){ + this.pointer++ + this.stack[this.pointer] = action + this.purge() + } + UndoStack.prototype.purge = function(){ + if (this.stack.length-1 == this.pointer) return + this.stack.length = this.pointer+1 + } + UndoStack.prototype.undo = function(){ + if (this.pointer == -1) return false + var action = this.stack[this.pointer] + this.debug && console.log("undo", action.type) + this.types[ action.type ].undo(action.undo) + this.pointer-- + return this.pointer > -1 + } + UndoStack.prototype.redo = function(){ + if (this.pointer == this.stack.length-1) return false + this.pointer++ + var action = this.stack[this.pointer] + this.debug && console.log("redo", action.type) + this.types[ action.type ].redo(action.redo) + return this.pointer < this.stack.length-1 + } + UndoStack.prototype.register = function(actionType){ + if (actionType.length) { + actionType.forEach(this.registerOne.bind(this)) + } + else { + this.registerOne(actionType) + } + } + UndoStack.prototype.registerOne = function(actionType){ + this.types[ actionType.type ] = actionType + } + if ('window' in this) { + window.UndoStack = new UndoStack + } + else { + module.exports = new UndoStack + } + +})() diff --git a/public/assets/javascripts/ui/editor/MediaEditor.js b/public/assets/javascripts/ui/editor/MediaEditor.js index b9eb8fc..e3a8f2e 100644 --- a/public/assets/javascripts/ui/editor/MediaEditor.js +++ b/public/assets/javascripts/ui/editor/MediaEditor.js @@ -139,12 +139,14 @@ var MediaEditor = FormView.extend({ }, unbind: function(){ - this.scenery.mx.bound = false - this.scenery = null + if (this.scenery && this.scenery.mx) { + this.scenery.mx.bound = false + } + this.scenery = null }, destroy: function(){ - ConfirmModal.confirm("Are you sure you want to this media?", function(){ + ConfirmModal.confirm("Are you sure you want delete to this media?", function(){ var scenery = this.scenery this.hide() Scenery.remove(scenery.id) diff --git a/public/assets/javascripts/ui/lib/Parser.js b/public/assets/javascripts/ui/lib/Parser.js index 8867c0b..1cf0418 100644 --- a/public/assets/javascripts/ui/lib/Parser.js +++ b/public/assets/javascripts/ui/lib/Parser.js @@ -84,8 +84,6 @@ var Parser = { type: 'GET', url: 'http://vimeo.com/api/v2/video/' + id + '.json', success: function(result){ - console.log(result) - // embed_privacy: "nowhere" if (result.length == 0) { return done(id, "", 640, 360) } var res = result[0] if (res.embed_privacy != "anywhere") { diff --git a/test/09-test-undo.js b/test/09-test-undo.js index f774c04..dbca90e 100644 --- a/test/09-test-undo.js +++ b/test/09-test-undo.js @@ -34,22 +34,22 @@ describe('undo', function(){ UndoStack.push({ type: "demo", - prev: state, - next: "one" + undo: state, + redo: "one" }) state = "one" UndoStack.push({ type: "demo", - prev: state, - next: "two" + undo: state, + redo: "two" }) state = "two" UndoStack.push({ type: "demo", - prev: state, - next: "three" + undo: state, + redo: "three" }) state = "three" @@ -123,8 +123,8 @@ describe('undo', function(){ UndoStack.push({ type: "demo", - prev: state, - next: "four" + undo: state, + redo: "four" }) state = "four" diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index 087c0d7..dfb3a83 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -29,7 +29,7 @@ - + -- cgit v1.2.3-70-g09d2