76 lines
1.7 KiB
JavaScript
76 lines
1.7 KiB
JavaScript
import {compose, Property} from '@avocado/core';
|
|
|
|
import * as Vector from './index'
|
|
|
|
export function VectorMixin(
|
|
vectorKey = 'vector',
|
|
x = 'x',
|
|
y = 'y',
|
|
meta = {},
|
|
) {
|
|
meta = {
|
|
default: [0, 0],
|
|
eq: function(l, r) {
|
|
return Vector.equals(l, r);
|
|
},
|
|
...meta,
|
|
};
|
|
const transformedKey = `$$avocado_property_${vectorKey}`;
|
|
meta.initialize = function() {
|
|
this[transformedKey] = Vector.copy(meta.default);
|
|
};
|
|
const bypass = `${transformedKey}$bypassChangedEvent`;
|
|
meta.set = meta.set || new Function('vector', `
|
|
this.${transformedKey} = [
|
|
this.${transformedKey}[0],
|
|
this.${transformedKey}[1],
|
|
];
|
|
if (this.${bypass}) {
|
|
this.${transformedKey}[0] = vector[0];
|
|
this.${transformedKey}[1] = vector[1];
|
|
}
|
|
else {
|
|
this.${x} = vector[0];
|
|
this.${y} = vector[1];
|
|
}
|
|
`);
|
|
function createPropertyPair(axe) {
|
|
return {
|
|
get: new Function(`
|
|
return this.${transformedKey}[${'x' === axe ? 0 : 1}];
|
|
`),
|
|
set: new Function('value', `
|
|
this.${bypass} = true;
|
|
this.${vectorKey} = [
|
|
${'x' === axe ? 'value' : `this.${x}`},
|
|
${'y' === axe ? 'value' : `this.${y}`},
|
|
];
|
|
this.${bypass} = false;
|
|
`),
|
|
};
|
|
}
|
|
const decorate = compose(
|
|
Property(x, {
|
|
emit: meta.emit,
|
|
track: meta.track,
|
|
...createPropertyPair('x'),
|
|
}),
|
|
Property(y, {
|
|
emit: meta.emit,
|
|
track: meta.track,
|
|
...createPropertyPair('y'),
|
|
}),
|
|
Property(vectorKey, meta),
|
|
);
|
|
return (Superclass) => {
|
|
return class Vector extends decorate(Superclass) {
|
|
|
|
constructor(...args) {
|
|
super(...args);
|
|
this[bypass] = false;
|
|
}
|
|
|
|
}
|
|
};
|
|
}
|