Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: option to vibrate on button press #580

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions runtimes/web/src/ui/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class InputState {
mouseButtons = 0;
}

const maxVibrateMs = 1024;

@customElement("wasm4-app")
export class App extends LitElement {
static styles = css`
Expand Down Expand Up @@ -76,6 +78,11 @@ export class App extends LitElement {

private readonly diskPrefix: string;

private _vibrateMs = 0;
get vibrateMs (): number {
return this._vibrateMs;
}

readonly onPointerUp = (event: PointerEvent) => {
if (event.pointerType == "touch") {
// Try to go fullscreen on mobile
Expand Down Expand Up @@ -670,6 +677,23 @@ export class App extends LitElement {
return netplay;
}

canVibrate (): boolean {
// No reliable way to tell, so best guess
return 'ontouchstart' in window && navigator.vibrate !== undefined;
}

cycleVibrateLevel (): number {
if (this._vibrateMs < 1) {
this._vibrateMs = 1;
} else if (this._vibrateMs < maxVibrateMs) {
this._vibrateMs *= 2;
} else {
this._vibrateMs = 0;
}

return this._vibrateMs;
}

getNetplaySummary () {
return this.netplay ? this.netplay.getSummary() : [];
}
Expand Down
28 changes: 24 additions & 4 deletions runtimes/web/src/ui/menu-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { customElement, state } from 'lit/decorators.js';
import { map } from 'lit/directives/map.js';

import { App } from "./app";
import { VirtualGamepad } from "./virtual-gamepad";
import * as constants from "../constants";

const optionContext = {
Expand All @@ -15,10 +16,11 @@ const optionIndex = [
CONTINUE: 0,
SAVE_STATE: 1,
LOAD_STATE: 2,
DISK_OPTIONS: 3,
BUTTON_VIBRATE: 3,
DISK_OPTIONS: 4,
// OPTIONS: null,
COPY_NETPLAY_LINK: 4,
RESET_CART: 5,
COPY_NETPLAY_LINK: 5,
RESET_CART: 6,
},
{
BACK: 0,
Expand All @@ -33,6 +35,7 @@ const options = [
"CONTINUE",
"SAVE STATE",
"LOAD STATE",
"VIBRATE:",
"DISK OPTIONS",
// "OPTIONS",
"COPY NETPLAY URL",
Expand Down Expand Up @@ -115,6 +118,7 @@ export class MenuOverlay extends LitElement {

@state() private selectedIdx = 0;
@state() private netplaySummary: { playerIdx: number, ping: number }[] = [];
@state() private vibrateLevel = "";
Copy link
Contributor Author

@BigRobCoder BigRobCoder Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried just referencing app.vibrateLevel, but wasn't seeing render updates immediately. Is this the correct pattern? Mirror the setting in a local @state() variable?


private netplayPollInterval?: number;

Expand Down Expand Up @@ -183,6 +187,9 @@ export class MenuOverlay extends LitElement {
this.app.loadGameState();
this.app.closeMenu();
break;
case this.optionIndex.BUTTON_VIBRATE:
this.vibrateLevel = this.app.cycleVibrateLevel() + "ms";
break;
case this.optionIndex.DISK_OPTIONS:
this.switchContext(optionContext.DISK);
break;
Expand Down Expand Up @@ -219,9 +226,17 @@ export class MenuOverlay extends LitElement {

if (pressedThisFrame & constants.BUTTON_DOWN) {
this.selectedIdx++;
if (this.optionContext === optionContext.DEFAULT && this.selectedIdx === this.optionIndex.BUTTON_VIBRATE &&
!this.app.canVibrate()) {
this.selectedIdx++;
}
}
if (pressedThisFrame & constants.BUTTON_UP) {
this.selectedIdx--;
if (this.optionContext === optionContext.DEFAULT && this.selectedIdx === this.optionIndex.BUTTON_VIBRATE &&
!this.app.canVibrate()) {
this.selectedIdx--;
}
}
this.selectedIdx = (this.selectedIdx + this.options.length) % this.options.length;
}
Expand All @@ -243,11 +258,16 @@ export class MenuOverlay extends LitElement {
}

render () {
if (!this.vibrateLevel) {
this.vibrateLevel = this.app.vibrateMs + "ms";
}
return html`
<div class="menu">
<ul style="display:${this.optionContext === optionContext.DEFAULT? "inherit": "none"}">
${map(options[optionContext.DEFAULT], (option, idx) =>
html`<li class="${this.selectedIdx == idx ? "selected" : ""}"}>${option}</li>`)}
idx == this.optionIndex.BUTTON_VIBRATE && !this.app.canVibrate() ? html`` :
html`<li class="${this.selectedIdx == idx ? "selected" : ""}"}>
${option}${idx == this.optionIndex.BUTTON_VIBRATE ? this.vibrateLevel : ""}</li>`)}
</ul>
<ul style="display:${this.optionContext === optionContext.DISK? "inherit": "none"}">
${map(options[optionContext.DISK], (option, idx) =>
Expand Down
7 changes: 7 additions & 0 deletions runtimes/web/src/ui/virtual-gamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export class VirtualGamepad extends LitElement {

readonly touchEvents = new Map();

private lastButtons = 0;

readonly onPointerEvent = (event: PointerEvent) => {
if (event.pointerType != "touch") {
return;
Expand Down Expand Up @@ -194,6 +196,11 @@ export class VirtualGamepad extends LitElement {
}
}

if (this.lastButtons != buttons && this.app.vibrateMs > 0 && this.app.canVibrate()) {
navigator.vibrate(this.app.vibrateMs);
}
this.lastButtons = buttons;

setClass(this.action1, "pressed", buttons & constants.BUTTON_X);
setClass(this.action2, "pressed", buttons & constants.BUTTON_Z);
setClass(this.dpad, "pressed-left", buttons & constants.BUTTON_LEFT);
Expand Down