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-03-29 14:06:06 +00:00
|
|
|
var GetObjectValue = require('../../utils/object/GetObjectValue');
|
2017-02-28 14:49:39 +00:00
|
|
|
|
|
|
|
var Graphics = new Class({
|
|
|
|
|
2017-04-05 01:10:48 +00:00
|
|
|
Extends: GameObject,
|
|
|
|
|
2017-02-28 14:49:39 +00:00
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-03-29 14:06:06 +00:00
|
|
|
function Graphics (state, options)
|
2017-02-28 14:49:39 +00:00
|
|
|
{
|
2017-03-29 14:06:06 +00:00
|
|
|
var x = GetObjectValue(options, 'x', 0);
|
|
|
|
var y = GetObjectValue(options, 'y', 0);
|
|
|
|
|
2017-02-28 14:49:39 +00:00
|
|
|
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-03-29 14:06:06 +00:00
|
|
|
|
|
|
|
this.defaultFillColor = -1;
|
|
|
|
this.defaultFillAlpha = 1;
|
|
|
|
|
|
|
|
this.defaultStrokeWidth = 1;
|
|
|
|
this.defaultStrokeColor = -1;
|
|
|
|
this.defaultStrokeAlpha = 1;
|
|
|
|
|
|
|
|
this.setDefaultStyles(options);
|
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// STYLES
|
|
|
|
|
2017-03-29 14:06:06 +00:00
|
|
|
setDefaultStyles: function (options)
|
|
|
|
{
|
|
|
|
if (GetObjectValue(options, 'lineStyle', null))
|
|
|
|
{
|
|
|
|
this.defaultStrokeWidth = GetObjectValue(options, 'lineStyle.width', 1);
|
|
|
|
this.defaultStrokeColor = GetObjectValue(options, 'lineStyle.color', 0xffffff);
|
|
|
|
this.defaultStrokeAlpha = GetObjectValue(options, 'lineStyle.alpha', 1);
|
|
|
|
|
|
|
|
this.lineStyle(this.defaultStrokeWidth, this.defaultStrokeColor, this.defaultStrokeAlpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetObjectValue(options, 'fillStyle', null))
|
|
|
|
{
|
|
|
|
this.defaultFillColor = GetObjectValue(options, 'fillStyle.color', 0xffffff);
|
|
|
|
this.defaultFillAlpha = GetObjectValue(options, 'fillStyle.alpha', 1);
|
|
|
|
|
|
|
|
this.fillStyle(this.defaultFillColor, this.defaultFillAlpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
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
|
|
|
{
|
2017-03-29 15:05:05 +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.LINE_STYLE,
|
|
|
|
lineWidth, color, alpha
|
2017-02-28 21:12:14 +00:00
|
|
|
);
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
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-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// PATH
|
|
|
|
|
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-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
closePath: function ()
|
2017-03-01 17:30:04 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.CLOSE_PATH
|
|
|
|
);
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fillPath: function ()
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.FILL_PATH
|
|
|
|
);
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
strokePath: function ()
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.STROKE_PATH
|
|
|
|
);
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// CIRCLE
|
2017-03-29 14:06:06 +00:00
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
fillCircleShape: function (circle)
|
|
|
|
{
|
|
|
|
return this.fillCircle(circle.x, circle.y, circle.radius);
|
2017-03-29 14:06:06 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
strokeCircleShape: function (circle)
|
2017-03-29 14:06:06 +00:00
|
|
|
{
|
2017-03-29 23:14:30 +00:00
|
|
|
return this.strokeCircle(circle.x, circle.y, circle.radius);
|
2017-03-29 14:06:06 +00:00
|
|
|
},
|
|
|
|
|
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-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
strokeCircle: function (x, y, radius)
|
2017-03-01 17:30:04 +00:00
|
|
|
{
|
2017-03-29 23:14:30 +00:00
|
|
|
this.beginPath();
|
|
|
|
this.arc(x, y, radius, 0, MATH_CONST.PI2);
|
|
|
|
this.closePath();
|
|
|
|
this.strokePath();
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-01 17:30:04 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// RECTANGLE
|
|
|
|
|
|
|
|
fillRectShape: function (rect)
|
2017-03-08 00:51:09 +00:00
|
|
|
{
|
2017-03-29 23:14:30 +00:00
|
|
|
return this.fillRect(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
},
|
2017-03-19 23:07:41 +00:00
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
strokeRectShape: function (rect)
|
|
|
|
{
|
|
|
|
return this.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
2017-03-08 00:51:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
fillRect: function (x, y, width, height)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
2017-03-29 23:14:30 +00:00
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.FILL_RECT,
|
|
|
|
x, y, width, height
|
|
|
|
);
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
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-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-02-28 21:12:14 +00:00
|
|
|
},
|
2017-02-28 16:52:09 +00:00
|
|
|
|
2017-03-30 00:47:15 +00:00
|
|
|
// POINT
|
|
|
|
|
|
|
|
fillPointShape: function (point, size)
|
|
|
|
{
|
|
|
|
return this.fillPoint(point.x, point.y, size);
|
|
|
|
},
|
|
|
|
|
|
|
|
fillPoint: function (x, y, size)
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.FILL_RECT,
|
|
|
|
x, y, size, size
|
|
|
|
);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// TRIANGLE
|
|
|
|
|
|
|
|
fillTriangleShape: function (triangle)
|
|
|
|
{
|
2017-03-29 23:33:53 +00:00
|
|
|
return this.fillTriangle(triangle.x1, triangle.y1, triangle.x2, triangle.y2, triangle.x3, triangle.y3);
|
2017-03-29 23:14:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
strokeTriangleShape: function (triangle)
|
|
|
|
{
|
2017-03-29 23:33:53 +00:00
|
|
|
return this.strokeTriangle(triangle.x1, triangle.y1, triangle.x2, triangle.y2, triangle.x3, triangle.y3);
|
2017-03-29 23:14:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fillTriangle: function (x0, y0, x1, y1, x2, y2)
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.FILL_TRIANGLE,
|
|
|
|
x0, y0, x1, y1, x2, y2
|
|
|
|
);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
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-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-08 00:51:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// LINE
|
|
|
|
|
|
|
|
strokeLineShape: function (line)
|
|
|
|
{
|
2017-03-31 01:34:43 +00:00
|
|
|
return this.lineBetween(line.x1, line.y1, line.x2, line.y2);
|
2017-03-29 23:14:30 +00:00
|
|
|
},
|
|
|
|
|
2017-03-30 00:47:15 +00:00
|
|
|
lineBetween: function (x1, y1, x2, y2)
|
|
|
|
{
|
|
|
|
this.beginPath();
|
|
|
|
this.moveTo(x1, y1);
|
|
|
|
this.lineTo(x2, y2);
|
|
|
|
this.strokePath();
|
|
|
|
this.closePath();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
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-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-02-28 21:12:14 +00:00
|
|
|
},
|
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-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-02-28 16:52:09 +00:00
|
|
|
},
|
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
lineFxTo: function (x, y, width, rgb)
|
2017-03-14 22:13:31 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
2017-03-15 19:23:10 +00:00
|
|
|
Commands.LINE_FX_TO,
|
|
|
|
x, y, width, rgb, 1
|
2017-03-19 23:07:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return this;
|
2017-03-14 22:13:31 +00:00
|
|
|
},
|
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
moveFxTo: function (x, y, width, rgb)
|
2017-03-14 22:13:31 +00:00
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
2017-03-15 19:23:10 +00:00
|
|
|
Commands.MOVE_FX_TO,
|
|
|
|
x, y, width, rgb, 1
|
2017-03-14 22:13:31 +00:00
|
|
|
);
|
2017-03-19 23:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-14 22:13:31 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 23:14:30 +00:00
|
|
|
// ARC
|
|
|
|
|
|
|
|
arc: function (x, y, radius, startAngle, endAngle, anticlockwise)
|
|
|
|
{
|
|
|
|
this.commandBuffer.push(
|
|
|
|
Commands.ARC,
|
|
|
|
x, y, radius, startAngle, endAngle, anticlockwise
|
|
|
|
);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
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-03-19 23:07:41 +00:00
|
|
|
|
2017-03-29 14:06:06 +00:00
|
|
|
if (this.defaultFillColor > -1)
|
|
|
|
{
|
|
|
|
this.fillStyle(this.defaultFillColor, this.defaultFillAlpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.defaultStrokeColor > -1)
|
|
|
|
{
|
|
|
|
this.lineStyle(this.defaultStrokeWidth, this.defaultStrokeColor, this.defaultStrokeAlpha);
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:07:41 +00:00
|
|
|
return this;
|
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;
|