diff --git a/projects/nat-archive-tool/src/nat/ArchiveController.kiss b/projects/nat-archive-tool/src/nat/ArchiveController.kiss index 899a5233..a34aeec7 100644 --- a/projects/nat-archive-tool/src/nat/ArchiveController.kiss +++ b/projects/nat-archive-tool/src/nat/ArchiveController.kiss @@ -3,16 +3,16 @@ (method :Void _collectAndValidateArg [:CommandArg arg :Dynamic->Void continuation] (case arg.type (SelectedEntry - (if (= 1 selectedEntries.length) - (continuation (first selectedEntries)) - (ui.reportError "The requested command expects 1 entry to be selected. You have selected: $selectedEntries.length"))) + (if (= 1 _selectedEntries.length) + (continuation (first _selectedEntries)) + (ui.reportError "The requested command expects 1 entry to be selected. You have selected: $_selectedEntries.length"))) ((SelectedEntries min max) (unless min (set min 0)) // TODO might want to optimize this O(n) count operation by pre-calculating it (unless max (set max (count archive.entries))) - (if !(<= min selectedEntries.length max) - (ui.reportError "The requested command expects between $min and $max entries to be selected. You have selected: $selectedEntries.length") - (continuation selectedEntries))) + (if !(<= min _selectedEntries.length max) + (ui.reportError "The requested command expects between $min and $max entries to be selected. You have selected: $_selectedEntries.length") + (continuation _selectedEntries))) ((Text maxLength) (unless maxLength (set maxLength Math.POSITIVE_INFINITY)) (ui.enterText @@ -113,14 +113,12 @@ (set lastCollector (_composeArgCollector collectedArgs arg lastCollector))) (lastCollector))) + (defMacro defCommand [name args &body body] (let [argPairs (groups (expList args) 2) methodArgs (for [name type] argPairs - // TODO write an exprCase macro that simplifies this terrible mess, - // and maybe adds back precise pattern matching instead of relying - // on partial string matching (exprCase type ((exprOr SelectedEntry OneEntry) `:nat.Entry ,name) ((exprOr (SelectedEntries _ _) (Entries _ _)) `:Array ,name) @@ -139,10 +137,12 @@ (var :Array commandNames []) +(method isSelected [:Entry e] + !(= -1 (_selectedEntries.indexOf e))) (defNew [&prop :Archive archive &prop :ArchiveUI ui] - [&mut :Array selectedEntries [] + [&mut :Array _selectedEntries [] &mut :ChangeSet lastChangeSet [] :Map commands (new Map) :NameSystem nameSystem (new NameSystem)] @@ -163,21 +163,28 @@ (+ "Available commands:\n" (commandNames.join "\n"))) []) + (method selectionCommand [entries] + (let [lastSelectedEntries _selectedEntries] + (set _selectedEntries entries) + (ui.onSelectionChanged entries lastSelectedEntries)) []) + (defCommand SelectEntry [e OneEntry] - (set selectedEntries [e]) []) + (selectionCommand [e])) (defCommand ToggleEntrySelection [e OneEntry] - (unless (selectedEntries.remove e) - (selectedEntries.push e)) []) + (let [newSelection (_selectedEntries.copy)] + (unless (newSelection.remove e) + (newSelection.push e)) + (selectionCommand newSelection))) (defCommand SelectEntries [entries (Entries null null)] - (set selectedEntries entries) []) + (selectionCommand entries) []) (defCommand SelectAllEntries [] - (set selectedEntries (for =>id e archive.entries e)) []) + (selectionCommand (for =>id e archive.entries e))) (defCommand SelectLastChangeSet [] - (set selectedEntries lastChangeSet) []) + (selectionCommand lastChangeSet)) (defCommand PrintSelectedEntries [entries (SelectedEntries null null)] (doFor e entries (ui.displayMessage (archive.fullString e))) []) diff --git a/projects/nat-archive-tool/src/nat/ArchiveUI.hx b/projects/nat-archive-tool/src/nat/ArchiveUI.hx index 0d1eb4cf..17a7e9ce 100644 --- a/projects/nat-archive-tool/src/nat/ArchiveUI.hx +++ b/projects/nat-archive-tool/src/nat/ArchiveUI.hx @@ -42,4 +42,9 @@ interface ArchiveUI { * Tell the user that something is wrong */ function reportError(error:String):Void; + + /** + * Update UI to show that the set of selected entries has changed + */ + function onSelectionChanged(selectedEntries:Array, lastSelectedEntries:Array):Void; } diff --git a/projects/nat-archive-tool/src/nat/CLI.kiss b/projects/nat-archive-tool/src/nat/CLI.kiss index 4d08aed5..8ab855e2 100644 --- a/projects/nat-archive-tool/src/nat/CLI.kiss +++ b/projects/nat-archive-tool/src/nat/CLI.kiss @@ -90,3 +90,7 @@ (method :Void reportError [error] (print error)) + +(method :Void onSelectionChanged [:Array selectedEntries :Array _] + (print "Selected:") + (controller.PrintSelectedEntries selectedEntries)) diff --git a/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss b/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss index 1ceeba0a..1ca517b4 100644 --- a/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss +++ b/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss @@ -22,12 +22,10 @@ (enableMouseDrag) })) - // TODO add onEntrySelected(), onEntryDeselected() callback to ArchiveUI and use these to change color if SelectByName or SelectByTags are used on an Entry that has a sprite in the entryspritesystem's dictionary (set mousePressedCallback ->[self _x _y] { (controller.ToggleEntrySelection e) - (updateColor) }) (set mouseStopDragCallback ->[self _dx _dy] @@ -35,7 +33,7 @@ (dictSet positions positionKey (object x (cast this.x Float) y (cast this.y Float) z 0.0))))) (method updateColor [] - (if !(= -1 (controller.selectedEntries.indexOf e)) + (if (controller.isSelected e) { (set color FlxColor.BLUE) } diff --git a/projects/nat-flixel-desktop-playground/source/PlayState.kiss b/projects/nat-flixel-desktop-playground/source/PlayState.kiss index e477c059..99dfbadb 100644 --- a/projects/nat-flixel-desktop-playground/source/PlayState.kiss +++ b/projects/nat-flixel-desktop-playground/source/PlayState.kiss @@ -208,3 +208,8 @@ (let [text (new FlxText 0 0 0 (error.replace "\n" "|"))] (text.setFormat null 8 FlxColor.RED) (showUI text))) + +(method :Void onSelectionChanged [:Array selectedEntries :Array lastSelectedEntries] + (doFor e (selectedEntries.concat lastSelectedEntries) + (whenLet [sprite (dictGet spriteSystem.sprites e.id)] + (sprite.updateColor)))) \ No newline at end of file