-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.js
43 lines (34 loc) · 985 Bytes
/
Button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function Button(sprite, position, callback, hoverSprite){
this.sprite = sprite;
this.hoverSprite = hoverSprite ? hoverSprite : sprite;
this.position = position;
this.callback = callback;
}
Button.prototype.draw = function(){
if(this.mouseInsideBorders()){
Canvas2D.drawImage(this.hoverSprite, this.position, 0, 1);
Canvas2D._canvas.style.cursor = "pointer";
}
else{
Canvas2D.drawImage(this.sprite, this.position, 0, 0.98);
}
}
Button.prototype.handleInput = function(){
if(Mouse.left.pressed && this.mouseInsideBorders()){
this.callback();
}
}
Button.prototype.mouseInsideBorders = function(){
mousePos = Mouse.position;
if(mousePos.x > this.position.x
&&
mousePos.x < this.position.x + this.sprite.width
&&
mousePos.y > this.position.y
&&
mousePos.y < this.position.y + this.sprite.height
){
return true;
}
return false;
}