forked from ParticulaCode/GoDiceJavaScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
222 lines (175 loc) · 8.1 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const connectedDice = {};
// Open the Bluetooth connection dialog for choosing a GoDice to connect
function openConnectionDialog() {
const newDice = new GoDice();
newDice.requestDevice();
}
/**
* Get a new dice element or it's instance if it already exists
* @param {string} diceID - the die unique identifier
*/
function getDiceHtmlEl(diceID) {
if (!document.getElementById(diceID)) {
const newDiceEl = document.createElement("div");
newDiceEl.id = diceID;
newDiceEl.className = "dice-wrapper";
return newDiceEl;
}
return document.getElementById(diceID);
}
GoDice.prototype.onDiceConnected = (diceId, diceInstance) => {
console.log("Dice connected: ", diceId);
// Called when a new die is connected - create a dedicate panel for this die
connectedDice[diceId] = diceInstance;
// get new die element or it's instance if it's already exists
const diceHtmlEl = getDiceHtmlEl(diceId);
// clear existing die element content if it exists
while (diceHtmlEl.firstChild) {
diceHtmlEl.removeChild(diceHtmlEl.lastChild);
}
// get die host from html, where we will put our connected dices
const diceHost = document.getElementById("dice-host");
// add name to die element
const diceName = document.createElement('div');
diceName.className = 'dice-name';
diceName.textContent = `Dice ID: ${diceId}`;
diceHtmlEl.append(diceName)
// add battery level button goDice.getBatteryLevel(diceID);
const batteryLevelButton = document.createElement('button');
batteryLevelButton.className = 'btn btn-outline-primary';
batteryLevelButton.onclick = diceInstance.getBatteryLevel.bind(diceInstance);
batteryLevelButton.textContent = 'Get Battery Level';
diceHtmlEl.append(batteryLevelButton)
// add battery level indicator
const batteryIndicator = document.createElement('div');
batteryIndicator.id = `${diceId}-battery-indicator`;
diceHtmlEl.append(batteryIndicator)
// set RGB color for the Led (e.g. blue)
const colorProfile = [[0, 0, 255], [0, 0, 255]];
// add "Led On" button to use goDice.switchOnLed(diceID,colorProfile) function
const ledOnButton = document.createElement('button');
ledOnButton.className = 'btn btn-outline-primary';
ledOnButton.onclick = diceInstance.setLed.bind(diceInstance, colorProfile[0], colorProfile[1])
ledOnButton.textContent = 'Switch On Led';
diceHtmlEl.append(ledOnButton)
// add "Led Off" button to use goDice.switchOffLed(diceID) function
const ledOffButton = document.createElement('button');
ledOffButton.className = 'btn btn-outline-primary';
ledOffButton.onclick = diceInstance.setLed.bind(diceInstance, [0], [0])
ledOffButton.textContent = 'Switch Off Led';
diceHtmlEl.append(ledOffButton)
// Pulse Led
const ledPulseButton = document.createElement('button');
ledPulseButton.className = 'btn btn-outline-primary';
ledPulseButton.onclick = diceInstance.pulseLed.bind(diceInstance, 5, 30, 20, [0, 0, 255])
ledPulseButton.textContent = "Pulse"
diceHtmlEl.append(ledPulseButton)
// get Dice color to use goDice.getDiceColor(diceID) function
const getDiceColorButton = document.createElement('button');
getDiceColorButton.className = 'btn btn-outline-primary';
getDiceColorButton.onclick = diceInstance.getDiceColor.bind(diceInstance)
getDiceColorButton.textContent = 'Get Dice Color';
diceHtmlEl.append(getDiceColorButton)
// Change die type buttons select
const d6Button = document.createElement('button');
d6Button.className = 'diebtn btn-outline-primary';
d6Button.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D6)
d6Button.textContent = 'D6';
diceHtmlEl.append(d6Button)
// D20 Shell
const d20Button = document.createElement('button');
d20Button.className = 'diebtn btn-outline-primary';
d20Button.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D20)
d20Button.textContent = 'D20';
diceHtmlEl.append(d20Button)
const d10Button = document.createElement('button');
d10Button.className = 'diebtn btn-outline-primary';
d10Button.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D10)
d10Button.textContent = 'D10';
diceHtmlEl.append(d10Button)
const d10XButton = document.createElement('button');
d10XButton.className = 'diebtn btn-outline-primary';
d10XButton.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D10X)
d10XButton.textContent = 'D10X';
diceHtmlEl.append(d10XButton)
// D24 Shell
const d4Button = document.createElement('button');
d4Button.className = 'diebtn btn-outline-primary';
d4Button.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D4)
d4Button.textContent = 'D4';
diceHtmlEl.append(d4Button)
const d8Button = document.createElement('button');
d8Button.className = 'diebtn btn-outline-primary';
d8Button.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D8)
d8Button.textContent = 'D8';
diceHtmlEl.append(d8Button)
const d12Button = document.createElement('button');
d12Button.className = 'diebtn btn-outline-primary';
d12Button.onclick = diceInstance.setDieType.bind(diceInstance, GoDice.diceTypes.D12)
d12Button.textContent = 'D12';
diceHtmlEl.append(d12Button)
// add battery level indicator
const colorIndicator = document.createElement('div');
colorIndicator.id = `${diceId}-color-indicator`;
diceHtmlEl.append(colorIndicator)
// add die status indicator
const dieStatus = document.createElement('div');
dieStatus.id = `${diceId}-die-status`;
diceHtmlEl.append(dieStatus)
// inject dice into html
diceHost.appendChild(diceHtmlEl);
};
GoDice.prototype.onRollStart = (diceId) => {
console.log("Roll Start: ", diceId);
// get rolling indicator
const diceIndicatorEl = document.getElementById(diceId + "-die-status");
// show rolling
diceIndicatorEl.textContent = "Rollling....";
};
GoDice.prototype.onDiceDisconnected = (diceId, diceInstance) => {
console.log("Roll Start: ", diceId);
// get rolling indicator
const diceIndicatorEl = document.getElementById(diceId + "-die-status");
// show rolling
diceIndicatorEl.textContent = "disconnected";
// Attempt to reconnect
diceInstance.attemptReconnect(diceId, diceInstance);
};
GoDice.prototype.onStable = (diceId, value, xyzArray) => {
console.log("Stable event: ", diceId, value);
// Get roll value indicator and show stable value
const diceIndicatorEl = document.getElementById(diceId + "-die-status");
diceIndicatorEl.textContent = "Stable: " + value;
};
GoDice.prototype.onTiltStable = (diceId, xyzArray, value) => {
console.log("TiltStable: ", diceId, xyzArray);
// Get tile indicator and show raw data
const diceIndicatorEl = document.getElementById(diceId + "-die-status");
diceIndicatorEl.textContent = "Tilt Stable: value: " + value + " , x=" + xyzArray[0] + " ,y=" + xyzArray[1] + " ,z=" + xyzArray[2];
};
GoDice.prototype.onFakeStable = (diceId, value, xyzArray) => {
console.log("FakeStable: ", diceId, value);
// Get tile indicator and show fake value
const diceIndicatorEl = document.getElementById(diceId + "-die-status");
diceIndicatorEl.textContent = "Fake Stable: " + value;
};
GoDice.prototype.onMoveStable = (diceId, value, xyzArray) => {
console.log("MoveStable: ", diceId, value);
// Get tile indicator and show fake value
const diceIndicatorEl = document.getElementById(diceId + "-die-status");
diceIndicatorEl.textContent = "Move Stable: " + value;
};
GoDice.prototype.onBatteryLevel = (diceId, batteryLevel) => {
console.log("BetteryLevel: ", diceId, batteryLevel);
// get dice battery indicator element
const batteryLevelEl = document.getElementById(diceId + "-battery-indicator");
// put battery level value into battery indicator html element
batteryLevelEl.textContent = batteryLevel;
};
GoDice.prototype.onDiceColor = (diceId, color) => {
console.log("DiceColor: ", diceId, color);
// get dice color indicator element
const diceColorEl = document.getElementById(diceId + "-color-indicator");
// put dice color value into battery indicator html element
diceColorEl.textContent = "Color: " + color;
};