add scale to each image in Images components

This commit is contained in:
2022-07-05 00:14:17 +00:00
parent d51940f1fc
commit 0d0f7b01d5
6 changed files with 68 additions and 19 deletions

View File

@@ -285,10 +285,10 @@
(method adjustImagePins [:Array<Entry> 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)]

View File

@@ -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)))

View File

@@ -0,0 +1,7 @@
package nat.components;
typedef Images2 = {
imageFiles:Array<FileRef>,
imageScales:Array<Float>,
pinnedImageIndex:Int
};

View File

@@ -2,6 +2,7 @@ package nat.systems;
import kiss.Prelude;
import kiss.List;
using StringTools;
@:build(kiss.Kiss.build())
class ImageAttachmentSystem extends AttachmentSystem {}

View File

@@ -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)))))
(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)))

View File

@@ -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