From 96ff07034f4bb9b3fb7e1df357a1da4b55e4f94c Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 16 Sep 2021 22:25:05 -0600 Subject: [PATCH] NAT playground accept commands with TextInput --- .../nat-flixel-playground/source/Main.kiss | 2 +- .../nat-flixel-playground/source/PlayState.hx | 4 ++ .../source/PlayState.kiss | 51 ++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/projects/nat-flixel-playground/source/Main.kiss b/projects/nat-flixel-playground/source/Main.kiss index 5c6e3d39..b062f683 100644 --- a/projects/nat-flixel-playground/source/Main.kiss +++ b/projects/nat-flixel-playground/source/Main.kiss @@ -1,3 +1,3 @@ (method new [] (super) - (addChild (new FlxGame 320 240 PlayState))) + (addChild (new FlxGame 320 240 PlayState 1 60 60 true false))) \ No newline at end of file diff --git a/projects/nat-flixel-playground/source/PlayState.hx b/projects/nat-flixel-playground/source/PlayState.hx index 816fe1ae..740f361f 100644 --- a/projects/nat-flixel-playground/source/PlayState.hx +++ b/projects/nat-flixel-playground/source/PlayState.hx @@ -8,6 +8,10 @@ import nat.BoolExpInterp; import nat.Archive; import nat.ArchiveUI; import nat.ArchiveController; +import flixel.addons.ui.FlxInputText; +import flixel.addons.ui.FlxUIPopup; +import flixel.text.FlxText; +import flixel.util.FlxColor; using StringTools; diff --git a/projects/nat-flixel-playground/source/PlayState.kiss b/projects/nat-flixel-playground/source/PlayState.kiss index b82311b0..2838f335 100644 --- a/projects/nat-flixel-playground/source/PlayState.kiss +++ b/projects/nat-flixel-playground/source/PlayState.kiss @@ -2,21 +2,51 @@ (method &override :Void create [] (super.create) - // TODO find a way to pass the archiveDir to a HaxeFlixel game - /*(let [[archiveDir] (Sys.args)] + // TODO find a better way to pass the archiveDir to a HaxeFlixel game + (let [archiveDir (or (Sys.getEnv "NAT_DIR") (throw "NAT_DIR environment variable must be set"))] (new ArchiveController (new Archive archiveDir) - this))*/) + this)) + + (add messages) + + // TODO make a button that can be clicked to run typeCommand() + // TODO make sprites for entries that have images + ) (method &override :Void update [:Float elapsed] - (super.update elapsed)) + (super.update elapsed) + + // Press ENTER to type a command to run + (when (and !textInput FlxG.keys.justPressed.ENTER) + (typeCommand)) + (when (and textInput !textInput.alive) + (set textInput.callback null) + (set textInput null))) + +(method :Void typeCommand [] + (enterText + "command to run" + ->commandName (controller.tryRunCommand commandName) + Math.POSITIVE_INFINITY)) (prop &mut :ArchiveController controller) (method :Void setController [controller] (set this.controller controller)) +(prop &mut :FlxInputText textInput null) + (method :Void enterText [prompt resolve maxLength] - (resolve "")) + (set textInput (new FlxInputText 0 0 300 prompt)) + (textInput.screenCenter) + (set textInput.callback + ->:Void [text action] + (case [text action] + ([text FlxInputText.ENTER_ACTION] + (textInput.kill) + (resolve text)) + (otherwise {}))) + (add textInput)) (method :Void enterNumber [prompt resolve min max &opt inStepsOf] (resolve 0)) @@ -27,6 +57,7 @@ (method :Void chooseEntries [prompt archive resolve min max] (_chooseEntries prompt archive resolve min max [])) +// TODO is it possible to resolve with less than max? (method :Void _chooseEntries [prompt archive resolve min max :Array collectedEntries] (let [&mut :Void->Void chooseNextEntry null @@ -48,8 +79,14 @@ (doFor e changeSet (print (archive.fullString e)))) +(prop &mut :Int messageY 0) +(prop :FlxGroup messages (new FlxGroup)) (method :Void displayMessage [message] - (print message)) + (messages.add (new FlxText 0 messageY 0 message)) + (+= messageY 8)) (method :Void reportError [error] - (print error)) + (let [text (new FlxText 0 messageY 0 error)] + (text.setFormat null 8 FlxColor.RED) + (messages.add text) + (+= messageY 8)))