summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-08-13 16:12:02 -0400
committerJules Laplace <jules@okfoc.us>2014-08-13 16:21:26 -0400
commit90cb5b343f3d56372f9b43faf215ed80dd879fe1 (patch)
treedfac1c586b713a70c9bd5ffce01f93a46a256e75
parent14228e82f4836b1bffa3389aa9e8d12959aa3810 (diff)
undo for scenery stuff
-rw-r--r--public/assets/javascripts/rectangles/engine/map/ui_editor.js16
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/_scenery.js9
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/move.js20
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/resize.js13
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/undo.js20
-rw-r--r--public/assets/javascripts/rectangles/models/wall.js8
-rw-r--r--public/assets/javascripts/rectangles/util/undostack.js (renamed from public/assets/javascripts/rectangles/util/undo.js)4
-rw-r--r--public/assets/javascripts/ui/editor/MediaEditor.js8
-rw-r--r--public/assets/javascripts/ui/lib/Parser.js2
-rw-r--r--test/09-test-undo.js16
-rw-r--r--views/partials/scripts.ejs2
11 files changed, 89 insertions, 29 deletions
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/undostack.js
index dfc74dc..b93c79e 100644
--- a/public/assets/javascripts/rectangles/util/undo.js
+++ b/public/assets/javascripts/rectangles/util/undostack.js
@@ -19,7 +19,7 @@
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.types[ action.type ].undo(action.undo)
this.pointer--
return this.pointer > -1
}
@@ -28,7 +28,7 @@
this.pointer++
var action = this.stack[this.pointer]
this.debug && console.log("redo", action.type)
- this.types[ action.type ].redo(action.next)
+ this.types[ action.type ].redo(action.redo)
return this.pointer < this.stack.length-1
}
UndoStack.prototype.register = function(actionType){
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 @@
<script type="text/javascript" src="/assets/javascripts/rectangles/util/wheel.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/util/mouse.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/util/keys.js"></script>
-<script type="text/javascript" src="/assets/javascripts/rectangles/util/undo.js"></script>
+<script type="text/javascript" src="/assets/javascripts/rectangles/util/undostack.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/models/vec2.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/models/vec3.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/models/mat4.js"></script>