Scenery.move = function(base){ var x, y, z, position, dimension, bounds var dragging = false, moved = false var oldState this.bind = function(){ Scenery.mouse.bind_el(base.mx.el) Scenery.mouse.on("down", down) Scenery.mouse.on("enter", switch_wall) Scenery.mouse.on("drag", drag) Scenery.mouse.on("up", up) } this.unbind = function(){ base.focused = false Scenery.mouse.unbind_el(base.mx.el) Scenery.mouse.off("down", down) Scenery.mouse.off("enter", switch_wall) Scenery.mouse.off("drag", drag) Scenery.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) { Scenery.resize.move_dots() } } function up (e, cursor){ if (! dragging || ! oldState) return if (moved) { UndoStack.push({ type: 'update-scenery', undo: oldState, redo: base.serialize(), }) // TODO: watch individual scenery 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) { Scenery.resize.rotate_dots() Scenery.resize.move_dots() } cursor.x.a = cursor.x.b //var delta = cursor.delta() drag(e, cursor) } return this }