Add button interaction
This commit is contained in:
parent
6223b35536
commit
4a253b0184
12 changed files with 247 additions and 23 deletions
|
@ -0,0 +1,44 @@
|
|||
import * as input from '../input.mjs';
|
||||
|
||||
export class Rect {
|
||||
constructor(x = 0, y = 0, w = 0, h = 0) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.w = w;
|
||||
this.h = h;
|
||||
|
||||
this.onclick = null;
|
||||
this.mouseHeld = false;
|
||||
}
|
||||
|
||||
tickMouse() {
|
||||
if (this.mouseHeld == true && !input.mouse.held[0] && this.mouseOver)
|
||||
if (this.onclick !== null)
|
||||
this.onclick();
|
||||
if (!this.mouseHeld && input.mouse.pressed[0] && this.mouseOver)
|
||||
this.mouseHeld = true;
|
||||
if (!input.mouse.held[0])
|
||||
this.mouseHeld = false;
|
||||
}
|
||||
|
||||
get shape() {
|
||||
return [this.x, this.y, this.w, this.h];
|
||||
}
|
||||
|
||||
get center() {
|
||||
return [this.x + this.w / 2, this.y + this.h / 2];
|
||||
}
|
||||
|
||||
get mouseOver() {
|
||||
return this.containsPoint(input.mouse.x, input.mouse.y);
|
||||
}
|
||||
|
||||
get mouseClicked() {
|
||||
return this.mouseOver() && input.mouse.pressed[0];
|
||||
}
|
||||
|
||||
containsPoint(x, y) {
|
||||
return x > this.x && x < this.x + this.w
|
||||
&& y > this.y && y < this.y + this.h;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue