-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathbutton.js
61 lines (51 loc) · 1.53 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// @ts-check
// Simple UI and setup for buttons
kaplay({
background: [135, 62, 132],
});
// reset cursor to default on frame start for easier cursor management
onUpdate(() => setCursor("default"));
// Function that adds a button to the game with a given text, position and function
function addButton(
txt = "start game",
p = vec2(200, 100),
f = () => debug.log("hello"),
) {
// add a parent background object
const btn = add([
rect(240, 80, { radius: 8 }),
pos(p),
area(),
scale(1),
anchor("center"),
outline(4),
color(255, 255, 255),
]);
// add a child object that displays the text
btn.add([
text(txt),
anchor("center"),
color(0, 0, 0),
]);
// onHoverUpdate() comes from area() component
// it runs every frame when the object is being hovered
btn.onHoverUpdate(() => {
const t = time() * 10;
btn.color = hsl2rgb((t / 10) % 1, 0.6, 0.7);
btn.scale = vec2(1.2);
setCursor("pointer");
});
// onHoverEnd() comes from area() component
// it runs once when the object stopped being hovered
btn.onHoverEnd(() => {
btn.scale = vec2(1);
btn.color = rgb();
});
// onClick() comes from area() component
// it runs once when the object is clicked
btn.onClick(f);
return btn;
}
// Adds the buttons with the function we added
addButton("Start", vec2(200, 100), () => debug.log("oh hi"));
addButton("Quit", vec2(200, 200), () => debug.log("bye"));