kaplay-ui 0.9.5 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/buttons/index.js +70 -0
- package/package.json +1 -1
package/buttons/index.js
CHANGED
|
@@ -71,3 +71,73 @@ export const addTextButton = (
|
|
|
71
71
|
|
|
72
72
|
return btn;
|
|
73
73
|
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Adds a clickable button icon button with hover effect
|
|
77
|
+
* @param {string} spriteName - Sprite to use as icon
|
|
78
|
+
* @param {Function} [onClick] - Click event handler (default is () => {}))
|
|
79
|
+
* @param {number} x - The x postion to set (default is center().x).
|
|
80
|
+
* @param {number} y - The x postion to set (default is center().y)
|
|
81
|
+
* @param {number} w - Width of button (default is 48)
|
|
82
|
+
* @param {number} h - Height of button (default is 48)
|
|
83
|
+
* @param {boolean} [hoverFx] - Add hover effect (default is true)
|
|
84
|
+
* @returns {GameObj}
|
|
85
|
+
*/
|
|
86
|
+
export const addIconButton = (
|
|
87
|
+
spriteName,
|
|
88
|
+
onClick = () => {},
|
|
89
|
+
x = center().x,
|
|
90
|
+
y = center().y,
|
|
91
|
+
w = 48,
|
|
92
|
+
h = 48,
|
|
93
|
+
hoverFx = true
|
|
94
|
+
) => {
|
|
95
|
+
// Shadow effect
|
|
96
|
+
const shadow = add([
|
|
97
|
+
rect(w, h, { radius: 8 }),
|
|
98
|
+
pos(x + 2, y + 2),
|
|
99
|
+
area(),
|
|
100
|
+
scale(1),
|
|
101
|
+
anchor("center"),
|
|
102
|
+
outline(1),
|
|
103
|
+
color(128, 128, 128),
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
// Add button
|
|
107
|
+
const iconBtn = add([
|
|
108
|
+
rect(w, h, { radius: 8 }),
|
|
109
|
+
pos(x, y),
|
|
110
|
+
area(),
|
|
111
|
+
scale(1),
|
|
112
|
+
anchor("center"),
|
|
113
|
+
outline(1),
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
// Add icon
|
|
117
|
+
iconBtn.add([sprite(spriteName), anchor("center")]);
|
|
118
|
+
|
|
119
|
+
// On click handler
|
|
120
|
+
iconBtn.onClick(() => {
|
|
121
|
+
setCursor("default");
|
|
122
|
+
onClick();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// On hover effect
|
|
126
|
+
iconBtn.onHover(() => {
|
|
127
|
+
setCursor("pointer");
|
|
128
|
+
if (hoverFx) {
|
|
129
|
+
iconBtn.scale = vec2(1.03);
|
|
130
|
+
shadow.scale = vec2(1.03);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
iconBtn.onHoverEnd(() => {
|
|
135
|
+
setCursor("default");
|
|
136
|
+
if (hoverFx) {
|
|
137
|
+
iconBtn.scale = vec2(1);
|
|
138
|
+
shadow.scale = vec2(1);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return iconBtn;
|
|
143
|
+
};
|