Gamepad api (#444)

Add gamepad API to JSO
This commit is contained in:
devnewton 2019-12-02 16:59:29 +01:00 committed by Alexey Andreev
parent 780a3a8cca
commit 0ff17fc2fb
11 changed files with 366 additions and 2 deletions

View File

@ -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();
}

View File

@ -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<Event> listener) {
addEventListener("beforeunload", listener);
}

View File

@ -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<GamepadEvent> listener) {
addEventListener("gamepadconnected", listener);
}
default void neglectGamepadConnected(EventListener<GamepadEvent> listener) {
removeEventListener("gamepadconnected", listener);
}
default void listenGamepadDisconnected(EventListener<GamepadEvent> listener) {
addEventListener("gamepaddisconnected", listener);
}
default void neglectGamepadDisconnected(EventListener<GamepadEvent> listener) {
removeEventListener("gamepaddisconnected", listener);
}
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

106
samples/gamepad/pom.xml Normal file
View File

@ -0,0 +1,106 @@
<!--
Copyright 2014 Alexey Andreev.
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm-samples</artifactId>
<version>0.7.0-SNAPSHOT</version>
</parent>
<artifactId>teavm-samples-gamepad</artifactId>
<packaging>war</packaging>
<name>TeaVM Web gamepad web application</name>
<description>A sample application that demonstrate how to use Web gamepad API.</description>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso-apis</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/generated/js</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>web-client</id>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
<mainClass>org.teavm.samples.gamepad.Application</mainClass>
<minifying>false</minifying>
<debugInformationGenerated>true</debugInformationGenerated>
<sourceMapsGenerated>true</sourceMapsGenerated>
<sourceFilesCopied>true</sourceFilesCopied>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>../../checkstyle.xml</configLocation>
<propertyExpansion>config_loc=${basedir}/../..</propertyExpansion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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("<p>");
sb.append("Pad: ").append(pad.getId()).append("<br>");
sb.append("Axes: ");
double[] axes = pad.getAxes();
for (int a = 0; a < axes.length; ++a) {
sb.append(axes[a]).append(" ");
}
sb.append("<br>");
sb.append("Buttons pressed: ");
int buttonNum = 1;
for (GamepadButton button : pad.getButtons()) {
if (button.isPressed()) {
sb.append(buttonNum).append(" ");
}
++buttonNum;
}
sb.append("</p>");
}
}
HTMLElement status = document.getElementById("gamepad-status");
status.setInnerHTML(sb.toString());
Window.requestAnimationFrame(Application::refresh);
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2014 Alexey Andreev.
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.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>

View File

@ -0,0 +1,28 @@
<!--
Copyright 2014 Alexey Andreev.
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.
-->
<!DOCTYPE html>
<html>
<head>
<title>Web Gamepad web application</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" charset="utf-8" src="teavm/classes.js"></script>
</head>
<body onload="main()">
<h1>Gamepad test application</h1>
Plug your gamepad and press some buttons.
<div id="gamepad-status"></div>
</body>
</html>

View File

@ -37,5 +37,6 @@
<module>async</module>
<module>kotlin</module>
<module>scala</module>
<module>gamepad</module>
</modules>
</project>
</project>