awv3
Version:
⚡ AWV3 embedded CAD
393 lines (324 loc) • 10.5 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import * as THREE from 'three';
import { exponential } from './easing';
var tweens = [];
var Tween =
/*#__PURE__*/
function () {
function Tween(parent, properties) {
this.parent = parent;
this.internalUpdate = null;
this.isPlaying = false;
this.paused = false;
this.duration = 0;
this.repeatTimes = 0;
this.repeatMode = Tween.Repeat.Normal;
this.reversed = false;
this.delayTime = 0;
this.onStartCallbackFired = false;
this.easingFunction = exponential.out;
this.startTime = null;
this.onStartCallback = null;
this.onUpdateCallback = null;
this.onCompleteCallback = null;
this.thenCallback = null;
this.onStopCallback = null;
this.valuesEnd = flatten(parent, typeof properties === 'function' ? properties() : _extends({}, properties));
this.valuesStart = {};
this.valuesStartRepeat = {};
this.object = {};
this.map = {};
for (var key in this.valuesEnd) {
var prop = this.map[key] = returnValue(parent, key),
value = prop[0][prop[1]];
this.object[key] = value;
this.valuesStart[key] = value;
this.valuesStartRepeat[key] = value;
}
}
var _proto = Tween.prototype;
_proto.hasProperty = function hasProperty(key) {
return this.object.hasOwnProperty(key);
};
_proto.getProperties = function getProperties() {
return _extends({}, this.valuesStart);
};
_proto.removeProperty = function removeProperty(key) {
delete this.valuesStart[key];
delete this.valuesEnd[key];
delete this.object[key];
delete this.map[key];
if (Object.keys(this.object).length === 0) this.stop();
return this;
};
_proto.from = function from(properties) {
if (!properties || Object.keys(this.map).length === 0) return this;
properties = typeof properties === 'function' ? properties() : _extends({}, properties);
var flattened = flatten(this.parent, properties);
var previous = undefined;
for (var key in flattened) {
var prop = returnValue(properties, key);
if (prop[0]) previous = prop[0];
prop[0] = prop[0] || previous;
var value = prop[0][prop[1]];
var actualObject = this.map[key];
actualObject[0][actualObject[1]] = value;
this.object[key] = value;
this.valuesStart[key] = value;
}
return this;
};
_proto.start = function start(length) {
this.duration = length || 0;
Tween.add(this);
this.isPlaying = true;
this.paused = false;
this.onStartCallbackFired = false;
this.startTime = null;
for (var _iterator = Tween.getAll(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var _item = _ref;
if (_item !== this && _item.isPlaying && _item.parent == this.parent) {
for (var key in this.object) {
if (_item.hasProperty(key)) _item.removeProperty(key);
}
}
}
if (this.duration == 0) {
for (var property in this.valuesEnd) {
var end = this.valuesEnd[property],
actualObject = this.map[property];
this.object[property] = end;
actualObject[0][actualObject[1]] = end;
}
}
this.parent.view && this.parent.view.invalidate();
return this;
};
_proto.now = function now() {
return this.start(0);
};
_proto.stop = function stop() {
this.isPlaying = false;
this.paused = false;
if (this.onStopCallback !== null) this.onStopCallback.call(this.object);
if (this.thenCallback !== null) {
this.thenCallback.call(this.object);
this.thenCallback = null;
}
return this;
};
_proto.pause = function pause() {
this.paused = true;
return this;
};
_proto.continue = function _continue() {
this.paused = false;
return this;
};
_proto.toggle = function toggle() {
this.paused = !this.paused;
return this;
};
_proto.delay = function delay(amount) {
this.delayTime = amount;
return this;
};
_proto.repeat = function repeat(times, yoyo) {
if (times === void 0) {
times = 0;
}
if (yoyo === void 0) {
yoyo = Tween.Repeat.Normal;
}
this.repeatTimes = times;
this.repeatMode = yoyo;
return this;
};
_proto.easing = function easing(_easing) {
this.easingFunction = _easing;
return this;
};
_proto.onStart = function onStart(callback) {
this.onStartCallback = callback;
return this;
};
_proto.onUpdate = function onUpdate(callback) {
this.onUpdateCallback = callback;
return this;
};
_proto.onComplete = function onComplete(callback) {
this.onCompleteCallback = callback;
return this;
};
_proto.wait = function wait() {
var _this = this;
if (!this.isPlaying) return Promise.resolve();else return new Promise(function (resolve) {
return _this.thenCallback = resolve;
});
};
_proto.onStop = function onStop(callback) {
this.onStopCallback = callback;
return this;
};
_proto.update = function update(time) {
if (this.paused) return true;
if (!this.isPlaying) return false;
if (this.startTime == null) {
this.startTime = window.performance.now();
this.startTime += this.delayTime;
}
var property;
if (time < this.startTime) return true;
if (this.onStartCallbackFired === false) {
if (this.onStartCallback !== null) this.onStartCallback.call(this.object);
this.onStartCallbackFired = true;
}
var elapsed = (time - this.startTime) / this.duration;
elapsed = elapsed > 1 ? 1 : elapsed;
var value = this.easingFunction(elapsed);
for (property in this.valuesEnd) {
var start = this.valuesStart[property],
end = this.valuesEnd[property],
fraction = start + (end - start) * value,
actualObject = this.map[property];
this.object[property] = fraction;
actualObject[0][actualObject[1]] = fraction;
}
if (this.onUpdateCallback !== null) this.onUpdateCallback.call(this.object, value, time);
if (elapsed == 1) {
if (this.repeatTimes > 0) {
if (isFinite(this.repeatTimes)) this.repeatTimes--;
for (property in this.valuesStartRepeat) {
if (typeof this.valuesEnd[property] === 'string') this.valuesStartRepeat[property] = this.valuesStartRepeat[property] + parseFloat(this.valuesEnd[property], 10);
if (this.repeatMode === Tween.Repeat.Yoyo) {
var tmp = this.valuesStartRepeat[property];
this.valuesStartRepeat[property] = this.valuesEnd[property];
this.valuesEnd[property] = tmp;
}
this.valuesStart[property] = this.valuesStartRepeat[property];
}
if (this.repeatMode === Tween.Repeat.Yoyo) this.reversed = !this.reversed;
this.startTime = time + this.delayTime;
return true;
} else {
if (this.onCompleteCallback !== null) this.onCompleteCallback.call(this.object);
if (this.thenCallback !== null) {
this.thenCallback.call(this.object);
this.thenCallback = null;
}
return false;
}
}
return true;
};
Tween.getAll = function getAll() {
return tweens;
};
Tween.removeAll = function removeAll() {
tweens = [];
};
Tween.add = function add(tween) {
tweens.push(tween);
};
Tween.remove = function remove(tween) {
var i = tweens.indexOf(tween);
if (i !== -1) tweens.splice(i, 1);
};
Tween.removeObjectTweens = function removeObjectTweens(object) {
Tween.getAll().forEach(function (item) {
if (item.parent == object) {
item.stop();
}
});
};
Tween.update = function update(time, renderer) {
time = time !== undefined ? time : window.performance.now();
if (tweens.length === 0) return false;
var index = 0,
length = tweens.length;
for (index; index < length; index++) {
var tween = tweens[index];
if (!tween.update(time)) {
tweens.splice(index--, 1);
length--;
} else if (!tween.paused && !!tween.parent.view) {
tween.parent.view.invalidate();
} else if (!tween.paused && renderer) {
renderer.invalidateViews();
}
}
return true;
};
return Tween;
}();
export { Tween as default };
Tween.Repeat = {
Normal: 1,
Yoyo: 2
};
function flatten(parent, source, pathArray, result) {
// Roll out merged arrays
if (Array.isArray(source) && source.length == 1 && typeof source[0] === 'object') {
source = mergeArray(parent, pathArray.join('.'), source[0], true);
}
pathArray = typeof pathArray === 'undefined' ? [] : pathArray;
result = typeof result === 'undefined' ? {} : result;
var key, value, newKey;
for (var i in source) {
if (source.hasOwnProperty(i)) {
key = i;
value = source[i];
pathArray.push(key);
if (typeof value === 'object' && value !== null) {
if (value instanceof THREE.Euler) {
result[key + '.x'] = value.x;
result[key + '.y'] = value.y;
result[key + '.z'] = value.z;
} else result = flatten(parent, value, pathArray, result);
} else if (typeof value === 'number') {
newKey = pathArray.join('.');
result[newKey] = value;
}
pathArray.pop();
}
}
return result;
}
function ref(obj, str) {
return str.split('.').reduce(function (o, x) {
return o[x];
}, obj);
}
function returnValue(obj, key) {
var parts = key.split(/\.(?=[^.]+$)/);
if (parts.length == 1) return [obj, parts[0], parts.length];else return [ref(obj, parts[0]), parts[1], parts.length];
}
function mergeArray(root, path, properties, justContent) {
var object = {},
result = [],
map = returnValue(root, path);
map[0][map[1]].forEach(function (item) {
if (Array.isArray(properties)) {
var array = {};
properties.forEach(function (prop) {
var obj = {};
obj[prop] = item[prop];
Object.assign(array, obj);
});
result.push(array);
} else if (typeof properties === 'object') {
result.push(properties);
}
});
if (map[2] == 1 || !!justContent) object = result;else object[map[1]] = result;
return object;
}