Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/ext-input

bm-ext MIT

Touch input for php-baremetal: the Touch interface, the Pressed/Moved/Released/Gesture events and a C API for touch drivers. Platform-neutral.

ext-input · PHP API

Events

React to touches instead of polling them: Touch::events() puts Pressed, Moved, Released and Gesture on the event bus.

A touch device is an object that implements Baremetal\Input\Touch. Its events are classes of Baremetal\Input, delivered to the listeners registered with Events::listen() of ext-events.

Touch::events()

Touch::events(): Baremetal\Source

Returns Baremetal\Source.

Turns on the events of the device. Nothing is emitted before the first call. It returns a Baremetal\Input\TouchEvents, a Baremetal\Source handle:

Method Effect
stop() No more touch events are emitted by this device. point() keeps working.
start() Emission on again.
running() Whether events are emitted.

Every call returns a new handle on the same source and turns the emission on; dropping a handle leaves it running. The four classes take four of the 32 event classes the sources of the bus can use. events() throws an \Exception when the source table of the bus is full (Touch::events(): unknown class '...' or too many sources).

The events

Class When Properties
Pressed A finger comes down. int $x, int $y
Moved The finger moves. No coalescing: each report that moves the point is one event. int $x, int $y
Released The finger goes up. int $x, int $y: the last point
Gesture The controller recognised a gesture. int $x, int $y, GestureType $type

All of them extend the abstract TouchEvent, and $device is the Touch object that raised the event, so a script with more than one touch device tells them apart.

GestureType

Case Value
GestureType::SwipeUp swipe-up
GestureType::SwipeDown swipe-down
GestureType::SwipeLeft swipe-left
GestureType::SwipeRight swipe-right
GestureType::Tap tap
GestureType::DoubleTap double-tap
GestureType::LongPress long-press

Example

The listeners run in the event-driven runtime.

<?php
use Baremetal\Events;
use Baremetal\Input\{Pressed, Moved, Released, Gesture, GestureType, TouchEvent};

$touch->events();   // $touch: a driver that implements Baremetal\Input\Touch

$trail = [];
Events::listen(Pressed::class, function (Pressed $e) use (&$trail) { $trail = [[$e->x, $e->y]]; });
Events::listen(Moved::class, function (Moved $e) use (&$trail) { $trail[] = [$e->x, $e->y]; });
Events::listen(Released::class, function (Released $e) use (&$trail) {
    printf("stroke of %d points, ended at %d,%d\n", count($trail), $e->x, $e->y);
});
Events::listen(Gesture::class, function (Gesture $e) {
    echo $e->type === GestureType::DoubleTap ? "clear\n" : "gesture: {$e->type->value}\n";
});

Related