diff --git a/src/Graphic/Graphic.coffee b/src/Graphic/Graphic.coffee index d8dc525..d7b8799 100644 --- a/src/Graphic/Graphic.coffee +++ b/src/Graphic/Graphic.coffee @@ -34,7 +34,7 @@ class Graphic.Element viewMatrix: highlight(graphic): A function that takes in a graphic and returns either - a color or null. + a {color, lineWidth} object or null. ### throw "Not implemented" @@ -94,7 +94,31 @@ class Graphic.Group extends Graphic.Element class Graphic.Anchor extends Graphic.Element + render: (opts) -> + @highlightIfNecessary(opts) + + hitDetect: (opts) -> + {x, y, viewMatrix} = opts + [myX, myY] = viewMatrix.compose(@matrix).origin() + distSq = (x - myX) * (x - myX) + (y - myY) * (y - myY) + if distSq <= @highlightRadius * @highlightRadius + return [@particularElement] + else + return null + + highlightIfNecessary: ({highlight, ctx, viewMatrix}) -> + return unless highlight + highlightSpec = highlight(this) + if highlightSpec + ctx.save() + ctx.beginPath(); + [myX, myY] = viewMatrix.compose(@matrix).origin() + ctx.arc(myX, myY, @highlightRadius, 0, 2 * Math.PI, false) + ctx.fillStyle = highlightSpec.color + ctx.fill() + ctx.restore() + highlightRadius: 5 @@ -104,7 +128,19 @@ class Graphic.Path extends Graphic.Element @performPaintOps(opts) @highlightIfNecessary(opts) + # Render anchor highlights, if necessary + for childGraphic in @childGraphics + childGraphic.render(opts) + hitDetect: (opts) -> + # Check for anchor hits + latestHit = null + for childGraphic in @childGraphics + latestHit = childGraphic.hitDetect(opts) ? latestHit + if latestHit + console.log('path hit anchor!') + return latestHit.concat(@particularElement) + opts.ctx = getDummyCanvasCtx() {ctx, x, y} = opts @buildPath(opts) diff --git a/src/Model/Element.coffee b/src/Model/Element.coffee index c66267b..5d5d132 100644 --- a/src/Model/Element.coffee +++ b/src/Model/Element.coffee @@ -170,6 +170,8 @@ module.exports = Element = Node.createVariant matrix: -> matrix = new Util.Matrix() + for position in @childrenOfType(Model.Position) + matrix = matrix.compose(position.matrix()) for transform in @childrenOfType(Model.Transform) matrix = matrix.compose(transform.matrix()) return matrix diff --git a/src/Model/Model.coffee b/src/Model/Model.coffee index 74d3b39..7cd7a2e 100644 --- a/src/Model/Model.coffee +++ b/src/Model/Model.coffee @@ -92,6 +92,28 @@ Model.Transform.addChildren [ ] +Model.Position = Model.Component.createVariant + label: "Position" + matrix: -> + {x, y} = @getAttributesValuesByName() + return Util.Matrix.naturalConstruct(x, y, 1, 1, 0) + defaultAttributesToChange: -> + {x, y} = @getAttributesByName() + return [x, y] + controllableAttributes: -> + {x, y} = @getAttributesByName() + return [x, y] + controlPoints: -> + {x, y} = @getAttributesByName() + return [ + {point: [0, 0], attributesToChange: [x, y], filled: true} + ] + +Model.Position.addChildren [ + createAttribute("X", "x", "0.00") + createAttribute("Y", "y", "0.00") +] + Model.Fill = Model.Component.createVariant label: "Fill" @@ -122,19 +144,25 @@ Model.Shape.addChildren [ ] +Model.Point = Model.Element.createVariant() +Model.Point.addChildren [ + Model.Position.createVariant() +] + + Model.Group = Model.Shape.createVariant label: "Group" graphicClass: Graphic.Group -Model.Anchor = Model.Shape.createVariant +Model.Anchor = Model.Point.createVariant label: "Anchor" graphicClass: Graphic.Anchor createAnchor = (x, y) -> anchor = Model.Anchor.createVariant() - transform = anchor.childOfType(Model.Transform) - attributes = transform.getAttributesByName() + position = anchor.childOfType(Model.Position) + attributes = position.getAttributesByName() attributes.x.setExpression(x) attributes.y.setExpression(y) return anchor