import {compose} from '@avocado/core'; import {Mixin, Property} from '@avocado/mixins' import * as Vector from './index' export function VectorMixin( vectorKey = 'vector', x = 'x', y = 'y', meta = {}, ) { meta = { default: [0, 0], emit: function (...args) { if (this.emit) { this.emit(...args); } }, eq: function(l, r) { return Vector.equals(l, r); }, get: function() { const vector = this[meta.transformProperty(vectorKey)]; if (!vector) { return [0, 0]; } return [vector[0], vector[1]]; }, transformProperty: (key) => `$$avocado_property_${key}`, ...meta, }; meta.set = meta.set || function(vector) { if (meta.track && meta.emit) { if (this[vectorKey][0] !== vector[0]) { meta.emit.call(this, `${x}Changed`, this[vectorKey][0], vector[0]); } if (this[vectorKey][1] !== vector[1]) { meta.emit.call(this, `${y}Changed`, this[vectorKey][1], vector[1]); } } this[meta.transformProperty(vectorKey)] = [vector[0], vector[1]]; } const decorate = compose( Property(vectorKey, meta), ); return (Superclass) => { return class Vector extends decorate(Superclass) { get [x]() { return this[vectorKey][0]; } set [x](x) { this[vectorKey] = [x, this[vectorKey][1]]; } get [y]() { return this[vectorKey][1]; } set [y](y) { this[vectorKey] = [this[vectorKey][0], y]; } } }; }