From 0bf50fee4d35467c5335cbaf6c1ca3b7ae2dd1e4 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 12 Aug 2014 16:01:42 -0400 Subject: different ui on map editor --- .../javascripts/rectangles/engine/map/ui_editor.js | 46 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'public/assets/javascripts/rectangles/engine/map/ui_editor.js') diff --git a/public/assets/javascripts/rectangles/engine/map/ui_editor.js b/public/assets/javascripts/rectangles/engine/map/ui_editor.js index 577ea32..016a8ad 100644 --- a/public/assets/javascripts/rectangles/engine/map/ui_editor.js +++ b/public/assets/javascripts/rectangles/engine/map/ui_editor.js @@ -56,18 +56,15 @@ Map.UI.Editor = function(map){ app.tube("builder-destroy-room", intersects[0]) return } - else if (intersects.length && (base.permissions.move || base.permissions.resize)) { + else if (intersects.length) { base.dragging = intersects[0] - base.resizing = base.permissions.resize && base.dragging.rect.nearEdge(cursor.x.a, cursor.y.a, resize_margin / map.zoom) + base.resizing = base.dragging.rect.nearEdge(cursor.x.a, cursor.y.a, resize_margin / map.zoom) base.dragging.rect.translation.sides = base.resizing app.tube("builder-pick-room", intersects[0]) } else if (base.permissions.create) { base.creating = true } - else if (intersects.length) { - app.tube("builder-pick-room", intersects[0]) - } if (e.shiftKey && base.dragging) { base.dragging.rect.quantize(10/map.zoom) @@ -77,6 +74,45 @@ Map.UI.Editor = function(map){ function move (e, cursor) { cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a) cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b) + + var intersects = Rooms.filter(function(r){ + return r.rect.contains(cursor.x.a, cursor.y.a) + }) + + if (base.permissions.destroy) { + map.el.className = "destroy" + } + else if (intersects.length) { + var edges = intersects[0].rect.nearEdge(cursor.x.a, cursor.y.a, resize_margin / map.zoom) + switch (edges) { + case FRONT_LEFT: + case BACK_RIGHT: + map.el.className = "nesw-resize" + break + + case FRONT_RIGHT: + case BACK_LEFT: + map.el.className = "nwse-resize" + break + + case FRONT: + case BACK: + map.el.className = "ns-resize" + break + + case LEFT: + case RIGHT: + map.el.className = "ew-resize" + break + + default: + map.el.className = "move" + break + } + } + else { + map.el.className = "" + } } function drag (e, cursor) { -- cgit v1.2.3-70-g09d2 From 838ccfe6c2125e464d3c95c6e222e7e762dc8fd2 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 12 Aug 2014 18:19:33 -0400 Subject: undo stack for undo/redo --- public/assets/javascripts/rectangles/_env.js | 14 +++++++++ .../javascripts/rectangles/engine/map/ui_editor.js | 21 +++++++++++-- .../javascripts/rectangles/engine/scenery/undo.js | 34 +++++++++++++++++----- .../assets/javascripts/rectangles/models/rect.js | 4 +++ .../assets/javascripts/rectangles/models/room.js | 8 +++++ .../assets/javascripts/rectangles/models/vec2.js | 4 +++ .../assets/javascripts/rectangles/models/wall.js | 2 -- public/assets/javascripts/rectangles/util/keys.js | 6 ++-- .../javascripts/rectangles/util/permissions.js | 13 ++++++--- .../javascripts/ui/builder/BuilderToolbar.js | 9 +++++- views/partials/scripts.ejs | 2 ++ 11 files changed, 98 insertions(+), 19 deletions(-) (limited to 'public/assets/javascripts/rectangles/engine/map/ui_editor.js') diff --git a/public/assets/javascripts/rectangles/_env.js b/public/assets/javascripts/rectangles/_env.js index 3cfe969..4b14a21 100644 --- a/public/assets/javascripts/rectangles/_env.js +++ b/public/assets/javascripts/rectangles/_env.js @@ -35,6 +35,20 @@ environment.init = function(){ zoom: -4.8 }) } + + keys.on("z", function(e){ + e.preventDefault() + if (e.ctrlKey || e.metaKey) { + if (e.shiftKey) { + var canRedo = UndoStack.redo() + console.log("can redo", canRedo) + } + else { + var canUndo = UndoStack.undo() + console.log("can undo", canUndo) + } + } + }) } environment.update = function(t){ map.update() diff --git a/public/assets/javascripts/rectangles/engine/map/ui_editor.js b/public/assets/javascripts/rectangles/engine/map/ui_editor.js index 016a8ad..4b5f784 100644 --- a/public/assets/javascripts/rectangles/engine/map/ui_editor.js +++ b/public/assets/javascripts/rectangles/engine/map/ui_editor.js @@ -30,6 +30,7 @@ Map.UI.Editor = function(map){ // function down (e, cursor){ + var room cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a) cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b) @@ -52,8 +53,17 @@ Map.UI.Editor = function(map){ if (intersects.length && base.permissions.destroy) { base.mouse.down = false - Rooms.remove(intersects[0]) - app.tube("builder-destroy-room", intersects[0]) + + room = intersects[0] + + UndoStack.push({ + type: "destroy-room", + prev: room.clone(), + next: { id: room.id }, + }) + + Rooms.remove(room) + app.tube("builder-destroy-room", room) return } else if (intersects.length) { @@ -151,6 +161,13 @@ Map.UI.Editor = function(map){ cursor.x.abs().quantize(1) cursor.y.abs().quantize(1) var room = Rooms.add_with_rect( cursor ) + + UndoStack.push({ + type: "create-room", + prev: { id: room.id }, + next: room.clone() + }) + app.tube("builder-pick-room", room) } } diff --git a/public/assets/javascripts/rectangles/engine/scenery/undo.js b/public/assets/javascripts/rectangles/engine/scenery/undo.js index fb221f5..b10a101 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/undo.js +++ b/public/assets/javascripts/rectangles/engine/scenery/undo.js @@ -25,24 +25,44 @@ // { - type: "create-rectangle", - undo: function(state){ + type: "create-room", + undo: function(room){ + Rooms.remove(room) + Rooms.clipper.update() }, - redo: function(state){ + redo: function(room){ + Rooms.add(room) + Rooms.clipper.update() + app.tube("builder-pick-room", room) }, }, { - type: "update-rectangle", + type: "update-room", undo: function(state){ + var room = Rooms.list[state.id] + room.rect.assign( state.rect ) + room.height = state.height + Rooms.clipper.update() + app.tube("builder-pick-room", room) }, redo: function(state){ + var room = Rooms.list[state.id] + room.rect.assign( state.rect ) + room.height = state.height + Rooms.clipper.update() + app.tube("builder-pick-room", room) }, }, { - type: "destroy-rectangle", - undo: function(state){ + type: "destroy-room", + undo: function(room){ + Rooms.add(room) + Rooms.clipper.update() + app.tube("builder-pick-room", room) }, - redo: function(state){ + redo: function(room){ + Rooms.remove(room) + Rooms.clipper.update() }, }, diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 58469bc..0642a65 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -39,6 +39,10 @@ Rect.prototype.clone = function(){ return new Rect( this.x.clone(), this.y.clone() ) } + Rect.prototype.assign = function(r) { + this.x.copy(r.x) + this.y.copy(r.y) + } Rect.prototype.center = function(){ return new vec2(this.x.midpoint(), this.y.midpoint()) } diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index c2850ba..6c0e1bb 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -40,6 +40,14 @@ this.focused = false } + Room.prototype.clone = function(){ + return new Room ({ + id: this.id, + rect: this.rect.clone(), + height: this.height, + }) + } + Room.prototype.toString = function(){ return this.rect.toString() } diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 104c6f7..214feb9 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -17,6 +17,10 @@ vec2.prototype.clone = function(){ return new vec2(this.a, this.b) } + vec2.prototype.assign = function(v){ + this.a = v.a + this.b = v.b + } vec2.prototype.abs = function(){ if (this.b < this.a) { this.invert() diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 027d5f5..91e7c18 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -146,8 +146,6 @@ window.Wall = (function(){ if (shouldFlip) { sortedWalls = sortedWalls.reverse() } - -console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) var len = sortedWalls.length diff --git a/public/assets/javascripts/rectangles/util/keys.js b/public/assets/javascripts/rectangles/util/keys.js index 5a5c9d2..62d763f 100644 --- a/public/assets/javascripts/rectangles/util/keys.js +++ b/public/assets/javascripts/rectangles/util/keys.js @@ -19,7 +19,7 @@ var keys = (function(){ break; default: if (keys.debug) console.log(key) - base.tube(key) + base.tube(key, e) break; } }) @@ -158,8 +158,8 @@ var keys = (function(){ 'backslash' : '220', 'closebracket' : '221', 'single_quote' : '222' - } - var KEY_NAMES = invert_hash(KEYCODES) + }, + KEY_NAMES = invert_hash(KEYCODES) return base })() \ No newline at end of file diff --git a/public/assets/javascripts/rectangles/util/permissions.js b/public/assets/javascripts/rectangles/util/permissions.js index 394b7d6..1b5a1b5 100644 --- a/public/assets/javascripts/rectangles/util/permissions.js +++ b/public/assets/javascripts/rectangles/util/permissions.js @@ -26,14 +26,12 @@ Permissions.prototype.assign = function (key, state) { Permissions.prototype.add = function (key) { var base = this - base[op] = true - return state + base[key] = true } Permissions.prototype.remove = function (key) { var base = this - base[op] = true - return state + base[key] = true } Permissions.prototype.clear = function () { @@ -42,3 +40,10 @@ Permissions.prototype.clear = function () { base[op] = false }) } + +Permissions.prototype.log = function () { + var base = this + base.keys.forEach(function(op){ + console.log(op, base[op]) + }) +} \ No newline at end of file diff --git a/public/assets/javascripts/ui/builder/BuilderToolbar.js b/public/assets/javascripts/ui/builder/BuilderToolbar.js index 239b05e..2eb7590 100644 --- a/public/assets/javascripts/ui/builder/BuilderToolbar.js +++ b/public/assets/javascripts/ui/builder/BuilderToolbar.js @@ -14,7 +14,14 @@ var BuilderToolbar = View.extend({ initialize: function(opt){ this.parent = opt.parent - map.ui.permissions.toggle() + this.resetPermissions() + }, + + resetPermissions: function(){ + map.ui.permissions.clear() + map.ui.permissions.add("create") + map.ui.permissions.add("move") + map.ui.permissions.add("resize") }, toggleMap: function(){ diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index a1f51ef..b63d1bf 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -28,6 +28,7 @@ + @@ -44,6 +45,7 @@ + -- cgit v1.2.3-70-g09d2 From b8b208d219cf782c39d054ad741724c6995a0b62 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 12 Aug 2014 18:49:01 -0400 Subject: undo stuff working on rooms editor --- .../javascripts/rectangles/engine/map/ui_editor.js | 45 +++++++++++++++++----- .../javascripts/rectangles/engine/scenery/undo.js | 6 +-- .../assets/javascripts/rectangles/models/rect.js | 4 +- .../assets/javascripts/rectangles/models/room.js | 6 +-- public/assets/javascripts/rectangles/util/undo.js | 3 ++ 5 files changed, 46 insertions(+), 18 deletions(-) (limited to 'public/assets/javascripts/rectangles/engine/map/ui_editor.js') diff --git a/public/assets/javascripts/rectangles/engine/map/ui_editor.js b/public/assets/javascripts/rectangles/engine/map/ui_editor.js index 4b5f784..c5c996c 100644 --- a/public/assets/javascripts/rectangles/engine/map/ui_editor.js +++ b/public/assets/javascripts/rectangles/engine/map/ui_editor.js @@ -58,7 +58,7 @@ Map.UI.Editor = function(map){ UndoStack.push({ type: "destroy-room", - prev: room.clone(), + prev: room.copy(), next: { id: room.id }, }) @@ -165,23 +165,36 @@ Map.UI.Editor = function(map){ UndoStack.push({ type: "create-room", prev: { id: room.id }, - next: room.clone() + next: room.copy() }) app.tube("builder-pick-room", room) } } - if (base.resizing) { - base.dragging.rect.resize() - } - else if (base.dragging) { - base.dragging.rect.translate() - } - + if (base.resizing || base.dragging) { + + var oldState = base.dragging.copy() + + if (base.resizing) { + base.dragging.rect.resize() + } + else if (base.dragging) { + base.dragging.rect.translate() + } + + UndoStack.push({ + type: "update-room", + prev: oldState, + next: base.dragging.copy() + }) + } + base.creating = base.dragging = base.resizing = false } + var wheelState, wheelTimeout + function mousewheel (e, val, delta){ var cursor = base.mouse.cursor @@ -190,8 +203,20 @@ Map.UI.Editor = function(map){ }) if (intersects.length) { + wheelState = wheelState || intersects[0].copy() + intersects[0].height = clamp( ~~(intersects[0].height - delta), height_min, height_max ) - Rooms.clipper.update() + + clearTimeout(wheelTimeout) + wheelTimeout = setTimeout(function(){ + UndoStack.push({ + type: "update-room", + prev: wheelState, + next: intersects[0].copy() + }) + Rooms.clipper.update() + wheelState = null + }, 500) } else { map.set_zoom(map.zoom_exponent - delta/20) diff --git a/public/assets/javascripts/rectangles/engine/scenery/undo.js b/public/assets/javascripts/rectangles/engine/scenery/undo.js index b10a101..4bdb2c4 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/undo.js +++ b/public/assets/javascripts/rectangles/engine/scenery/undo.js @@ -31,7 +31,7 @@ Rooms.clipper.update() }, redo: function(room){ - Rooms.add(room) + Rooms.add(new Room(room)) Rooms.clipper.update() app.tube("builder-pick-room", room) }, @@ -56,7 +56,7 @@ { type: "destroy-room", undo: function(room){ - Rooms.add(room) + Rooms.add(new Room(room)) Rooms.clipper.update() app.tube("builder-pick-room", room) }, @@ -77,4 +77,4 @@ }, ]) -})() \ No newline at end of file +})() diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 0642a65..3f94740 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -40,8 +40,8 @@ return new Rect( this.x.clone(), this.y.clone() ) } Rect.prototype.assign = function(r) { - this.x.copy(r.x) - this.y.copy(r.y) + this.x.assign(r.x) + this.y.assign(r.y) } Rect.prototype.center = function(){ return new vec2(this.x.midpoint(), this.y.midpoint()) diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index 6c0e1bb..aa18f6d 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -40,12 +40,12 @@ this.focused = false } - Room.prototype.clone = function(){ - return new Room ({ + Room.prototype.copy = function(){ + return { id: this.id, rect: this.rect.clone(), height: this.height, - }) + } } Room.prototype.toString = function(){ diff --git a/public/assets/javascripts/rectangles/util/undo.js b/public/assets/javascripts/rectangles/util/undo.js index 5d8593c..dfc74dc 100644 --- a/public/assets/javascripts/rectangles/util/undo.js +++ b/public/assets/javascripts/rectangles/util/undo.js @@ -1,6 +1,7 @@ (function(){ var UndoStack = function(){ + this.debug = true this.stack = [] this.types = {} this.pointer = -1 @@ -17,6 +18,7 @@ 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 @@ -25,6 +27,7 @@ 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 } -- 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 'public/assets/javascripts/rectangles/engine/map/ui_editor.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