Redoing debug config

This commit is contained in:
Richard Davey 2019-11-26 16:39:34 +00:00
parent 7e4577556c
commit 7b280e4fb2

View file

@ -198,25 +198,27 @@ var World = new Class({
/** /**
* The default configuration values. * The default configuration values.
* *
* @name Phaser.Physics.Matter.World#defaults * @name Phaser.Physics.Matter.World#debugConfig
* @type {object} * @type {object}
* @since 3.0.0 * @since 3.22.0
*/ */
this.defaults = { this.debugConfig = {
debugShowBody: GetFastValue(config, 'debugShowBody', true), showBody: GetFastValue(config, 'debugShowBody', true),
debugShowStaticBody: GetFastValue(config, 'debugShowStaticBody', true), showStaticBody: GetFastValue(config, 'debugShowStaticBody', true),
debugShowVelocity: GetFastValue(config, 'debugShowVelocity', true), showSleeping: GetFastValue(config, 'debugShowSleeping', false),
bodyDebugColor: GetFastValue(config, 'debugBodyColor', 0xff00ff), showJoint: GetFastValue(config, 'debugShowJoint', true),
bodyDebugFillColor: GetFastValue(config, 'debugBodyFillColor', 0xe3a7e3), showInternalEdges: GetFastValue(config, 'debugShowInternalEdges', false),
staticBodyDebugColor: GetFastValue(config, 'debugStaticBodyColor', 0x0000ff), showConvexHulls: GetFastValue(config, 'debugShowConvexHulls', false),
velocityDebugColor: GetFastValue(config, 'debugVelocityColor', 0x00ff00),
debugShowJoint: GetFastValue(config, 'debugShowJoint', true), renderFill: GetFastValue(config, 'renderFill', true),
jointDebugColor: GetFastValue(config, 'debugJointColor', 0x000000), renderStroke: GetFastValue(config, 'renderStroke', true),
debugWireframes: GetFastValue(config, 'debugWireframes', true), fillColor: GetFastValue(config, 'debugFillColor', 0x0e1a8b),
debugShowInternalEdges: GetFastValue(config, 'debugShowInternalEdges', false), strokeColor: GetFastValue(config, 'debugStrokeColor', 0x1c30ef),
debugShowConvexHulls: GetFastValue(config, 'debugShowConvexHulls', false), staticFillColor: GetFastValue(config, 'debugStaticFillColor', 0x198c10),
debugConvexHullColor: GetFastValue(config, 'debugConvexHullColor', 0xaaaaaa), staticStrokeColor: GetFastValue(config, 'debugStaticStrokeColor', 0x20f410),
debugShowSleeping: GetFastValue(config, 'debugShowSleeping', false)
jointColor: GetFastValue(config, 'debugJointColor', 0xdddddd),
hullColor: GetFastValue(config, 'debugHullColor', 0xaaaaaa)
}; };
if (this.drawDebug) if (this.drawDebug)
@ -721,7 +723,13 @@ var World = new Class({
*/ */
postUpdate: function () postUpdate: function ()
{ {
if (!this.drawDebug) var config = this.debugConfig;
var showBody = config.showBody;
var showStaticBody = config.showStaticBody;
var showJoint = config.showJoint;
if (!this.drawDebug || (!showBody && !showStaticBody && !showJoint))
{ {
return; return;
} }
@ -730,23 +738,11 @@ var World = new Class({
var bodies = Composite.allBodies(this.localWorld); var bodies = Composite.allBodies(this.localWorld);
if (this.defaults.debugWireframes)
{
if (this.defaults.debugShowConvexHulls)
{
this.renderConvexHulls(bodies);
}
this.renderWireframes(bodies);
}
else
{
this.renderBodies(bodies); this.renderBodies(bodies);
}
if (this.defaults.debugShowJoint) if (showJoint)
{ {
this.renderJoints(); // this.renderJoints();
} }
}, },
@ -758,7 +754,6 @@ var World = new Class({
* @since 3.14.0 * @since 3.14.0
* *
* @param {array} bodies - An array of bodies from the localWorld. * @param {array} bodies - An array of bodies from the localWorld.
*/
renderConvexHulls: function (bodies) renderConvexHulls: function (bodies)
{ {
var graphics = this.debugGraphic; var graphics = this.debugGraphic;
@ -788,6 +783,7 @@ var World = new Class({
graphics.strokePath(); graphics.strokePath();
}, },
*/
/** /**
* Renders the wireframes of the given array of bodies. * Renders the wireframes of the given array of bodies.
@ -797,11 +793,11 @@ var World = new Class({
* @since 3.14.0 * @since 3.14.0
* *
* @param {array} bodies - An array of bodies from the localWorld. * @param {array} bodies - An array of bodies from the localWorld.
*/
renderWireframes: function (bodies) renderWireframes: function (bodies)
{ {
var graphics = this.debugGraphic; var graphics = this.debugGraphic;
var showInternalEdges = this.defaults.debugShowInternalEdges; var showInternalEdges = this.defaults.debugShowInternalEdges;
var showStatic = this.defaults.debugShowStaticBody;
graphics.lineStyle(1, this.defaults.bodyDebugColor); graphics.lineStyle(1, this.defaults.bodyDebugColor);
@ -811,7 +807,7 @@ var World = new Class({
{ {
var body = bodies[i]; var body = bodies[i];
if (!body.render.visible) if (!body.render.visible || (body.isStatic && !showStatic))
{ {
continue; continue;
} }
@ -847,6 +843,7 @@ var World = new Class({
graphics.strokePath(); graphics.strokePath();
}, },
*/
/** /**
* Renders the array of bodies. * Renders the array of bodies.
@ -861,43 +858,73 @@ var World = new Class({
{ {
var graphics = this.debugGraphic; var graphics = this.debugGraphic;
var showInternalEdges = this.defaults.debugShowInternalEdges || !this.defaults.debugWireframes; var config = this.debugConfig;
var showSleeping = this.defaults.debugShowSleeping;
var wireframes = this.defaults.debugWireframes; var showBody = config.showBody;
var showStaticBody = config.showStaticBody;
var showSleeping = config.showSleeping;
var showInternalEdges = config.showInternalEdges;
var showConvexHulls = config.showConvexHulls;
var renderFill = config.renderFill;
var renderStroke = config.renderStroke;
var fillColor = config.fillColor;
var strokeColor = config.strokeColor;
var staticFillColor = config.staticFillColor;
var staticStrokeColor = config.staticStrokeColor;
var hullColor = config.hullColor;
var body; var body;
var part; var part;
var i; var render;
var k;
for (i = 0; i < bodies.length; i++) for (var i = 0; i < bodies.length; i++)
{ {
body = bodies[i]; body = bodies[i];
// 1) Don't show invisible bodies
if (!body.render.visible) if (!body.render.visible)
{ {
continue; continue;
} }
// Handle compound parts // 2) Don't show static bodies, OR
for (k = body.parts.length > 1 ? 1 : 0; k < body.parts.length; k++) // 3) Don't show dynamic bodies
{ if ((!showStaticBody && body.isStatic) || (!showBody && !body.isStatic))
part = body.parts[k];
if (!part.render.visible)
{ {
continue; continue;
} }
// Handle compound parts
var partsLength = body.parts.length;
for (var k = (partsLength > 1) ? 1 : 0; k < partsLength; k++)
{
part = body.parts[k];
render = part.render;
if (!render.visible)
{
continue;
}
var opacity = render.opacity;
if (showSleeping && body.isSleeping) if (showSleeping && body.isSleeping)
{ {
graphics.lineStyle(1, this.defaults.bodyDebugColor, 0.5 * part.render.opacity); opacity *= 0.5;
graphics.fillStyle(this.defaults.bodyDebugColor, 0.5 * part.render.opacity); }
if (body.isStatic)
{
graphics.lineStyle(1, staticStrokeColor, opacity);
graphics.fillStyle(staticFillColor, opacity);
} }
else else
{ {
graphics.lineStyle(1, this.defaults.bodyDebugColor, part.render.opacity); graphics.lineStyle(1, strokeColor, opacity);
graphics.fillStyle(this.defaults.bodyDebugColor, part.render.opacity); graphics.fillStyle(fillColor, opacity);
} }
// Part polygon // Part polygon
@ -935,15 +962,37 @@ var World = new Class({
graphics.closePath(); graphics.closePath();
} }
if (!wireframes) if (renderFill)
{ {
graphics.fillPath(); graphics.fillPath();
} }
else
if (renderStroke)
{ {
graphics.strokePath(); graphics.strokePath();
} }
} }
// Render Convex Hulls
if (showConvexHulls && partsLength > 1)
{
var verts = body.vertices;
graphics.lineStyle(1, hullColor);
graphics.beginPath();
graphics.moveTo(verts[0].x, verts[0].y);
for (var v = 1; v < verts.length; v++)
{
graphics.lineTo(verts[v].x, verts[v].y);
}
graphics.lineTo(verts[0].x, verts[0].y);
graphics.strokePath();
}
} }
}, },