aboutsummaryrefslogtreecommitdiff
path: root/src/js/main.js
blob: 0b80e3aec2df2fb9612d9a34d655461362d16800 (plain) (blame)
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
import { uBitBLE, MESEvents } from "./uBit";
import { notif_alert, notif_warn, notif_info, notif_success } from './notification';
import { Gamepad } from './gamepad';

/* Attempt to install service worker */
let sw = "service-worker.js";
if (navigator.serviceWorker) {
    navigator.serviceWorker.register(
        sw, {scope: '/microbit-gamepad/'}
    ).then(registration => {
        registration.onupdatefound = () => {
            const installingWorker = registration.installing;
            if (installingWorker == null) { return; }
            installingWorker.onstatechange = () => {
                if (installingWorker.state === "installed") {
                    if (navigator.serviceWorker.controller) {
                        notif_info("New content is available, relaunch the app to install it.");
                    } else {
                        notif_success("Content is cached for offline use.");
                    }
                }
            };
        };
        registration.update();
    }).catch(error => {
        notif_warn("Could not install service worker...");
        console.error("Error during service worker registration:", error);
    });
}

/* Allow the ignore-landscape-warning button to work */
document.getElementById("btn_ignore_landscape_warning").addEventListener("click", () => {
    document.body.classList.add("ignore-landscape-warning");
});

/* Show a warning if bluetooth is unavailable in the browser. */
if (!navigator.bluetooth) {
    //alert("Bluetooth not enabled in your browser, this won't work...");
    console.error("You do not have a bluetooth enabled browser, you need to have a bluetooth enabled browser...");
    notif_alert("Your browser does not seem to support bluetooth, try using Google Chrome or Microsoft Edge.");
}

/* Define and initialize things */
let gamepad = new Gamepad();
window.gamepad = gamepad;
let ubit = new uBitBLE();
window.ubit = ubit;

/* Setup storage and picker for the gamepad layout */
document.querySelector(".settings-dialog #layout").addEventListener("change", (v) => {
    gamepad.setGamepadLayout(v.target.value);
    localStorage.setItem("gamepadLayout", v.target.value);
    document.querySelector(".button-states pre").innerHTML = "No buttons pressed yet";
});
if (localStorage.getItem("gamepadLayout") === null) { localStorage.setItem("gamepadLayout", "1"); }
gamepad.setGamepadLayout(localStorage.getItem("gamepadLayout"));
document.querySelector(".button-states pre").innerHTML = "No buttons pressed yet";
document.querySelector(".settings-dialog #layout").value = localStorage.getItem("gamepadLayout");

/* Setup storage for toggling touches */
document.querySelector(".settings-dialog #show-touches").addEventListener("change", (v) => {
    gamepad.stage.showTouches = v.target.checked;
    localStorage.setItem("showTouches", v.target.checked);
});
if (localStorage.getItem("showTouches") === null) { localStorage.setItem("showTouches", false); }
gamepad.stage.showTouches = localStorage.getItem("showTouches") == "true";
document.querySelector(".settings-dialog #show-touches").checked = localStorage.getItem("showTouches") == "true";

/* Setup storage for toggling alt text */
document.querySelector(".settings-dialog #show-gamepad-alt-text").addEventListener("change", (v) => {
    gamepad.showAltText = v.target.checked;
    localStorage.setItem("showAltText", v.target.checked);
});
if (localStorage.getItem("showAltText") === null) { localStorage.setItem("showAltText", false); }
gamepad.showAltText = localStorage.getItem("showAltText") == "true";
document.querySelector(".settings-dialog #show-gamepad-alt-text").checked = localStorage.getItem("showAltText") == "true";

/* Setup storage for toggling vibration/haptic feedback */
document.querySelector(".settings-dialog #enable-haptic").addEventListener("change", (v) => {
    gamepad.enableVibration = v.target.checked;
    localStorage.setItem("enableHaptic", v.target.checked);
});
if (localStorage.getItem("enableHaptic") === null) { localStorage.setItem("enableHaptic", true); }
gamepad.enableVibration = localStorage.getItem("enableHaptic") == "true";
document.querySelector(".settings-dialog #enable-haptic").checked = localStorage.getItem("enableHaptic") == "true";

/* Setup storage for toggling debug mode */
document.querySelector(".settings-dialog #enable-debug").addEventListener("change", (v) => {
    gamepad.showDebug = v.target.checked;
    if (v.target.checked) {
        document.body.classList.add("debug");
    } else {
        document.body.classList.remove("debug");
    }
    localStorage.setItem("enableDebug", v.target.checked);
});
if (localStorage.getItem("enableDebug") === null) { localStorage.setItem("enableDebug", false); }
gamepad.showDebug = localStorage.getItem("enableDebug") == "true";
if (localStorage.getItem("enableDebug") === "true") {
    document.body.classList.add("debug");
} else {
    document.body.classList.remove("debug");
}
document.querySelector(".settings-dialog #enable-debug").checked = localStorage.getItem("enableDebug") == "true";

