From 8889b63df737a0e36d6259bfca48d37d0cf2c140 Mon Sep 17 00:00:00 2001 From: pizzadox9999 Date: Wed, 3 Jul 2024 19:30:06 +0200 Subject: [PATCH] jso: add TouchEvent bindings --- .../teavm/jso/browser/WindowEventTarget.java | 3 +- .../java/org/teavm/jso/dom/events/Touch.java | 123 ++++++++++++++++++ .../org/teavm/jso/dom/events/TouchEvent.java | 81 ++++++++++++ .../jso/dom/events/TouchEventTarget.java | 63 +++++++++ .../org/teavm/jso/dom/html/HTMLElement.java | 3 +- 5 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 jso/apis/src/main/java/org/teavm/jso/dom/events/Touch.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEvent.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEventTarget.java 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 296213a2d..47470e11c 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 @@ -27,9 +27,10 @@ import org.teavm.jso.dom.events.MessageEvent; import org.teavm.jso.dom.events.MouseEventTarget; import org.teavm.jso.dom.events.Registration; import org.teavm.jso.dom.events.StorageEvent; +import org.teavm.jso.dom.events.TouchEventTarget; public interface WindowEventTarget extends EventTarget, FocusEventTarget, MouseEventTarget, KeyboardEventTarget, - LoadEventTarget, GamepadEventTarget { + LoadEventTarget, GamepadEventTarget, TouchEventTarget { @Deprecated default void listenBeforeOnload(EventListener listener) { addEventListener("beforeunload", listener); diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/events/Touch.java b/jso/apis/src/main/java/org/teavm/jso/dom/events/Touch.java new file mode 100644 index 000000000..be763fd46 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/dom/events/Touch.java @@ -0,0 +1,123 @@ +/* + * Copyright 2024 pizzadox9999, 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. + */ +package org.teavm.jso.dom.events; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + *

The Touch interface represents a single contact point on a touch-sensitive device. + * The contact point is commonly a finger or stylus and the device may be a touchscreen or trackpad.

+ * + *

The {@link #getRadiusX()}, {@link #getRadiusY()}, and {@link #getRotationAngle()} + * describe the area of contact between the user and the screen, the touch area. + * This can be helpful when dealing with imprecise pointing devices such as fingers. + * These values are set to describe an ellipse that as closely as possible matches + * the entire area of contact (such as the user's fingertip).

+ */ +public interface Touch extends JSObject { + /** + * Returns a unique identifier for this Touch object. A given touch point (say, + * by a finger) will have the same identifier for the duration of its movement + * around the surface. This lets you ensure that you're tracking the same touch + * all the time. + */ + @JSProperty + int getIdentifier(); + + /** + * Returns the X coordinate of the touch point relative to the left edge of the + * screen. + */ + @JSProperty + double getScreenX(); + + /** + * Returns the Y coordinate of the touch point relative to the top edge of the + * screen. + */ + @JSProperty + double getScreenY(); + + /** + * Returns the X coordinate of the touch point relative to the left edge of the + * browser viewport, not including any scroll offset. + */ + @JSProperty + double getClientX(); + + /** + * Returns the Y coordinate of the touch point relative to the top edge of the + * browser viewport, not including any scroll offset. + */ + @JSProperty + double getClientY(); + + /** + * Returns the X coordinate of the touch point relative to the left edge of the + * document. Unlike clientX, this value includes the horizontal scroll offset, + * if any. + */ + @JSProperty + double getPageX(); + + /** + * Returns the Y coordinate of the touch point relative to the top of the + * document. Unlike clientY, this value includes the vertical scroll offset, if + * any. + */ + @JSProperty + double getPageY(); + + /** + * Returns the {@link TouchEventTarget} on which the touch point started when it was first placed + * on the surface, even if the touch point has since moved outside the + * interactive area of that element or even been removed from the document. + */ + @JSProperty + TouchEventTarget getTarget(); + + /** + * Returns the X radius of the ellipse that most closely circumscribes the area + * of contact with the screen. The value is in pixels of the same scale as + * screenX. + */ + @JSProperty + double getRadiusX(); + + /** + * Returns the Y radius of the ellipse that most closely circumscribes the area + * of contact with the screen. The value is in pixels of the same scale as + * screenY. + */ + @JSProperty + double getRadiusY(); + + /** + * Returns the angle (in degrees) that the ellipse described by radiusX and + * radiusY must be rotated, clockwise, to most accurately cover the area of + * contact between the user and the surface. + */ + @JSProperty + double getRotationAngle(); + + /** + * Returns the amount of pressure being applied to the surface by the user, as a + * float between 0.0 (no pressure) and 1.0 (maximum pressure). + */ + @JSProperty + double getForce(); +} \ No newline at end of file diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEvent.java b/jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEvent.java new file mode 100644 index 000000000..05e28db88 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEvent.java @@ -0,0 +1,81 @@ +/* + * Copyright 2024 pizzadox9999, 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. + */ +package org.teavm.jso.dom.events; + +import org.teavm.jso.JSProperty; +import org.teavm.jso.core.JSArrayReader; + +/** + *

The TouchEvent interface represents an UIEvent which is sent when the state + * of contacts with a touch-sensitive surface changes. + * This surface can be a touch screen or trackpad, for example. + * The event can describe one or more points of contact with the screen and includes support + * for detecting movement, addition and removal of contact points, and so forth.

+ * + *

Touches are represented by the {@link Touch} object; each touch is described by a position, + * size and shape, amount of pressure, and target element.

+ */ +public interface TouchEvent extends Event { + /** + * A {@code boolean} value indicating whether the alt key was down when the touch + * event was fired. + */ + @JSProperty + boolean isAltKey(); + + /** + * A list of all the {@link Touch} objects representing individual points of + * contact whose states changed between the previous touch event and this one. + */ + @JSProperty + JSArrayReader getChangedTouches(); + + /** + * A {@code boolean} value indicating whether the control key was down when the + * touch event was fired. + */ + @JSProperty + boolean isCtrlKey(); + + /** + * A {@code boolean} value indicating whether the meta key was down when the + * touch event was fired. + */ + @JSProperty + boolean isMetaKey(); + + /** + * A {@code boolean} value indicating whether the shift key was down when the + * touch event was fired. + */ + @JSProperty + boolean isShiftKey(); + + /** + * A list of all the {@link Touch} objects that are both currently in contact with + * the touch surface and were also started on the same element that is the + * target of the event. + */ + @JSProperty + JSArrayReader getTargetTouches(); + + /** + * A list of all the {@link Touch} objects representing all current points of + * contact with the surface, regardless of target or changed status. + */ + @JSProperty + JSArrayReader getTouches(); +} \ No newline at end of file diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEventTarget.java b/jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEventTarget.java new file mode 100644 index 000000000..5fd97bcbd --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/dom/events/TouchEventTarget.java @@ -0,0 +1,63 @@ +/* + * Copyright 2024 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. + */ +package org.teavm.jso.dom.events; + +public interface TouchEventTarget extends EventTarget { + /** + * The touchstart event is fired when one or more touch points are placed on the touch surface. + */ + default Registration onTouchStart(EventListener listener) { + return onEvent("touchstart", listener); + } + + /** + * The touchend event fires when one or more touch points are removed from the touch surface. + * Remember that it is possible to get a touchcancel event instead. + */ + default Registration onTouchEnd(EventListener listener) { + return onEvent("touchend", listener); + } + + /** + *

The touchcancel event is fired when one or more touch points have been disrupted in an + * implementation-specific manner.

+ * + *

Some examples of situations that will trigger a touchcancel event:

+ * + *
    + *
  • A hardware event occurs that cancels the touch activities. + * This may include, for example, the user switching applications using an application switcher + * interface or the "home" button on a mobile device.
  • + *
  • The device's screen orientation is changed while the touch is active.
  • + *
  • The browser decides that the user started touch input accidentally. + * This can happen if, for example, the hardware supports palm rejection to prevent a hand resting on + * the display while using a stylus from accidentally triggering events.
  • + *
  • The {@code touch-action} CSS property prevents the input from continuing. + * When the user interacts with too many fingers simultaneously, the browser can fire this event + * for all existing pointers (even if the user is still touching the screen).
  • + *
+ */ + default Registration onTouchCancel(EventListener listener) { + return onEvent("touchcancel", listener); + } + + /** + * The touchmove event is fired when one or more touch points are moved along the touch surface. + */ + default Registration onTouchMove(EventListener listener) { + return onEvent("touchmove", listener); + } +} diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java index b40546484..20786be97 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java @@ -23,6 +23,7 @@ import org.teavm.jso.dom.events.FocusEventTarget; import org.teavm.jso.dom.events.KeyboardEventTarget; import org.teavm.jso.dom.events.LoadEventTarget; import org.teavm.jso.dom.events.MouseEventTarget; +import org.teavm.jso.dom.events.TouchEventTarget; import org.teavm.jso.dom.events.WheelEventTarget; import org.teavm.jso.dom.types.DOMTokenList; import org.teavm.jso.dom.xml.Element; @@ -30,7 +31,7 @@ import org.teavm.jso.dom.xml.Node; import org.teavm.jso.dom.xml.NodeList; public abstract class HTMLElement implements Element, ElementCSSInlineStyle, EventTarget, FocusEventTarget, - MouseEventTarget, WheelEventTarget, KeyboardEventTarget, LoadEventTarget { + MouseEventTarget, WheelEventTarget, KeyboardEventTarget, LoadEventTarget, TouchEventTarget { @Override public abstract NodeList getElementsByTagName(String name);