Projektmanagement-Game/public/js/Button.js

73 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2021-06-14 11:14:16 +00:00
function Button(default_color, hover_color, select_color, width, height, x, y, text, status, click) {
2021-06-08 13:38:43 +00:00
this.graphics = new PIXI.Graphics();
this.default_color = default_color;
this.hover_color = hover_color;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.text = text;
this.status = status;
2021-06-11 08:17:38 +00:00
this.pointerdown = click;
2021-06-08 13:38:43 +00:00
this.selected = false;
let _this = this;
this.changeButtonColor = function (color) {
this.graphics.clear();
this.graphics.lineStyle(4, 0x000000, 1);
this.graphics.beginFill(color);
this.graphics.drawRect(this.x, this.y, this.width, this.height);
this.graphics.endFill();
2021-06-11 05:55:46 +00:00
};
2021-06-08 13:38:43 +00:00
this.selectButton = function () {
this.selected = true;
this.changeButtonColor(select_color);
2021-06-11 05:55:46 +00:00
};
2021-06-08 13:38:43 +00:00
this.unSelectButton = function () {
this.selected = false;
this.changeButtonColor(default_color);
2021-06-11 05:55:46 +00:00
};
2021-06-08 13:38:43 +00:00
this.getButton = function () {
const style = new PIXI.TextStyle({
fontFamily: 'Arial',
2021-06-14 11:14:16 +00:00
fontSize: 40,
2021-06-08 13:38:43 +00:00
wordWrap: true,
2021-06-14 11:14:16 +00:00
wordWrapWidth: this.width,
breakWords: true,
padding: 50
2021-06-08 13:38:43 +00:00
});
this.graphics.clear();
this.graphics.lineStyle(4, 0x000000, 1);
this.graphics.beginFill(this.default_color);
this.graphics.drawRect(this.x, this.y, this.width, this.height);
this.graphics.endFill();
let text_field = new PIXI.Text(this.text, style);
text_field.x = this.x + this.width / 2 - text_field.width / 2;
text_field.y = this.y + this.height / 2 - text_field.height / 2;
this.graphics.addChild(text_field);
this.graphics.interactive = true;
this.graphics.buttonMode = true;
this.graphics.defaultCursor = 'pointer';
2021-06-11 08:17:38 +00:00
this.graphics.on('pointerdown', function () {
2021-06-08 13:38:43 +00:00
click();
});
this.graphics.on('mouseover', function () {
if (!_this.selected) {
_this.changeButtonColor(_this.hover_color);
}
});
this.graphics.on('mouseout', function () {
if (!_this.selected) _this.changeButtonColor(_this.default_color);
});
return this.graphics;
2021-06-11 05:55:46 +00:00
};
2021-06-08 13:38:43 +00:00
}