From 2d53027da38e59ecd98524de311818a7d4a7d826 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 27 Jul 2022 16:32:16 +0000 Subject: [PATCH] defAndCall macro for refactoring --- .../source/HabitState.kiss | 21 +++++++++++++------ .../src/kiss_tools/RefactorUtil.kiss | 8 +++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 projects/kiss-tools/src/kiss_tools/RefactorUtil.kiss diff --git a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss index fcbc7bc8..cf8cc0e1 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss @@ -1,3 +1,5 @@ +(loadFrom "kiss-tools" "src/kiss_tools/RefactorUtil.kiss") + (prop &mut :Jigsawx jigsaw) (prop &mut :FlxCamera pieceCamera) (prop &mut :FlxCamera uiCamera) @@ -27,11 +29,18 @@ (when FlxG.keys.justPressed.ESCAPE (Sys.exit 0)) - (when FlxG.keys.justPressed.SPACE - (set save.data.backgroundIndex #{(save.data.backgroundIndex + 1) % backgroundOptions.length;}#) - (save.flush) - // setModel so the entry text gets remade in inverted colors - (setModel model (nth model.rewardFiles rewardFileIndex))) + // TODO provide a saner/configurable set of bindings to trigger these ui action functions + { + (when FlxG.keys.justPressed.SPACE + (defAndCall method toggleBackgroundColor + (set save.data.backgroundIndex #{(save.data.backgroundIndex + 1) % backgroundOptions.length;}#) + (save.flush) + // setModel so the entry text gets remade in inverted/lightened colors when necessary + (setModel model (nth model.rewardFiles rewardFileIndex)))) + (when FlxG.keys.justPressed.ENTER + 0) + } + // drag along connected pieces (when draggingSprite @@ -41,7 +50,7 @@ (doFor s (recursivelyConnectedPieces draggingSprite) (+= s.x dx) (+= s.y dy))) - // This hacks around a tricky edge case + // This hacks around a tricky edge case -- or so I thought (when FlxG.mouse.justReleased (draggingSprite.stopDrag))) diff --git a/projects/kiss-tools/src/kiss_tools/RefactorUtil.kiss b/projects/kiss-tools/src/kiss_tools/RefactorUtil.kiss new file mode 100644 index 00000000..e302fc82 --- /dev/null +++ b/projects/kiss-tools/src/kiss_tools/RefactorUtil.kiss @@ -0,0 +1,8 @@ +// Quick and dirty way to make a block reusable without restructuring code +// TODO won't work if locals are used in the body +(defMacro defAndCall [type name &body body] + (assert (exprCase type + (function true) + (method true) + (_ false)) "the first argument to defAndCall must be a symbol: function or method") + `{(,type ,name [] ,@body) (,name)})