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

Watch Expressions #1790

Merged
merged 2 commits into from
Mar 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions flixel/system/debug/console/ConsoleCommands.hx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class ConsoleCommands

console.registerFunction("create", create, "Creates a new FlxObject and registers it - by default at the mouse position. \"create(ObjClass:Class<T>, PlaceAtMouse:Bool, ExtraParams:Array<Dynamic>)\" Ex: \"create(FlxSprite, false, [100, 100])\"");

console.registerFunction("watch", watch, "Adds the specified field of an object to the watch window.");
console.registerFunction("removeWatch", removeWatch, "Removes the specified field of an object from the watch window.");
console.registerFunction("watchExpr", watchExpr, "Adds the specified expression to the watch window. Be sure any objects, functions, and classes used are registered!");
console.registerFunction("removeExpr", removeExpr, "Removes the specified expression from the watch window or removes expressions by their given display names.");
console.registerFunction("watchMouse", watchMouse, "Adds the mouse coordinates to the watch window.");
console.registerFunction("track", track, "Adds a tracker window for the specified object or class.");

Expand Down Expand Up @@ -163,6 +167,32 @@ class ConsoleCommands
ConsoleUtil.log("Functions registered: \n" + FlxStringUtil.formatStringMap(_console.registeredFunctions));
}

private function watch(AnyObject:Dynamic, VariableName:String, ?DisplayName:String):Void
{
if (AnyObject != null) {
FlxG.watch.add(AnyObject, VariableName, DisplayName);
}
}

private function removeWatch(AnyObject:Dynamic, VariableName:String):Void
{
if (AnyObject != null) {
FlxG.watch.remove(AnyObject, VariableName);
}
}

private function watchExpr(Expression:String, ?DisplayName:String):Void
{
if (Expression != null && Expression.length > 0) {
FlxG.watch.addExpr(Expression, DisplayName);
}
}

private function removeExpr(?Expression:String, ?DisplayName:String):Void
{
FlxG.watch.removeExpr(Expression, DisplayName);
}

private function watchMouse():Void
{
if (!_watchingMouse)
Expand Down
26 changes: 26 additions & 0 deletions flixel/system/debug/watch/Watch.hx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ class Watch extends Window
}
}

/**
* Remove an expression from the watch list in the debugger.
* You can pass the display name or the entire expression itself to remove it.
*
* @param Expression The Haxe expression that you want to remove. Pass null if you wish to remove it by its display name instead.
* @param DisplayName The name of the expression you want to remove. Pass null (or don't pass anything) if the previous parameter is not null.
*/
public function removeExpr(?Expression:String, ?DisplayName:String):Void
{
if (DisplayName != null)
{
for (i in 0..._watchEntries.length)
{
var watchEntry:WatchEntry = _watchEntries[i];
if (watchEntry != null && watchEntry.object == null && watchEntry.custom == DisplayName)
{
removeEntry(watchEntry, i);
}
}
}
else if (Expression != null)
{
remove(null, Expression);
}
}

/**
* Helper function to acutally remove an entry.
*/
Expand Down
29 changes: 24 additions & 5 deletions flixel/system/debug/watch/WatchEntry.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import flash.text.TextFieldType;
import flash.text.TextFormat;
import flixel.FlxG;
import flixel.system.FlxAssets;
import flixel.system.debug.console.ConsoleUtil;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import flixel.util.FlxStringUtil;
import openfl.events.FocusEvent;
Expand Down Expand Up @@ -50,6 +51,7 @@ class WatchEntry implements IFlxDestroyable
private var _whiteText:TextFormat;
private var _blackText:TextFormat;
private var _isQuickWatch:Bool = false;
private var _isExprWatch:Bool = false;

/**
* Creates a new watch entry in the watch window.
Expand All @@ -66,9 +68,17 @@ class WatchEntry implements IFlxDestroyable
{
editing = false;

if (object == null && field == null && custom != null)
if (object == null && custom != null)
{
_isQuickWatch = true;
if (field == null)
{
_isQuickWatch = true;
}
else
{
_isExprWatch = true;
this.field = field;
}
}
else
{
Expand Down Expand Up @@ -98,7 +108,7 @@ class WatchEntry implements IFlxDestroyable
valueDisplay.multiline = false;
valueDisplay.selectable = true;
valueDisplay.doubleClickEnabled = true;
if (!_isQuickWatch) // No editing for quickWatch
if (!_isQuickWatch && !_isExprWatch) // No editing for quickWatch/watchExpr
{
valueDisplay.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
valueDisplay.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
Expand Down Expand Up @@ -166,8 +176,17 @@ class WatchEntry implements IFlxDestroyable
if (editing || _isQuickWatch)
return;

var property:Dynamic = Reflect.getProperty(object, field);
valueDisplay.text = Std.string(property);
if (!_isExprWatch)
{
var property:Dynamic = Reflect.getProperty(object, field);
valueDisplay.text = Std.string(property);
}
#if hscript
else
{
valueDisplay.text = Std.string(ConsoleUtil.runCommand(field));
}
#end
}
#end

Expand Down
28 changes: 28 additions & 0 deletions flixel/system/frontEnds/WatchFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@ class WatchFrontEnd
#end
}

/**
* Add an expression to the watch list in the debugger.
* The expression gets evaluated with hscript, and you can see its current value all the time.
*
* @param Expression A Haxe expression written as a string that will be evaluated and watched.
* @param DisplayName Optional, display your own string instead of the class name + variable name: e.g. "enemy count".
*/
public function addExpr(Expression:String, ?DisplayName:String):Void
{
#if (FLX_DEBUG && hscript)
FlxG.game.debugger.watch.add(null, Expression, DisplayName);
#end
}

/**
* Remove an expression from the watch list in the debugger.
* You can pass the display name or the entire expression itself to remove it.
*
* @param Expression The Haxe expression that you want to remove. Pass null if you wish to remove it by its display name instead.
* @param DisplayName The name of the expression you want to remove. Pass null (or don't pass anything) if the previous parameter is not null.
*/
public function removeExpr(?Expression:String, ?DisplayName:String):Void
{
#if (FLX_DEBUG && hscript)
FlxG.game.debugger.watch.removeExpr(Expression, DisplayName);
#end
}

/**
* Just needed to create an instance.
*/
Expand Down