/* Setup buttons for opening/closing settings panel */
document.querySelector("#btn_show_settings").addEventListener("click", () => {
    document.querySelector(".settings-dialog").classList.add("shown");
});
document.querySelector("#btn_hide_settings").addEventListener("click", () => {
    document.querySelector(".settings-dialog").classList.remove("shown");
});

/* Setup actions for bluetooth connect/disconnect buttons */
document.querySelector("#btn_disconnect").addEventListener("click", () => {
    ubit.disconnect();
});
document.getElementById("btn_connect").addEventListener("click", async () => {
    if (!navigator.bluetooth) {
        notif_alert("You need a bluetooth enabled browser for this app to work, try chrome.");
    }
    try {
        await ubit.searchDevice();
    } catch (e) {
        notif_alert(`Could not connect to device: ${e}.`);
    }
});

/* Handle gamepad events */
let gamepadState = {};
gamepad.onTouchEvent(e => {
    /* This is just for the debug data */
    if (["touchstart", "touchmove"].includes(e.action)) {
        gamepadState[e.id] = {state: true, ...e};
    }
    if (["touchend"].includes(e.action)) {
        gamepadState[e.id] = {state: false, ...e};
    }
    let debugString = "";
    for (const [key, value] of Object.entries(gamepadState)) {
        debugString += `${key}: ${value.state ? 'Pressed' : 'Not pressed'}`;
        if (value.hasOwnProperty("x")) {
            debugString += ` (x: ${value.x}, y: ${value.y})`;
        }
        debugString += `\n`;
    }
    document.querySelector(".button-states pre").innerHTML = debugString;
});

gamepad.onTouchEvent(e => {
    const event_type = MESEvents.MES_DPAD_CONTROLLER_ID;
    let event_value = null;
    if (e.action == "touchstart") {
        if        (e.id == "A") {
            event_value = MESEvents.MES_DPAD_BUTTON_A_DOWN;
        } else if (e.id == "B") {
            event_value = MESEvents.MES_DPAD_BUTTON_B_DOWN;
        } else if (e.id == "C") {
            event_value = MESEvents.MES_DPAD_BUTTON_C_DOWN;
        } else if (e.id == "D") {
            event_value = MESEvents.MES_DPAD_BUTTON_D_DOWN;
        } else if (e.id == "1") {
            event_value = MESEvents.MES_DPAD_BUTTON_1_DOWN;
        } else if (e.id == "2") {
            event_value = MESEvents.MES_DPAD_BUTTON_2_DOWN;
        } else if (e.id == "3") {
            event_value = MESEvents.MES_DPAD_BUTTON_3_DOWN;
        } else if (e.id == "4") {
            event_value = MESEvents.MES_DPAD_BUTTON_4_DOWN;
        }
    } else if (e.action == "touchend") {
        if        (e.id == "A") {
            event_value = MESEvents.MES_DPAD_BUTTON_A_UP;
        } else if (e.id == "B") {
            event_value = MESEvents.MES_DPAD_BUTTON_B_UP;
        } else if (e.id == "C") {
            event_value = MESEvents.MES_DPAD_BUTTON_C_UP;
        } else if (e.id == "D") {
            event_value = MESEvents.MES_DPAD_BUTTON_D_UP;
        } else if (e.id == "1") {
            event_value = MESEvents.MES_DPAD_BUTTON_1_UP;
        } else if (e.id == "2") {
            event_value = MESEvents.MES_DPAD_BUTTON_2_UP;
        } else if (e.id == "3") {
            event_value = MESEvents.MES_DPAD_BUTTON_3_UP;
        } else if (e.id == "4") {
            event_value = MESEvents.MES_DPAD_BUTTON_4_UP;
        }
    }
    if ((ubit.isConnected()) && (event_value != null)) {
        ubit.sendEvent(event_type, event_value);
    }

    if ((e.id == "right") && e.hasOwnProperty("x")) {
        ubit.sendUart(`x:${e.x}\n`);
    }
    if ((e.id == "left") && e.hasOwnProperty("y")) {
        ubit.sendUart(`y:${e.y}\n`);
    }
});

/* Setup handlers for ubit (bluetooth) events */
ubit.onConnect(() => {
    document.body.classList.add("connected");
});

ubit.onDisconnect(() => {
    document.body.classList.remove("connected");
});