writeOnSprite allow multiline with anchors

This commit is contained in:
2022-01-03 13:15:07 -07:00
parent c39ef96548
commit 37be7bbc08
2 changed files with 24 additions and 16 deletions

View File

@@ -20,6 +20,8 @@ typedef RelativePosition = {
?anchorY:RelativeCoordinate, // default Percent(0.5)
?sizeX:RelativeCoordinate,
?sizeY:RelativeCoordinate,
?offsetX:Int,
?offsetY:Int
};
@:build(kiss.Kiss.build())

View File

@@ -8,18 +8,19 @@
(-= y (coordIn pos.anchorY (/ stamp.height stamp.scale.y)))
[x y]))
(function :Int coordIn [:RelativeCoordinate coord :Int range]
(Math.round
(case coord
((when (and (>= p -1) (< p 0)) (Percent p))
(+ range (* p range)))
((when (and (>= p -range) (< p 0)) (Pixels p))
(+ range p))
((when (<= p 1) (Percent p))
(* range p))
((when (<= p range) (Pixels p))
p)
(otherwise (throw "$coord is out of range $range")))))
(function :Int coordIn [:RelativeCoordinate coord :Int range &opt :Int offset]
(+ (or offset 0)
(Math.round
(case coord
((when (and (>= p -1) (< p 0)) (Percent p))
(+ range (* p range)))
((when (and (>= p -range) (< p 0)) (Pixels p))
(+ range p))
((when (<= p 1) (Percent p))
(* range p))
((when (<= p range) (Pixels p))
p)
(otherwise (throw "$coord is out of range $range"))))))
(function :Void scaleStampOn [:FlxSprite stamp :FlxSprite canvas :RelativePosition pos]
(let [&mut x 0 &mut y 0]
@@ -37,7 +38,12 @@
// TODO allow specifying size relative to canvas
(function :Void writeOnSprite [:String text :Int size :FlxSprite canvas :RelativePosition pos &opt :FlxColor color]
(let [text (new FlxText 0 0 0 text size)]
(when color
(set text.color color))
(drawOnSprite text canvas pos)))
(let [lines (text.split "\n")
&mut offsetY (/ (* size lines.length) -2)]
(doFor text lines
(set pos.offsetY offsetY)
(+= offsetY size)
(let [text (new FlxText 0 0 0 text size)]
(when color
(set text.color color))
(drawOnSprite text canvas pos)))))