|
| 1 | +package flixel.addons.transition; |
| 2 | + |
| 3 | +// TODO: remove this check when min flixel version is 5.6.0, |
| 4 | +// So that FlxAddonDefines will handle this |
| 5 | +#if (flixel < "5.3.0") |
| 6 | +#error "Flixel-Addons is not compatible with flixel versions older than 5.3.0"; |
| 7 | +#end |
| 8 | + |
| 9 | +import flixel.FlxState; |
| 10 | + |
| 11 | +/** |
| 12 | + * A `FlxState` which can perform visual transitions |
| 13 | + * |
| 14 | + * Usage: |
| 15 | + * |
| 16 | + * First, extend `FlxTransitionableState` as ie, `FooState`. |
| 17 | + * |
| 18 | + * Method 1: |
| 19 | + * |
| 20 | + * ```haxe |
| 21 | + * var in:TransitionData = new TransitionData(...); // add your data where "..." is |
| 22 | + * var out:TransitionData = new TransitionData(...); |
| 23 | + * |
| 24 | + * FlxG.switchState(new FooState(in,out)); |
| 25 | + * ``` |
| 26 | + * |
| 27 | + * Method 2: |
| 28 | + * |
| 29 | + * ```haxe |
| 30 | + * FlxTransitionableState.defaultTransIn = new TransitionData(...); |
| 31 | + * FlxTransitionableState.defaultTransOut = new TransitionData(...); |
| 32 | + * |
| 33 | + * FlxG.switchState(new FooState()); |
| 34 | + * ``` |
| 35 | + */ |
| 36 | +class FlxTransitionableSubState extends FlxSubState |
| 37 | +{ |
| 38 | + // beginning & ending transitions for THIS state: |
| 39 | + public var transIn:TransitionData; |
| 40 | + public var transOut:TransitionData; |
| 41 | + |
| 42 | + public var hasTransIn(get, never):Bool; |
| 43 | + public var hasTransOut(get, never):Bool; |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a state with the ability to do visual transitions |
| 47 | + * @param TransIn Plays when the state begins |
| 48 | + * @param TransOut Plays when the state ends |
| 49 | + */ |
| 50 | + public function new(?TransIn:TransitionData, ?TransOut:TransitionData) |
| 51 | + { |
| 52 | + transIn = TransIn; |
| 53 | + transOut = TransOut; |
| 54 | + |
| 55 | + if (transIn == null && FlxTransitionableState.defaultTransIn != null) |
| 56 | + { |
| 57 | + transIn = FlxTransitionableState.defaultTransIn; |
| 58 | + } |
| 59 | + if (transOut == null && FlxTransitionableState.defaultTransOut != null) |
| 60 | + { |
| 61 | + transOut = FlxTransitionableState.defaultTransOut; |
| 62 | + } |
| 63 | + super(); |
| 64 | + } |
| 65 | + |
| 66 | + override function destroy():Void |
| 67 | + { |
| 68 | + super.destroy(); |
| 69 | + transIn = null; |
| 70 | + transOut = null; |
| 71 | + _onExit = null; |
| 72 | + } |
| 73 | + |
| 74 | + override function create():Void |
| 75 | + { |
| 76 | + super.create(); |
| 77 | + transitionIn(); |
| 78 | + } |
| 79 | + |
| 80 | + override function startOutro(onOutroComplete:() -> Void) |
| 81 | + { |
| 82 | + if (!hasTransOut) |
| 83 | + onOutroComplete(); |
| 84 | + else if (!_exiting) |
| 85 | + { |
| 86 | + // play the exit transition, and when it's done call FlxG.switchState |
| 87 | + _exiting = true; |
| 88 | + transitionOut(onOutroComplete); |
| 89 | + |
| 90 | + if (FlxTransitionableState.skipNextTransOut) |
| 91 | + { |
| 92 | + FlxTransitionableState.skipNextTransOut = false; |
| 93 | + finishTransOut(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Starts the in-transition. Can be called manually at any time. |
| 100 | + */ |
| 101 | + public function transitionIn():Void |
| 102 | + { |
| 103 | + if (transIn != null && transIn.type != NONE) |
| 104 | + { |
| 105 | + if (FlxTransitionableState.skipNextTransIn) |
| 106 | + { |
| 107 | + FlxTransitionableState.skipNextTransIn = false; |
| 108 | + if (finishTransIn != null) |
| 109 | + { |
| 110 | + finishTransIn(); |
| 111 | + } |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + var _trans = createTransition(transIn); |
| 116 | + |
| 117 | + _trans.setStatus(FULL); |
| 118 | + openSubState(_trans); |
| 119 | + |
| 120 | + _trans.finishCallback = finishTransIn; |
| 121 | + _trans.start(OUT); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Starts the out-transition. Can be called manually at any time. |
| 127 | + */ |
| 128 | + public function transitionOut(?OnExit:Void->Void):Void |
| 129 | + { |
| 130 | + _onExit = OnExit; |
| 131 | + if (hasTransOut) |
| 132 | + { |
| 133 | + var _trans = createTransition(transOut); |
| 134 | + |
| 135 | + _trans.setStatus(EMPTY); |
| 136 | + openSubState(_trans); |
| 137 | + |
| 138 | + _trans.finishCallback = finishTransOut; |
| 139 | + _trans.start(IN); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + _onExit(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + var transOutFinished:Bool = false; |
| 148 | + |
| 149 | + var _exiting:Bool = false; |
| 150 | + var _onExit:Void->Void; |
| 151 | + |
| 152 | + function get_hasTransIn():Bool |
| 153 | + { |
| 154 | + return transIn != null && transIn.type != NONE; |
| 155 | + } |
| 156 | + |
| 157 | + function get_hasTransOut():Bool |
| 158 | + { |
| 159 | + return transOut != null && transOut.type != NONE; |
| 160 | + } |
| 161 | + |
| 162 | + function createTransition(data:TransitionData):Transition |
| 163 | + { |
| 164 | + return switch (data.type) |
| 165 | + { |
| 166 | + case CUSTOM: data.transitionState; |
| 167 | + case TILES: new Transition(data); |
| 168 | + case FADE: new Transition(data); |
| 169 | + default: null; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + function finishTransIn() |
| 174 | + { |
| 175 | + closeSubState(); |
| 176 | + } |
| 177 | + |
| 178 | + function finishTransOut() |
| 179 | + { |
| 180 | + transOutFinished = true; |
| 181 | + |
| 182 | + if (!_exiting) |
| 183 | + { |
| 184 | + closeSubState(); |
| 185 | + } |
| 186 | + |
| 187 | + if (_onExit != null) |
| 188 | + { |
| 189 | + _onExit(); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments