From a49662863b9ac3b5cad12f2cedbac3c8d4dac845 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 15 Dec 2021 15:53:07 -0700 Subject: [PATCH] draw and write on FlxSprites relative to their boundaries --- .../src/hollywoo_flixel/FlxMovie.hx | 1 + .../src/hollywoo_flixel/SpriteTools.hx | 24 ++++++++++++++ .../src/hollywoo_flixel/SpriteTools.kiss | 31 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.hx create mode 100644 projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.kiss diff --git a/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx b/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx index f529b0cb..341cbfcd 100644 --- a/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx +++ b/projects/hollywoo-flixel/src/hollywoo_flixel/FlxMovie.hx @@ -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; diff --git a/projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.hx b/projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.hx new file mode 100644 index 00000000..e5443484 --- /dev/null +++ b/projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.hx @@ -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 {} diff --git a/projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.kiss b/projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.kiss new file mode 100644 index 00000000..8bb0c364 --- /dev/null +++ b/projects/hollywoo-flixel/src/hollywoo_flixel/SpriteTools.kiss @@ -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 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)) \ No newline at end of file