Input¶
RmlUi receives mouse and keyboard input from an RmlInputHandler, a
DataNode attached below the mouse watcher.
The handler collects the pointer position, button presses, key presses and
text input that flow through the data graph, and each region it is assigned to
(RmlRegion.set_input_handler()) feeds them to its context every frame,
translated into the region’s own pixel coordinates. One handler may serve
several regions.
PT(RmlInputHandler) handler = new RmlInputHandler("rml-input");
window->get_mouse().attach_new_node(handler);
region->set_input_handler(handler);
When the pointer leaves the window the handler sends RmlUi a mouse-leave, so hover and active states are cleared rather than left latched.
A region receives the input that arrives after it is given the handler. A handler that serves several regions gives each all of it, in each region’s own coordinates.
The keyboard mapping is RmlInputHandler.get_rml_key(), from an engine
ButtonHandle to RmlUi’s key identifier; it returns 0 for buttons RmlUi
has no name for.
Text input¶
Keystrokes are queued as they arrive during the frame and are delivered to the context – and thus to the focused element – when the region updates, during the cull traversal. Two things follow:
Calling
RmlElement.focus()on a text field in response to the key that opened it hands that same key to the field. Defer the focus by one frame,taskMgr.doMethodLater(0, ...)runs on the next.The engine’s own key events keep arriving whatever RmlUi has focused. A form is most reliably submitted from the engine’s
"enter"event, reading the field’sRmlElement.value, rather than from the document’ssubmitevent.
Touch screens¶
On a touch screen (GraphicsWindow.has_touch_screen()) the finger that
drives the window’s pointer is handed to RmlUi as a touch, through RmlUi’s own
touch input (Context::ProcessTouchStart and friends), rather than as a
mouse:
A tap presses and clicks what it lands on, also when the finger goes down and up within a single frame.
A drag that starts on a scrollable element (
overflow: autoorscroll) scrolls it, and when the finger lifts while moving the content carries on with RmlUi’s inertia. Once the finger has moved more than a few pixels the press is no longer a click, so a list can be scrolled from one of its buttons.When the finger lifts, the pointer leaves the interface: no
:hoverstyle stays behind on the last element touched.One finger at a time: while a touch is down, other fingers are ignored – they do not press anything, nor move the touch. The touch follows its own finger even when another finger has become the window’s pointer.
The handler tells a touch from a mouse press by the window’s fingers
(GraphicsWindow.get_num_touches()); a mouse press on the same device is
a mouse press, and desktop mouse input is unchanged. A tap that comes and goes
within one frame is not seen as a finger and arrives as a mouse click, which
has the same effect.
Feeding input yourself¶
The handler’s pointer_move(),
pointer_leave(), button_down(),
button_up(), keystroke(),
wheel() and touch_start(),
touch_move(), touch_end() and
touch_cancel() give it input that did not come through
the data graph – a pointer the application maps onto an interface in the
scene, or a test. Positions are in window pixels, y down; they are delivered
with the rest when the region next updates.
The mouse cursor¶
RmlUi documents can ask for a cursor (cursor: pointer and friends);
RmlContext.enable_mouse_cursor() decides whether such requests reach the
window. RmlContext.hover_element and
RmlContext.focus_element tell what the pointer is over and what has
keyboard focus; RmlContext.get_element_at_point() hit-tests any point
in region pixels.
Regions on off-screen buffers¶
The handler transmits window coordinates. For a region on the main window, or on a buffer whose texture fills the window, that is the right thing; for an interface rendered onto an object in the scene, as in the console sample, the mapping from the pointer to the object’s surface is the application’s – the sample sidesteps it by forwarding key events from the engine to the document and not using the mouse at all.