kaplay-ui 0.18.4 → 0.20.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/README.md +46 -6
- package/inputs/index.js +93 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,9 +57,9 @@ add(checkBox);
|
|
|
57
57
|
|
|
58
58
|
A cehckbox has the following state:
|
|
59
59
|
|
|
60
|
-
| state
|
|
61
|
-
| --------- |
|
|
62
|
-
| `checked` | `boolean` | false
|
|
60
|
+
| state | Type | Default | State change by |
|
|
61
|
+
| --------- | --------- | ------- | ----------------- |
|
|
62
|
+
| `checked` | `boolean` | false | Click on checkbox |
|
|
63
63
|
|
|
64
64
|
##### Parameters
|
|
65
65
|
|
|
@@ -74,6 +74,46 @@ A checkbox can take the following parameters:
|
|
|
74
74
|
|
|
75
75
|
<br>
|
|
76
76
|
|
|
77
|
+
#### Switch
|
|
78
|
+
|
|
79
|
+
To make a text button, one must use the exported <code>makeSwitch()</code> function.
|
|
80
|
+
|
|
81
|
+
##### Example
|
|
82
|
+
|
|
83
|
+
An example usage of a text button.
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
import kaplay from "kaplay";
|
|
87
|
+
import "kaplay/global";
|
|
88
|
+
|
|
89
|
+
import { makeSwitch } from "kaplay-ui/inputs";
|
|
90
|
+
|
|
91
|
+
kaplay();
|
|
92
|
+
|
|
93
|
+
const switchBtn = makeSwitch();
|
|
94
|
+
|
|
95
|
+
add(switchBtn);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
##### State
|
|
99
|
+
|
|
100
|
+
A cehckbox has the following state:
|
|
101
|
+
|
|
102
|
+
| state | Type | Default | State change by |
|
|
103
|
+
| ---------- | --------- | ------- | --------------- |
|
|
104
|
+
| `switched` | `boolean` | false | Click on switch |
|
|
105
|
+
|
|
106
|
+
##### Parameters
|
|
107
|
+
|
|
108
|
+
A switch can take the following parameters:
|
|
109
|
+
|
|
110
|
+
| Parameter | Type | Default | Required | Description |
|
|
111
|
+
| --------- | -------- | ------- | -------- | ----------------- |
|
|
112
|
+
| `x` | `number` | 0 | ❌ No | Switch x position |
|
|
113
|
+
| `y` | `number` | 0 | ❌ No | Switch x position |
|
|
114
|
+
| `width` | `number` | 50 | ❌ No | Switch width |
|
|
115
|
+
| `height` | `number` | 25 | ❌ No | Switch height |
|
|
116
|
+
|
|
77
117
|
#### Text Button
|
|
78
118
|
|
|
79
119
|
To make a text button, one must use the exported <code>makeTextButton()</code> function.
|
|
@@ -137,9 +177,9 @@ add(toggle);
|
|
|
137
177
|
|
|
138
178
|
A cehckbox has the following state:
|
|
139
179
|
|
|
140
|
-
| state
|
|
141
|
-
| --------- |
|
|
142
|
-
| `toggled` | `boolean` | false
|
|
180
|
+
| state | Type | Default | State change by |
|
|
181
|
+
| --------- | --------- | ------- | --------------- |
|
|
182
|
+
| `toggled` | `boolean` | false | Click on toggle |
|
|
143
183
|
|
|
144
184
|
##### Parameters
|
|
145
185
|
|
package/inputs/index.js
CHANGED
|
@@ -131,3 +131,96 @@ export const makeCheckbox = (x = 0, y = 0, width = 25, height = 25) => {
|
|
|
131
131
|
|
|
132
132
|
return checkbox;
|
|
133
133
|
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Makes switch
|
|
137
|
+
* @param {number} x - Switch x postion (default is 0)
|
|
138
|
+
* @param {number} y - Switch y postion (default is 0)
|
|
139
|
+
* @param {number} width - Switch width (default is 50)
|
|
140
|
+
* @param {number} height - Switch height (default is 25)
|
|
141
|
+
* @returns {GameObj}
|
|
142
|
+
*/
|
|
143
|
+
export const makeSwitch = (x = 0, y = 0, width = 50, height = 25) => {
|
|
144
|
+
// Make switch base
|
|
145
|
+
const switchBase = make([
|
|
146
|
+
rect(width, height),
|
|
147
|
+
pos(x, y),
|
|
148
|
+
opacity(0),
|
|
149
|
+
area(),
|
|
150
|
+
{
|
|
151
|
+
switched: false,
|
|
152
|
+
},
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
// Make switch line
|
|
156
|
+
const switchLine = make([
|
|
157
|
+
rect(width, height / 2, { radius: height / 4 }),
|
|
158
|
+
color(169, 169, 169),
|
|
159
|
+
pos(0, height / 2 - height / 4),
|
|
160
|
+
area(),
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
// Make switch circle
|
|
164
|
+
const switchCircle = make([circle(height / 2), pos(height / 2, height / 2)]);
|
|
165
|
+
|
|
166
|
+
// On click
|
|
167
|
+
switchBase.onClick(() => {
|
|
168
|
+
if (switchBase.switched) {
|
|
169
|
+
switchLine.use(color(169, 169, 169));
|
|
170
|
+
switchCircle.use(color(255, 255, 255));
|
|
171
|
+
switchCircle.use(pos(height / 2, height / 2));
|
|
172
|
+
switchBase.switched = false;
|
|
173
|
+
} else {
|
|
174
|
+
switchLine.use(color(148, 188, 236));
|
|
175
|
+
switchCircle.use(color(24, 118, 210));
|
|
176
|
+
switchCircle.use(pos(width - switchCircle.radius, height / 2));
|
|
177
|
+
switchBase.switched = true;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Add line and circle
|
|
182
|
+
switchBase.add(switchLine);
|
|
183
|
+
switchBase.add(switchCircle);
|
|
184
|
+
|
|
185
|
+
return switchBase;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Makes text input
|
|
190
|
+
* @param {number} x - Text input x postion (default is 0)
|
|
191
|
+
* @param {number} y - Text input y postion (default is 0)
|
|
192
|
+
* @param {number} width - Text input width (default is 400)
|
|
193
|
+
* @param {number} txtSize - Text input text size (default is 15)
|
|
194
|
+
* @param {number} pad - Text input padding (default is 10)
|
|
195
|
+
* @param {boolean} hasFocus - Text input focus (default is true)
|
|
196
|
+
* @returns {GameObj}
|
|
197
|
+
*/
|
|
198
|
+
export const makeTextInput = (
|
|
199
|
+
x = 0,
|
|
200
|
+
y = 0,
|
|
201
|
+
width = 400,
|
|
202
|
+
txtSize = 15,
|
|
203
|
+
pad = 10,
|
|
204
|
+
hasFocus = true
|
|
205
|
+
) => {
|
|
206
|
+
// Text input container
|
|
207
|
+
const inputBase = make([
|
|
208
|
+
rect(width, txtSize + pad * 2),
|
|
209
|
+
pos(x, y),
|
|
210
|
+
area(),
|
|
211
|
+
outline(1),
|
|
212
|
+
]);
|
|
213
|
+
|
|
214
|
+
// Text input
|
|
215
|
+
const input = make([
|
|
216
|
+
text("", { width: width - pad * 2, size: txtSize }),
|
|
217
|
+
textInput(hasFocus),
|
|
218
|
+
color(BLACK),
|
|
219
|
+
pos(pad, pad),
|
|
220
|
+
area(),
|
|
221
|
+
]);
|
|
222
|
+
|
|
223
|
+
inputBase.add(input);
|
|
224
|
+
|
|
225
|
+
return inputBase;
|
|
226
|
+
};
|