rt-graphic-component/components/EsbButton/ThEsbButton.js

80 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-01-02 14:36:10 +08:00
import { Graphics } from 'pixi.js';
import { JlGraphic, VectorText } from 'jl-graphic';
const esbButtonConsts = {
codeFontSize: 12,
codeColor: 0xffffff,
bodyLineColor: 0xffffff,
bodyLineWidth: 4,
bodyRectLineColor: 0xffffff,
bodyRectLineWidth: 2,
bodyRectWidth: 20,
bodyRectHeight: 20,
bodyCircleRadius: 5,
bodyCircleColor: 0xffffff,
bodyColor: 0x000000,
pressedColor: 0xff0000,
};
class EsbButton extends JlGraphic {
static Type = 'esbButton';
codeGraph = new VectorText('');
circleBody = new Graphics();
rectBody = new Graphics();
lineBody = new Graphics();
constructor() {
super(EsbButton.Type);
this.addChild(this.codeGraph);
this.addChild(this.rectBody);
this.addChild(this.lineBody);
this.addChild(this.circleBody);
this.codeGraph.name = 'esb_code';
}
get datas() {
return this.getDatas();
}
get state() {
return this.getStates();
}
doRepaint() {
const codeGraph = this.codeGraph;
codeGraph.text = this.datas.code;
codeGraph.style.fill = esbButtonConsts.codeColor;
codeGraph.setVectorFontSize(esbButtonConsts.codeFontSize);
codeGraph.anchor.set(0.5);
const codeTransform = this.datas?.childTransforms?.find((item) => item.name === 'esb_code');
if (codeTransform) {
const position = codeTransform?.transform.position;
const rotation = codeTransform?.transform?.rotation;
codeGraph.position.set(position?.x, position?.y);
codeGraph.rotation = rotation || 0;
}
else {
codeGraph.position.set(-30, 0);
}
this.circleBody.clear();
this.circleBody.beginFill(this.state.down
? esbButtonConsts.pressedColor
: esbButtonConsts.bodyCircleColor, 1);
this.circleBody.drawCircle(0, 0, esbButtonConsts.bodyCircleRadius);
this.circleBody.endFill();
this.rectBody.clear();
this.rectBody.beginFill(esbButtonConsts.bodyColor, 0);
this.rectBody.lineStyle(esbButtonConsts.bodyRectLineWidth, this.state.down
? esbButtonConsts.pressedColor
: esbButtonConsts.bodyRectLineColor);
this.rectBody.drawRect(-esbButtonConsts.bodyRectWidth / 2, -esbButtonConsts.bodyRectHeight / 2, esbButtonConsts.bodyRectWidth, esbButtonConsts.bodyRectHeight);
this.rectBody.endFill();
this.lineBody.clear();
const lineY = this.datas.flip
? esbButtonConsts.bodyRectHeight / 2
: -esbButtonConsts.bodyRectHeight / 2;
this.lineBody.lineStyle(esbButtonConsts.bodyLineWidth, this.state.down
? esbButtonConsts.pressedColor
: esbButtonConsts.bodyLineColor);
this.lineBody.moveTo(-esbButtonConsts.bodyRectWidth / 2, lineY);
this.lineBody.lineTo(esbButtonConsts.bodyRectWidth / 2, lineY);
}
}
export { EsbButton };