diff --git a/assets/images/transitions/circle.png b/assets/images/transitions/circle.png index 93b03c6d..a9645ed8 100644 Binary files a/assets/images/transitions/circle.png and b/assets/images/transitions/circle.png differ diff --git a/assets/images/transitions/diagonal_gradient.png b/assets/images/transitions/diagonal_gradient.png index 289bb6f0..9555ea7d 100644 Binary files a/assets/images/transitions/diagonal_gradient.png and b/assets/images/transitions/diagonal_gradient.png differ diff --git a/assets/images/transitions/diamond.png b/assets/images/transitions/diamond.png index 80b800af..4abb5cc8 100644 Binary files a/assets/images/transitions/diamond.png and b/assets/images/transitions/diamond.png differ diff --git a/assets/images/transitions/square.png b/assets/images/transitions/square.png index 866f064a..a6667f2c 100644 Binary files a/assets/images/transitions/square.png and b/assets/images/transitions/square.png differ diff --git a/flixel/addons/transition/FlxTransitionSprite.hx b/flixel/addons/transition/FlxTransitionSprite.hx index 67780982..f75b4120 100644 --- a/flixel/addons/transition/FlxTransitionSprite.hx +++ b/flixel/addons/transition/FlxTransitionSprite.hx @@ -1,14 +1,63 @@ package flixel.addons.transition; -import flixel.addons.transition.FlxTransitionSprite.TransitionStatus; import flixel.FlxG; import flixel.FlxSprite; +import flixel.addons.transition.FlxTransitionSprite; import flixel.graphics.FlxGraphic; import flixel.math.FlxPoint; import flixel.system.FlxAssets.FlxGraphicAsset; import flixel.util.FlxTimer; import openfl.display.BitmapData; +#if html5 +@:keep @:bitmap("assets/images/transitions/circle.png") +private class RawGraphicTransTileCircle extends BitmapData {} +class GraphicTransTileCircle extends RawGraphicTransTileCircle +{ + static inline var WIDTH = 544; + static inline var HEIGHT = 32; + + public function new(?onLoad) + { + super(WIDTH, HEIGHT, true, 0xFFffffff, onLoad); + // Set properties because `@:bitmap` constructors ignore width/height + this.width = WIDTH; + this.height = HEIGHT; + } +} + +@:keep @:bitmap("assets/images/transitions/diamond.png") +private class RawGraphicTransTileDiamond extends BitmapData {} +class GraphicTransTileDiamond extends RawGraphicTransTileDiamond +{ + static inline var WIDTH = 544; + static inline var HEIGHT = 32; + + public function new(?onLoad) + { + super(WIDTH, HEIGHT, true, 0xFFffffff, onLoad); + // Set properties because `@:bitmap` constructors ignore width/height + this.width = WIDTH; + this.height = HEIGHT; + } +} + +@:keep @:bitmap("assets/images/transitions/square.png") +class RawGraphicTransTileSquare extends BitmapData {} +class GraphicTransTileSquare extends RawGraphicTransTileSquare +{ + static inline var WIDTH = 544; + static inline var HEIGHT = 32; + + public function new(?onLoad) + { + super(WIDTH, HEIGHT, true, 0xFFffffff, onLoad); + // Set properties because `@:bitmap` constructors ignore width/height + this.width = WIDTH; + this.height = HEIGHT; + } +} +#else @:keep @:bitmap("assets/images/transitions/circle.png") class GraphicTransTileCircle extends BitmapData {} @@ -17,6 +66,7 @@ class GraphicTransTileDiamond extends BitmapData {} @:keep @:bitmap("assets/images/transitions/square.png") class GraphicTransTileSquare extends BitmapData {} +#end /** * @@ -45,6 +95,9 @@ class FlxTransitionSprite extends FlxSprite } _delay = Delay; loadGraphic(Graphic, true, GraphicWidth, GraphicHeight); + + if (graphic == null) + return; graphic.persist = true; graphic.destroyOnNoUse = false; diff --git a/flixel/addons/transition/Transition.hx b/flixel/addons/transition/Transition.hx index 989f1485..3802bfc0 100644 --- a/flixel/addons/transition/Transition.hx +++ b/flixel/addons/transition/Transition.hx @@ -34,17 +34,10 @@ class Transition extends FlxSubState add(_effect); } - override public function update(elapsed:Float):Void - { - super.update(elapsed); - _effect.update(elapsed); - } - public override function destroy():Void { super.destroy(); finishCallback = null; - _effect.destroy(); _effect = null; } diff --git a/flixel/addons/transition/TransitionFade.hx b/flixel/addons/transition/TransitionFade.hx index cf05b8e6..37e7b923 100644 --- a/flixel/addons/transition/TransitionFade.hx +++ b/flixel/addons/transition/TransitionFade.hx @@ -4,6 +4,7 @@ import flash.display.BitmapData; import flixel.addons.transition.TransitionEffect; import flixel.addons.transition.FlxTransitionSprite.TransitionStatus; import flixel.FlxSprite; +import flixel.graphics.FlxGraphic; import flixel.tweens.FlxTween; import flixel.util.FlxColor; import flixel.util.FlxGradient; @@ -12,208 +13,191 @@ import openfl.Assets; import openfl.display.BitmapDataChannel; import openfl.geom.Matrix; import openfl.geom.Point; +import openfl.geom.Rectangle; +#if html5 +@:keep @:bitmap("assets/images/transitions/diagonal_gradient.png") +private class RawGraphicDiagonalGradient extends BitmapData {} +class GraphicDiagonalGradient extends RawGraphicDiagonalGradient +{ + static inline var WIDTH = 319; + static inline var HEIGHT = 128; + + public function new(?onLoad) + { + super(WIDTH, HEIGHT, true, 0xFFffffff, onLoad); + // Set properties because `@:bitmap` constructors ignore width/height + this.width = WIDTH; + this.height = HEIGHT; + } +} +#else @:keep @:bitmap("assets/images/transitions/diagonal_gradient.png") private class GraphicDiagonalGradient extends BitmapData {} +#end +typedef TweenEndValues = { ?x:Float, ?y:Float, ?alpha:Float }; /** * * @author larsiusprime */ class TransitionFade extends TransitionEffect { + public static inline var GRADIENT_PATH = "flixel/images/transitions/diagonal_gradient.png"; + var back:FlxSprite; - var tweenStr:String = ""; - var tweenStr2:String = ""; - var tweenValStart:Float = 0; - var tweenValStart2:Float = 0; - var tweenValEnd:Float = 0; - var tweenValEnd2:Float = 0; - + public function new(data:TransitionData) { super(data); - + back = makeSprite(data.direction.x, data.direction.y, data.region); back.scrollFactor.set(0, 0); add(back); } - + public override function destroy():Void { super.destroy(); back = null; } - - public override function start(NewStatus:TransitionStatus):Void + + public override function start(newStatus:TransitionStatus):Void { - super.start(NewStatus); - - setTweenValues(NewStatus, _data.direction.x, _data.direction.y); - - switch (tweenStr) - { - case "alpha": - back.alpha = tweenValStart; - case "x": - back.x = tweenValStart; - case "y": - back.y = tweenValStart; - } - switch (tweenStr2) - { - case "alpha": - back.alpha = tweenValStart2; - case "x": - back.x = tweenValStart2; - case "y": - back.y = tweenValStart2; - } - - var Values:Dynamic = {}; - Reflect.setField(Values, tweenStr, tweenValEnd); - if (tweenStr2 != "") - { - Reflect.setField(Values, tweenStr2, tweenValEnd2); - } + super.start(newStatus); + + final endValues:TweenEndValues = {}; + setTweenValues(newStatus == IN, _data.direction.x, _data.direction.y, back, endValues); + _data.tweenOptions.onComplete = finishTween; - FlxTween.tween(back, Values, _data.duration, _data.tweenOptions); + FlxTween.tween(back, endValues, _data.duration, _data.tweenOptions); } - - function setTweenValues(NewStatus:TransitionStatus, DirX:Float, DirY:Float):Void + + function setTweenValues(isIn:Bool, dirX:Float, dirY:Float, sprite:FlxSprite, values:TweenEndValues) { - if (DirX == 0 && DirY == 0) + final isOut = !isIn; + if (dirX == 0 && dirY == 0) { // no direction - tweenStr = "alpha"; - tweenValStart = NewStatus == IN ? 0.0 : 1.0; - tweenValEnd = NewStatus == IN ? 1.0 : 0.0; + sprite.alpha = isIn ? 0 : 1; + values.alpha = isOut ? 0 : 1; } - else if (Math.abs(DirX) > 0 && DirY == 0) + else if (dirX != 0 && dirY != 0) { - // horizontal wipe - tweenStr = "x"; - if (DirX > 0) + // diagonal wipe + if (dirX > 0) { - tweenValStart = NewStatus == IN ? -back.width : 0; - tweenValEnd = NewStatus == IN ? 0 : -back.width; + sprite.x = isIn ? -back.width : 0; + values.x = isOut ? -back.width : 0; } else { - tweenValStart = NewStatus == IN ? FlxG.width : -back.width / 2; - tweenValEnd = NewStatus == IN ? -back.width / 2 : FlxG.width; + sprite.x = isIn ? FlxG.width : FlxG.width - back.width; + values.x = isOut ? FlxG.width : FlxG.width - back.width; } + + return; } - else if ((DirX == 0 && Math.abs(DirY) > 0)) + else if (dirX != 0) { - // vertical wipe - tweenStr = "y"; - if (DirY > 0) + // horizontal wipe + if (dirX > 0) { - tweenValStart = NewStatus == IN ? -back.height : 0; - tweenValEnd = NewStatus == IN ? 0 : -back.height; + sprite.x = isIn ? -back.width : 0; + values.x = isOut ? -back.width : 0; } else { - tweenValStart = NewStatus == IN ? FlxG.height : -back.height / 2; - tweenValEnd = NewStatus == IN ? -back.height / 2 : FlxG.height; + sprite.x = isIn ? FlxG.width : -back.width / 2; + values.x = isOut ? FlxG.width : -back.width / 2; } } - else if (Math.abs(DirX) > 0 && Math.abs(DirY) > 0) + else { - // diagonal wipe - tweenStr = "x"; - tweenStr2 = "y"; - if (DirX > 0) - { - tweenValStart = NewStatus == IN ? -back.width : 0; - tweenValEnd = NewStatus == IN ? 0 : -back.width; - } - else - { - tweenValStart = NewStatus == IN ? FlxG.width : -back.width * (2 / 3); - tweenValEnd = NewStatus == IN ? -back.width * (2 / 3) : FlxG.width; - } - if (DirY > 0) + // vertical wipe + if (dirY > 0) { - tweenValStart2 = NewStatus == IN ? -back.height : 0; - tweenValEnd2 = NewStatus == IN ? 0 : -back.height; + sprite.y = isIn ? -back.height : 0; + values.y = isOut ? -back.height : 0; } else { - tweenValStart2 = NewStatus == IN ? FlxG.height : -back.height * (2 / 3); - tweenValEnd2 = NewStatus == IN ? -back.height * (2 / 3) : FlxG.height; + sprite.y = isIn ? FlxG.height : -back.height / 2; + values.y = isOut ? FlxG.height : -back.height / 2; } } } - - function makeSprite(DirX:Float, DirY:Float, region:FlxRect):FlxSprite + + function makeSprite(dirX:Float, dirY:Float, region:FlxRect):FlxSprite { - var s = new FlxSprite(region.x, region.y); - var locX:Float = 0; - var locY:Float = 0; - var angle:Int = 0; - var pixels:BitmapData = null; - if (DirX == 0 && DirY == 0) + final sprite = new FlxSprite(region.x, region.y); + + if (dirX == 0 && dirY == 0) { // no direction - s.makeGraphic(Std.int(region.width), Std.int(region.height), _data.color); + sprite.makeGraphic(Std.int(region.width), Std.int(region.height), _data.color); } - else if (DirX == 0 && Math.abs(DirY) > 0) + else if (dirX == 0 && dirY != 0) { // vertical wipe - locY = DirY > 0 ? region.height : 0; - angle = DirY > 0 ? 90 : 270; - s.makeGraphic(1, Std.int(region.height * 2), _data.color); - pixels = s.pixels; - var gvert = FlxGradient.createGradientBitmapData(1, Std.int(region.height), [_data.color, FlxColor.TRANSPARENT], 1, angle); - pixels.copyPixels(gvert, gvert.rect, new Point(0, locY)); - s.pixels = pixels; - s.scale.set(region.width, 1.0); - s.updateHitbox(); + sprite.makeGraphic(1, Std.int(region.height * 2), _data.color); + final angle = dirY > 0 ? 90 : 270; + final gradient = FlxGradient.createGradientBitmapData(1, Std.int(region.height), [_data.color, FlxColor.TRANSPARENT], 1, angle); + final destY = dirY > 0 ? region.height : 0; + sprite.pixels.copyPixels(gradient, gradient.rect, new Point(0, destY)); + sprite.scale.set(region.width, 1.0); + sprite.updateHitbox(); } - else if (Math.abs(DirX) > 0 && DirY == 0) + else if (dirX != 0 && dirY == 0) { // horizontal wipe - locX = DirX > 0 ? region.width : 0; - angle = DirX > 0 ? 0 : 180; - s.makeGraphic(Std.int(region.width * 2), 1, _data.color); - pixels = s.pixels; - var ghorz = FlxGradient.createGradientBitmapData(Std.int(region.width), 1, [_data.color, FlxColor.TRANSPARENT], 1, angle); - pixels.copyPixels(ghorz, ghorz.rect, new Point(locX, 0)); - s.pixels = pixels; - s.scale.set(1.0, region.height); - s.updateHitbox(); + final destX = dirX > 0 ? region.width : 0; + final angle = dirX > 0 ? 0 : 180; + sprite.makeGraphic(Std.int(region.width * 2), 1, _data.color); + final gradient = FlxGradient.createGradientBitmapData(Std.int(region.width), 1, [_data.color, FlxColor.TRANSPARENT], 1, angle); + sprite.pixels.copyPixels(gradient, gradient.rect, new Point(destX, 0)); + sprite.scale.set(1.0, region.height); + sprite.updateHitbox(); } - else if (Math.abs(DirX) > 0 && Math.abs(DirY) > 0) + else if (dirX != 0 && dirY != 0) { // diagonal wipe - locY = DirY > 0 ? region.height : 0; - s.loadGraphic(getGradient()); - s.flipX = DirX < 0; - s.flipY = DirY < 0; + sprite.loadGraphic(getGradient()); + sprite.color = _data.color; + sprite.flipX = dirX < 0; + sprite.flipY = dirY < 0; } - return s; + + return sprite; } - - function getGradient():BitmapData + + function getGradient():FlxGraphic { - // TODO: this could perhaps be optimized a lot by creating a single-pixel wide sprite, rotating it, scaling it super big, and positioning it properly - var rawBmp = new GraphicDiagonalGradient(0, 0); - var gdiag:BitmapData = cast rawBmp; - var gdiag_scaled:BitmapData = new BitmapData(FlxG.width * 2, FlxG.height * 2, true); - var m:Matrix = new Matrix(); - m.scale(gdiag_scaled.width / gdiag.width, gdiag_scaled.height / gdiag.height); - gdiag_scaled.draw(gdiag, m, null, null, null, true); - var theColor:FlxColor = _data.color; - var final_pixels:BitmapData = new BitmapData(FlxG.width * 3, FlxG.height * 3, true, theColor); - final_pixels.copyChannel(gdiag_scaled, gdiag_scaled.rect, - new Point(final_pixels.width - gdiag_scaled.width, final_pixels.height - gdiag_scaled.height), BitmapDataChannel.RED, BitmapDataChannel.ALPHA); - gdiag.dispose(); - gdiag_scaled.dispose(); - return final_pixels; + // TODO: create this gradient using FlxGradient + final gameWidth = FlxG.width; + final gameHeight = FlxG.height; + final source = FlxG.bitmap.add(GRADIENT_PATH).bitmap; + final key = '$GRADIENT_PATH:${gameWidth}x${gameHeight}'; + + var graphic = FlxG.bitmap.get(key); + if (graphic == null) + { + final gradient = new BitmapData(Math.floor(gameWidth * 2.5), gameHeight, true, 0x0); + + // draw the gradient in the cleared area + final matrix:Matrix = new Matrix(); + matrix.scale(gradient.width / source.width, gradient.height / source.height); + gradient.draw(source, matrix, null, null, null, true); + + // Don't destroy transition bitmaps + graphic = FlxG.bitmap.add(gradient, false, key); + graphic.persist = true; + graphic.destroyOnNoUse = false; + } + + return graphic; } - + function finishTween(f:FlxTween):Void { delayThenFinish(); diff --git a/include.xml b/include.xml index ede498f9..665e6da8 100644 --- a/include.xml +++ b/include.xml @@ -4,4 +4,6 @@ + +