Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 38 additions & 8 deletions flixel/FlxObject.hx
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Copy link
Member

@Beeblerox Beeblerox Oct 2, 2016

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?

public var debugColorScheme(default, set):DebugColorScheme;

private function set_debugColorScheme(value:DebugColorScheme):DebugColorScheme
{
    return debugColorScheme = value;
}

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 update

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no problem

{
this.debugColorScheme = debugColorScheme;
}

/**
* Setting this to true will prevent the object from appearing
* when FlxG.debugger.drawDebug is true.
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe rename highlight parameter to partial as well?

{
// 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
Expand Down
116 changes: 74 additions & 42 deletions flixel/tile/FlxTilemap.hx
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd rather remove this var and check if _tileWidth and _tileHeight are initialized (have values more than 0).

so we could have something like:

override private function set_debugColorScheme(value:DebugColorScheme):DebugColorScheme
    {
        this.debugColorScheme = value;

        if (_tileWidth > 0 && _tileHeight > 0)
        {   
            updateDebugTileBoundingBoxes();
        }

        return value;
    }


override public function setDebugColorScheme(debugColorScheme:DebugColorScheme)
{
this.debugColorScheme = debugColorScheme;
if (updateDebugTileBoundingBoxesInit)
updateDebugTileBoundingBoxes();
}

private var _debugTileNotSolid:BitmapData = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and don't forget to dispose these bitmaps in destroy() method. Change lines

_debugTileNotSolid = null;
_debugTilePartial = null;
_debugTileSolid = null;

to

_debugTileNotSolid = FlxDestroyUtil.dispose(_debugTileNotSolid);
_debugTilePartial = FlxDestroyUtil.dispose(_debugTilePartial);
_debugTileSolid = FlxDestroyUtil.dispose(_debugTileSolid);

(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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Member

@Beeblerox Beeblerox Oct 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too much repetitions.

Maybe it would be better to have this?

private function updateDebugTileBoundingBoxes():Void 
    {
        if (FlxG.renderBlit)
        {
            _debugTileSolid = updateDebugTile(_debugTileSolid, debugColorScheme.solid);
            _debugTilePartial = updateDebugTile(_debugTilePartial, debugColorScheme.partial);
            _debugTileNotSolid = updateDebugTile(_debugTileNotSolid, debugColorScheme.notSolid);
        }
    }

    private function updateDebugTile(tileBitmap:BitmapData, color:FlxColor):BitmapData
    {
        if (tileBitmap != null && (tileBitmap.width != _tileWidth || tileBitmap.height != _tileHeight))
            tileBitmap = FlxDestroyUtil.dispose(tileBitmap);

        if (tileBitmap == null)
            tileBitmap = makeDebugTile(color);
        else
        {
            tileBitmap.fillRect(tileBitmap.rect, FlxColor.TRANSPARENT);
            drawDebugTile(tileBitmap, color);
        }

        return tileBitmap;
    }

_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
{
Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -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

/**
Expand Down