awv3
Version:
⚡ AWV3 embedded CAD
128 lines (99 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _weakMap = require("babel-runtime/core-js/weak-map");
var _weakMap2 = _interopRequireDefault(_weakMap);
var _keys = require("babel-runtime/core-js/object/keys");
var _keys2 = _interopRequireDefault(_keys);
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require("babel-runtime/helpers/createClass");
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var StoredProperties = function () {
function StoredProperties() {
(0, _classCallCheck3.default)(this, StoredProperties);
this.props = {}; // saved original properties of a material
this.refcount = 0; // so that we won't forget original properties before all restore animation are complete
}
(0, _createClass3.default)(StoredProperties, [{
key: "store",
value: function store(material, props) {
var animationProps = {};
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)((0, _keys2.default)(props)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
if (material[key] === undefined) continue; // skip properties unappliable to this material
animationProps[key] = props[key];
if (this.props[key] === undefined) this.props[key] = material[key].clone ? material[key].clone() : material[key];
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return material.animate(animationProps);
}
}, {
key: "restore",
value: function restore(material) {
return material.animate(this.props);
}
}]);
return StoredProperties;
}();
/**
* @class that stores and restores the original color and opacity of materials
*/
var MaterialStore = function () {
function MaterialStore() {
(0, _classCallCheck3.default)(this, MaterialStore);
this.storedPropertiesMap = new _weakMap2.default();
}
/**
* Stores properties of a material into a map and creates an animation
* @param {THREE.Material} material
* @params props - material properties to animate to
* @return {Tween} animation - not started animation to the given properties
*/
(0, _createClass3.default)(MaterialStore, [{
key: "store",
value: function store(material, props) {
if (!this.storedPropertiesMap.has(material)) this.storedPropertiesMap.set(material, new StoredProperties());
++this.storedPropertiesMap.get(material).refcount;
return this.storedPropertiesMap.get(material).store(material, props);
}
/**
* Restores the original properties of a material from the map
* @param {THREE.Material} material - material that was stored earlier
* @return {Tween} animation - not started restoration animation
*/
}, {
key: "restore",
value: function restore(material) {
var _this = this;
if (!this.storedPropertiesMap.has(material)) return material.animate({});
return this.storedPropertiesMap.get(material).restore(material).onComplete(function () {
if (! --_this.storedPropertiesMap.get(material).refcount) _this.storedPropertiesMap.delete(material);
});
}
}]);
return MaterialStore;
}();
exports.default = MaterialStore;