Input
Forwarding mouse, wheel, keyboard, IME, touch and gamepad input from your game to a view.
Aperture UI does not read devices. Your input layer turns platform events into view coordinates
and calls the matching function on each UIView. Do this before you call tick() in the same
frame, so the page sees the input in the frame it arrived.
Coordinates
Every position is in the view's viewport pixels, with (0, 0) at the top left. Convert from
window coordinates first, whenever the view does not fill the window at its native size:
const float viewX = (windowX - viewLeft) * (viewWidth / viewDrawnWidth);
const float viewY = (windowY - viewTop) * (viewHeight / viewDrawnHeight);viewWidth and viewHeight are what you passed to setViewportSize. viewDrawnWidth and
viewDrawnHeight are the size of the rectangle on screen. For a full-screen view at native
resolution the transform is the identity. A position that is wrong by a scale factor is the usual
cause of clicks that land beside the control.
Mouse
aperture::UIView::ModifierKeys mods;
mods.shift = IsShiftDown();
mods.ctrl = IsCtrlDown();
mods.alt = IsAltDown();
mods.meta = IsMetaDown();
view.onMouseMove(x, y, mods);
view.onMouseDown(x, y, aperture::UIView::MouseButton::Left, mods);
view.onMouseUp(x, y, aperture::UIView::MouseButton::Left, mods);
view.onClick(x, y, aperture::UIView::MouseButton::Left, mods);Send onClick after every onMouseUp. The engine does not make a click out of a press and a
release. Without onClick, a page gets mousedown and mouseup but no click, and buttons do
nothing. APPlayer, CitySlice and the Unreal plugin all send the pair. onDoubleClick
raises dblclick. onContextMenu(x, y, mods) raises contextmenu and returns true when the page
cancelled it.
UIView::MouseButton is Left, Right, Middle, Back or Forward. Moves drive :hover and
the mouseenter, mouseleave, mouseover and mouseout events. A press, move and release also
raise pointerdown, pointermove and pointerup, so pages written with pointer events work.
Wheel
view.onWheel(x, y, deltaX, deltaY, aperture::UIView::WheelDeltaMode::Pixel, mods);A positive deltaY scrolls down. WheelDeltaMode is Pixel, Line or Page. Choose the unit
your device reports and leave the conversion to the page.
Keyboard
view.onKeyDown("ArrowUp", "ArrowUp", mods, /*repeat=*/false);
view.onKeyUp("ArrowUp", "ArrowUp", mods);
view.onKeyPress(U'a', mods); // a typed character, after any layout and IME processingkey and code follow the W3C KeyboardEvent model:
| Value | Meaning |
|---|---|
key | The logical key after layout and modifiers: "a", "A", "Enter", "ArrowUp". |
code | The physical key, independent of layout: "KeyA", "Digit1", "Enter". |
Use code for game bindings. Send text through onKeyPress with the character, as a char32_t.
onKeyDown and onKeyUp return true when the page handled the event.
What the engine does by itself with a key: Tab and Shift+Tab move focus, Enter in a text field
submits the form that owns it, and the keys edit a focused text field. Enter and Space do not
click a focused button or link. The page sees keydown on the focused element (on the root when
nothing is focused) and decides. For keyboard or gamepad activation, add a handler:
document.addEventListener("keydown", (event) => {
if (event.key === "Enter" && event.target.tagName === "BUTTON") event.target.click();
});IME
view.onIMECompositionUpdate(compositionUtf8, cursorByteOffset);
view.onIMECompositionCommit(finalUtf8);
view.onIMECompositionCancel();The text is UTF-8, and the cursor is a byte offset into it. The functions exist in builds with
the Skribidi text editor (APUI_USE_SKB_EDITOR), which is on in the SDK's build settings.
Platforms whose soft keyboards deliver only committed text can use onKeyPress.
Touch
std::vector<aperture::UIView::TouchPoint> touches;
touches.push_back({0, 100.0f, 200.0f, 8.0f, 8.0f, 1.0f});
view.onTouchStart(touches, mods);
view.onTouchMove(touches, mods);
view.onTouchEnd(touches, mods);
view.onTouchCancel(touches);TouchPoint field | Meaning |
|---|---|
id | A touch identifier that stays the same until the touch ends. |
x, y | Viewport pixels. |
radiusX, radiusY | The contact radius. |
force | Pressure from 0.0 to 1.0, when the device reports it. |
They raise touchstart, touchmove, touchend and touchcancel. A cancelled gesture is not the
same as one that ended: send onTouchCancel when the system takes the touch away.
The view has mouse and touch functions and none for a pen or for unified pointer events. If your platform gives you pointer events, split them before you forward: a mouse becomes mouse calls, a finger becomes touch calls, and a pen becomes either, whichever carries what your content needs.
Focus and gamepads
A gamepad usually moves focus, not a pointer. The view tracks focus:
view.moveFocusSpatial(0); // 0 left, 1 up, 2 right, 3 down: the control in that direction
view.moveFocus(false); // the next control in tab order (true: the previous one)
view.setFocusAt(x, y); // focus whatever is under a point
view.clearFocus();
if (view.hasFocus()) {
char id[128];
const size_t length = view.getFocusedElementIdRaw(id, sizeof(id)); // the focused element's id
}moveFocusSpatial is what a d-pad or stick should drive, because "left" means the control to the
left, which tab order cannot say. Both functions return true when focus moved. The Unreal plugin
maps the d-pad and left stick to moveFocusSpatial, the shoulder buttons to moveFocus, the
bottom face button to an Enter key pair and the right face button to Escape. Because Enter
does not click a button, give the page the keydown handler above for the accept button to do
anything.
Script can inspect document.activeElement. Hosts can use getFocusedElementIdRaw without
including the DOM element API, or use getFocusedElement() with the packaged DOM headers.
Keep native element access on the view's owning thread; see
Scripting.
Hit testing and click-through
hitTest(x, y) returns the element under a point, or an empty pointer when there is none.
Elements with pointer-events: none and elements with visibility: hidden are skipped, but their
descendants can still be hit. For a HUD whose empty areas must let the game see the click, make the
full-screen wrappers pointer-events: none and the controls pointer-events: auto, then ask
before you forward:
if (view.hitTest(x, y))
{
view.onMouseDown(x, y, aperture::UIView::MouseButton::Left, mods);
// ... the matching onMouseUp and onClick
}
else
{
game.OnWorldClick(x, y);
}Without pointer-events: none on the page's root and body, a point inside the viewport usually
lands on one of them, and hitTest never reports an empty area.
An input adapter
APPlayer's SDL2InputAdapter and GamepadNavigator show one way to organize this: a class
that owns the platform events and calls the view. They are in the source tree, so you have them
with the Source add-on. The package does not include the adapter interface they use. Write the
translation function that suits your engine, as the Unreal plugin does with its Slate widget.