Map.UI = Map.UI || {} Map.UI.Editor = function(map){ var base = this base.creating = base.dragging = base.resizing = false base.mouse = new mouse({ el: map.el, down: down, move: move, drag: drag, up: up, rightclick: rightclick, }) base.wheel = new wheel({ el: map.el, update: mousewheel, }) base.permissions = new Permissions({ create: true, move: true, resize: true, destroy: false, }) // 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) if (e.ctrlKey || e.which === 3) { cursor.quantize(1/map.zoom) map.center.a = cursor.x.a map.center.b = -cursor.y.a cursor.x.b = cursor.x.a cursor.y.b = cursor.y.a base.mouse.down = false e.preventDefault() e.stopPropagation() return } var intersects = Rooms.filter(function(r){ return r.focused = r.rect.contains(cursor.x.a, cursor.y.a) }) if (intersects.length && (base.permissions.destroy || e.altKey)) { base.mouse.down = false room = intersects[0] UndoStack.push({ type: "destroy-room", undo: room.copy(), redo: { id: room.id }, }) Rooms.remove(room) app.tube("builder-destroy-room", room) return } else if (intersects.length) { base.dragging = intersects[0] 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 } if (e.shiftKey && base.dragging) { base.dragging.rect.quantize(10/map.zoom) } } 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) { cursor.x.b = ((cursor.x.b/map.dimensions.a)+0.5) * map.dimensions.a / map.zoom + map.center.a cursor.y.b = ((cursor.y.b/map.dimensions.b)-0.5) * map.dimensions.b / map.zoom - map.center.b if (base.resizing) { var x_length = base.dragging.rect.x.length(), y_length = base.dragging.rect.y.length() if (base.resizing & LEFT) { base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), x_length - side_max, x_length - side_min ) } if (base.resizing & RIGHT) { base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), side_min - x_length, side_max - x_length ) } if (base.resizing & FRONT) { base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), y_length - side_max, y_length - side_min ) } if (base.resizing & BACK) { base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), side_min - y_length, side_max - y_length ) } } else if (base.dragging) { base.dragging.rect.translation.a = cursor.x.magnitude() base.dragging.rect.translation.b = cursor.y.magnitude() } } function up (e, cursor, new_cursor) { new_cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a) new_cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b) if (base.creating) { if (cursor.height() > side_min && cursor.width() > side_min) { cursor.x.abs().quantize(1) cursor.y.abs().quantize(1) var room = Rooms.add_with_rect( cursor ) Rooms.rebuild() UndoStack.push({ type: "create-room", undo: { id: room.id }, redo: room.copy() }) Rooms.rebuild() app.tube("builder-pick-room", room) } } 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", undo: oldState, redo: base.dragging.copy() }) Rooms.rebuild() } var intersects = Rooms.filter(function(r){ return r.focused = r.rect.contains(cursor.x.a, cursor.y.a) }) if (! intersects.length) { app.tube("builder-pick-nothing") } base.creating = base.dragging = base.resizing = false } var wheelState, wheelTimeout function mousewheel (e, deltaY, deltaX){ var cursor = base.mouse.cursor var intersects = Rooms.filter(function(r){ return r.focused = r.rect.contains(cursor.x.a, cursor.y.a) }) if (intersects.length) { wheelState = wheelState || intersects[0].copy() intersects[0].height = clamp( ~~(intersects[0].height - deltaY), height_min, height_max ) clearTimeout(wheelTimeout) wheelTimeout = setTimeout(function(){ UndoStack.push({ type: "update-room", undo: wheelState, redo: intersects[0].copy() }) Rooms.rebuild() wheelState = null }, 500) } else { map.set_zoom(map.zoom_exponent - deltaY/20) } } function rightclick (e){ } }