avocado-old/packages/graphics/traits/textual.trait.js
2020-06-19 22:01:45 -05:00

75 lines
1.3 KiB
JavaScript

import {compose} from '@avocado/core';
import {StateProperty, Trait} from '@avocado/entity';
import {Text} from '@avocado/graphics';
const decorate = compose(
StateProperty('text', {
track: true,
}),
)
export default class Textual extends decorate(Trait) {
static defaultState() {
return {
text: '',
textStyle: {},
};
}
static describeState() {
return {
text: {
type: 'string',
label: 'Text',
},
textStyle: {
type: 'object',
label: 'Text style',
},
};
}
static type() {
return 'textual';
}
constructor(entity, params, state) {
super(entity, params, state);
this._text = undefined;
}
loadText() {
if (!this.entity.container) {
return;
}
if (this._text) {
this.entity.container.removeChild(this._text);
}
this._text = new Text(this.state.text, this.state.textStyle);
this._text.zIndex = 1000;
this.entity.container.addChild(this._text);
}
listeners() {
return {
textChanged: () => {
this.loadText();
},
traitAdded: (type) => {
if (-1 === [
'textual',
'visible',
].indexOf(type)) {
return;
}
this.loadText();
},
};
}
}