draw and write on FlxSprites relative to their boundaries

This commit is contained in:
2021-12-15 15:53:07 -07:00
parent 55a9be798c
commit ea7f469cd7
3 changed files with 56 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import flixel.system.FlxSound;
import hollywoo.Movie;
import hollywoo_flixel.ActorFlxSprite;
import hollywoo_flixel.SceneFlxState;
import hollywoo_flixel.SpriteTools;
enum FlxStagePosition {
Left;

View File

@@ -0,0 +1,24 @@
package hollywoo_flixel;
import kiss.Prelude;
import kiss.List;
import flixel.FlxSprite;
import flixel.util.FlxColor;
import flixel.text.FlxText;
import flixel.math.FlxPoint;
enum RelativeCoordinate {
// Negative means to count back from the far edge
Pixels(p:Int); // Pixels from an edge.
Percent(p:Float); // Percent from an edge. -1 to 1
}
typedef RelativePosition = {
x:RelativeCoordinate,
y:RelativeCoordinate,
?anchorX:RelativeCoordinate, // default Percent(0.5)
?anchorY:RelativeCoordinate, // default Percent(0.5)
};
@:build(kiss.Kiss.build())
class SpriteTools {}

View File

@@ -0,0 +1,31 @@
// 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]
(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))
&mut y (coordIn pos.y (/ canvas.height canvas.scale.y))]
(-= x (coordIn pos.anchorX (/ stamp.width stamp.scale.x)))
(-= 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")))))
// TODO allow specifying size relative to canvas
(function :Void drawOnSprite [:FlxSprite stamp :FlxSprite canvas :RelativePosition pos]
(let [[x y] (positionOn stamp canvas pos)]
(canvas.stamp stamp x y)))
// TODO allow specifying size relative to canvas
(function :Void writeOnSprite [:String text :Int size :FlxSprite canvas :RelativePosition pos]
(drawOnSprite (new FlxText 0 0 0 text size) canvas pos))