diff options
| -rw-r--r-- | assets/javascripts/rectangles/_env.js | 2 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/map/draw.js | 2 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/map/ui.js | 43 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/rect.js | 34 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/wall.js | 13 |
5 files changed, 78 insertions, 16 deletions
diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js index ce19296..3f9d811 100644 --- a/assets/javascripts/rectangles/_env.js +++ b/assets/javascripts/rectangles/_env.js @@ -33,7 +33,7 @@ environment.init = function(){ app.movements.gravity(true) - $("#map").hide() + // $("#map").hide() builder.init() clipper.init() diff --git a/assets/javascripts/rectangles/map/draw.js b/assets/javascripts/rectangles/map/draw.js index 560cf38..8e63003 100644 --- a/assets/javascripts/rectangles/map/draw.js +++ b/assets/javascripts/rectangles/map/draw.js @@ -47,7 +47,7 @@ map.draw = new function(){ } base.mouse = function(mouse){ - var radius = 5 / map.zoom + var radius = 3 / map.zoom ctx.fillStyle = "rgba(255,0,0,0.4)"; ctx.beginPath(); diff --git a/assets/javascripts/rectangles/map/ui.js b/assets/javascripts/rectangles/map/ui.js index caa2a81..d442c99 100644 --- a/assets/javascripts/rectangles/map/ui.js +++ b/assets/javascripts/rectangles/map/ui.js @@ -1,11 +1,16 @@ +var height_min = 200, + height_max = 2000, + side_min = 10, + side_max = 5000, + resize_margin = 8 map.ui = new function(){ var base = this - var height_min = 200, - height_max = 2000 - base.creating = base.dragging = false + + + base.creating = base.dragging = base.resizing = false base.mouse = new mouse({ el: map.el, @@ -44,8 +49,10 @@ map.ui = new function(){ return r.focused = r.rect.contains(cursor.x.a, cursor.y.a) }) - if (intersects.length){ + 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 } else { base.creating = true @@ -65,7 +72,24 @@ map.ui = new function(){ cursor.x.b = ((cursor.x.b/w)+0.5) * map.bounds.a / map.zoom + map.center.a cursor.y.b = ((cursor.y.b/h)-0.5) * map.bounds.b / map.zoom - map.center.b - if (base.dragging) { + 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() } @@ -76,18 +100,21 @@ map.ui = new function(){ new_cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b) if (base.creating) { - if (cursor.height() != 0 && cursor.width() != 0) { + if (cursor.height() > side_min && cursor.width() > side_min) { cursor.x.abs().quantize(1) cursor.y.abs().quantize(1) clipper.add_room( cursor ) } } - if (base.dragging) { + if (base.resizing) { + base.dragging.rect.resize() + } + else if (base.dragging) { base.dragging.rect.translate() } - base.creating = base.dragging = false + base.creating = base.dragging = base.resizing = false } function mousewheel (e, val, delta){ diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js index a5ae9bf..aca9212 100644 --- a/assets/javascripts/rectangles/models/rect.js +++ b/assets/javascripts/rectangles/models/rect.js @@ -53,6 +53,24 @@ window.Rect = (function(){ this.translation.a = this.translation.b = 0 return this } + Rect.prototype.resize = function(translation, sides){ + var translation = translation || this.translation + sides = sides || translation.sides + + if (sides & LEFT) { + this.x.a += translation.a + } + if (sides & RIGHT) { + this.x.b += translation.a + } + if (sides & FRONT) { + this.y.a += translation.b + } + if (sides & BACK) { + this.y.b += translation.b + } + this.translation.a = this.translation.b = 0 + } Rect.prototype.contains = function(x,y){ return this.x.contains(x) && this.y.contains(y) } @@ -62,6 +80,22 @@ window.Rect = (function(){ Rect.prototype.intersects = function(r){ return this.x.intersects(r.x) && this.y.intersects(r.y) } + Rect.prototype.nearEdge = function (x, y, r) { + var edges = 0 + if (x < this.x.a+r) { + edges |= LEFT + } + else if (x > this.x.b-r) { + edges |= RIGHT + } + if (y < this.y.a+r) { + edges |= FRONT + } + else if (y > this.y.b-r) { + edges |= BACK + } + return edges + } Rect.prototype.width = function(){ return this.x.length() } Rect.prototype.height = function(){ return this.y.length() } Rect.prototype.toString = function(){ diff --git a/assets/javascripts/rectangles/models/wall.js b/assets/javascripts/rectangles/models/wall.js index 73b98b8..8a74782 100644 --- a/assets/javascripts/rectangles/models/wall.js +++ b/assets/javascripts/rectangles/models/wall.js @@ -1,6 +1,7 @@ -window.Wall = (function(){ +var painting_distance_from_wall = 8 + - var PAINTING_DISTANCE_FROM_WALL = 5 +window.Wall = (function(){ var Wall = function(opt){ this.id = opt.id @@ -77,18 +78,18 @@ window.Wall = (function(){ switch (this.side) { case FRONT: x = major_axis.midpoint() - z = minor_axis.a + PAINTING_DISTANCE_FROM_WALL + z = minor_axis.a + painting_distance_from_wall break case BACK: x = major_axis.midpoint() - z = minor_axis.b - PAINTING_DISTANCE_FROM_WALL + z = minor_axis.b - painting_distance_from_wall break case LEFT: - x = minor_axis.a + PAINTING_DISTANCE_FROM_WALL + x = minor_axis.a + painting_distance_from_wall z = major_axis.midpoint() break case RIGHT: - x = minor_axis.b - PAINTING_DISTANCE_FROM_WALL + x = minor_axis.b - painting_distance_from_wall z = major_axis.midpoint() break } |
