From 65b741db3eb3dc662c19e71801c93a3c08f0bc56 Mon Sep 17 00:00:00 2001 From: MSGhero Date: Sat, 26 Mar 2016 17:10:56 -0500 Subject: [PATCH 1/2] Watch Expressions Added watching the hscript-parsed output of an expression --- .../system/debug/console/ConsoleCommands.hx | 30 +++++++++++++++++++ flixel/system/debug/watch/Watch.hx | 26 ++++++++++++++++ flixel/system/debug/watch/WatchEntry.hx | 27 +++++++++++++---- flixel/system/frontEnds/WatchFrontEnd.hx | 28 +++++++++++++++++ 4 files changed, 106 insertions(+), 5 deletions(-) diff --git a/flixel/system/debug/console/ConsoleCommands.hx b/flixel/system/debug/console/ConsoleCommands.hx index 64878cd4a6..f355e1c21d 100644 --- a/flixel/system/debug/console/ConsoleCommands.hx +++ b/flixel/system/debug/console/ConsoleCommands.hx @@ -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, PlaceAtMouse:Bool, ExtraParams:Array)\" 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."); @@ -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) diff --git a/flixel/system/debug/watch/Watch.hx b/flixel/system/debug/watch/Watch.hx index 701ad5dd9d..5268eb0c0a 100644 --- a/flixel/system/debug/watch/Watch.hx +++ b/flixel/system/debug/watch/Watch.hx @@ -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. */ diff --git a/flixel/system/debug/watch/WatchEntry.hx b/flixel/system/debug/watch/WatchEntry.hx index 4a765207e2..c2576d3cb9 100644 --- a/flixel/system/debug/watch/WatchEntry.hx +++ b/flixel/system/debug/watch/WatchEntry.hx @@ -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; @@ -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. @@ -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 { @@ -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); @@ -166,8 +176,15 @@ 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); + } + else + { + valueDisplay.text = Std.string(ConsoleUtil.runCommand(field)); + } } #end diff --git a/flixel/system/frontEnds/WatchFrontEnd.hx b/flixel/system/frontEnds/WatchFrontEnd.hx index 39100c9049..7d8aa73e08 100644 --- a/flixel/system/frontEnds/WatchFrontEnd.hx +++ b/flixel/system/frontEnds/WatchFrontEnd.hx @@ -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 + 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 + FlxG.game.debugger.watch.removeExpr(Expression, DisplayName); + #end + } + /** * Just needed to create an instance. */ From df95d23244ffb5d76c6a21899ec84079651f34bc Mon Sep 17 00:00:00 2001 From: MSGhero Date: Sun, 27 Mar 2016 11:22:33 -0400 Subject: [PATCH 2/2] Check if hscript is included --- flixel/system/debug/watch/WatchEntry.hx | 2 ++ flixel/system/frontEnds/WatchFrontEnd.hx | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/flixel/system/debug/watch/WatchEntry.hx b/flixel/system/debug/watch/WatchEntry.hx index c2576d3cb9..cc8bbbdb77 100644 --- a/flixel/system/debug/watch/WatchEntry.hx +++ b/flixel/system/debug/watch/WatchEntry.hx @@ -181,10 +181,12 @@ class WatchEntry implements IFlxDestroyable var property:Dynamic = Reflect.getProperty(object, field); valueDisplay.text = Std.string(property); } + #if hscript else { valueDisplay.text = Std.string(ConsoleUtil.runCommand(field)); } + #end } #end diff --git a/flixel/system/frontEnds/WatchFrontEnd.hx b/flixel/system/frontEnds/WatchFrontEnd.hx index 7d8aa73e08..7532f2f5d8 100644 --- a/flixel/system/frontEnds/WatchFrontEnd.hx +++ b/flixel/system/frontEnds/WatchFrontEnd.hx @@ -90,7 +90,7 @@ class WatchFrontEnd */ public function addExpr(Expression:String, ?DisplayName:String):Void { - #if FLX_DEBUG + #if (FLX_DEBUG && hscript) FlxG.game.debugger.watch.add(null, Expression, DisplayName); #end } @@ -104,7 +104,7 @@ class WatchFrontEnd */ public function removeExpr(?Expression:String, ?DisplayName:String):Void { - #if FLX_DEBUG + #if (FLX_DEBUG && hscript) FlxG.game.debugger.watch.removeExpr(Expression, DisplayName); #end }