-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Debug Draw Static Helpers (#2929)
This PR adds new `ex.Debug` static for more convenient debug drawing where you might not have a graphics context accessible to you. This works by batching up all the debug draw requests and flushing them during the debug draw step. * `ex.Debug.drawRay(ray: Ray, options?: { distance?: number, color?: Color })` * `ex.Debug.drawBounds(boundingBox: BoundingBox, options?: { color?: Color })` * `ex.Debug.drawCircle(center: Vector, radius: number, options?: ...)` * `ex.Debug.drawPolygon(points: Vector[], options?: { color?: Color })` * `ex.Debug.drawText(text: string, pos: Vector)` * `ex.Debug.drawLine(start: Vector, end: Vector, options?: LineGraphicsOptions)` * `ex.Debug.drawLines(points: Vector[], options?: LineGraphicsOptions)` * `drawPoint(point: Vector, options?: PointGraphicsOptions)`
- Loading branch information
Showing
26 changed files
with
378 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './Debug'; | ||
export * from './DebugConfig'; | ||
export * from './DebugFlags'; | ||
export * from './DebugSystem'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Vector } from '../Math/vector'; | ||
import { ExcaliburGraphicsContext, LineGraphicsOptions, PointGraphicsOptions } from './Context/ExcaliburGraphicsContext'; | ||
import { Color } from '../Color'; | ||
import { Ray } from '../Math/ray'; | ||
import { BoundingBox } from '../excalibur'; | ||
|
||
export class Debug { | ||
static _drawCalls: ((ctx: ExcaliburGraphicsContext) => void)[] = []; | ||
static _ctx: ExcaliburGraphicsContext; | ||
static z: number = Infinity; | ||
static registerGraphicsContext(ctx: ExcaliburGraphicsContext) { | ||
Debug._ctx = ctx; | ||
} | ||
static draw(debugDrawCall: (ctx: ExcaliburGraphicsContext) => void) { | ||
this._drawCalls.push(debugDrawCall); | ||
} | ||
|
||
static drawPoint(point: Vector, options?: PointGraphicsOptions) { | ||
Debug.draw(ctx => { | ||
ctx.debug.drawPoint(point, options); | ||
}); | ||
} | ||
|
||
static drawLine(start: Vector, end: Vector, options?: LineGraphicsOptions) { | ||
Debug.draw(ctx => { | ||
ctx.debug.drawLine(start, end, options); | ||
}); | ||
} | ||
|
||
static drawLines(points: Vector[], options?: LineGraphicsOptions) { | ||
if (points.length > 1) { | ||
Debug.draw(ctx => { | ||
for (let i = 0; i < points.length - 1; i++) { | ||
ctx.debug.drawLine(points[i], points[i + 1], options); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
static drawText(text: string, pos: Vector) { | ||
Debug.draw(ctx => { | ||
ctx.debug.drawText(text, pos); | ||
}); | ||
} | ||
|
||
static drawPolygon(points: Vector[], options?: { color?: Color }) { | ||
if (points.length > 1) { | ||
Debug.draw(ctx => { | ||
const firstPoint = points[0]; | ||
const polygon = [...points, firstPoint]; | ||
for (let i = 0; i < polygon.length - 1; i++) { | ||
ctx.debug.drawLine(polygon[i], polygon[i + 1], options); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
static drawCircle(center: Vector, radius: number, options?: { | ||
color?: Color, | ||
strokeColor?: Color, | ||
width?: number | ||
}) { | ||
const { color, strokeColor, width} = { | ||
color: Color.Black, | ||
strokeColor: undefined, | ||
width: undefined, | ||
...options | ||
}; | ||
Debug.draw(ctx => { | ||
ctx.drawCircle(center, radius, color, strokeColor, width); | ||
}); | ||
} | ||
|
||
static drawBounds(boundingBox: BoundingBox, options?: { color?: Color }): void { | ||
Debug.draw(ctx => { | ||
ctx.debug.drawRect(boundingBox.left, boundingBox.top, boundingBox.width, boundingBox.height, options); | ||
}); | ||
} | ||
|
||
static drawRay(ray: Ray, options?: { distance?: number, color?: Color }) { | ||
const { distance, color } = { | ||
color: Color.Blue, | ||
distance: 10, | ||
...options | ||
}; | ||
Debug.draw((ctx) => { | ||
const start = ray.pos; | ||
const end = ray.pos.add(ray.dir.scale(distance)); | ||
|
||
ctx.debug.drawLine(start, end, { color }); | ||
}); | ||
} | ||
|
||
static flush(ctx: ExcaliburGraphicsContext) { | ||
ctx.save(); | ||
ctx.z = Debug.z; | ||
for (const drawCall of Debug._drawCalls) { | ||
drawCall(ctx); | ||
} | ||
ctx.restore(); | ||
Debug.clear(); | ||
} | ||
|
||
static clear() { | ||
Debug._drawCalls.length = 0; | ||
} | ||
} |
Oops, something went wrong.