allowOutOfRange for SpriteTools positioning

This commit is contained in:
2023-06-22 13:21:46 -06:00
parent 7381447087
commit 70a1f88015

View File

@@ -1,24 +1,24 @@
// Calculate where to draw the given stamp sprite on the given canvas sprite, as percentages or pixels from edge
(function :Array<Int> positionOn [:FlxSprite stamp :FlxSprite canvas :RelativePosition pos]
(function :Array<Int> positionOn [:FlxSprite stamp :FlxSprite canvas :RelativePosition pos &opt :Bool allowOutOfRange]
(unless pos.anchorX (set pos.anchorX (Percent 0.5)))
(unless pos.anchorY (set pos.anchorY (Percent 0.5)))
(let [&mut x (coordIn pos.x (/ canvas.width canvas.scale.x) pos.offsetX)
&mut y (coordIn pos.y (/ canvas.height canvas.scale.y) pos.offsetY)]
(let [&mut x (coordIn pos.x (/ canvas.width canvas.scale.x) pos.offsetX allowOutOfRange)
&mut y (coordIn pos.y (/ canvas.height canvas.scale.y) pos.offsetY allowOutOfRange)]
(-= x (coordIn pos.anchorX stamp.width))
(-= y (coordIn pos.anchorY stamp.height))
[x y]))
(function :Int coordIn [:RelativeCoordinate coord :Float range &opt :Int offset]
(function :Int coordIn [:RelativeCoordinate coord :Float range &opt :Int offset &opt :Bool allowOutOfRange]
(+ (or offset 0)
(Math.round
(case coord
((when (and (>= p -1) (< p 0)) (Percent p))
((when (and (or allowOutOfRange (>= p -1)) (< p 0)) (Percent p))
(+ range (* p range)))
((when (and (>= p -range) (< p 0)) (Pixels p))
((when (and (or allowOutOfRange (>= p -range)) (< p 0)) (Pixels p))
(+ range p))
((when (<= p 1) (Percent p))
((when (or allowOutOfRange (<= p 1)) (Percent p))
(* range p))
((when (<= p range) (Pixels p))
((when (or allowOutOfRange (<= p range)) (Pixels p))
p)
(otherwise (throw "$coord is out of range $range"))))))