123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- function Button(canvas) {
- this.text = "Button";
- this.options = {
- position:{x: 0, y: 0, align: "center", baseline: "middle"},
- padding: 10,
- background: {color: "#777777", hover: "#333333"},
- font: {color:"#DDDDDD", stroke: "#333333", size: 72, family: "sans-serif"}
- };
- this.textMetrics = null;
- this.clickHandler = function() {};
- this.init = function(text, options) {
- this.text = text;
- this.options = system.objectMerge(this.options, options);
- }
- this.updateDelta = function(delta, canvas) {
- };
- this.addClickEvent = function(handler) {
- this.clickHandler = handler;
- }
- this.draw = function(context) {
- context.save();
- context.translate(this.options.position.x, this.options.position.y);
- context.font = this.options.font.size + "px " + this.options.font.family;
- context.textAlign = this.options.position.align;
- context.textBaseline = this.options.position.baseline;
- context.lineWidth = 2;
- if(this.isHovered) {
- context.fillStyle = this.options.background.hover;
- canvas.style.cursor = "pointer";
- } else {
- context.fillStyle = this.options.background.color;
- canvas.style.cursor = "default";
- }
- this.textMetrics = context.measureText(this.text);
- context.beginPath();
- var buttonRect = {x:0, y: 0, width: 100, height: 100};
- buttonRect.x = -(this.textMetrics.width / 2) - this.options.padding;
- if(this.options.position.align == "left") {
- buttonRect.x = -this.options.padding;
- }
- buttonRect.y = -(this.options.font.size) / 2 - this.options.padding;
- buttonRect.width = this.textMetrics.width + (this.options.padding * 2);
- buttonRect.height = this.options.font.size + (this.options.padding * 2);
- context.rect(buttonRect.x, buttonRect.y, buttonRect.width, buttonRect.height);
- context.fill();
- context.fillStyle = this.options.font.color;
- context.strokeStyle = this.options.font.stroke;
- if(this.isHovered) {
- context.fillStyle = this.options.font.hover;
- }
- context.fillText(this.text, 0, 0);
- context.restore();
- };
- this.checkIfHovered = function(x, y) {
- if(this.textMetrics == null) {
- return false;
- }
- var topLeft = {};
- topLeft.x = this.options.position.x - (this.textMetrics.width / 2) - this.options.padding;
- topLeft.y = this.options.position.y - (this.options.font.size / 2) - this.options.padding;
- var bottomRight = {};
- bottomRight.x = this.options.position.x + (this.textMetrics.width / 2) + this.options.padding;
- bottomRight.y = this.options.position.y + (this.options.font.size / 2) + this.options.padding;
- if(x > topLeft.x && x < bottomRight.x) {
- if(y > topLeft.y && y < bottomRight.y) {
- return true;
- }
- }
- return false;
- }
- this.mouseMove = function(x, y) {
- this.isHovered = this.checkIfHovered(x, y);
- }
- this.touchMove = function(x, y) {
- this.isHovered = this.checkIfHovered(x, y);
- }
- this.mouseUp = function(button, x, y) {
- if(this.checkIfHovered(x, y)) {
- this.clickHandler(button);
- }
- }
- this.mouseDown = function(button, x, y) {
- if(this.checkIfHovered(x, y)) {
- this.clickHandler(button);
- }
- }
- this.touchStart = function(x, y) {
- if(this.checkIfHovered(x, y)) {
- this.clickHandler(0);
- }
- };
- this.touchEnd = function(x, y) {
- if(this.checkIfHovered(x, y)) {
- this.clickHandler(0);
- }
- };
- };
|