|
| 1 | +package flixel.tweens.misc; |
| 2 | + |
| 3 | +import flixel.tweens.FlxTween; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tweens multiple numeric properties of an object simultaneously. |
| 7 | + */ |
| 8 | +class VarTween extends FlxTween |
| 9 | +{ |
| 10 | + var _object:Dynamic; |
| 11 | + var _properties:Dynamic; |
| 12 | + var _propertyInfos:Array<VarTweenProperty>; |
| 13 | + |
| 14 | + function new(options:TweenOptions, ?manager:FlxTweenManager) |
| 15 | + { |
| 16 | + super(options, manager); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Tweens multiple numeric public properties. |
| 21 | + * |
| 22 | + * @param object The object containing the properties. |
| 23 | + * @param properties An object containing key/value pairs of properties and target values. |
| 24 | + * @param duration Duration of the tween. |
| 25 | + */ |
| 26 | + public function tween(object:Dynamic, properties:Dynamic, duration:Float):VarTween |
| 27 | + { |
| 28 | + #if FLX_DEBUG |
| 29 | + if (object == null) throw "Cannot tween variables of an object that is null."; |
| 30 | + else if (properties == null) throw "Cannot tween null properties."; |
| 31 | + #end |
| 32 | + |
| 33 | + _object = object; |
| 34 | + _properties = properties; |
| 35 | + _propertyInfos = []; |
| 36 | + this.duration = duration; |
| 37 | + start(); |
| 38 | + initializeVars(); |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + override function update(elapsed:Float):Void |
| 43 | + { |
| 44 | + var delay:Float = (executions > 0) ? loopDelay : startDelay; |
| 45 | + |
| 46 | + // Leave properties alone until delay is over |
| 47 | + if (_secondsSinceStart < delay) super.update(elapsed); |
| 48 | + else |
| 49 | + { |
| 50 | + // Wait until the delay is done to set the starting values of tweens |
| 51 | + if (Math.isNaN(_propertyInfos[0].startValue)) setStartValues(); |
| 52 | + |
| 53 | + super.update(elapsed); |
| 54 | + |
| 55 | + if (active) for (info in _propertyInfos) |
| 56 | + Reflect.setProperty(info.object, info.field, info.startValue + info.range * scale); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + function initializeVars():Void |
| 61 | + { |
| 62 | + var fieldPaths:Array<String>; |
| 63 | + if (Reflect.isObject(_properties)) fieldPaths = Reflect.fields(_properties); |
| 64 | + else |
| 65 | + throw "Unsupported properties container - use an object containing key/value pairs."; |
| 66 | + |
| 67 | + for (fieldPath in fieldPaths) |
| 68 | + { |
| 69 | + var target = _object; |
| 70 | + var path = fieldPath.split("."); |
| 71 | + var field = path.pop(); |
| 72 | + for (component in path) |
| 73 | + { |
| 74 | + target = Reflect.getProperty(target, component); |
| 75 | + if (!Reflect.isObject(target)) throw 'The object does not have the property "$component" in "$fieldPath"'; |
| 76 | + } |
| 77 | + |
| 78 | + _propertyInfos.push({ |
| 79 | + object: target, |
| 80 | + field: field, |
| 81 | + startValue: Math.NaN, // gets set after delay |
| 82 | + range: Reflect.getProperty(_properties, fieldPath) |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + function setStartValues() |
| 88 | + { |
| 89 | + for (info in _propertyInfos) |
| 90 | + { |
| 91 | + // if (Reflect.getProperty(info.object, info.field) == null) throw 'The object does not have the property "${info.field}"'; |
| 92 | + if (Reflect.getProperty(info.object, info.field) == null) |
| 93 | + { |
| 94 | + trace('The object does not have the property "${info.field}"'); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + var value:Dynamic = Reflect.getProperty(info.object, info.field); |
| 99 | + // if (Math.isNaN(value)) throw 'The property "${info.field}" is not numeric.'; |
| 100 | + if (Math.isNaN(value)) |
| 101 | + { |
| 102 | + trace('The property "${info.field}" is not numeric'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + info.startValue = value; |
| 107 | + info.range = info.range - value; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + override public function destroy():Void |
| 112 | + { |
| 113 | + super.destroy(); |
| 114 | + _object = null; |
| 115 | + _properties = null; |
| 116 | + _propertyInfos = null; |
| 117 | + } |
| 118 | + |
| 119 | + override function isTweenOf(object:Dynamic, ?field:String):Bool |
| 120 | + { |
| 121 | + if (object == _object && field == null) return true; |
| 122 | + |
| 123 | + for (property in _propertyInfos) |
| 124 | + { |
| 125 | + if (object == property.object && (field == property.field || field == null)) return true; |
| 126 | + } |
| 127 | + |
| 128 | + return false; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +private typedef VarTweenProperty = |
| 133 | +{ |
| 134 | + object:Dynamic, |
| 135 | + field:String, |
| 136 | + startValue:Float, |
| 137 | + range:Float |
| 138 | +} |
0 commit comments