UNPKG

@fuwu-yuan/bgew

Version:

Baguette Game Engine Web is a french 2D Web game engine

118 lines (117 loc) 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rectangle = void 0; const entity_1 = require("../entity"); class Rectangle extends entity_1.Entity { constructor(x, y, width, height, strokeColor = null, fillColor = null, hoverStrokeColor = null, hoverFillColor = null, clickStrokeColor = null, clickFillColor = null, radius = null) { super(x, y, width, height); this._strokeColor = "rgba(0,0,0, 1.0)"; this._fillColor = "rgba(255, 255, 255, 1.0)"; this._hoverStrokeColor = "rgba(0,0,0, 1.0)"; this._hoverFillColor = "rgba(255, 255, 255, 1.0)"; this._clickStrokeColor = "rgba(0,0,0, 1.0)"; this._clickFillColor = "rgba(255, 255, 255, 1.0)"; this._radius = { tl: 0, tr: 0, br: 0, bl: 0 }; this._clicked = false; 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)); if (strokeColor) { this.strokeColor = strokeColor; this.hoverStrokeColor = strokeColor; this.clickStrokeColor = strokeColor; } if (fillColor) { this.fillColor = fillColor; this.hoverFillColor = fillColor; this.clickFillColor = fillColor; } if (hoverStrokeColor) { this.hoverStrokeColor = hoverStrokeColor; } if (hoverFillColor) { this.hoverFillColor = hoverFillColor; } if (clickStrokeColor) { this.clickStrokeColor = clickStrokeColor; } if (clickFillColor) { this.clickFillColor = clickFillColor; } if (radius) { this.radius = radius; } } onMouseDown(event) { this._clicked = true; } onMouseUp(event) { this._clicked = false; } onMouseEnter(event) { //this.board?.changeCursor("pointer"); } onMouseLeave(event) { //this.board?.changeCursor("default"); } get clicked() { return this._clicked; } set clicked(clicked) { this._clicked = clicked; } draw(ctx) { super.draw(ctx); //set color ctx.strokeStyle = this.strokeColor; ctx.fillStyle = this.fillColor; if (this.clicked) { ctx.strokeStyle = this.clickStrokeColor; ctx.fillStyle = this.clickFillColor; } else if (this.hovered) { ctx.strokeStyle = ctx.fillStyle = this.hoverStrokeColor; ctx.fillStyle = ctx.fillStyle = this.hoverFillColor; } //draw square let path = this.getPath2D(); ctx.fill(path); ctx.stroke(path); } getPath2D() { let path = new Path2D(); path.moveTo(this.x + this.radius.tl, this.y); path.lineTo(this.x + this.width - this.radius.tr, this.y); path.quadraticCurveTo(this.x + this.width, this.y, this.x + this.width, this.y + this.radius.tr); path.lineTo(this.x + this.width, this.y + this.height - this.radius.br); path.quadraticCurveTo(this.x + this.width, this.y + this.height, this.x + this.width - this.radius.br, this.y + this.height); path.lineTo(this.x + this.radius.bl, this.y + this.height); path.quadraticCurveTo(this.x, this.y + this.height, this.x, this.y + this.height - this.radius.bl); path.lineTo(this.x, this.y + this.radius.tl); path.quadraticCurveTo(this.x, this.y, this.x + this.radius.tl, this.y); path.closePath(); return path; } update(delta) { super.update(delta); } /********************* * Getters & Setters * *********************/ get hoverFillColor() { return this._hoverFillColor; } set hoverFillColor(value) { this._hoverFillColor = value; } get hoverStrokeColor() { return this._hoverStrokeColor; } set hoverStrokeColor(value) { this._hoverStrokeColor = value; } get fillColor() { return this._fillColor; } set fillColor(value) { this._fillColor = value; } get strokeColor() { return this._strokeColor; } set strokeColor(value) { this._strokeColor = value; } get clickFillColor() { return this._clickFillColor; } set clickFillColor(value) { this._clickFillColor = value; } get clickStrokeColor() { return this._clickStrokeColor; } set clickStrokeColor(value) { this._clickStrokeColor = value; } get radius() { return this._radius; } set radius(value) { this._radius = value; } } exports.Rectangle = Rectangle;