From 6c26e87840d2f401a52741017b965b1d2164d1b4 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 22 Mar 2019 13:15:57 -0500 Subject: [PATCH] feat: ShapeView --- packages/graphics/index.js | 1 + packages/graphics/shape-view.js | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 packages/graphics/shape-view.js diff --git a/packages/graphics/index.js b/packages/graphics/index.js index 4441068..c26658e 100644 --- a/packages/graphics/index.js +++ b/packages/graphics/index.js @@ -6,4 +6,5 @@ export {Image} from './image'; export {Primitives} from './primitives'; export {Renderable} from './renderable'; export {Renderer} from './renderer'; +export {ShapeView} from './shape-view'; export {Sprite} from './sprite'; diff --git a/packages/graphics/shape-view.js b/packages/graphics/shape-view.js new file mode 100644 index 0000000..906e84a --- /dev/null +++ b/packages/graphics/shape-view.js @@ -0,0 +1,62 @@ +import {ShapeList, PolygonShape} from '@avocado/physics'; + +import {Color} from './color'; +import {Container} from './container'; +import {Primitives} from './primitives'; +import {Renderable} from './renderable'; + +export class ShapeView extends Renderable { + + constructor(shape) { + super(); + this.container = new Container(); + this.shape = shape; + if (shape instanceof PolygonShape) { + this.redrawPolygonLines(); + shape.on('aabbChanged', () => { + this.redrawPolygonLines(); + }); + } + if (shape instanceof ShapeList) { + for (const child of shape) { + const childShapeView = new ShapeView(child); + this.container.addChild(childShapeView); + } + } + this.container.position = shape.position; + shape.on('positionChanged', () => { + this.container.position = shape.position; + }) + } + + get internal() { + return this.container.internal; + } + + redrawPolygonLines() { + const primitives = new Primitives(); + let firstVertice; + let lastVertice; + for (const vertice of this.shape) { + if (!firstVertice) { + firstVertice = vertice; + } + if (lastVertice) { + primitives.drawLine( + lastVertice, + vertice, + Primitives.lineStyle(new Color(255, 0, 255), 1), + ); + } + lastVertice = vertice; + } + primitives.drawLine( + lastVertice, + firstVertice, + Primitives.lineStyle(new Color(255, 0, 255), 1), + ); + this.container.removeAllChildren(); + this.container.addChild(primitives); + } + +}