From 0d0f7b01d5b90fcc86f0eac9c86b1ff5e9cd56bd Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 5 Jul 2022 00:14:17 +0000 Subject: [PATCH] add scale to each image in Images components --- .../src/nat/ArchiveController.kiss | 18 ++++---- projects/nat-archive-tool/src/nat/Lib.kiss | 7 ++++ .../src/nat/components/Images2.hx | 7 ++++ .../src/nat/systems/ImageAttachmentSystem.hx | 1 + .../nat/systems/ImageAttachmentSystem.kiss | 41 ++++++++++++++++--- .../source/EntrySprite.kiss | 13 +++--- 6 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 projects/nat-archive-tool/src/nat/components/Images2.hx diff --git a/projects/nat-archive-tool/src/nat/ArchiveController.kiss b/projects/nat-archive-tool/src/nat/ArchiveController.kiss index 14e71b8c..a8037227 100644 --- a/projects/nat-archive-tool/src/nat/ArchiveController.kiss +++ b/projects/nat-archive-tool/src/nat/ArchiveController.kiss @@ -285,10 +285,10 @@ (method adjustImagePins [:Array entries increment] (doFor e entries - (if (hasComponent e Images) - (withWritableComponents archive e [images Images] + (if (hasComponent e Images2) + (withWritableComponents archive e [images Images2] (set images.pinnedImageIndex (max 0 (min (- images.imageFiles.length 1) (+ increment images.pinnedImageIndex))))) - (ui.reportError "Entry $e has no Images component"))) + (ui.reportError "Entry $e has no Images2 component"))) entries) (defCommand PinNextImage [entries (SelectedEntries 1 null)] @@ -299,10 +299,14 @@ (defCommand SetScale [entries (SelectedEntries 1 null) scale (Number 0 null null)] (doFor e entries - (if (hasComponent e Scale) - (withWritableComponents archive e [scaleComponent Scale] - (set scaleComponent scale)) - (addComponent archive e Scale scale))) + (cond + ((hasComponent e Images2) + (withWritableComponents archive e [i2 Images2] + (setNth i2.imageScales i2.pinnedImageIndex scale))) + ((hasComponent e Scale) + (withWritableComponents archive e [scaleComponent Scale] + (set scaleComponent scale))) + (true (addComponent archive e Scale scale)))) entries) (defCommand CreatePlayground [name (Text null) catsMatchExp (Text null)] diff --git a/projects/nat-archive-tool/src/nat/Lib.kiss b/projects/nat-archive-tool/src/nat/Lib.kiss index e27f5ad6..7402ed48 100644 --- a/projects/nat-archive-tool/src/nat/Lib.kiss +++ b/projects/nat-archive-tool/src/nat/Lib.kiss @@ -109,3 +109,10 @@ (withWritableComponents archive e [tags Tags] (doFor tag tagsToRemove (tags.remove tag))))) +(function :Float getScale [:nat.Entry e] + (if (hasComponent e Images2) + (let [i2 (readComponent e Images2)] + (nth i2.imageScales i2.pinnedImageIndex)) + (if (hasComponent e Scale) + (readComponent e Scale) + 1.0))) \ No newline at end of file diff --git a/projects/nat-archive-tool/src/nat/components/Images2.hx b/projects/nat-archive-tool/src/nat/components/Images2.hx new file mode 100644 index 00000000..877ef397 --- /dev/null +++ b/projects/nat-archive-tool/src/nat/components/Images2.hx @@ -0,0 +1,7 @@ +package nat.components; + +typedef Images2 = { + imageFiles:Array, + imageScales:Array, + pinnedImageIndex:Int +}; diff --git a/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.hx b/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.hx index 31f29602..9bc73fad 100644 --- a/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.hx +++ b/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.hx @@ -2,6 +2,7 @@ package nat.systems; import kiss.Prelude; import kiss.List; +using StringTools; @:build(kiss.Kiss.build()) class ImageAttachmentSystem extends AttachmentSystem {} diff --git a/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.kiss b/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.kiss index 0a54c973..216dfd83 100644 --- a/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.kiss +++ b/projects/nat-archive-tool/src/nat/systems/ImageAttachmentSystem.kiss @@ -4,8 +4,39 @@ (super ["jpg" "jpeg" "png"] ->[archive e imageFiles &opt ui] - (unless (hasComponent e Images) - (addComponent archive e Images - (object - imageFiles imageFiles - pinnedImageIndex 0))))) \ No newline at end of file + (unless (upgradeToVersion2 archive e) + // TODO an edge case exists when more image files are added to an Entry + // after it is assigned an Images component. The new files won't be added + (unless (hasComponent e Images2) + (addComponent archive e Images2 + (object + imageFiles imageFiles + imageScales (for _ (range imageFiles.length) 1.0) + pinnedImageIndex 0)))))) + +// Retrofit Images components into Images2 components, +// which can track a separate scale value for each image. +// Also, remove text image files -- now that text sprites +// are generated on the fly. +(function :Bool upgradeToVersion2 [:nat.Archive archive :nat.Entry e] + (when (hasComponent e Images) + (let [images1 (readComponent e Images) + scale (readComponentOr e Scale 1.0) + pIdx images1.pinnedImageIndex + textImageFiles (filter images1.imageFiles ->[:String f] (f.startsWith "textImage"))] + (removeComponent archive e Images) + (removeFiles archive e textImageFiles) + (doFor f textImageFiles (images1.imageFiles.remove f)) + // If all images were removed (the entry only had a text image), don't add Images2 + (unless images1.imageFiles (return true)) + + (addComponent archive e Images2 + (object + imageFiles images1.imageFiles + imageScales (cast (concat + (for _ (range pIdx) 1.0) + [scale] + (for _ (range (- images1.imageFiles.length 1 pIdx)) 1.0))) + pinnedImageIndex pIdx)) + (removeComponent archive e Scale) + true))) \ No newline at end of file diff --git a/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss b/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss index 19e05b09..40186c76 100644 --- a/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss +++ b/projects/nat-flixel-desktop-playground/source/EntrySprite.kiss @@ -8,18 +8,17 @@ &prop :ArchiveController controller] [&mut :Bool selected false] (super position.x position.y) - (if (hasComponent e Images) - (let [images (readComponent e Images)] + (if (hasComponent e Images2) + (let [images (readComponent e Images2)] (.onComplete (BitmapData.loadFromFile (joinPath archive.archiveDir "files" (nth images.imageFiles images.pinnedImageIndex))) ->bitmapData { (loadGraphic bitmapData) })) - (set pixels .pixels (new FlxText 0 0 0 (readComponent e Name) PlayState.TEXT_SIZE))) + (set pixels .pixels (new FlxText 0 0 0 (readComponent e Name) PlayState.TEXT_SIZE))) (updateColor) - (when (hasComponent e Scale) - (let [:Float scale (readComponent e Scale)] - (this.scale.set scale scale) - (updateHitbox))) + (let [:Float scale (getScale e)] + (this.scale.set scale scale) + (updateHitbox)) (enableMouseClicks false) (enableMouseDrag) (set mouseStartDragCallback