2019-03-18 20:06:47 -05:00
|
|
|
const PIXI = 'undefined' !== typeof window ? require('pixi.js') : undefined;
|
|
|
|
|
|
|
|
import {Color} from './color';
|
|
|
|
import {Renderable} from './renderable';
|
|
|
|
|
|
|
|
export class Primitives extends Renderable {
|
|
|
|
|
|
|
|
static fillStyle(color) {
|
|
|
|
return {color};
|
|
|
|
}
|
|
|
|
|
|
|
|
static lineStyle(color, thickness = 1) {
|
|
|
|
return {color, thickness};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives = new PIXI.Graphics();
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.clear();
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
drawCircle(position, radius, lineStyle, fillStyle) {
|
|
|
|
this._wrapStyle('drawCircle', '3rd', lineStyle, fillStyle, () => {
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.drawCircle(position[0], position[1], radius);
|
2019-03-18 20:06:47 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
drawLine(p1, p2, lineStyle, fillStyle) {
|
|
|
|
this._wrapStyle('drawLine', '3rd', lineStyle, fillStyle, () => {
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.moveTo(p1[0], p1[1]);
|
|
|
|
this.primitives.lineTo(p2[0], p2[1]);
|
2019-03-18 20:06:47 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
drawRectangle(rectangle, lineStyle, fillStyle) {
|
|
|
|
this._wrapStyle('drawLine', '2nd', lineStyle, fillStyle, () => {
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.drawRect(
|
2019-03-18 20:06:47 -05:00
|
|
|
rectangle[0],
|
|
|
|
rectangle[1],
|
|
|
|
rectangle[2],
|
|
|
|
rectangle[3]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-25 10:18:41 -05:00
|
|
|
get internal() {
|
|
|
|
return this.primitives;
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:06:47 -05:00
|
|
|
_wrapStyle(method, where, lineStyle, fillStyle, fn) {
|
|
|
|
if (!lineStyle) {
|
|
|
|
throw new TypeError(
|
|
|
|
`Primitives::${method} expects lineStyle as ${where} parameter`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (fillStyle) {
|
|
|
|
const {color} = fillStyle;
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.beginFill(color.toInteger(), color.alpha)
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
const {color, thickness} = lineStyle;
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.lineStyle(thickness, color.toInteger(), color.alpha)
|
2019-03-18 20:06:47 -05:00
|
|
|
fn();
|
|
|
|
if (fillStyle) {
|
2019-03-25 10:18:41 -05:00
|
|
|
this.primitives.endFill();
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|