New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FlxObject: allow configuring debug rect colors #1847
Changes from 9 commits
33bc69c
2c49fd1
6979f62
73b8298
acf1b49
b8acc9e
5c9bc5a
34e32f9
87245e0
1e11a66
78041d3
e84a954
902cd09
6ce5a9b
3243245
1fe8ae2
a000ff7
d66a7ef
9f01371
412ea4c
873c236
1c168b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,15 @@ import flixel.util.FlxPath; | |
import flixel.util.FlxSpriteUtil; | ||
import flixel.util.FlxStringUtil; | ||
|
||
#if FLX_DEBUG | ||
typedef DebugColorScheme = | ||
{ | ||
@:optional var notSolid:FlxColor; | ||
var solid:FlxColor; | ||
@:optional var partial:FlxColor; | ||
} | ||
#end | ||
|
||
/** | ||
* This is the base class for most of the display objects (FlxSprite, FlxText, etc). | ||
* It includes some basic attributes about game objects, basic state information, sizes, scrolling, and basic physics and motion. | ||
|
@@ -540,6 +549,17 @@ class FlxObject extends FlxBasic | |
* Overriding this will force a specific color to be used for debug rect. | ||
*/ | ||
public var debugBoundingBoxColor:Null<Int> = null; | ||
|
||
public var debugColorScheme(default, null):DebugColorScheme; | ||
|
||
/** | ||
* Can be overriden for RenderBlit mode to re-render stuff with new colors | ||
*/ | ||
public function setDebugColorScheme(debugColorScheme:DebugColorScheme) | ||
{ | ||
this.debugColorScheme = debugColorScheme; | ||
} | ||
|
||
/** | ||
* Setting this to true will prevent the object from appearing | ||
* when FlxG.debugger.drawDebug is true. | ||
|
@@ -571,6 +591,9 @@ class FlxObject extends FlxBasic | |
y = Y; | ||
width = Width; | ||
height = Height; | ||
#if FLX_DEBUG | ||
debugColorScheme = { solid: FlxColor.RED, partial: FlxColor.GREEN, notSolid: FlxColor.BLUE }; | ||
#end | ||
|
||
initVars(); | ||
} | ||
|
@@ -1034,26 +1057,33 @@ class FlxObject extends FlxBasic | |
} | ||
|
||
var rect = getBoundingBox(camera); | ||
|
||
var gfx:Graphics = beginDrawDebug(camera); | ||
drawDebugBoundingBox(gfx, rect, allowCollisions, immovable); | ||
endDrawDebug(camera); | ||
} | ||
|
||
function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:Int, highlight:Bool) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe rename |
||
{ | ||
// Find the color to use | ||
var color:Null<Int> = debugBoundingBoxColor; | ||
if (color == null) | ||
{ | ||
if (allowCollisions != FlxObject.NONE) | ||
{ | ||
color = immovable ? FlxColor.GREEN : FlxColor.RED; | ||
color = highlight ? debugColorScheme.partial : debugColorScheme.solid; | ||
} | ||
else | ||
{ | ||
color = FlxColor.BLUE; | ||
color = debugColorScheme.notSolid; | ||
} | ||
} | ||
|
||
//fill static graphics object with square shape | ||
var gfx:Graphics = beginDrawDebug(camera); | ||
gfx.lineStyle(1, color, 0.5); | ||
gfx.drawRect(rect.x, rect.y, rect.width, rect.height); | ||
endDrawDebug(camera); | ||
if (color != FlxColor.TRANSPARENT) | ||
{ | ||
//fill static graphics object with square shape | ||
gfx.lineStyle(1, color, 0.5); | ||
gfx.drawRect(rect.x, rect.y, rect.width, rect.height); | ||
} | ||
} | ||
|
||
private inline function beginDrawDebug(camera:FlxCamera):Graphics | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,9 +128,18 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
private var _scaledTileHeight:Float = 0; | ||
|
||
#if FLX_DEBUG | ||
private var _debugTileNotSolid:BitmapData; | ||
private var _debugTilePartial:BitmapData; | ||
private var _debugTileSolid:BitmapData; | ||
private var updateDebugTileBoundingBoxesInit = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd rather remove this var and check if so we could have something like:
|
||
|
||
override public function setDebugColorScheme(debugColorScheme:DebugColorScheme) | ||
{ | ||
this.debugColorScheme = debugColorScheme; | ||
if (updateDebugTileBoundingBoxesInit) | ||
updateDebugTileBoundingBoxes(); | ||
} | ||
|
||
private var _debugTileNotSolid:BitmapData = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and don't forget to dispose these bitmaps in
to
(this don't have to be the part of this PR, but it will make code a tiny-tiny bit better :) |
||
private var _debugTilePartial:BitmapData = null; | ||
private var _debugTileSolid:BitmapData = null; | ||
private var _debugRect:Rectangle; | ||
#end | ||
|
||
|
@@ -168,6 +177,8 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
FlxG.cameras.cameraResized.add(onCameraChanged); | ||
|
||
#if FLX_DEBUG | ||
debugColorScheme = { solid: FlxColor.GREEN, partial: FlxColor.PINK, notSolid: FlxColor.TRANSPARENT }; | ||
|
||
if (FlxG.renderBlit) | ||
FlxG.debugger.drawDebugChanged.add(onDrawDebugChanged); | ||
#end | ||
|
@@ -290,14 +301,42 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
|
||
// Create debug tiles for rendering bounding boxes on demand | ||
#if FLX_DEBUG | ||
updateDebugTileBoundingBoxes(); | ||
updateDebugTileBoundingBoxesInit = true; | ||
#end | ||
} | ||
|
||
#if FLX_DEBUG | ||
private function updateDebugTileBoundingBoxes():Void | ||
{ | ||
if (FlxG.renderBlit) | ||
{ | ||
_debugTileNotSolid = makeDebugTile(FlxColor.BLUE); | ||
_debugTilePartial = makeDebugTile(FlxColor.PINK); | ||
_debugTileSolid = makeDebugTile(FlxColor.GREEN); | ||
if (_debugTileSolid == null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too much repetitions. Maybe it would be better to have this?
|
||
_debugTileSolid = makeDebugTile(debugColorScheme.solid); | ||
else | ||
{ | ||
_debugTileSolid.fillRect(_debugTileSolid.rect, FlxColor.TRANSPARENT); | ||
drawDebugTile(_debugTileSolid, debugColorScheme.solid); | ||
} | ||
|
||
if (_debugTilePartial == null) | ||
_debugTilePartial = makeDebugTile(debugColorScheme.partial); | ||
else | ||
{ | ||
_debugTilePartial.fillRect(_debugTilePartial.rect, FlxColor.TRANSPARENT); | ||
drawDebugTile(_debugTilePartial, debugColorScheme.partial); | ||
} | ||
|
||
if (_debugTileNotSolid == null) | ||
_debugTileNotSolid = makeDebugTile(debugColorScheme.notSolid); | ||
else | ||
{ | ||
_debugTileNotSolid.fillRect(_debugTileNotSolid.rect, FlxColor.TRANSPARENT); | ||
drawDebugTile(_debugTileNotSolid, debugColorScheme.notSolid); | ||
} | ||
} | ||
#end | ||
} | ||
#end | ||
|
||
override private function computeDimensions():Void | ||
{ | ||
|
@@ -346,12 +385,9 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
_helperPoint.x = x - Camera.scroll.x * scrollFactor.x; | ||
_helperPoint.y = y - Camera.scroll.y * scrollFactor.y; | ||
|
||
var debugColor:FlxColor; | ||
var drawX:Float; | ||
var drawY:Float; | ||
|
||
var rectWidth:Float = _scaledTileWidth; | ||
var rectHeight:Float = _scaledTileHeight; | ||
var rect = FlxRect.get(0, 0, rectWidth, rectHeight); | ||
|
||
// Copy tile images into the tile buffer | ||
// Modified from getScreenPosition() | ||
|
@@ -370,6 +406,8 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
var columnIndex:Int; | ||
var tile:FlxTile; | ||
|
||
var gfx:Graphics = Camera.debugLayer.graphics; | ||
|
||
for (row in 0...screenRows) | ||
{ | ||
columnIndex = rowIndex; | ||
|
@@ -380,33 +418,19 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
|
||
if (tile != null && tile.visible) | ||
{ | ||
drawX = _helperPoint.x + (columnIndex % widthInTiles) * rectWidth; | ||
drawY = _helperPoint.y + Math.floor(columnIndex / widthInTiles) * rectHeight; | ||
|
||
if (tile.allowCollisions <= FlxObject.NONE) | ||
{ | ||
debugColor = FlxColor.BLUE; | ||
} | ||
else if (tile.allowCollisions != FlxObject.ANY) | ||
{ | ||
debugColor = FlxColor.PINK; | ||
} | ||
else | ||
{ | ||
debugColor = FlxColor.GREEN; | ||
} | ||
|
||
// Copied from makeDebugTile | ||
var gfx:Graphics = Camera.debugLayer.graphics; | ||
gfx.lineStyle(1, debugColor, 0.5); | ||
gfx.drawRect(drawX, drawY, rectWidth, rectHeight); | ||
rect.x = _helperPoint.x + (columnIndex % widthInTiles) * rectWidth; | ||
rect.y = _helperPoint.y + Math.floor(columnIndex / widthInTiles) * rectHeight; | ||
|
||
drawDebugBoundingBox(gfx, rect, tile.allowCollisions, tile.allowCollisions != FlxObject.ANY); | ||
} | ||
|
||
columnIndex++; | ||
} | ||
|
||
rowIndex += widthInTiles; | ||
} | ||
|
||
rect.put(); | ||
} | ||
#end | ||
|
||
|
@@ -993,26 +1017,34 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile> | |
* Just generates a wireframe box the size of a tile with the specified color. | ||
*/ | ||
#if FLX_DEBUG | ||
private function makeDebugTile(Color:FlxColor):BitmapData | ||
private function makeDebugTile(color:FlxColor):BitmapData | ||
{ | ||
if (FlxG.renderTile) | ||
return null; | ||
|
||
var debugTile:BitmapData; | ||
debugTile = new BitmapData(_tileWidth, _tileHeight, true, 0); | ||
|
||
var gfx:Graphics = FlxSpriteUtil.flashGfx; | ||
gfx.clear(); | ||
gfx.moveTo(0, 0); | ||
gfx.lineStyle(1, Color, 0.5); | ||
gfx.lineTo(_tileWidth - 1, 0); | ||
gfx.lineTo(_tileWidth - 1, _tileHeight - 1); | ||
gfx.lineTo(0, _tileHeight - 1); | ||
gfx.lineTo(0, 0); | ||
|
||
debugTile.draw(FlxSpriteUtil.flashGfxSprite); | ||
drawDebugTile(debugTile, color); | ||
return debugTile; | ||
} | ||
|
||
private function drawDebugTile(debugTile:BitmapData, color:FlxColor):Void | ||
{ | ||
if (color != FlxColor.TRANSPARENT) | ||
{ | ||
var gfx:Graphics = FlxSpriteUtil.flashGfx; | ||
gfx.clear(); | ||
gfx.moveTo(0, 0); | ||
gfx.lineStyle(1, color, 0.5); | ||
gfx.lineTo(_tileWidth - 1, 0); | ||
gfx.lineTo(_tileWidth - 1, _tileHeight - 1); | ||
gfx.lineTo(0, _tileHeight - 1); | ||
gfx.lineTo(0, 0); | ||
|
||
debugTile.draw(FlxSpriteUtil.flashGfxSprite); | ||
} | ||
} | ||
#end | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not change this method to
debugColorScheme
setter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because as Gama11 said it triggers on assigning the structure, but doesn't on field value change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't quite understand this statement. Could you explain?
I've made this change in my fork and it works like i expect it to work. Maybe there is some misunderstanding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you propose is the original way I've done it.
On native targets it works fine because it just gets the colors every frame and uses them.
On Flash (+ FlxTilemap) there is some update stuff to be done on each change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently
_tileMap.debugColorScheme.solid = Std.random(0xFFFFFF);
doesn't trigger the updateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @Gama11 that it's a bit confusing this way :-/ Maybe he has better idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ony way to work around this is to simply have a separate class field for each color.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it would be a simple solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@starry-abyss so do you agree to add 3 debug color properties instead of
sebugColorScheme
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, no problem