From 0ff17fc2fb62a8b4a772b7b27840726e92c99c37 Mon Sep 17 00:00:00 2001 From: devnewton <1859457+devnewton@users.noreply.github.com> Date: Mon, 2 Dec 2019 16:59:29 +0100 Subject: [PATCH] Gamepad api (#444) Add gamepad API to JSO --- .../java/org/teavm/jso/browser/Navigator.java | 4 + .../teavm/jso/browser/WindowEventTarget.java | 3 +- .../jso/dom/events/GamepadEventTarget.java | 37 ++++++ .../java/org/teavm/jso/gamepad/Gamepad.java | 45 ++++++++ .../org/teavm/jso/gamepad/GamepadButton.java | 27 +++++ .../org/teavm/jso/gamepad/GamepadEvent.java | 24 ++++ samples/gamepad/pom.xml | 106 ++++++++++++++++++ .../teavm/samples/gamepad/Application.java | 70 ++++++++++++ .../gamepad/src/main/webapp/WEB-INF/web.xml | 21 ++++ samples/gamepad/src/main/webapp/index.html | 28 +++++ samples/pom.xml | 3 +- 11 files changed, 366 insertions(+), 2 deletions(-) create mode 100644 jso/apis/src/main/java/org/teavm/jso/dom/events/GamepadEventTarget.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/gamepad/Gamepad.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadButton.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadEvent.java create mode 100644 samples/gamepad/pom.xml create mode 100644 samples/gamepad/src/main/java/org/teavm/samples/gamepad/Application.java create mode 100644 samples/gamepad/src/main/webapp/WEB-INF/web.xml create mode 100644 samples/gamepad/src/main/webapp/index.html diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java b/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java index 901585adb..254953411 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java @@ -16,6 +16,7 @@ package org.teavm.jso.browser; import org.teavm.jso.JSBody; +import org.teavm.jso.gamepad.Gamepad; import org.teavm.jso.geolocation.Geolocation; public final class Navigator { @@ -36,4 +37,7 @@ public final class Navigator { @JSBody(script = "return navigator.languages;") public static native String[] getLanguages(); + + @JSBody(script = "return navigator.getGamepads();") + public static native Gamepad[] getGamepads(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/WindowEventTarget.java b/jso/apis/src/main/java/org/teavm/jso/browser/WindowEventTarget.java index c7365cc9d..5c1420a5a 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/WindowEventTarget.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/WindowEventTarget.java @@ -19,6 +19,7 @@ import org.teavm.jso.dom.events.Event; import org.teavm.jso.dom.events.EventListener; import org.teavm.jso.dom.events.EventTarget; import org.teavm.jso.dom.events.FocusEventTarget; +import org.teavm.jso.dom.events.GamepadEventTarget; import org.teavm.jso.dom.events.HashChangeEvent; import org.teavm.jso.dom.events.KeyboardEventTarget; import org.teavm.jso.dom.events.LoadEventTarget; @@ -26,7 +27,7 @@ import org.teavm.jso.dom.events.MessageEvent; import org.teavm.jso.dom.events.MouseEventTarget; public interface WindowEventTarget extends EventTarget, FocusEventTarget, MouseEventTarget, KeyboardEventTarget, - LoadEventTarget { + LoadEventTarget, GamepadEventTarget { default void listenBeforeOnload(EventListener listener) { addEventListener("beforeunload", listener); } diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/events/GamepadEventTarget.java b/jso/apis/src/main/java/org/teavm/jso/dom/events/GamepadEventTarget.java new file mode 100644 index 000000000..3605b981f --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/dom/events/GamepadEventTarget.java @@ -0,0 +1,37 @@ +/* + * Copyright 2019 devnewton. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.dom.events; + +import org.teavm.jso.gamepad.GamepadEvent; + +public interface GamepadEventTarget extends EventTarget { + default void listenGamepadConnected(EventListener listener) { + addEventListener("gamepadconnected", listener); + } + + default void neglectGamepadConnected(EventListener listener) { + removeEventListener("gamepadconnected", listener); + } + + default void listenGamepadDisconnected(EventListener listener) { + addEventListener("gamepaddisconnected", listener); + } + + default void neglectGamepadDisconnected(EventListener listener) { + removeEventListener("gamepaddisconnected", listener); + } + +} diff --git a/jso/apis/src/main/java/org/teavm/jso/gamepad/Gamepad.java b/jso/apis/src/main/java/org/teavm/jso/gamepad/Gamepad.java new file mode 100644 index 000000000..20fcb2e2f --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/gamepad/Gamepad.java @@ -0,0 +1,45 @@ +/* + * Copyright 2019 devnewton. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.gamepad; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +public interface Gamepad extends JSObject { + @JSProperty + double[] getAxes(); + + @JSProperty + GamepadButton[] getButtons(); + + @JSProperty + boolean isConnected(); + + @JSProperty + String getId(); + + @JSProperty + int getDisplayId(); + + @JSProperty + int getIndex(); + + @JSProperty + String getMapping(); + + @JSProperty + double getTimestamp(); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadButton.java b/jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadButton.java new file mode 100644 index 000000000..d7391662f --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadButton.java @@ -0,0 +1,27 @@ +/* + * Copyright 2019 devnewton. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.gamepad; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +public interface GamepadButton extends JSObject { + @JSProperty + double getValue(); + + @JSProperty + boolean isPressed(); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadEvent.java b/jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadEvent.java new file mode 100644 index 000000000..6a29bdca6 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/gamepad/GamepadEvent.java @@ -0,0 +1,24 @@ +/* + * Copyright 2019 devnewton. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.gamepad; + +import org.teavm.jso.JSProperty; +import org.teavm.jso.dom.events.Event; + +public interface GamepadEvent extends Event { + @JSProperty + Gamepad getGamepad(); +} diff --git a/samples/gamepad/pom.xml b/samples/gamepad/pom.xml new file mode 100644 index 000000000..b7c22ccc2 --- /dev/null +++ b/samples/gamepad/pom.xml @@ -0,0 +1,106 @@ + + + 4.0.0 + + + org.teavm + teavm-samples + 0.7.0-SNAPSHOT + + teavm-samples-gamepad + + war + + TeaVM Web gamepad web application + A sample application that demonstrate how to use Web gamepad API. + + + + org.teavm + teavm-classlib + ${project.version} + provided + + + org.teavm + teavm-jso-apis + ${project.version} + provided + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + + + + maven-war-plugin + 2.4 + + + + ${project.build.directory}/generated/js + + + + + + org.teavm + teavm-maven-plugin + ${project.version} + + + web-client + prepare-package + + compile + + + ${project.build.directory}/generated/js/teavm + org.teavm.samples.gamepad.Application + false + true + true + true + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + ../../checkstyle.xml + config_loc=${basedir}/../.. + + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + diff --git a/samples/gamepad/src/main/java/org/teavm/samples/gamepad/Application.java b/samples/gamepad/src/main/java/org/teavm/samples/gamepad/Application.java new file mode 100644 index 000000000..f79ce55eb --- /dev/null +++ b/samples/gamepad/src/main/java/org/teavm/samples/gamepad/Application.java @@ -0,0 +1,70 @@ +/* + * Copyright 2019 devnewton. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.samples.gamepad; + +import org.teavm.jso.browser.Navigator; +import org.teavm.jso.browser.Window; +import org.teavm.jso.dom.html.HTMLDocument; +import org.teavm.jso.dom.html.HTMLElement; +import org.teavm.jso.gamepad.Gamepad; +import org.teavm.jso.gamepad.GamepadButton; + +/** + * + * @author devnewton + */ +public final class Application { + + private static final HTMLDocument document = Window.current().getDocument(); + + private Application() { + } + + public static void main(String[] args) { + refresh(0); + } + + public static void refresh(double timestamp) { + StringBuilder sb = new StringBuilder(); + for (Gamepad pad : Navigator.getGamepads()) { + if (null != pad) { + sb.append("

"); + sb.append("Pad: ").append(pad.getId()).append("
"); + + sb.append("Axes: "); + double[] axes = pad.getAxes(); + for (int a = 0; a < axes.length; ++a) { + sb.append(axes[a]).append(" "); + } + sb.append("
"); + + sb.append("Buttons pressed: "); + int buttonNum = 1; + for (GamepadButton button : pad.getButtons()) { + if (button.isPressed()) { + sb.append(buttonNum).append(" "); + } + ++buttonNum; + } + + sb.append("

"); + } + } + HTMLElement status = document.getElementById("gamepad-status"); + status.setInnerHTML(sb.toString()); + Window.requestAnimationFrame(Application::refresh); + } +} diff --git a/samples/gamepad/src/main/webapp/WEB-INF/web.xml b/samples/gamepad/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..bfc410b12 --- /dev/null +++ b/samples/gamepad/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/samples/gamepad/src/main/webapp/index.html b/samples/gamepad/src/main/webapp/index.html new file mode 100644 index 000000000..adcee9bd9 --- /dev/null +++ b/samples/gamepad/src/main/webapp/index.html @@ -0,0 +1,28 @@ + + + + + Web Gamepad web application + + + + +

Gamepad test application

+ Plug your gamepad and press some buttons. +
+ + diff --git a/samples/pom.xml b/samples/pom.xml index 486aa8b23..2930bdb18 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -37,5 +37,6 @@ async kotlin scala + gamepad - \ No newline at end of file +