summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/sculpture/move.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/engine/sculpture/move.js')
-rw-r--r--public/assets/javascripts/rectangles/engine/sculpture/move.js158
1 files changed, 158 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/engine/sculpture/move.js b/public/assets/javascripts/rectangles/engine/sculpture/move.js
new file mode 100644
index 0000000..b5a4b40
--- /dev/null
+++ b/public/assets/javascripts/rectangles/engine/sculpture/move.js
@@ -0,0 +1,158 @@
+
+Sculpture.move = function(base){
+
+ var x, y, z, position, dimension, bounds
+ var dragging = false, moved = false
+ var oldState
+
+ this.bind = function(){
+ Sculpture.mouse.bind_el(base.mx.el)
+ Sculpture.mouse.on("down", down)
+ Sculpture.mouse.on("enter", switch_wall)
+ Sculpture.mouse.on("drag", drag)
+ Sculpture.mouse.on("up", up)
+ }
+
+ this.unbind = function(){
+ base.focused = false
+ Sculpture.mouse.unbind_el(base.mx.el)
+ Sculpture.mouse.off("down", down)
+ Sculpture.mouse.off("enter", switch_wall)
+ Sculpture.mouse.off("drag", drag)
+ Sculpture.mouse.off("up", up)
+ }
+
+ function down (e, cursor){
+ if (e.target != base.mx.el && (e.target != base.mx.overlay)) return;
+ if (editor.permissions.destroy) {
+ base.remove()
+ return
+ }
+
+ // load the modal
+ app.controller.pick(base)
+
+ if (! base.focused) {
+ e.clickAccepted = false
+ base.focused = true
+ return
+ }
+ if (! (editor.permissions.move || editor.permissions.resize) ) {
+ e.clickAccepted = false
+ return
+ }
+ dragging = true
+ moved = false
+ x = base.mx.x
+ y = base.mx.y
+ z = base.mx.z
+
+ bounds = base.bounds
+ dimension = base.dimensions
+ position = base.wall.mxToPosition( base.mx, dimension )
+
+ oldState = base.serialize()
+ document.body.classList.add("dragging")
+ }
+
+ function drag (e, cursor){
+ if (! dragging) return
+
+ moved = true
+
+ var flipX = base.wall.side & (FRONT | RIGHT)
+
+ var delta = cursor.delta()
+ delta.mul( cursor_amp ) // TODO: this should be proportional to your distance from the wall
+ if (flipX) { delta.a *= -1 }
+ delta.b *= -1
+
+ var new_bounds = base.wall.surface.translate( bounds, dimension, position, delta )
+ if (new_bounds) bounds = new_bounds
+ if (flipX) { delta.a *= -1 }
+
+ base.mx.y = position.b + delta.b + dimension.b / 2
+ switch (base.wall.side) {
+ case FRONT:
+ case BACK:
+ base.mx.x = position.a + delta.a * cos(base.wall.rotationY) + dimension.a / 2
+ break
+ case LEFT:
+ case RIGHT:
+ base.mx.z = position.a + delta.a * sin(base.wall.rotationY) + dimension.a / 2
+ break
+ }
+
+ if (editor.permissions.resize) {
+ Sculpture.resize.move_dots()
+ }
+ }
+
+ function up (e, cursor){
+ if (! dragging || ! oldState) return
+
+ if (moved) {
+ UndoStack.push({
+ type: 'update-sculpture',
+ undo: oldState,
+ redo: base.serialize(),
+ })
+
+ // TODO: watch individual sculpture object here
+ Minotaur.watch( app.router.editorView.settings )
+ }
+
+ dragging = moved = false
+ oldState = null
+ document.body.classList.remove("dragging")
+ }
+
+ function switch_wall (e, target, cursor){
+ if (! dragging) return
+ if (target.wall.id == base.wall.id) return
+
+ var old_wall_side = base.wall.side
+ var wall_group = old_wall_side | target.wall.side
+ var new_bounds = target.wall.surface.bounds_at_index_with_dimensions(target.index || 0, dimension)
+
+ if (! new_bounds) return
+
+ base.wall = target.wall
+ base.bounds = bounds = new_bounds
+
+ position.a = bounds.x.midpoint() - dimension.a / 2
+
+ if (old_wall_side !== target.wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) {
+ switch (old_wall_side) {
+ case FRONT:
+ position.a = bounds.x.a + dimension.a / 2
+ break
+ case BACK:
+ position.a = bounds.x.b - dimension.a / 2
+ break
+ case LEFT:
+ position.a = bounds.x.a + dimension.a / 2
+ break
+ case RIGHT:
+ position.a = bounds.x.b - dimension.a / 2
+ break
+ }
+ }
+
+ var mx_delta = base.wall.positionToMx( position, dimension )
+ base.mx.move(mx_delta)
+
+ if (editor.permissions.resize) {
+ Sculpture.resize.rotate_dots()
+ Sculpture.resize.move_dots()
+ }
+
+ cursor.x.a = cursor.x.b
+ //var delta = cursor.delta()
+ drag(e, cursor)
+
+ }
+
+ return this
+
+} \ No newline at end of file