2017-02-28 14:49:39 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Components = require('../../components');
|
|
|
|
var Render = require('./GraphicsRender');
|
2017-02-28 21:12:14 +00:00
|
|
|
var Commands = require('./Commands');
|
2017-03-02 02:06:37 +00:00
|
|
|
var MATH_CONST = require('../../math/const');
|
2017-02-28 14:49:39 +00:00
|
|
|
|
|
|
|
var Graphics = new Class({
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Graphics (state, x, y)
|
|
|
|
{
|
|
|
|
GameObject.call(this, state);
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-02-28 14:49:39 +00:00
|
|
|
this.setPosition(x, y);
|
2017-03-02 02:06:37 +00:00
|
|
|
|
|
|
|
this.commandBuffer = [];
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
arc: function (x, y, radius, startAngle, endAngle, anticlockwise)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.ARC,
|
|
|
|
x, y, radius, startAngle, endAngle, anticlockwise
|
|
|
|
);
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
lineStyle: function (lineWidth, color, alpha)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
2017-03-01 17:30:04 +00:00
|
|
|
Commands.LINE_STYLE,
|
|
|
|
lineWidth, color, alpha
|
2017-02-28 21:12:14 +00:00
|
|
|
);
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
fillStyle: function (color, alpha)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
2017-03-02 02:06:37 +00:00
|
|
|
if (alpha === undefined) { alpha = 1; }
|
|
|
|
|
2017-02-28 21:12:14 +00:00
|
|
|
this.commandBuffer.push(
|
2017-03-01 17:30:04 +00:00
|
|
|
Commands.FILL_STYLE,
|
|
|
|
color, alpha
|
2017-02-28 21:12:14 +00:00
|
|
|
);
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
beginPath: function ()
|
2017-03-01 17:30:04 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.BEGIN_PATH
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
closePath: function ()
|
2017-03-01 17:30:04 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.CLOSE_PATH
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
fillPath: function ()
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.FILL_PATH
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
strokePath: function ()
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.STROKE_PATH
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
fillCircle: function (x, y, radius)
|
2017-03-01 17:30:04 +00:00
|
|
|
{
|
2017-03-01 21:08:10 +00:00
|
|
|
this.beginPath();
|
2017-03-02 02:06:37 +00:00
|
|
|
this.arc(x, y, radius, 0, MATH_CONST.PI2);
|
2017-03-01 21:08:10 +00:00
|
|
|
this.fillPath();
|
|
|
|
this.closePath();
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
fillRect: function (x, y, width, height)
|
2017-03-01 17:30:04 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
2017-03-01 21:08:10 +00:00
|
|
|
Commands.FILL_RECT,
|
2017-03-01 17:30:04 +00:00
|
|
|
x, y, width, height
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-03-08 00:51:09 +00:00
|
|
|
fillTriangle: function (x0, y0, x1, y1, x2, y2)
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.FILL_TRIANGLE,
|
|
|
|
x0, y0, x1, y1, x2, y2
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
strokeCircle: function (x, y, radius)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
2017-03-01 21:08:10 +00:00
|
|
|
this.beginPath();
|
2017-03-02 02:06:37 +00:00
|
|
|
this.arc(x, y, radius, 0, MATH_CONST.PI2);
|
2017-03-01 21:08:10 +00:00
|
|
|
this.closePath();
|
2017-03-08 23:49:44 +00:00
|
|
|
this.strokePath();
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
2017-02-28 14:49:39 +00:00
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
strokeRect: function (x, y, width, height)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
2017-03-01 23:23:46 +00:00
|
|
|
this.beginPath();
|
|
|
|
this.moveTo(x, y);
|
|
|
|
this.lineTo(x + width, y);
|
|
|
|
this.lineTo(x + width, y + height);
|
|
|
|
this.lineTo(x, y + height);
|
|
|
|
this.lineTo(x, y);
|
2017-03-02 00:40:03 +00:00
|
|
|
this.strokePath();
|
2017-03-01 23:23:46 +00:00
|
|
|
this.closePath();
|
2017-02-28 21:12:14 +00:00
|
|
|
},
|
2017-02-28 16:52:09 +00:00
|
|
|
|
2017-03-08 00:51:09 +00:00
|
|
|
strokeTriangle: function (x0, y0, x1, y1, x2, y2)
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.STROKE_TRIANGLE,
|
|
|
|
x0, y0, x1, y1, x2, y2
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
lineTo: function (x, y)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.LINE_TO,
|
|
|
|
x, y
|
|
|
|
);
|
|
|
|
},
|
2017-02-28 16:52:09 +00:00
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
moveTo: function (x, y)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.MOVE_TO,
|
|
|
|
x, y
|
|
|
|
);
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
clear: function ()
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
2017-03-02 02:06:37 +00:00
|
|
|
this.commandBuffer.length = 0;
|
2017-02-28 16:52:09 +00:00
|
|
|
}
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-02-28 14:49:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Graphics;
|