@fuwu-yuan/bgew
Version:
Baguette Game Engine Web is a french 2D Web game engine
122 lines (121 loc) • 4.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Label = void 0;
const entity_1 = require("../entity");
class Label extends entity_1.Entity {
constructor(x, y, text, ctx) {
super(x, y, 0, 0);
this._fontFamily = "sans-serif";
this._fontSize = 20;
this._hoverFontSize = 0;
this._clickFontSize = 0;
this._fontColor = "rgba(0,0,0, 1.0)";
this._hoverFontColor = "";
this._hoverCursor = "";
this._clickFontColor = "";
this._clicked = false;
this.ctx = ctx;
this.onMouseEvent("mousedown", this.onMouseDown.bind(this));
this.onMouseEvent("mouseup", this.onMouseUp.bind(this));
this.onMouseEvent("mouseenter", this.onMouseEnter.bind(this));
this.onMouseEvent("mouseleave", this.onMouseLeave.bind(this));
this._text = text;
}
onMouseDown(event) {
this._clicked = true;
}
onMouseUp(event) {
this._clicked = false;
}
onMouseEnter(event) {
var _a;
if (this._hoverCursor !== "")
(_a = this.board) === null || _a === void 0 ? void 0 : _a.changeCursor(this._hoverCursor);
}
onMouseLeave(event) {
var _a;
if (this._hoverCursor !== "")
(_a = this.board) === null || _a === void 0 ? void 0 : _a.restoreCursor();
}
get clicked() {
return this._clicked;
}
set clicked(clicked) {
this._clicked = clicked;
}
draw(ctx) {
super.draw(ctx);
//text options
var fontSize = this.fontSize;
ctx.fillStyle = this.fontColor;
if (this.clicked) {
if (this.clickFontSize > 0)
fontSize = this.clickFontSize;
if (this.clickFontColor !== "")
ctx.fillStyle = this.clickFontColor;
}
else if (this.hovered) {
if (this.hoverFontSize > 0)
fontSize = this.hoverFontSize;
if (this.hoverFontColor !== "")
ctx.fillStyle = this.hoverFontColor;
}
ctx.font = fontSize + "px " + this.fontFamily;
//text position
var textX = this.x;
var textY = this.y + fontSize;
let lines = this._text.split("\n");
for (let i = 0; i < lines.length; i++) {
//draw the text
ctx.fillText(lines[i], textX, textY + (i * fontSize));
}
}
get height() {
let lines = this._text.split("\n");
return lines.length * this.fontSize;
}
set height(height) {
this._height = height;
}
get width() {
let width = 0;
if (this.ctx) {
this.ctx.font = this.fontSize + "px " + this.fontFamily;
let lines = this._text.split("\n");
for (let i = 0; i < lines.length; i++) {
let tmp = this.ctx.measureText(this._text).width;
if (tmp > width)
width = tmp;
}
}
return width;
}
set width(width) {
this._width = width;
}
update(delta) {
super.update(delta);
}
/*********************
* Getters & Setters *
*********************/
get text() { return this._text; }
set text(value) { this._text = value; }
get clickFontColor() { return this._clickFontColor; }
set clickFontColor(value) { this._clickFontColor = value; }
get hoverFontColor() { return this._hoverFontColor; }
set hoverFontColor(value) { this._hoverFontColor = value; }
get fontColor() { return this._fontColor; }
set fontColor(value) { this._fontColor = value; }
get fontSize() { return this._fontSize; }
set fontSize(value) { this._fontSize = value; this._hoverFontSize = value; }
get clickFontSize() { return this._clickFontSize; }
set clickFontSize(value) { this._clickFontSize = value; }
get hoverFontSize() { return this._hoverFontSize; }
set hoverFontSize(value) { this._hoverFontSize = value; }
get hoverCursor() { return this._hoverCursor; }
set hoverCursor(value) { this._hoverCursor = value; }
get fontFamily() { return this._fontFamily; }
set fontFamily(value) { this._fontFamily = value; }
}
exports.Label = Label;