summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-08-12 18:49:01 -0400
committerJules Laplace <jules@okfoc.us>2014-08-12 18:49:01 -0400
commitb8b208d219cf782c39d054ad741724c6995a0b62 (patch)
tree7f77e4c10c3d86d7a80d6cf1c8ff20f9c1e393c3
parent838ccfe6c2125e464d3c95c6e222e7e762dc8fd2 (diff)
undo stuff working on rooms editor
-rw-r--r--public/assets/javascripts/rectangles/engine/map/ui_editor.js43
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/undo.js6
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js4
-rw-r--r--public/assets/javascripts/rectangles/models/room.js6
-rw-r--r--public/assets/javascripts/rectangles/util/undo.js3
5 files changed, 45 insertions, 17 deletions
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
}