move SpriteTools to a new kiss-flixel haxelib

This commit is contained in:
2022-03-28 15:34:20 -06:00
parent 7a91e0d2e7
commit 0fbb526e70
6 changed files with 21 additions and 4 deletions

View File

@@ -4,6 +4,7 @@
"classPath": "src/",
"dependencies": {
"kiss": "",
"kiss-flixel": "",
"hollywoo": ""
},
"url": "https://github.com/NQNStudios/kisslang",

View File

@@ -12,13 +12,12 @@ import hollywoo.Movie;
import hollywoo.Scene;
import hollywoo.Director;
import hollywoo_flixel.FlxMovie;
import hollywoo_flixel.SpriteTools;
import flixel.util.FlxColor;
import flixel.text.FlxText;
import flixel.system.FlxSound;
import flixel.util.FlxTimer;
import haxe.Constraints;
import kiss_flixel.SpriteTools;
@:build(kiss.Kiss.build())
class FlxDirector implements Director<String, FlxStagePosition, FlxStageFacing, FlxScreenPosition, ActorFlxSprite, FlxSound, String, FlxSprite, FlxSound> {}

View File

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

View File

@@ -1,28 +0,0 @@
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)
?sizeX:RelativeCoordinate,
?sizeY:RelativeCoordinate,
?offsetX:Int,
?offsetY:Int
};
@:build(kiss.Kiss.build())
class SpriteTools {}

View File

@@ -1,53 +0,0 @@
// 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) pos.offsetX)
&mut y (coordIn pos.y (/ canvas.height canvas.scale.y) pos.offsetY)]
(-= x (coordIn pos.anchorX stamp.width))
(-= y (coordIn pos.anchorY stamp.height))
[x y]))
(function :Int coordIn [:RelativeCoordinate coord :Float 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]
(when pos.sizeX
(set x (coordIn pos.sizeX canvas.frameWidth)))
(when pos.sizeY
(set y (coordIn pos.sizeY canvas.frameHeight)))
(stamp.setGraphicSize x y)
(stamp.updateHitbox)))
(function :Void drawOnSprite [:FlxSprite stamp :FlxSprite canvas :RelativePosition pos]
(scaleStampOn stamp canvas pos)
(let [[x y] (positionOn stamp canvas pos)]
(let [oX stamp.origin.x
oY stamp.origin.y]
(stamp.origin.set 0 0)
(canvas.stamp stamp x y)
(stamp.origin.set oX oY))))
// TODO allow specifying size relative to canvas
(function :Void writeOnSprite [:String text :Int size :FlxSprite canvas :RelativePosition pos &opt :FlxColor color]
(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)))))