diff options
Diffstat (limited to 'public/assets/javascripts/rectangles/engine')
4 files changed, 68 insertions, 10 deletions
diff --git a/public/assets/javascripts/rectangles/engine/map/tools/ortho.js b/public/assets/javascripts/rectangles/engine/map/tools/ortho.js index 6ced728..374c822 100644 --- a/public/assets/javascripts/rectangles/engine/map/tools/ortho.js +++ b/public/assets/javascripts/rectangles/engine/map/tools/ortho.js @@ -48,7 +48,7 @@ var OrthoPolylineTool = MapTool.extend(function (base) { } else { map.ui.placing = true - shapes.workline = new Polyline () + shapes.workline = new OrthoPolyline () shapes.workline.add(p) first_point = prev_point = p horizontal = false diff --git a/public/assets/javascripts/rectangles/engine/shapes/ortho.js b/public/assets/javascripts/rectangles/engine/shapes/ortho.js new file mode 100644 index 0000000..8a8f928 --- /dev/null +++ b/public/assets/javascripts/rectangles/engine/shapes/ortho.js @@ -0,0 +1,41 @@ +// An OrthoPolyline is a Polyline where all angles are 90 degrees. + +var OrthoPolyline = Polyline.extend(function(base){ + var exports = {} + exports.type = function(){ + return "ortho" + } + exports.instantiate = function(){ + return new OrthoPolyline + } + exports.canCloseWith = function(p){ + return (this.points.length > 2 && this.points[0].distanceTo( p ) < 10/map.zoom) + } + exports.draw = function(ctx, fillStyle, strokeStyle){ + var points = this.points + if (! points.length) return + if (points.length == 1) { + ctx.fillStyle = "#f80" + map.draw.dot_at(this.points[0].a, points[0].b, 5) + } + if (points.length > 1) { + ctx.fillStyle = fillStyle + ctx.strokeStyle = strokeStyle + ctx.lineWidth = 2 / map.zoom + ctx.beginPath() + ctx.moveTo(points[0].a, points[0].b) + points.forEach(function(point, i){ + i && ctx.lineTo(point.a, point.b) + }) + strokeStyle && ctx.stroke() + if (! map.ui.placing || this.closed) { + fillStyle && ctx.fill() + } + } + } + exports.close = function(){ + this.points[this.points.length] = this.points[0] + this.closed = true + } + return exports +}) diff --git a/public/assets/javascripts/rectangles/engine/shapes/polyline.js b/public/assets/javascripts/rectangles/engine/shapes/polyline.js index fc6cad7..65e22ad 100644 --- a/public/assets/javascripts/rectangles/engine/shapes/polyline.js +++ b/public/assets/javascripts/rectangles/engine/shapes/polyline.js @@ -9,6 +9,12 @@ var Polyline = Fiber.extend(function(base){ this.mx_points = [] this.closed = false } + exports.type = function(){ + return "polyline" + } + exports.instantiate = function(){ + return new Polyline + } exports.add = function(p){ this.points.push( p ) this.mx_points.push( new MX.Point(p) ) @@ -25,19 +31,19 @@ var Polyline = Fiber.extend(function(base){ exports.getHeadAtIndex = function(index){ if (index == 0) { return null } if (index == this.points.length-1) { return this.clone() } - var head = new Polyline() + var head = this.instantiate() head.points = this.points.slice(0, index+1) return head } exports.getTailAtIndex = function(index){ if (index == this.points.length-1) { return null } if (index == 0) { return this.clone() } - var tail = new Polyline() + var tail = this.instantiate() tail.points = this.points.slice(index, this.points.length) return tail } exports.clone = function(){ - var clone = new Polyline() + var clone = this.instantiate() clone.points = this.points.concat() } exports.hasPointNear = function(p){ @@ -148,10 +154,13 @@ var Polyline = Fiber.extend(function(base){ return segments } exports.serialize = function(){ - return this.points.map(function(point){ return [point.a, point.b] }) + return { + type: this.type(), + points: this.points.map(function(point){ return [point.a, point.b] }), + } } - exports.deserialize = function(points){ - this.points = points.map(function(point){ return new vec2(point[0], point[1]) }) + exports.deserialize = function(data){ + this.points = (data.points || data).map(function(point){ return new vec2(point[0], point[1]) }) } exports.reset = function(){ this.mx_points.forEach(function(mx){ scene.remove(mx) }) diff --git a/public/assets/javascripts/rectangles/engine/shapes/shapelist.js b/public/assets/javascripts/rectangles/engine/shapes/shapelist.js index 75ecae6..e5a70fb 100644 --- a/public/assets/javascripts/rectangles/engine/shapes/shapelist.js +++ b/public/assets/javascripts/rectangles/engine/shapes/shapelist.js @@ -87,9 +87,17 @@ var ShapeList = Fiber.extend(function(base){ }) } exports.deserialize = function(data){ - data && data.forEach(function(points){ - var line = new Polyline() - line.deserialize(points) + data && data.forEach(function(shape_data){ + var line + switch (shape_data.type) { + case 'ortho': + line = new OrthoPolyline() + break + default: + line = new Polyline() + break + } + line.deserialize(shape_data) shapes.add(line) }.bind(this)) } |
